blob: 497c9ee6af53c259e451ff2490b6d9ef9f363fec [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;
221
Ted Kremeneke68fff62010-02-17 00:41:32 +0000222 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000223 string = want_display_name? clang_getCursorDisplayName(Cursor)
224 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000225 printf("%s=%s", clang_getCString(ks),
226 clang_getCString(string));
227 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000228 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000229
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000230 Referenced = clang_getCursorReferenced(Cursor);
231 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000232 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
233 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
234 printf("[");
235 for (I = 0; I != N; ++I) {
236 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000237 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000238 if (I)
239 printf(", ");
240
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000241 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000242 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000243 printf("%d:%d", line, column);
244 }
245 printf("]");
246 } else {
247 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000248 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000249 printf(":%d:%d", line, column);
250 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000251 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000252
253 if (clang_isCursorDefinition(Cursor))
254 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000255
256 switch (clang_getCursorAvailability(Cursor)) {
257 case CXAvailability_Available:
258 break;
259
260 case CXAvailability_Deprecated:
261 printf(" (deprecated)");
262 break;
263
264 case CXAvailability_NotAvailable:
265 printf(" (unavailable)");
266 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000267
268 case CXAvailability_NotAccessible:
269 printf(" (inaccessible)");
270 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000271 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000272
Douglas Gregorcc889662012-05-08 00:14:45 +0000273 NumPlatformAvailability
274 = clang_getCursorPlatformAvailability(Cursor,
275 &AlwaysDeprecated,
276 &DeprecatedMessage,
277 &AlwaysUnavailable,
278 &UnavailableMessage,
279 PlatformAvailability, 2);
280 if (AlwaysUnavailable) {
281 printf(" (always unavailable: \"%s\")",
282 clang_getCString(UnavailableMessage));
283 } else if (AlwaysDeprecated) {
284 printf(" (always deprecated: \"%s\")",
285 clang_getCString(DeprecatedMessage));
286 } else {
287 for (I = 0; I != NumPlatformAvailability; ++I) {
288 if (I >= 2)
289 break;
290
291 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
292 if (PlatformAvailability[I].Unavailable)
293 printf(", unavailable");
294 else {
295 printVersion(", introduced=", PlatformAvailability[I].Introduced);
296 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
297 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
298 }
299 if (clang_getCString(PlatformAvailability[I].Message)[0])
300 printf(", message=\"%s\"",
301 clang_getCString(PlatformAvailability[I].Message));
302 printf(")");
303 }
304 }
305 for (I = 0; I != NumPlatformAvailability; ++I) {
306 if (I >= 2)
307 break;
308 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
309 }
310
311 clang_disposeString(DeprecatedMessage);
312 clang_disposeString(UnavailableMessage);
313
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000314 if (clang_CXXMethod_isStatic(Cursor))
315 printf(" (static)");
316 if (clang_CXXMethod_isVirtual(Cursor))
317 printf(" (virtual)");
318
Ted Kremenek95f33552010-08-26 01:42:22 +0000319 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
320 CXType T =
321 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
322 CXString S = clang_getTypeKindSpelling(T.kind);
323 printf(" [IBOutletCollection=%s]", clang_getCString(S));
324 clang_disposeString(S);
325 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000326
327 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
328 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
329 unsigned isVirtual = clang_isVirtualBase(Cursor);
330 const char *accessStr = 0;
331
332 switch (access) {
333 case CX_CXXInvalidAccessSpecifier:
334 accessStr = "invalid"; break;
335 case CX_CXXPublic:
336 accessStr = "public"; break;
337 case CX_CXXProtected:
338 accessStr = "protected"; break;
339 case CX_CXXPrivate:
340 accessStr = "private"; break;
341 }
342
343 printf(" [access=%s isVirtual=%s]", accessStr,
344 isVirtual ? "true" : "false");
345 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000346
347 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
348 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
349 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
350 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000351 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000352 printf(" [Specialization of %s:%d:%d]",
353 clang_getCString(Name), line, column);
354 clang_disposeString(Name);
355 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000356
357 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
358 if (num_overridden) {
359 unsigned I;
360 printf(" [Overrides ");
361 for (I = 0; I != num_overridden; ++I) {
362 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000363 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000364 if (I)
365 printf(", ");
366 printf("@%d:%d", line, column);
367 }
368 printf("]");
369 clang_disposeOverriddenCursors(overridden);
370 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000371
372 if (Cursor.kind == CXCursor_InclusionDirective) {
373 CXFile File = clang_getIncludedFile(Cursor);
374 CXString Included = clang_getFileName(File);
375 printf(" (%s)", clang_getCString(Included));
376 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000377
378 if (clang_isFileMultipleIncludeGuarded(TU, File))
379 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000380 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000381
382 CursorExtent = clang_getCursorExtent(Cursor);
383 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
384 CXNameRange_WantQualifier
385 | CXNameRange_WantSinglePiece
386 | CXNameRange_WantTemplateArgs,
387 0);
388 if (!clang_equalRanges(CursorExtent, RefNameRange))
389 PrintRange(RefNameRange, "SingleRefName");
390
391 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
392 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
393 CXNameRange_WantQualifier
394 | CXNameRange_WantTemplateArgs,
395 RefNameRangeNr);
396 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
397 break;
398 if (!clang_equalRanges(CursorExtent, RefNameRange))
399 PrintRange(RefNameRange, "RefName");
400 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000401 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000402}
Steve Naroff89922f82009-08-31 00:59:03 +0000403
Ted Kremeneke68fff62010-02-17 00:41:32 +0000404static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000405 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000406 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000407 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000408 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000409 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000410 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000411 clang_disposeString(source);
412 return "<invalid loc>";
413 }
414 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000415 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000416 clang_disposeString(source);
417 return b;
418 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000419}
420
Ted Kremenek0d435192009-11-17 18:13:31 +0000421/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000422/* Callbacks. */
423/******************************************************************************/
424
425typedef void (*PostVisitTU)(CXTranslationUnit);
426
Douglas Gregora88084b2010-02-18 18:08:43 +0000427void PrintDiagnostic(CXDiagnostic Diagnostic) {
428 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000429 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000430 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000431 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000432 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
433 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000434 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000435
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000436 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000437 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000438
Douglas Gregor274f1902010-02-22 23:17:23 +0000439 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
440 fprintf(stderr, "%s\n", clang_getCString(Msg));
441 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000442
Douglas Gregora9b06d42010-11-09 06:24:54 +0000443 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
444 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000445 if (!file)
446 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000447
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000448 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000449 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000450 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000451 CXSourceRange range;
452 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
453 CXSourceLocation start = clang_getRangeStart(range);
454 CXSourceLocation end = clang_getRangeEnd(range);
455 unsigned start_line, start_column, end_line, end_column;
456 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000457 clang_getSpellingLocation(start, &start_file, &start_line,
458 &start_column, 0);
459 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000460 if (clang_equalLocations(start, end)) {
461 /* Insertion. */
462 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000463 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000464 clang_getCString(insertion_text), start_line, start_column);
465 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
466 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000467 if (start_file == file && end_file == file) {
468 fprintf(out, "FIX-IT: Remove ");
469 PrintExtent(out, start_line, start_column, end_line, end_column);
470 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000471 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000472 } else {
473 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000474 if (start_file == end_file) {
475 fprintf(out, "FIX-IT: Replace ");
476 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000477 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000478 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000479 break;
480 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000481 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000482 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000483}
484
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000485void PrintDiagnosticSet(CXDiagnosticSet Set) {
486 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
487 for ( ; i != n ; ++i) {
488 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
489 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000490 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000491 if (ChildDiags)
492 PrintDiagnosticSet(ChildDiags);
493 }
494}
495
496void PrintDiagnostics(CXTranslationUnit TU) {
497 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
498 PrintDiagnosticSet(TUSet);
499 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000500}
501
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000502void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000503 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000504 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000505 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000506 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000507 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000508 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000509 unsigned long amount = usage.entries[i].amount;
510 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000511 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000512 ((double) amount)/(1024*1024));
513 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000514 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000515 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000516 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000517}
518
Ted Kremenekce2ae882010-01-26 17:59:48 +0000519/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000520/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000521/******************************************************************************/
522
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000523static const char *FileCheckPrefix = "CHECK";
524
Douglas Gregora7bde202010-01-19 00:34:46 +0000525static void PrintCursorExtent(CXCursor C) {
526 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000527 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000528}
529
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000530/* Data used by all of the visitors. */
531typedef struct {
532 CXTranslationUnit TU;
533 enum CXCursorKind *Filter;
534} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000535
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000536
Ted Kremeneke68fff62010-02-17 00:41:32 +0000537enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000538 CXCursor Parent,
539 CXClientData ClientData) {
540 VisitorData *Data = (VisitorData *)ClientData;
541 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000542 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000543 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000544 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000545 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000546 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000547 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000548 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000549 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000550 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000551 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000552
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000553 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000554}
Steve Naroff50398192009-08-28 15:28:48 +0000555
Ted Kremeneke68fff62010-02-17 00:41:32 +0000556static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000557 CXCursor Parent,
558 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000559 const char *startBuf, *endBuf;
560 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
561 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000562 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000563
Douglas Gregorb6998662010-01-19 19:34:47 +0000564 if (Cursor.kind != CXCursor_FunctionDecl ||
565 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000566 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000567
568 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
569 &startLine, &startColumn,
570 &endLine, &endColumn);
571 /* Probe the entire body, looking for both decls and refs. */
572 curLine = startLine;
573 curColumn = startColumn;
574
575 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000576 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000577 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000578 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000580 if (*startBuf == '\n') {
581 startBuf++;
582 curLine++;
583 curColumn = 1;
584 } else if (*startBuf != '\t')
585 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586
Douglas Gregor98258af2010-01-18 22:46:11 +0000587 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000588 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000589
Douglas Gregor1db19de2010-01-19 21:36:55 +0000590 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000591 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000592 CXSourceLocation RefLoc
593 = clang_getLocation(Data->TU, file, curLine, curColumn);
594 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000595 if (Ref.kind == CXCursor_NoDeclFound) {
596 /* Nothing found here; that's fine. */
597 } else if (Ref.kind != CXCursor_FunctionDecl) {
598 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
599 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000600 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000601 printf("\n");
602 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000603 }
Ted Kremenek74844072010-02-17 00:41:20 +0000604 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000605 startBuf++;
606 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000607
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000608 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000609}
610
Ted Kremenek7d405622010-01-12 23:34:26 +0000611/******************************************************************************/
612/* USR testing. */
613/******************************************************************************/
614
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000615enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
616 CXClientData ClientData) {
617 VisitorData *Data = (VisitorData *)ClientData;
618 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000619 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000620 const char *cstr = clang_getCString(USR);
621 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000622 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000623 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000624 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000625 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
626
Douglas Gregora7bde202010-01-19 00:34:46 +0000627 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000628 printf("\n");
629 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000630
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000631 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000632 }
633
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000634 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000635}
636
637/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000638/* Inclusion stack testing. */
639/******************************************************************************/
640
641void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
642 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000643
Ted Kremenek16b55a72010-01-26 19:31:51 +0000644 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000645 CXString fname;
646
647 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000648 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000649 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000650
Ted Kremenek16b55a72010-01-26 19:31:51 +0000651 for (i = 0; i < includeStackLen; ++i) {
652 CXFile includingFile;
653 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000654 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
655 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000656 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000657 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000658 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000659 }
660 printf("\n");
661}
662
663void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000664 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000665}
666
667/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000668/* Linkage testing. */
669/******************************************************************************/
670
671static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
672 CXClientData d) {
673 const char *linkage = 0;
674
675 if (clang_isInvalid(clang_getCursorKind(cursor)))
676 return CXChildVisit_Recurse;
677
678 switch (clang_getCursorLinkage(cursor)) {
679 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000680 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
681 case CXLinkage_Internal: linkage = "Internal"; break;
682 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
683 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000684 }
685
686 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000687 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000688 printf("linkage=%s\n", linkage);
689 }
690
691 return CXChildVisit_Recurse;
692}
693
694/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000695/* Typekind testing. */
696/******************************************************************************/
697
698static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
699 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000700 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
701 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000702 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000703 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000704 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000705 if (clang_isConstQualifiedType(T))
706 printf(" const");
707 if (clang_isVolatileQualifiedType(T))
708 printf(" volatile");
709 if (clang_isRestrictQualifiedType(T))
710 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000711 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000712 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000713 {
714 CXType CT = clang_getCanonicalType(T);
715 if (!clang_equalTypes(T, CT)) {
716 CXString CS = clang_getTypeKindSpelling(CT.kind);
717 printf(" [canonical=%s]", clang_getCString(CS));
718 clang_disposeString(CS);
719 }
720 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000721 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000722 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000723 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000724 if (RT.kind != CXType_Invalid) {
725 CXString RS = clang_getTypeKindSpelling(RT.kind);
726 printf(" [result=%s]", clang_getCString(RS));
727 clang_disposeString(RS);
728 }
729 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000730 /* Print the argument types if they exist. */
731 {
732 int numArgs = clang_Cursor_getNumArguments(cursor);
733 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000734 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000735 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000736 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000737 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
738 if (T.kind != CXType_Invalid) {
739 CXString S = clang_getTypeKindSpelling(T.kind);
740 printf(" %s", clang_getCString(S));
741 clang_disposeString(S);
742 }
743 }
744 printf("]");
745 }
746 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000747 /* Print if this is a non-POD type. */
748 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000749
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000750 printf("\n");
751 }
752 return CXChildVisit_Recurse;
753}
754
755
756/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000757/* Loading ASTs/source. */
758/******************************************************************************/
759
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000760static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000761 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000762 CXCursorVisitor Visitor,
763 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000764
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000765 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000766 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000767
768 if (Visitor) {
769 enum CXCursorKind K = CXCursor_NotImplemented;
770 enum CXCursorKind *ck = &K;
771 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000772
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000773 /* Perform some simple filtering. */
774 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000775 else if (!strcmp(filter, "all-display") ||
776 !strcmp(filter, "local-display")) {
777 ck = NULL;
778 want_display_name = 1;
779 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000780 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000781 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
782 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
783 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
784 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
785 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
786 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
787 else {
788 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
789 return 1;
790 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000791
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000792 Data.TU = TU;
793 Data.Filter = ck;
794 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000795 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000796
Ted Kremenekce2ae882010-01-26 17:59:48 +0000797 if (PV)
798 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000799
Douglas Gregora88084b2010-02-18 18:08:43 +0000800 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000801 if (checkForErrors(TU) != 0) {
802 clang_disposeTranslationUnit(TU);
803 return -1;
804 }
805
Ted Kremenek0d435192009-11-17 18:13:31 +0000806 clang_disposeTranslationUnit(TU);
807 return 0;
808}
809
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000810int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000811 const char *prefix, CXCursorVisitor Visitor,
812 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000813 CXIndex Idx;
814 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000815 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000816 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000817 !strcmp(filter, "local") ? 1 : 0,
818 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000819
Ted Kremenek020a0952010-02-11 07:41:25 +0000820 if (!CreateTranslationUnit(Idx, file, &TU)) {
821 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000822 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000823 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000824
Ted Kremenek020a0952010-02-11 07:41:25 +0000825 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
826 clang_disposeIndex(Idx);
827 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000828}
829
Ted Kremenekce2ae882010-01-26 17:59:48 +0000830int perform_test_load_source(int argc, const char **argv,
831 const char *filter, CXCursorVisitor Visitor,
832 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000833 CXIndex Idx;
834 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000835 struct CXUnsavedFile *unsaved_files = 0;
836 int num_unsaved_files = 0;
837 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000838
Daniel Dunbarada487d2009-12-01 02:03:10 +0000839 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000840 (!strcmp(filter, "local") ||
841 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000842 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000843
Ted Kremenek020a0952010-02-11 07:41:25 +0000844 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
845 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000846 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000847 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000848
Douglas Gregordca8ee82011-05-06 16:33:08 +0000849 TU = clang_parseTranslationUnit(Idx, 0,
850 argv + num_unsaved_files,
851 argc - num_unsaved_files,
852 unsaved_files, num_unsaved_files,
853 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000854 if (!TU) {
855 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000856 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000857 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000858 return 1;
859 }
860
Ted Kremenekce2ae882010-01-26 17:59:48 +0000861 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000862 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000863 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000864 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000865}
866
Douglas Gregorabc563f2010-07-19 21:46:24 +0000867int perform_test_reparse_source(int argc, const char **argv, int trials,
868 const char *filter, CXCursorVisitor Visitor,
869 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000870 CXIndex Idx;
871 CXTranslationUnit TU;
872 struct CXUnsavedFile *unsaved_files = 0;
873 int num_unsaved_files = 0;
874 int result;
875 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000876 int remap_after_trial = 0;
877 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000878
879 Idx = clang_createIndex(/* excludeDeclsFromPCH */
880 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000881 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000882
Douglas Gregorabc563f2010-07-19 21:46:24 +0000883 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
884 clang_disposeIndex(Idx);
885 return -1;
886 }
887
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000888 /* Load the initial translation unit -- we do this without honoring remapped
889 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000890 TU = clang_parseTranslationUnit(Idx, 0,
891 argv + num_unsaved_files,
892 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000893 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000894 if (!TU) {
895 fprintf(stderr, "Unable to load translation unit!\n");
896 free_remapped_files(unsaved_files, num_unsaved_files);
897 clang_disposeIndex(Idx);
898 return 1;
899 }
900
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000901 if (checkForErrors(TU) != 0)
902 return -1;
903
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000904 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
905 remap_after_trial =
906 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
907 }
908
Douglas Gregorabc563f2010-07-19 21:46:24 +0000909 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000910 if (clang_reparseTranslationUnit(TU,
911 trial >= remap_after_trial ? num_unsaved_files : 0,
912 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000913 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000914 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000915 clang_disposeTranslationUnit(TU);
916 free_remapped_files(unsaved_files, num_unsaved_files);
917 clang_disposeIndex(Idx);
918 return -1;
919 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000920
921 if (checkForErrors(TU) != 0)
922 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000923 }
924
925 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000926
Douglas Gregorabc563f2010-07-19 21:46:24 +0000927 free_remapped_files(unsaved_files, num_unsaved_files);
928 clang_disposeIndex(Idx);
929 return result;
930}
931
Ted Kremenek0d435192009-11-17 18:13:31 +0000932/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000933/* Logic for testing clang_getCursor(). */
934/******************************************************************************/
935
Douglas Gregordd3e5542011-05-04 00:14:37 +0000936static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000937 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000938 unsigned end_line, unsigned end_col,
939 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000940 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000941 if (prefix)
942 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000943 PrintExtent(stdout, start_line, start_col, end_line, end_col);
944 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000945 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000946 printf("\n");
947}
948
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000949static int perform_file_scan(const char *ast_file, const char *source_file,
950 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000951 CXIndex Idx;
952 CXTranslationUnit TU;
953 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000954 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000955 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000956 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000957 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000958
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000959 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
960 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000961 fprintf(stderr, "Could not create Index\n");
962 return 1;
963 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000964
Ted Kremenek1c6da172009-11-17 19:37:36 +0000965 if (!CreateTranslationUnit(Idx, ast_file, &TU))
966 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000967
Ted Kremenek1c6da172009-11-17 19:37:36 +0000968 if ((fp = fopen(source_file, "r")) == NULL) {
969 fprintf(stderr, "Could not open '%s'\n", source_file);
970 return 1;
971 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972
Douglas Gregorb9790342010-01-22 21:44:22 +0000973 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000974 for (;;) {
975 CXCursor cursor;
976 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000977
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000978 if (c == '\n') {
979 ++line;
980 col = 1;
981 } else
982 ++col;
983
984 /* Check the cursor at this position, and dump the previous one if we have
985 * found something new.
986 */
987 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
988 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
989 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000990 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000991 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000992 start_line = line;
993 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000994 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000995 if (c == EOF)
996 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000997
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000998 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000999 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001000
Ted Kremenek1c6da172009-11-17 19:37:36 +00001001 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001002 clang_disposeTranslationUnit(TU);
1003 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001004 return 0;
1005}
1006
1007/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001008/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001009/******************************************************************************/
1010
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001011/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1012 on failure. If successful, the pointer *filename will contain newly-allocated
1013 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001014int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001015 unsigned *column, unsigned *second_line,
1016 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001017 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001018 const char *last_colon = strrchr(input, ':');
1019 unsigned values[4], i;
1020 unsigned num_values = (second_line && second_column)? 4 : 2;
1021
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001022 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001023 if (!last_colon || last_colon == input) {
1024 if (num_values == 4)
1025 fprintf(stderr, "could not parse filename:line:column:line:column in "
1026 "'%s'\n", input);
1027 else
1028 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001029 return 1;
1030 }
1031
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001032 for (i = 0; i != num_values; ++i) {
1033 const char *prev_colon;
1034
1035 /* Parse the next line or column. */
1036 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1037 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001038 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001039 (i % 2 ? "column" : "line"), input);
1040 return 1;
1041 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001042
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001043 if (i + 1 == num_values)
1044 break;
1045
1046 /* Find the previous colon. */
1047 prev_colon = last_colon - 1;
1048 while (prev_colon != input && *prev_colon != ':')
1049 --prev_colon;
1050 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001051 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001052 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001053 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001054 }
1055
1056 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001057 }
1058
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001059 *line = values[0];
1060 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001061
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001062 if (second_line && second_column) {
1063 *second_line = values[2];
1064 *second_column = values[3];
1065 }
1066
Douglas Gregor88d23952009-11-09 18:19:57 +00001067 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001068 *filename = (char*)malloc(last_colon - input + 1);
1069 memcpy(*filename, input, last_colon - input);
1070 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001071 return 0;
1072}
1073
1074const char *
1075clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1076 switch (Kind) {
1077 case CXCompletionChunk_Optional: return "Optional";
1078 case CXCompletionChunk_TypedText: return "TypedText";
1079 case CXCompletionChunk_Text: return "Text";
1080 case CXCompletionChunk_Placeholder: return "Placeholder";
1081 case CXCompletionChunk_Informative: return "Informative";
1082 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1083 case CXCompletionChunk_LeftParen: return "LeftParen";
1084 case CXCompletionChunk_RightParen: return "RightParen";
1085 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1086 case CXCompletionChunk_RightBracket: return "RightBracket";
1087 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1088 case CXCompletionChunk_RightBrace: return "RightBrace";
1089 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1090 case CXCompletionChunk_RightAngle: return "RightAngle";
1091 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001092 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001093 case CXCompletionChunk_Colon: return "Colon";
1094 case CXCompletionChunk_SemiColon: return "SemiColon";
1095 case CXCompletionChunk_Equal: return "Equal";
1096 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1097 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001098 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001099
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001100 return "Unknown";
1101}
1102
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001103static int checkForErrors(CXTranslationUnit TU) {
1104 unsigned Num, i;
1105 CXDiagnostic Diag;
1106 CXString DiagStr;
1107
1108 if (!getenv("CINDEXTEST_FAILONERROR"))
1109 return 0;
1110
1111 Num = clang_getNumDiagnostics(TU);
1112 for (i = 0; i != Num; ++i) {
1113 Diag = clang_getDiagnostic(TU, i);
1114 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1115 DiagStr = clang_formatDiagnostic(Diag,
1116 clang_defaultDiagnosticDisplayOptions());
1117 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1118 clang_disposeString(DiagStr);
1119 clang_disposeDiagnostic(Diag);
1120 return -1;
1121 }
1122 clang_disposeDiagnostic(Diag);
1123 }
1124
1125 return 0;
1126}
1127
Douglas Gregor3ac73852009-11-09 16:04:45 +00001128void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001129 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001130
Douglas Gregor3ac73852009-11-09 16:04:45 +00001131 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001132 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001133 CXString text;
1134 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001135 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001136 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001137
Douglas Gregor3ac73852009-11-09 16:04:45 +00001138 if (Kind == CXCompletionChunk_Optional) {
1139 fprintf(file, "{Optional ");
1140 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001141 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001142 file);
1143 fprintf(file, "}");
1144 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001145 }
1146
1147 if (Kind == CXCompletionChunk_VerticalSpace) {
1148 fprintf(file, "{VerticalSpace }");
1149 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001150 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001151
Douglas Gregord5a20892009-11-09 17:05:28 +00001152 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001153 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001154 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001155 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001156 cstr ? cstr : "");
1157 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001158 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001159
Douglas Gregor3ac73852009-11-09 16:04:45 +00001160}
1161
1162void print_completion_result(CXCompletionResult *completion_result,
1163 CXClientData client_data) {
1164 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001165 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001166 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001167 enum CXCursorKind ParentKind;
1168 CXString ParentName;
1169
Ted Kremeneke68fff62010-02-17 00:41:32 +00001170 fprintf(file, "%s:", clang_getCString(ks));
1171 clang_disposeString(ks);
1172
Douglas Gregor3ac73852009-11-09 16:04:45 +00001173 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001174 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001175 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001176 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1177 case CXAvailability_Available:
1178 break;
1179
1180 case CXAvailability_Deprecated:
1181 fprintf(file, " (deprecated)");
1182 break;
1183
1184 case CXAvailability_NotAvailable:
1185 fprintf(file, " (unavailable)");
1186 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001187
1188 case CXAvailability_NotAccessible:
1189 fprintf(file, " (inaccessible)");
1190 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001191 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001192
1193 annotationCount = clang_getCompletionNumAnnotations(
1194 completion_result->CompletionString);
1195 if (annotationCount) {
1196 unsigned i;
1197 fprintf(file, " (");
1198 for (i = 0; i < annotationCount; ++i) {
1199 if (i != 0)
1200 fprintf(file, ", ");
1201 fprintf(file, "\"%s\"",
1202 clang_getCString(clang_getCompletionAnnotation(
1203 completion_result->CompletionString, i)));
1204 }
1205 fprintf(file, ")");
1206 }
1207
Douglas Gregorba103062012-03-27 23:34:16 +00001208 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1209 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1210 &ParentKind);
1211 if (ParentKind != CXCursor_NotImplemented) {
1212 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1213 fprintf(file, " (parent: %s '%s')",
1214 clang_getCString(KindSpelling),
1215 clang_getCString(ParentName));
1216 clang_disposeString(KindSpelling);
1217 }
1218 clang_disposeString(ParentName);
1219 }
1220
Douglas Gregor58ddb602010-08-23 23:00:57 +00001221 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001222}
1223
Douglas Gregor3da626b2011-07-07 16:03:39 +00001224void print_completion_contexts(unsigned long long contexts, FILE *file) {
1225 fprintf(file, "Completion contexts:\n");
1226 if (contexts == CXCompletionContext_Unknown) {
1227 fprintf(file, "Unknown\n");
1228 }
1229 if (contexts & CXCompletionContext_AnyType) {
1230 fprintf(file, "Any type\n");
1231 }
1232 if (contexts & CXCompletionContext_AnyValue) {
1233 fprintf(file, "Any value\n");
1234 }
1235 if (contexts & CXCompletionContext_ObjCObjectValue) {
1236 fprintf(file, "Objective-C object value\n");
1237 }
1238 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1239 fprintf(file, "Objective-C selector value\n");
1240 }
1241 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1242 fprintf(file, "C++ class type value\n");
1243 }
1244 if (contexts & CXCompletionContext_DotMemberAccess) {
1245 fprintf(file, "Dot member access\n");
1246 }
1247 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1248 fprintf(file, "Arrow member access\n");
1249 }
1250 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1251 fprintf(file, "Objective-C property access\n");
1252 }
1253 if (contexts & CXCompletionContext_EnumTag) {
1254 fprintf(file, "Enum tag\n");
1255 }
1256 if (contexts & CXCompletionContext_UnionTag) {
1257 fprintf(file, "Union tag\n");
1258 }
1259 if (contexts & CXCompletionContext_StructTag) {
1260 fprintf(file, "Struct tag\n");
1261 }
1262 if (contexts & CXCompletionContext_ClassTag) {
1263 fprintf(file, "Class name\n");
1264 }
1265 if (contexts & CXCompletionContext_Namespace) {
1266 fprintf(file, "Namespace or namespace alias\n");
1267 }
1268 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1269 fprintf(file, "Nested name specifier\n");
1270 }
1271 if (contexts & CXCompletionContext_ObjCInterface) {
1272 fprintf(file, "Objective-C interface\n");
1273 }
1274 if (contexts & CXCompletionContext_ObjCProtocol) {
1275 fprintf(file, "Objective-C protocol\n");
1276 }
1277 if (contexts & CXCompletionContext_ObjCCategory) {
1278 fprintf(file, "Objective-C category\n");
1279 }
1280 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1281 fprintf(file, "Objective-C instance method\n");
1282 }
1283 if (contexts & CXCompletionContext_ObjCClassMessage) {
1284 fprintf(file, "Objective-C class method\n");
1285 }
1286 if (contexts & CXCompletionContext_ObjCSelectorName) {
1287 fprintf(file, "Objective-C selector name\n");
1288 }
1289 if (contexts & CXCompletionContext_MacroName) {
1290 fprintf(file, "Macro name\n");
1291 }
1292 if (contexts & CXCompletionContext_NaturalLanguage) {
1293 fprintf(file, "Natural language\n");
1294 }
1295}
1296
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001297int my_stricmp(const char *s1, const char *s2) {
1298 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001299 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001300 if (c1 < c2)
1301 return -1;
1302 else if (c1 > c2)
1303 return 1;
1304
1305 ++s1;
1306 ++s2;
1307 }
1308
1309 if (*s1)
1310 return 1;
1311 else if (*s2)
1312 return -1;
1313 return 0;
1314}
1315
Douglas Gregor1982c182010-07-12 18:38:41 +00001316int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001317 const char *input = argv[1];
1318 char *filename = 0;
1319 unsigned line;
1320 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001321 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001322 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001323 struct CXUnsavedFile *unsaved_files = 0;
1324 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001325 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001326 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001327 unsigned I, Repeats = 1;
1328 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1329
1330 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1331 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001332
Douglas Gregor1982c182010-07-12 18:38:41 +00001333 if (timing_only)
1334 input += strlen("-code-completion-timing=");
1335 else
1336 input += strlen("-code-completion-at=");
1337
Ted Kremeneke68fff62010-02-17 00:41:32 +00001338 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001339 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001340 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001341
Douglas Gregor735df882009-12-02 09:21:34 +00001342 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1343 return -1;
1344
Douglas Gregor32be4a52010-10-11 21:37:58 +00001345 CIdx = clang_createIndex(0, 0);
1346
1347 if (getenv("CINDEXTEST_EDITING"))
1348 Repeats = 5;
1349
1350 TU = clang_parseTranslationUnit(CIdx, 0,
1351 argv + num_unsaved_files + 2,
1352 argc - num_unsaved_files - 2,
1353 0, 0, getDefaultParsingOptions());
1354 if (!TU) {
1355 fprintf(stderr, "Unable to load translation unit!\n");
1356 return 1;
1357 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001358
1359 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1360 fprintf(stderr, "Unable to reparse translation init!\n");
1361 return 1;
1362 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001363
1364 for (I = 0; I != Repeats; ++I) {
1365 results = clang_codeCompleteAt(TU, filename, line, column,
1366 unsaved_files, num_unsaved_files,
1367 completionOptions);
1368 if (!results) {
1369 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001370 return 1;
1371 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001372 if (I != Repeats-1)
1373 clang_disposeCodeCompleteResults(results);
1374 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001375
Douglas Gregorec6762c2009-12-18 16:20:58 +00001376 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001377 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001378 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001379 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001380 CXString objCSelector;
1381 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001382 if (!timing_only) {
1383 /* Sort the code-completion results based on the typed text. */
1384 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1385
Douglas Gregor1982c182010-07-12 18:38:41 +00001386 for (i = 0; i != n; ++i)
1387 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001388 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001389 n = clang_codeCompleteGetNumDiagnostics(results);
1390 for (i = 0; i != n; ++i) {
1391 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1392 PrintDiagnostic(diag);
1393 clang_disposeDiagnostic(diag);
1394 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001395
1396 contexts = clang_codeCompleteGetContexts(results);
1397 print_completion_contexts(contexts, stdout);
1398
Douglas Gregor0a47d692011-07-26 15:24:30 +00001399 containerKind = clang_codeCompleteGetContainerKind(results,
1400 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001401
1402 if (containerKind != CXCursor_InvalidCode) {
1403 /* We have found a container */
1404 CXString containerUSR, containerKindSpelling;
1405 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1406 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1407 clang_disposeString(containerKindSpelling);
1408
1409 if (containerIsIncomplete) {
1410 printf("Container is incomplete\n");
1411 }
1412 else {
1413 printf("Container is complete\n");
1414 }
1415
1416 containerUSR = clang_codeCompleteGetContainerUSR(results);
1417 printf("Container USR: %s\n", clang_getCString(containerUSR));
1418 clang_disposeString(containerUSR);
1419 }
1420
Douglas Gregor0a47d692011-07-26 15:24:30 +00001421 objCSelector = clang_codeCompleteGetObjCSelector(results);
1422 selectorString = clang_getCString(objCSelector);
1423 if (selectorString && strlen(selectorString) > 0) {
1424 printf("Objective-C selector: %s\n", selectorString);
1425 }
1426 clang_disposeString(objCSelector);
1427
Douglas Gregorec6762c2009-12-18 16:20:58 +00001428 clang_disposeCodeCompleteResults(results);
1429 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001430 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001431 clang_disposeIndex(CIdx);
1432 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001433
Douglas Gregor735df882009-12-02 09:21:34 +00001434 free_remapped_files(unsaved_files, num_unsaved_files);
1435
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001436 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001437}
1438
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001439typedef struct {
1440 char *filename;
1441 unsigned line;
1442 unsigned column;
1443} CursorSourceLocation;
1444
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001445static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001446 CXIndex CIdx;
1447 int errorCode;
1448 struct CXUnsavedFile *unsaved_files = 0;
1449 int num_unsaved_files = 0;
1450 CXTranslationUnit TU;
1451 CXCursor Cursor;
1452 CursorSourceLocation *Locations = 0;
1453 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001454 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001455 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001456
Ted Kremeneke68fff62010-02-17 00:41:32 +00001457 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001458 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1459 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001460
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001461 /* Parse the locations. */
1462 assert(NumLocations > 0 && "Unable to count locations?");
1463 Locations = (CursorSourceLocation *)malloc(
1464 NumLocations * sizeof(CursorSourceLocation));
1465 for (Loc = 0; Loc < NumLocations; ++Loc) {
1466 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001467 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1468 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001469 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001470 return errorCode;
1471 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001472
1473 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001474 &num_unsaved_files))
1475 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001476
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001477 if (getenv("CINDEXTEST_EDITING"))
1478 Repeats = 5;
1479
1480 /* Parse the translation unit. When we're testing clang_getCursor() after
1481 reparsing, don't remap unsaved files until the second parse. */
1482 CIdx = clang_createIndex(1, 1);
1483 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1484 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001485 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001486 unsaved_files,
1487 Repeats > 1? 0 : num_unsaved_files,
1488 getDefaultParsingOptions());
1489
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001490 if (!TU) {
1491 fprintf(stderr, "unable to parse input\n");
1492 return -1;
1493 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001494
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001495 if (checkForErrors(TU) != 0)
1496 return -1;
1497
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001498 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001499 if (Repeats > 1 &&
1500 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1501 clang_defaultReparseOptions(TU))) {
1502 clang_disposeTranslationUnit(TU);
1503 return 1;
1504 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001505
1506 if (checkForErrors(TU) != 0)
1507 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001508
1509 for (Loc = 0; Loc < NumLocations; ++Loc) {
1510 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1511 if (!file)
1512 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001513
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001514 Cursor = clang_getCursor(TU,
1515 clang_getLocation(TU, file, Locations[Loc].line,
1516 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001517
1518 if (checkForErrors(TU) != 0)
1519 return -1;
1520
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001521 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001522 CXCompletionString completionString = clang_getCursorCompletionString(
1523 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001524 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1525 CXString Spelling;
1526 const char *cspell;
1527 unsigned line, column;
1528 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1529 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001530 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001531 PrintCursorExtent(Cursor);
1532 Spelling = clang_getCursorSpelling(Cursor);
1533 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001534 if (cspell && strlen(cspell) != 0) {
1535 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001536 printf(" Spelling=%s (", cspell);
1537 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001538 CXSourceRange range =
1539 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001540 if (clang_Range_isNull(range))
1541 break;
1542 PrintRange(range, 0);
1543 }
1544 printf(")");
1545 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001546 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001547 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1548 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001549 if (completionString != NULL) {
1550 printf("\nCompletion string: ");
1551 print_completion_string(completionString, stdout);
1552 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001553 printf("\n");
1554 free(Locations[Loc].filename);
1555 }
1556 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001557 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001558
Douglas Gregora88084b2010-02-18 18:08:43 +00001559 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001560 clang_disposeTranslationUnit(TU);
1561 clang_disposeIndex(CIdx);
1562 free(Locations);
1563 free_remapped_files(unsaved_files, num_unsaved_files);
1564 return 0;
1565}
1566
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001567static enum CXVisitorResult findFileRefsVisit(void *context,
1568 CXCursor cursor, CXSourceRange range) {
1569 if (clang_Range_isNull(range))
1570 return CXVisit_Continue;
1571
1572 PrintCursor(cursor);
1573 PrintRange(range, "");
1574 printf("\n");
1575 return CXVisit_Continue;
1576}
1577
1578static int find_file_refs_at(int argc, const char **argv) {
1579 CXIndex CIdx;
1580 int errorCode;
1581 struct CXUnsavedFile *unsaved_files = 0;
1582 int num_unsaved_files = 0;
1583 CXTranslationUnit TU;
1584 CXCursor Cursor;
1585 CursorSourceLocation *Locations = 0;
1586 unsigned NumLocations = 0, Loc;
1587 unsigned Repeats = 1;
1588 unsigned I;
1589
1590 /* Count the number of locations. */
1591 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1592 ++NumLocations;
1593
1594 /* Parse the locations. */
1595 assert(NumLocations > 0 && "Unable to count locations?");
1596 Locations = (CursorSourceLocation *)malloc(
1597 NumLocations * sizeof(CursorSourceLocation));
1598 for (Loc = 0; Loc < NumLocations; ++Loc) {
1599 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1600 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1601 &Locations[Loc].line,
1602 &Locations[Loc].column, 0, 0)))
1603 return errorCode;
1604 }
1605
1606 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1607 &num_unsaved_files))
1608 return -1;
1609
1610 if (getenv("CINDEXTEST_EDITING"))
1611 Repeats = 5;
1612
1613 /* Parse the translation unit. When we're testing clang_getCursor() after
1614 reparsing, don't remap unsaved files until the second parse. */
1615 CIdx = clang_createIndex(1, 1);
1616 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1617 argv + num_unsaved_files + 1 + NumLocations,
1618 argc - num_unsaved_files - 2 - NumLocations,
1619 unsaved_files,
1620 Repeats > 1? 0 : num_unsaved_files,
1621 getDefaultParsingOptions());
1622
1623 if (!TU) {
1624 fprintf(stderr, "unable to parse input\n");
1625 return -1;
1626 }
1627
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001628 if (checkForErrors(TU) != 0)
1629 return -1;
1630
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001631 for (I = 0; I != Repeats; ++I) {
1632 if (Repeats > 1 &&
1633 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1634 clang_defaultReparseOptions(TU))) {
1635 clang_disposeTranslationUnit(TU);
1636 return 1;
1637 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001638
1639 if (checkForErrors(TU) != 0)
1640 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001641
1642 for (Loc = 0; Loc < NumLocations; ++Loc) {
1643 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1644 if (!file)
1645 continue;
1646
1647 Cursor = clang_getCursor(TU,
1648 clang_getLocation(TU, file, Locations[Loc].line,
1649 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001650
1651 if (checkForErrors(TU) != 0)
1652 return -1;
1653
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001654 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001655 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001656 PrintCursor(Cursor);
1657 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001658 clang_findReferencesInFile(Cursor, file, visitor);
1659 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001660
1661 if (checkForErrors(TU) != 0)
1662 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001663 }
1664 }
1665 }
1666
1667 PrintDiagnostics(TU);
1668 clang_disposeTranslationUnit(TU);
1669 clang_disposeIndex(CIdx);
1670 free(Locations);
1671 free_remapped_files(unsaved_files, num_unsaved_files);
1672 return 0;
1673}
1674
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001675typedef struct {
1676 const char *check_prefix;
1677 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001678 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001679 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001680 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001681} IndexData;
1682
1683static void printCheck(IndexData *data) {
1684 if (data->check_prefix) {
1685 if (data->first_check_printed) {
1686 printf("// %s-NEXT: ", data->check_prefix);
1687 } else {
1688 printf("// %s : ", data->check_prefix);
1689 data->first_check_printed = 1;
1690 }
1691 }
1692}
1693
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001694static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001695 CXString filename = clang_getFileName((CXFile)file);
1696 printf("%s", clang_getCString(filename));
1697 clang_disposeString(filename);
1698}
1699
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001700static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1701 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001702 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001703 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001704 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001705 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001706 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001707
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001708 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001709 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1710 if (line == 0) {
1711 printf("<null loc>");
1712 return;
1713 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001714 if (!file) {
1715 printf("<no idxfile>");
1716 return;
1717 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001718 filename = clang_getFileName((CXFile)file);
1719 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001720 if (strcmp(cname, index_data->main_filename) == 0)
1721 isMainFile = 1;
1722 else
1723 isMainFile = 0;
1724 clang_disposeString(filename);
1725
1726 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001727 printCXIndexFile(file);
1728 printf(":");
1729 }
1730 printf("%d:%d", line, column);
1731}
1732
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001733static unsigned digitCount(unsigned val) {
1734 unsigned c = 1;
1735 while (1) {
1736 if (val < 10)
1737 return c;
1738 ++c;
1739 val /= 10;
1740 }
1741}
1742
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001743static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1744 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001745 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001746 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001747 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001748 unsigned line, column;
1749
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001750 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001751 if (!name)
1752 name = "<anon-tag>";
1753
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001754 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001755 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001756 newStr = (char *)malloc(strlen(name) +
1757 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001758 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001759 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001760}
1761
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001762static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1763 CXIdxClientContainer container;
1764 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001765 if (!container)
1766 printf("[<<NULL>>]");
1767 else
1768 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001769}
1770
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001771static const char *getEntityKindString(CXIdxEntityKind kind) {
1772 switch (kind) {
1773 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1774 case CXIdxEntity_Typedef: return "typedef";
1775 case CXIdxEntity_Function: return "function";
1776 case CXIdxEntity_Variable: return "variable";
1777 case CXIdxEntity_Field: return "field";
1778 case CXIdxEntity_EnumConstant: return "enumerator";
1779 case CXIdxEntity_ObjCClass: return "objc-class";
1780 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1781 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001782 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1783 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001784 case CXIdxEntity_ObjCProperty: return "objc-property";
1785 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1786 case CXIdxEntity_Enum: return "enum";
1787 case CXIdxEntity_Struct: return "struct";
1788 case CXIdxEntity_Union: return "union";
1789 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001790 case CXIdxEntity_CXXNamespace: return "namespace";
1791 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1792 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1793 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1794 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1795 case CXIdxEntity_CXXConstructor: return "constructor";
1796 case CXIdxEntity_CXXDestructor: return "destructor";
1797 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1798 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1799 }
1800 assert(0 && "Garbage entity kind");
1801 return 0;
1802}
1803
1804static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1805 switch (kind) {
1806 case CXIdxEntity_NonTemplate: return "";
1807 case CXIdxEntity_Template: return "-template";
1808 case CXIdxEntity_TemplatePartialSpecialization:
1809 return "-template-partial-spec";
1810 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001811 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001812 assert(0 && "Garbage entity kind");
1813 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001814}
1815
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001816static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1817 switch (kind) {
1818 case CXIdxEntityLang_None: return "<none>";
1819 case CXIdxEntityLang_C: return "C";
1820 case CXIdxEntityLang_ObjC: return "ObjC";
1821 case CXIdxEntityLang_CXX: return "C++";
1822 }
1823 assert(0 && "Garbage language kind");
1824 return 0;
1825}
1826
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001827static void printEntityInfo(const char *cb,
1828 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001829 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001830 const char *name;
1831 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001832 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001833 index_data = (IndexData *)client_data;
1834 printCheck(index_data);
1835
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001836 if (!info) {
1837 printf("%s: <<NULL>>", cb);
1838 return;
1839 }
1840
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001841 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001842 if (!name)
1843 name = "<anon-tag>";
1844
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001845 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1846 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001847 printf(" | name: %s", name);
1848 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001849 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001850
1851 for (i = 0; i != info->numAttributes; ++i) {
1852 const CXIdxAttrInfo *Attr = info->attributes[i];
1853 printf(" <attribute>: ");
1854 PrintCursor(Attr->cursor);
1855 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001856}
1857
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001858static void printBaseClassInfo(CXClientData client_data,
1859 const CXIdxBaseClassInfo *info) {
1860 printEntityInfo(" <base>", client_data, info->base);
1861 printf(" | cursor: ");
1862 PrintCursor(info->cursor);
1863 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001864 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001865}
1866
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001867static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1868 CXClientData client_data) {
1869 unsigned i;
1870 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1871 printEntityInfo(" <protocol>", client_data,
1872 ProtoInfo->protocols[i]->protocol);
1873 printf(" | cursor: ");
1874 PrintCursor(ProtoInfo->protocols[i]->cursor);
1875 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001876 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001877 printf("\n");
1878 }
1879}
1880
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001881static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001882 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001883 CXString str;
1884 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001885 unsigned numDiags, i;
1886 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001887 IndexData *index_data;
1888 index_data = (IndexData *)client_data;
1889 printCheck(index_data);
1890
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001891 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1892 for (i = 0; i != numDiags; ++i) {
1893 diag = clang_getDiagnosticInSet(diagSet, i);
1894 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1895 cstr = clang_getCString(str);
1896 printf("[diagnostic]: %s\n", cstr);
1897 clang_disposeString(str);
1898
1899 if (getenv("CINDEXTEST_FAILONERROR") &&
1900 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1901 index_data->fail_for_error = 1;
1902 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001903 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001904}
1905
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001906static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1907 CXFile file, void *reserved) {
1908 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001909 CXString filename;
1910
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001911 index_data = (IndexData *)client_data;
1912 printCheck(index_data);
1913
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001914 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001915 index_data->main_filename = clang_getCString(filename);
1916 clang_disposeString(filename);
1917
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001918 printf("[enteredMainFile]: ");
1919 printCXIndexFile((CXIdxClientFile)file);
1920 printf("\n");
1921
1922 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001923}
1924
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001925static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001926 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001927 IndexData *index_data;
1928 index_data = (IndexData *)client_data;
1929 printCheck(index_data);
1930
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001931 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001932 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001933 printf(" | name: \"%s\"", info->filename);
1934 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001935 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001936 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001937
1938 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001939}
1940
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001941static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001942 void *reserved) {
1943 IndexData *index_data;
1944 index_data = (IndexData *)client_data;
1945 printCheck(index_data);
1946
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001947 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001948 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001949}
1950
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001951static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001952 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001953 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001954 const CXIdxObjCCategoryDeclInfo *CatInfo;
1955 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001956 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001957 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001958 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001959 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001960 index_data = (IndexData *)client_data;
1961
1962 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1963 printf(" | cursor: ");
1964 PrintCursor(info->cursor);
1965 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001966 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001967 printf(" | semantic-container: ");
1968 printCXIndexContainer(info->semanticContainer);
1969 printf(" | lexical-container: ");
1970 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001971 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001972 printf(" | isDef: %d", info->isDefinition);
1973 printf(" | isContainer: %d", info->isContainer);
1974 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001975
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001976 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001977 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001978 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001979 PrintCursor(Attr->cursor);
1980 printf("\n");
1981 }
1982
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001983 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1984 const char *kindName = 0;
1985 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1986 switch (K) {
1987 case CXIdxObjCContainer_ForwardRef:
1988 kindName = "forward-ref"; break;
1989 case CXIdxObjCContainer_Interface:
1990 kindName = "interface"; break;
1991 case CXIdxObjCContainer_Implementation:
1992 kindName = "implementation"; break;
1993 }
1994 printCheck(index_data);
1995 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1996 }
1997
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001998 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001999 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2000 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002001 printf(" | cursor: ");
2002 PrintCursor(CatInfo->classCursor);
2003 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002004 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002005 printf("\n");
2006 }
2007
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002008 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2009 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002010 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002011 printf("\n");
2012 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002013 }
2014
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002015 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2016 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002017 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002018
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002019 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2020 if (PropInfo->getter) {
2021 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2022 printf("\n");
2023 }
2024 if (PropInfo->setter) {
2025 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2026 printf("\n");
2027 }
2028 }
2029
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002030 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2031 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2032 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2033 printf("\n");
2034 }
2035 }
2036
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002037 if (info->declAsContainer)
2038 clang_index_setClientContainer(info->declAsContainer,
2039 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002040}
2041
2042static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002043 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002044 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002045 printf(" | cursor: ");
2046 PrintCursor(info->cursor);
2047 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002048 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002049 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002050 printf(" | container: ");
2051 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002052 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002053 switch (info->kind) {
2054 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002055 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002056 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002057 printf("\n");
2058}
2059
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002060static int index_abortQuery(CXClientData client_data, void *reserved) {
2061 IndexData *index_data;
2062 index_data = (IndexData *)client_data;
2063 return index_data->abort;
2064}
2065
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002066static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002067 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002068 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002069 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002070 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002071 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002072 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002073 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002074 index_indexEntityReference
2075};
2076
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002077static unsigned getIndexOptions(void) {
2078 unsigned index_opts;
2079 index_opts = 0;
2080 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2081 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2082 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2083 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2084
2085 return index_opts;
2086}
2087
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002088static int index_file(int argc, const char **argv) {
2089 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002090 CXIndex Idx;
2091 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002092 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002093 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002094 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002095
2096 check_prefix = 0;
2097 if (argc > 0) {
2098 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2099 check_prefix = argv[0] + strlen("-check-prefix=");
2100 ++argv;
2101 --argc;
2102 }
2103 }
2104
2105 if (argc == 0) {
2106 fprintf(stderr, "no compiler arguments\n");
2107 return -1;
2108 }
2109
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002110 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2111 /* displayDiagnosics=*/1))) {
2112 fprintf(stderr, "Could not create Index\n");
2113 return 1;
2114 }
2115 idxAction = 0;
2116 result = 1;
2117
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002118 index_data.check_prefix = check_prefix;
2119 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002120 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002121 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002122
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002123 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002124 idxAction = clang_IndexAction_create(Idx);
2125 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002126 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002127 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002128 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002129 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002130
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002131 clang_IndexAction_dispose(idxAction);
2132 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002133 return result;
2134}
2135
2136static int index_tu(int argc, const char **argv) {
2137 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002138 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002139 CXTranslationUnit TU;
2140 const char *check_prefix;
2141 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002142 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002143 int result;
2144
2145 check_prefix = 0;
2146 if (argc > 0) {
2147 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2148 check_prefix = argv[0] + strlen("-check-prefix=");
2149 ++argv;
2150 --argc;
2151 }
2152 }
2153
2154 if (argc == 0) {
2155 fprintf(stderr, "no ast file\n");
2156 return -1;
2157 }
2158
2159 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2160 /* displayDiagnosics=*/1))) {
2161 fprintf(stderr, "Could not create Index\n");
2162 return 1;
2163 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002164 idxAction = 0;
2165 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002166
2167 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002168 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002169
2170 index_data.check_prefix = check_prefix;
2171 index_data.first_check_printed = 0;
2172 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002173 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002174
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002175 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002176 idxAction = clang_IndexAction_create(Idx);
2177 result = clang_indexTranslationUnit(idxAction, &index_data,
2178 &IndexCB,sizeof(IndexCB),
2179 index_opts, TU);
2180 if (index_data.fail_for_error)
2181 goto finished;
2182
2183 finished:
2184 clang_IndexAction_dispose(idxAction);
2185 clang_disposeIndex(Idx);
2186
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002187 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002188}
2189
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002190int perform_token_annotation(int argc, const char **argv) {
2191 const char *input = argv[1];
2192 char *filename = 0;
2193 unsigned line, second_line;
2194 unsigned column, second_column;
2195 CXIndex CIdx;
2196 CXTranslationUnit TU = 0;
2197 int errorCode;
2198 struct CXUnsavedFile *unsaved_files = 0;
2199 int num_unsaved_files = 0;
2200 CXToken *tokens;
2201 unsigned num_tokens;
2202 CXSourceRange range;
2203 CXSourceLocation startLoc, endLoc;
2204 CXFile file = 0;
2205 CXCursor *cursors = 0;
2206 unsigned i;
2207
2208 input += strlen("-test-annotate-tokens=");
2209 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2210 &second_line, &second_column)))
2211 return errorCode;
2212
2213 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2214 return -1;
2215
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002216 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002217 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2218 argv + num_unsaved_files + 2,
2219 argc - num_unsaved_files - 3,
2220 unsaved_files,
2221 num_unsaved_files,
2222 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002223 if (!TU) {
2224 fprintf(stderr, "unable to parse input\n");
2225 clang_disposeIndex(CIdx);
2226 free(filename);
2227 free_remapped_files(unsaved_files, num_unsaved_files);
2228 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002229 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002230 errorCode = 0;
2231
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002232 if (checkForErrors(TU) != 0)
2233 return -1;
2234
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002235 if (getenv("CINDEXTEST_EDITING")) {
2236 for (i = 0; i < 5; ++i) {
2237 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2238 clang_defaultReparseOptions(TU))) {
2239 fprintf(stderr, "Unable to reparse translation unit!\n");
2240 errorCode = -1;
2241 goto teardown;
2242 }
2243 }
2244 }
2245
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002246 if (checkForErrors(TU) != 0) {
2247 errorCode = -1;
2248 goto teardown;
2249 }
2250
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002251 file = clang_getFile(TU, filename);
2252 if (!file) {
2253 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2254 errorCode = -1;
2255 goto teardown;
2256 }
2257
2258 startLoc = clang_getLocation(TU, file, line, column);
2259 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002260 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002261 column);
2262 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002263 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002264 }
2265
2266 endLoc = clang_getLocation(TU, file, second_line, second_column);
2267 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002268 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002269 second_line, second_column);
2270 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002271 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002272 }
2273
2274 range = clang_getRange(startLoc, endLoc);
2275 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002276
2277 if (checkForErrors(TU) != 0) {
2278 errorCode = -1;
2279 goto teardown;
2280 }
2281
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002282 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2283 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002284
2285 if (checkForErrors(TU) != 0) {
2286 errorCode = -1;
2287 goto teardown;
2288 }
2289
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002290 for (i = 0; i != num_tokens; ++i) {
2291 const char *kind = "<unknown>";
2292 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2293 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2294 unsigned start_line, start_column, end_line, end_column;
2295
2296 switch (clang_getTokenKind(tokens[i])) {
2297 case CXToken_Punctuation: kind = "Punctuation"; break;
2298 case CXToken_Keyword: kind = "Keyword"; break;
2299 case CXToken_Identifier: kind = "Identifier"; break;
2300 case CXToken_Literal: kind = "Literal"; break;
2301 case CXToken_Comment: kind = "Comment"; break;
2302 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002303 clang_getSpellingLocation(clang_getRangeStart(extent),
2304 0, &start_line, &start_column, 0);
2305 clang_getSpellingLocation(clang_getRangeEnd(extent),
2306 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002307 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002308 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002309 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002310 if (!clang_isInvalid(cursors[i].kind)) {
2311 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002312 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002313 }
2314 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002315 }
2316 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002317 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002318
2319 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002320 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002321 clang_disposeTranslationUnit(TU);
2322 clang_disposeIndex(CIdx);
2323 free(filename);
2324 free_remapped_files(unsaved_files, num_unsaved_files);
2325 return errorCode;
2326}
2327
Ted Kremenek0d435192009-11-17 18:13:31 +00002328/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002329/* USR printing. */
2330/******************************************************************************/
2331
2332static int insufficient_usr(const char *kind, const char *usage) {
2333 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2334 return 1;
2335}
2336
2337static unsigned isUSR(const char *s) {
2338 return s[0] == 'c' && s[1] == ':';
2339}
2340
2341static int not_usr(const char *s, const char *arg) {
2342 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2343 return 1;
2344}
2345
2346static void print_usr(CXString usr) {
2347 const char *s = clang_getCString(usr);
2348 printf("%s\n", s);
2349 clang_disposeString(usr);
2350}
2351
2352static void display_usrs() {
2353 fprintf(stderr, "-print-usrs options:\n"
2354 " ObjCCategory <class name> <category name>\n"
2355 " ObjCClass <class name>\n"
2356 " ObjCIvar <ivar name> <class USR>\n"
2357 " ObjCMethod <selector> [0=class method|1=instance method] "
2358 "<class USR>\n"
2359 " ObjCProperty <property name> <class USR>\n"
2360 " ObjCProtocol <protocol name>\n");
2361}
2362
2363int print_usrs(const char **I, const char **E) {
2364 while (I != E) {
2365 const char *kind = *I;
2366 unsigned len = strlen(kind);
2367 switch (len) {
2368 case 8:
2369 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2370 if (I + 2 >= E)
2371 return insufficient_usr(kind, "<ivar name> <class USR>");
2372 if (!isUSR(I[2]))
2373 return not_usr("<class USR>", I[2]);
2374 else {
2375 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002376 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002377 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002378 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2379 }
2380
2381 I += 3;
2382 continue;
2383 }
2384 break;
2385 case 9:
2386 if (memcmp(kind, "ObjCClass", 9) == 0) {
2387 if (I + 1 >= E)
2388 return insufficient_usr(kind, "<class name>");
2389 print_usr(clang_constructUSR_ObjCClass(I[1]));
2390 I += 2;
2391 continue;
2392 }
2393 break;
2394 case 10:
2395 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2396 if (I + 3 >= E)
2397 return insufficient_usr(kind, "<method selector> "
2398 "[0=class method|1=instance method] <class USR>");
2399 if (!isUSR(I[3]))
2400 return not_usr("<class USR>", I[3]);
2401 else {
2402 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002403 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002404 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002405 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2406 }
2407 I += 4;
2408 continue;
2409 }
2410 break;
2411 case 12:
2412 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2413 if (I + 2 >= E)
2414 return insufficient_usr(kind, "<class name> <category name>");
2415 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2416 I += 3;
2417 continue;
2418 }
2419 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2420 if (I + 1 >= E)
2421 return insufficient_usr(kind, "<protocol name>");
2422 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2423 I += 2;
2424 continue;
2425 }
2426 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2427 if (I + 2 >= E)
2428 return insufficient_usr(kind, "<property name> <class USR>");
2429 if (!isUSR(I[2]))
2430 return not_usr("<class USR>", I[2]);
2431 else {
2432 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002433 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002434 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002435 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2436 }
2437 I += 3;
2438 continue;
2439 }
2440 break;
2441 default:
2442 break;
2443 }
2444 break;
2445 }
2446
2447 if (I != E) {
2448 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2449 display_usrs();
2450 return 1;
2451 }
2452 return 0;
2453}
2454
2455int print_usrs_file(const char *file_name) {
2456 char line[2048];
2457 const char *args[128];
2458 unsigned numChars = 0;
2459
2460 FILE *fp = fopen(file_name, "r");
2461 if (!fp) {
2462 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2463 return 1;
2464 }
2465
2466 /* This code is not really all that safe, but it works fine for testing. */
2467 while (!feof(fp)) {
2468 char c = fgetc(fp);
2469 if (c == '\n') {
2470 unsigned i = 0;
2471 const char *s = 0;
2472
2473 if (numChars == 0)
2474 continue;
2475
2476 line[numChars] = '\0';
2477 numChars = 0;
2478
2479 if (line[0] == '/' && line[1] == '/')
2480 continue;
2481
2482 s = strtok(line, " ");
2483 while (s) {
2484 args[i] = s;
2485 ++i;
2486 s = strtok(0, " ");
2487 }
2488 if (print_usrs(&args[0], &args[i]))
2489 return 1;
2490 }
2491 else
2492 line[numChars++] = c;
2493 }
2494
2495 fclose(fp);
2496 return 0;
2497}
2498
2499/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002500/* Command line processing. */
2501/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002502int write_pch_file(const char *filename, int argc, const char *argv[]) {
2503 CXIndex Idx;
2504 CXTranslationUnit TU;
2505 struct CXUnsavedFile *unsaved_files = 0;
2506 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002507 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002508
2509 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2510
2511 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2512 clang_disposeIndex(Idx);
2513 return -1;
2514 }
2515
2516 TU = clang_parseTranslationUnit(Idx, 0,
2517 argv + num_unsaved_files,
2518 argc - num_unsaved_files,
2519 unsaved_files,
2520 num_unsaved_files,
2521 CXTranslationUnit_Incomplete);
2522 if (!TU) {
2523 fprintf(stderr, "Unable to load translation unit!\n");
2524 free_remapped_files(unsaved_files, num_unsaved_files);
2525 clang_disposeIndex(Idx);
2526 return 1;
2527 }
2528
Douglas Gregor39c411f2011-07-06 16:43:36 +00002529 switch (clang_saveTranslationUnit(TU, filename,
2530 clang_defaultSaveOptions(TU))) {
2531 case CXSaveError_None:
2532 break;
2533
2534 case CXSaveError_TranslationErrors:
2535 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2536 filename);
2537 result = 2;
2538 break;
2539
2540 case CXSaveError_InvalidTU:
2541 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2542 filename);
2543 result = 3;
2544 break;
2545
2546 case CXSaveError_Unknown:
2547 default:
2548 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2549 result = 1;
2550 break;
2551 }
2552
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002553 clang_disposeTranslationUnit(TU);
2554 free_remapped_files(unsaved_files, num_unsaved_files);
2555 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002556 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002557}
2558
2559/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002560/* Serialized diagnostics. */
2561/******************************************************************************/
2562
2563static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2564 switch (error) {
2565 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2566 case CXLoadDiag_None: break;
2567 case CXLoadDiag_Unknown: return "Unknown";
2568 case CXLoadDiag_InvalidFile: return "Invalid File";
2569 }
2570 return "None";
2571}
2572
2573static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2574 switch (severity) {
2575 case CXDiagnostic_Note: return "note";
2576 case CXDiagnostic_Error: return "error";
2577 case CXDiagnostic_Fatal: return "fatal";
2578 case CXDiagnostic_Ignored: return "ignored";
2579 case CXDiagnostic_Warning: return "warning";
2580 }
2581 return "unknown";
2582}
2583
2584static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002585 if (indent == 0)
2586 return;
2587 fprintf(stderr, "+");
2588 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002589 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002590 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002591 --indent;
2592 }
2593}
2594
2595static void printLocation(CXSourceLocation L) {
2596 CXFile File;
2597 CXString FileName;
2598 unsigned line, column, offset;
2599
2600 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2601 FileName = clang_getFileName(File);
2602
2603 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2604 clang_disposeString(FileName);
2605}
2606
2607static void printRanges(CXDiagnostic D, unsigned indent) {
2608 unsigned i, n = clang_getDiagnosticNumRanges(D);
2609
2610 for (i = 0; i < n; ++i) {
2611 CXSourceLocation Start, End;
2612 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2613 Start = clang_getRangeStart(SR);
2614 End = clang_getRangeEnd(SR);
2615
2616 printIndent(indent);
2617 fprintf(stderr, "Range: ");
2618 printLocation(Start);
2619 fprintf(stderr, " ");
2620 printLocation(End);
2621 fprintf(stderr, "\n");
2622 }
2623}
2624
2625static void printFixIts(CXDiagnostic D, unsigned indent) {
2626 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002627 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002628 for (i = 0 ; i < n; ++i) {
2629 CXSourceRange ReplacementRange;
2630 CXString text;
2631 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2632
2633 printIndent(indent);
2634 fprintf(stderr, "FIXIT: (");
2635 printLocation(clang_getRangeStart(ReplacementRange));
2636 fprintf(stderr, " - ");
2637 printLocation(clang_getRangeEnd(ReplacementRange));
2638 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2639 clang_disposeString(text);
2640 }
2641}
2642
2643static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002644 unsigned i, n;
2645
Ted Kremenek15322172011-11-10 08:43:12 +00002646 if (!Diags)
2647 return;
2648
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002649 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002650 for (i = 0; i < n; ++i) {
2651 CXSourceLocation DiagLoc;
2652 CXDiagnostic D;
2653 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002654 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002655 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002656 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002657
2658 D = clang_getDiagnosticInSet(Diags, i);
2659 DiagLoc = clang_getDiagnosticLocation(D);
2660 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2661 FileName = clang_getFileName(File);
2662 DiagSpelling = clang_getDiagnosticSpelling(D);
2663
2664 printIndent(indent);
2665
2666 fprintf(stderr, "%s:%d:%d: %s: %s",
2667 clang_getCString(FileName),
2668 line,
2669 column,
2670 getSeverityString(clang_getDiagnosticSeverity(D)),
2671 clang_getCString(DiagSpelling));
2672
2673 DiagOption = clang_getDiagnosticOption(D, 0);
2674 DiagOptionStr = clang_getCString(DiagOption);
2675 if (DiagOptionStr) {
2676 fprintf(stderr, " [%s]", DiagOptionStr);
2677 }
2678
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002679 DiagCat = clang_getDiagnosticCategoryText(D);
2680 DiagCatStr = clang_getCString(DiagCat);
2681 if (DiagCatStr) {
2682 fprintf(stderr, " [%s]", DiagCatStr);
2683 }
2684
Ted Kremenek15322172011-11-10 08:43:12 +00002685 fprintf(stderr, "\n");
2686
2687 printRanges(D, indent);
2688 printFixIts(D, indent);
2689
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002690 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002691 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2692
2693 clang_disposeString(FileName);
2694 clang_disposeString(DiagSpelling);
2695 clang_disposeString(DiagOption);
2696 }
2697}
2698
2699static int read_diagnostics(const char *filename) {
2700 enum CXLoadDiag_Error error;
2701 CXString errorString;
2702 CXDiagnosticSet Diags = 0;
2703
2704 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2705 if (!Diags) {
2706 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2707 getDiagnosticCodeStr(error),
2708 clang_getCString(errorString));
2709 clang_disposeString(errorString);
2710 return 1;
2711 }
2712
2713 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002714 fprintf(stderr, "Number of diagnostics: %d\n",
2715 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002716 clang_disposeDiagnosticSet(Diags);
2717 return 0;
2718}
2719
2720/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002721/* Command line processing. */
2722/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002723
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002724static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002725 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002726 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002727 if (strcmp(s, "-usrs") == 0)
2728 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002729 if (strncmp(s, "-memory-usage", 13) == 0)
2730 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002731 return NULL;
2732}
2733
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002734static void print_usage(void) {
2735 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002736 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002737 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002738 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002739 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002740 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002741 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002742 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002743 "[FileCheck prefix]\n");
2744 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002745 " c-index-test -test-load-tu <AST file> <symbol filter> "
2746 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002747 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2748 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002749 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002750 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002751 " c-index-test -test-load-source-memory-usage "
2752 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002753 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2754 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002755 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002756 " c-index-test -test-load-source-usrs-memory-usage "
2757 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002758 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2759 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002760 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002761 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002762 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002763 " c-index-test -test-print-typekind {<args>}*\n"
2764 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002765 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002766 " c-index-test -write-pch <file> <compiler arguments>\n");
2767 fprintf(stderr,
2768 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002769 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002770 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002771 " all - load all symbols, including those from PCH\n"
2772 " local - load all symbols except those in PCH\n"
2773 " category - only load ObjC categories (non-PCH)\n"
2774 " interface - only load ObjC interfaces (non-PCH)\n"
2775 " protocol - only load ObjC protocols (non-PCH)\n"
2776 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002777 " typedef - only load typdefs (non-PCH)\n"
2778 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002779}
2780
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002781/***/
2782
2783int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002784 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002785 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2786 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002787 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002788 return perform_code_completion(argc, argv, 0);
2789 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2790 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002791 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2792 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002793 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2794 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002795 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2796 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002797 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2798 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002799 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002800 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002801 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002802 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2803 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002804 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002805 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2806 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2807 if (I) {
2808 int trials = atoi(argv[2]);
2809 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2810 NULL);
2811 }
2812 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002813 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002814 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002815
2816 PostVisitTU postVisit = 0;
2817 if (strstr(argv[1], "-memory-usage"))
2818 postVisit = PrintMemoryUsage;
2819
Ted Kremenek7d405622010-01-12 23:34:26 +00002820 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002821 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2822 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002823 }
2824 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002825 return perform_file_scan(argv[2], argv[3],
2826 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002827 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2828 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002829 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2830 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2831 PrintInclusionStack);
2832 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2833 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2834 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002835 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2836 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2837 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002838 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2839 return perform_test_load_source(argc - 2, argv + 2, "all",
2840 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002841 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2842 if (argc > 2)
2843 return print_usrs(argv + 2, argv + argc);
2844 else {
2845 display_usrs();
2846 return 1;
2847 }
2848 }
2849 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2850 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002851 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2852 return write_pch_file(argv[2], argc - 3, argv + 3);
2853
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002854 print_usage();
2855 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002856}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002857
2858/***/
2859
2860/* We intentionally run in a separate thread to ensure we at least minimal
2861 * testing of a multithreaded environment (for example, having a reduced stack
2862 * size). */
2863
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002864typedef struct thread_info {
2865 int argc;
2866 const char **argv;
2867 int result;
2868} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002869void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002870 thread_info *client_data = client_data_v;
2871 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00002872#ifdef __CYGWIN__
2873 fflush(stdout); /* stdout is not flushed on Cygwin. */
2874#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002875}
2876
2877int main(int argc, const char **argv) {
2878 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002879
Douglas Gregor61605982010-10-27 16:00:01 +00002880 if (getenv("CINDEXTEST_NOTHREADS"))
2881 return cindextest_main(argc, argv);
2882
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002883 client_data.argc = argc;
2884 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002885 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002886 return client_data.result;
2887}