blob: 73e3e589ce0ab1f4083fa521525a6cd771becffd [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
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000165static void PrintCString(const char *Prefix, const char *CStr) {
166 printf(" %s=[", Prefix);
167 if (CStr != NULL && CStr[0] != '\0') {
168 for ( ; *CStr; ++CStr) {
169 const char C = *CStr;
170 switch (C) {
171 case '\n': printf("\\n"); break;
172 case '\r': printf("\\r"); break;
173 case '\t': printf("\\t"); break;
174 case '\v': printf("\\v"); break;
175 case '\f': printf("\\f"); break;
176 default: putchar(C); break;
177 }
178 }
179 }
180 printf("]");
181}
182
Douglas Gregor430d7a12011-07-25 17:48:11 +0000183static void PrintRange(CXSourceRange R, const char *str) {
184 CXFile begin_file, end_file;
185 unsigned begin_line, begin_column, end_line, end_column;
186
187 clang_getSpellingLocation(clang_getRangeStart(R),
188 &begin_file, &begin_line, &begin_column, 0);
189 clang_getSpellingLocation(clang_getRangeEnd(R),
190 &end_file, &end_line, &end_column, 0);
191 if (!begin_file || !end_file)
192 return;
193
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000194 if (str)
195 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000196 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
197}
198
Douglas Gregor358559d2010-10-02 22:49:11 +0000199int want_display_name = 0;
200
Douglas Gregorcc889662012-05-08 00:14:45 +0000201static void printVersion(const char *Prefix, CXVersion Version) {
202 if (Version.Major < 0)
203 return;
204 printf("%s%d", Prefix, Version.Major);
205
206 if (Version.Minor < 0)
207 return;
208 printf(".%d", Version.Minor);
209
210 if (Version.Subminor < 0)
211 return;
212 printf(".%d", Version.Subminor);
213}
214
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000215static void PrintCursor(CXCursor Cursor) {
216 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000217 if (clang_isInvalid(Cursor.kind)) {
218 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
219 printf("Invalid Cursor => %s", clang_getCString(ks));
220 clang_disposeString(ks);
221 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000222 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000223 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000224 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000225 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000226 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000227 CXCursor *overridden;
228 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000229 unsigned RefNameRangeNr;
230 CXSourceRange CursorExtent;
231 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000232 int AlwaysUnavailable;
233 int AlwaysDeprecated;
234 CXString UnavailableMessage;
235 CXString DeprecatedMessage;
236 CXPlatformAvailability PlatformAvailability[2];
237 int NumPlatformAvailability;
238 int I;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000239 CXString RawComment;
240 const char *RawCommentCString;
241 CXString BriefComment;
242 const char *BriefCommentCString;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000243
Ted Kremeneke68fff62010-02-17 00:41:32 +0000244 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000245 string = want_display_name? clang_getCursorDisplayName(Cursor)
246 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000247 printf("%s=%s", clang_getCString(ks),
248 clang_getCString(string));
249 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000250 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000251
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000252 Referenced = clang_getCursorReferenced(Cursor);
253 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000254 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
255 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
256 printf("[");
257 for (I = 0; I != N; ++I) {
258 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000259 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000260 if (I)
261 printf(", ");
262
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000263 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000264 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000265 printf("%d:%d", line, column);
266 }
267 printf("]");
268 } else {
269 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000270 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000271 printf(":%d:%d", line, column);
272 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000273 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000274
275 if (clang_isCursorDefinition(Cursor))
276 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000277
278 switch (clang_getCursorAvailability(Cursor)) {
279 case CXAvailability_Available:
280 break;
281
282 case CXAvailability_Deprecated:
283 printf(" (deprecated)");
284 break;
285
286 case CXAvailability_NotAvailable:
287 printf(" (unavailable)");
288 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000289
290 case CXAvailability_NotAccessible:
291 printf(" (inaccessible)");
292 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000293 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000294
Douglas Gregorcc889662012-05-08 00:14:45 +0000295 NumPlatformAvailability
296 = clang_getCursorPlatformAvailability(Cursor,
297 &AlwaysDeprecated,
298 &DeprecatedMessage,
299 &AlwaysUnavailable,
300 &UnavailableMessage,
301 PlatformAvailability, 2);
302 if (AlwaysUnavailable) {
303 printf(" (always unavailable: \"%s\")",
304 clang_getCString(UnavailableMessage));
305 } else if (AlwaysDeprecated) {
306 printf(" (always deprecated: \"%s\")",
307 clang_getCString(DeprecatedMessage));
308 } else {
309 for (I = 0; I != NumPlatformAvailability; ++I) {
310 if (I >= 2)
311 break;
312
313 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
314 if (PlatformAvailability[I].Unavailable)
315 printf(", unavailable");
316 else {
317 printVersion(", introduced=", PlatformAvailability[I].Introduced);
318 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
319 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
320 }
321 if (clang_getCString(PlatformAvailability[I].Message)[0])
322 printf(", message=\"%s\"",
323 clang_getCString(PlatformAvailability[I].Message));
324 printf(")");
325 }
326 }
327 for (I = 0; I != NumPlatformAvailability; ++I) {
328 if (I >= 2)
329 break;
330 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
331 }
332
333 clang_disposeString(DeprecatedMessage);
334 clang_disposeString(UnavailableMessage);
335
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000336 if (clang_CXXMethod_isStatic(Cursor))
337 printf(" (static)");
338 if (clang_CXXMethod_isVirtual(Cursor))
339 printf(" (virtual)");
340
Ted Kremenek95f33552010-08-26 01:42:22 +0000341 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
342 CXType T =
343 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
344 CXString S = clang_getTypeKindSpelling(T.kind);
345 printf(" [IBOutletCollection=%s]", clang_getCString(S));
346 clang_disposeString(S);
347 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000348
349 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
350 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
351 unsigned isVirtual = clang_isVirtualBase(Cursor);
352 const char *accessStr = 0;
353
354 switch (access) {
355 case CX_CXXInvalidAccessSpecifier:
356 accessStr = "invalid"; break;
357 case CX_CXXPublic:
358 accessStr = "public"; break;
359 case CX_CXXProtected:
360 accessStr = "protected"; break;
361 case CX_CXXPrivate:
362 accessStr = "private"; break;
363 }
364
365 printf(" [access=%s isVirtual=%s]", accessStr,
366 isVirtual ? "true" : "false");
367 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000368
369 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
370 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
371 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
372 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000373 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000374 printf(" [Specialization of %s:%d:%d]",
375 clang_getCString(Name), line, column);
376 clang_disposeString(Name);
377 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000378
379 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
380 if (num_overridden) {
381 unsigned I;
382 printf(" [Overrides ");
383 for (I = 0; I != num_overridden; ++I) {
384 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000385 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000386 if (I)
387 printf(", ");
388 printf("@%d:%d", line, column);
389 }
390 printf("]");
391 clang_disposeOverriddenCursors(overridden);
392 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000393
394 if (Cursor.kind == CXCursor_InclusionDirective) {
395 CXFile File = clang_getIncludedFile(Cursor);
396 CXString Included = clang_getFileName(File);
397 printf(" (%s)", clang_getCString(Included));
398 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000399
400 if (clang_isFileMultipleIncludeGuarded(TU, File))
401 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000402 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000403
404 CursorExtent = clang_getCursorExtent(Cursor);
405 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
406 CXNameRange_WantQualifier
407 | CXNameRange_WantSinglePiece
408 | CXNameRange_WantTemplateArgs,
409 0);
410 if (!clang_equalRanges(CursorExtent, RefNameRange))
411 PrintRange(RefNameRange, "SingleRefName");
412
413 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
414 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
415 CXNameRange_WantQualifier
416 | CXNameRange_WantTemplateArgs,
417 RefNameRangeNr);
418 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
419 break;
420 if (!clang_equalRanges(CursorExtent, RefNameRange))
421 PrintRange(RefNameRange, "RefName");
422 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000423
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000424 RawComment = clang_Cursor_getRawCommentText(Cursor);
425 RawCommentCString = clang_getCString(RawComment);
426 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
427 PrintCString("RawComment", RawCommentCString);
428 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000429
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000430 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
431 BriefCommentCString = clang_getCString(BriefComment);
432 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
433 PrintCString("BriefComment", BriefCommentCString);
434 clang_disposeString(BriefComment);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000435 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000436 clang_disposeString(RawComment);
Steve Naroff699a07d2009-09-25 21:32:34 +0000437 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000438}
Steve Naroff89922f82009-08-31 00:59:03 +0000439
Ted Kremeneke68fff62010-02-17 00:41:32 +0000440static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000441 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000442 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000443 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000444 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000445 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000446 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000447 clang_disposeString(source);
448 return "<invalid loc>";
449 }
450 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000451 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000452 clang_disposeString(source);
453 return b;
454 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000455}
456
Ted Kremenek0d435192009-11-17 18:13:31 +0000457/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000458/* Callbacks. */
459/******************************************************************************/
460
461typedef void (*PostVisitTU)(CXTranslationUnit);
462
Douglas Gregora88084b2010-02-18 18:08:43 +0000463void PrintDiagnostic(CXDiagnostic Diagnostic) {
464 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000465 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000466 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000467 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000468 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
469 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000470 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000471
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000472 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000473 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000474
Douglas Gregor274f1902010-02-22 23:17:23 +0000475 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
476 fprintf(stderr, "%s\n", clang_getCString(Msg));
477 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000478
Douglas Gregora9b06d42010-11-09 06:24:54 +0000479 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
480 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000481 if (!file)
482 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000483
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000484 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000485 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000486 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000487 CXSourceRange range;
488 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
489 CXSourceLocation start = clang_getRangeStart(range);
490 CXSourceLocation end = clang_getRangeEnd(range);
491 unsigned start_line, start_column, end_line, end_column;
492 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000493 clang_getSpellingLocation(start, &start_file, &start_line,
494 &start_column, 0);
495 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000496 if (clang_equalLocations(start, end)) {
497 /* Insertion. */
498 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000499 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000500 clang_getCString(insertion_text), start_line, start_column);
501 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
502 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000503 if (start_file == file && end_file == file) {
504 fprintf(out, "FIX-IT: Remove ");
505 PrintExtent(out, start_line, start_column, end_line, end_column);
506 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000507 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000508 } else {
509 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000510 if (start_file == end_file) {
511 fprintf(out, "FIX-IT: Replace ");
512 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000513 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000514 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000515 break;
516 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000517 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000518 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000519}
520
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000521void PrintDiagnosticSet(CXDiagnosticSet Set) {
522 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
523 for ( ; i != n ; ++i) {
524 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
525 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000526 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000527 if (ChildDiags)
528 PrintDiagnosticSet(ChildDiags);
529 }
530}
531
532void PrintDiagnostics(CXTranslationUnit TU) {
533 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
534 PrintDiagnosticSet(TUSet);
535 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000536}
537
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000538void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000539 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000540 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000541 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000542 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000543 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000544 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000545 unsigned long amount = usage.entries[i].amount;
546 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000547 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000548 ((double) amount)/(1024*1024));
549 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000550 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000551 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000552 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000553}
554
Ted Kremenekce2ae882010-01-26 17:59:48 +0000555/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000556/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000557/******************************************************************************/
558
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000559static const char *FileCheckPrefix = "CHECK";
560
Douglas Gregora7bde202010-01-19 00:34:46 +0000561static void PrintCursorExtent(CXCursor C) {
562 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000563 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000564}
565
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000566/* Data used by all of the visitors. */
567typedef struct {
568 CXTranslationUnit TU;
569 enum CXCursorKind *Filter;
570} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000571
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000572
Ted Kremeneke68fff62010-02-17 00:41:32 +0000573enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000574 CXCursor Parent,
575 CXClientData ClientData) {
576 VisitorData *Data = (VisitorData *)ClientData;
577 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000578 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000579 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000580 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000581 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000582 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000583 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000584 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000585 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000586 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000587 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000588
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000589 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000590}
Steve Naroff50398192009-08-28 15:28:48 +0000591
Ted Kremeneke68fff62010-02-17 00:41:32 +0000592static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000593 CXCursor Parent,
594 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000595 const char *startBuf, *endBuf;
596 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
597 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000598 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000599
Douglas Gregorb6998662010-01-19 19:34:47 +0000600 if (Cursor.kind != CXCursor_FunctionDecl ||
601 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000602 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000603
604 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
605 &startLine, &startColumn,
606 &endLine, &endColumn);
607 /* Probe the entire body, looking for both decls and refs. */
608 curLine = startLine;
609 curColumn = startColumn;
610
611 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000612 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000613 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000614 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000615
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000616 if (*startBuf == '\n') {
617 startBuf++;
618 curLine++;
619 curColumn = 1;
620 } else if (*startBuf != '\t')
621 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000622
Douglas Gregor98258af2010-01-18 22:46:11 +0000623 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000624 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000625
Douglas Gregor1db19de2010-01-19 21:36:55 +0000626 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000627 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000628 CXSourceLocation RefLoc
629 = clang_getLocation(Data->TU, file, curLine, curColumn);
630 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000631 if (Ref.kind == CXCursor_NoDeclFound) {
632 /* Nothing found here; that's fine. */
633 } else if (Ref.kind != CXCursor_FunctionDecl) {
634 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
635 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000636 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000637 printf("\n");
638 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000639 }
Ted Kremenek74844072010-02-17 00:41:20 +0000640 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000641 startBuf++;
642 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000643
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000644 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000645}
646
Ted Kremenek7d405622010-01-12 23:34:26 +0000647/******************************************************************************/
648/* USR testing. */
649/******************************************************************************/
650
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000651enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
652 CXClientData ClientData) {
653 VisitorData *Data = (VisitorData *)ClientData;
654 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000655 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000656 const char *cstr = clang_getCString(USR);
657 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000658 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000659 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000660 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000661 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
662
Douglas Gregora7bde202010-01-19 00:34:46 +0000663 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000664 printf("\n");
665 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000666
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000667 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000668 }
669
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000670 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000671}
672
673/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000674/* Inclusion stack testing. */
675/******************************************************************************/
676
677void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
678 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000679
Ted Kremenek16b55a72010-01-26 19:31:51 +0000680 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000681 CXString fname;
682
683 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000684 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000685 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000686
Ted Kremenek16b55a72010-01-26 19:31:51 +0000687 for (i = 0; i < includeStackLen; ++i) {
688 CXFile includingFile;
689 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000690 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
691 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000692 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000693 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000694 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000695 }
696 printf("\n");
697}
698
699void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000700 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000701}
702
703/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000704/* Linkage testing. */
705/******************************************************************************/
706
707static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
708 CXClientData d) {
709 const char *linkage = 0;
710
711 if (clang_isInvalid(clang_getCursorKind(cursor)))
712 return CXChildVisit_Recurse;
713
714 switch (clang_getCursorLinkage(cursor)) {
715 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000716 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
717 case CXLinkage_Internal: linkage = "Internal"; break;
718 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
719 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000720 }
721
722 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000723 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000724 printf("linkage=%s\n", linkage);
725 }
726
727 return CXChildVisit_Recurse;
728}
729
730/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000731/* Typekind testing. */
732/******************************************************************************/
733
734static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
735 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000736 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
737 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000738 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000739 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000740 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000741 if (clang_isConstQualifiedType(T))
742 printf(" const");
743 if (clang_isVolatileQualifiedType(T))
744 printf(" volatile");
745 if (clang_isRestrictQualifiedType(T))
746 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000747 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000748 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000749 {
750 CXType CT = clang_getCanonicalType(T);
751 if (!clang_equalTypes(T, CT)) {
752 CXString CS = clang_getTypeKindSpelling(CT.kind);
753 printf(" [canonical=%s]", clang_getCString(CS));
754 clang_disposeString(CS);
755 }
756 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000757 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000758 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000759 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000760 if (RT.kind != CXType_Invalid) {
761 CXString RS = clang_getTypeKindSpelling(RT.kind);
762 printf(" [result=%s]", clang_getCString(RS));
763 clang_disposeString(RS);
764 }
765 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000766 /* Print the argument types if they exist. */
767 {
768 int numArgs = clang_Cursor_getNumArguments(cursor);
769 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000770 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000771 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000772 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000773 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
774 if (T.kind != CXType_Invalid) {
775 CXString S = clang_getTypeKindSpelling(T.kind);
776 printf(" %s", clang_getCString(S));
777 clang_disposeString(S);
778 }
779 }
780 printf("]");
781 }
782 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000783 /* Print if this is a non-POD type. */
784 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000785
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000786 printf("\n");
787 }
788 return CXChildVisit_Recurse;
789}
790
791
792/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000793/* Loading ASTs/source. */
794/******************************************************************************/
795
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000796static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000797 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000798 CXCursorVisitor Visitor,
799 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000800
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000801 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000802 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000803
804 if (Visitor) {
805 enum CXCursorKind K = CXCursor_NotImplemented;
806 enum CXCursorKind *ck = &K;
807 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000808
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000809 /* Perform some simple filtering. */
810 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000811 else if (!strcmp(filter, "all-display") ||
812 !strcmp(filter, "local-display")) {
813 ck = NULL;
814 want_display_name = 1;
815 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000816 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000817 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
818 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
819 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
820 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
821 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
822 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
823 else {
824 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
825 return 1;
826 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000827
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000828 Data.TU = TU;
829 Data.Filter = ck;
830 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000831 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000832
Ted Kremenekce2ae882010-01-26 17:59:48 +0000833 if (PV)
834 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000835
Douglas Gregora88084b2010-02-18 18:08:43 +0000836 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000837 if (checkForErrors(TU) != 0) {
838 clang_disposeTranslationUnit(TU);
839 return -1;
840 }
841
Ted Kremenek0d435192009-11-17 18:13:31 +0000842 clang_disposeTranslationUnit(TU);
843 return 0;
844}
845
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000846int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000847 const char *prefix, CXCursorVisitor Visitor,
848 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000849 CXIndex Idx;
850 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000851 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000852 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000853 !strcmp(filter, "local") ? 1 : 0,
854 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000855
Ted Kremenek020a0952010-02-11 07:41:25 +0000856 if (!CreateTranslationUnit(Idx, file, &TU)) {
857 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000858 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000859 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000860
Ted Kremenek020a0952010-02-11 07:41:25 +0000861 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
862 clang_disposeIndex(Idx);
863 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000864}
865
Ted Kremenekce2ae882010-01-26 17:59:48 +0000866int perform_test_load_source(int argc, const char **argv,
867 const char *filter, CXCursorVisitor Visitor,
868 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000869 CXIndex Idx;
870 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000871 struct CXUnsavedFile *unsaved_files = 0;
872 int num_unsaved_files = 0;
873 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000874
Daniel Dunbarada487d2009-12-01 02:03:10 +0000875 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000876 (!strcmp(filter, "local") ||
877 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000878 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000879
Ted Kremenek020a0952010-02-11 07:41:25 +0000880 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
881 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000882 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000883 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000884
Douglas Gregordca8ee82011-05-06 16:33:08 +0000885 TU = clang_parseTranslationUnit(Idx, 0,
886 argv + num_unsaved_files,
887 argc - num_unsaved_files,
888 unsaved_files, num_unsaved_files,
889 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000890 if (!TU) {
891 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000892 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000893 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000894 return 1;
895 }
896
Ted Kremenekce2ae882010-01-26 17:59:48 +0000897 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000898 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000899 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000900 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000901}
902
Douglas Gregorabc563f2010-07-19 21:46:24 +0000903int perform_test_reparse_source(int argc, const char **argv, int trials,
904 const char *filter, CXCursorVisitor Visitor,
905 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000906 CXIndex Idx;
907 CXTranslationUnit TU;
908 struct CXUnsavedFile *unsaved_files = 0;
909 int num_unsaved_files = 0;
910 int result;
911 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000912 int remap_after_trial = 0;
913 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000914
915 Idx = clang_createIndex(/* excludeDeclsFromPCH */
916 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000917 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000918
Douglas Gregorabc563f2010-07-19 21:46:24 +0000919 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
920 clang_disposeIndex(Idx);
921 return -1;
922 }
923
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000924 /* Load the initial translation unit -- we do this without honoring remapped
925 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000926 TU = clang_parseTranslationUnit(Idx, 0,
927 argv + num_unsaved_files,
928 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000929 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000930 if (!TU) {
931 fprintf(stderr, "Unable to load translation unit!\n");
932 free_remapped_files(unsaved_files, num_unsaved_files);
933 clang_disposeIndex(Idx);
934 return 1;
935 }
936
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000937 if (checkForErrors(TU) != 0)
938 return -1;
939
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000940 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
941 remap_after_trial =
942 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
943 }
944
Douglas Gregorabc563f2010-07-19 21:46:24 +0000945 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000946 if (clang_reparseTranslationUnit(TU,
947 trial >= remap_after_trial ? num_unsaved_files : 0,
948 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000949 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000950 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000951 clang_disposeTranslationUnit(TU);
952 free_remapped_files(unsaved_files, num_unsaved_files);
953 clang_disposeIndex(Idx);
954 return -1;
955 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000956
957 if (checkForErrors(TU) != 0)
958 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000959 }
960
961 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000962
Douglas Gregorabc563f2010-07-19 21:46:24 +0000963 free_remapped_files(unsaved_files, num_unsaved_files);
964 clang_disposeIndex(Idx);
965 return result;
966}
967
Ted Kremenek0d435192009-11-17 18:13:31 +0000968/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000969/* Logic for testing clang_getCursor(). */
970/******************************************************************************/
971
Douglas Gregordd3e5542011-05-04 00:14:37 +0000972static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000973 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000974 unsigned end_line, unsigned end_col,
975 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000976 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000977 if (prefix)
978 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000979 PrintExtent(stdout, start_line, start_col, end_line, end_col);
980 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000981 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000982 printf("\n");
983}
984
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000985static int perform_file_scan(const char *ast_file, const char *source_file,
986 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000987 CXIndex Idx;
988 CXTranslationUnit TU;
989 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000990 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000991 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000992 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000993 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000994
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000995 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
996 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000997 fprintf(stderr, "Could not create Index\n");
998 return 1;
999 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001000
Ted Kremenek1c6da172009-11-17 19:37:36 +00001001 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1002 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001003
Ted Kremenek1c6da172009-11-17 19:37:36 +00001004 if ((fp = fopen(source_file, "r")) == NULL) {
1005 fprintf(stderr, "Could not open '%s'\n", source_file);
1006 return 1;
1007 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001008
Douglas Gregorb9790342010-01-22 21:44:22 +00001009 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001010 for (;;) {
1011 CXCursor cursor;
1012 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001013
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001014 if (c == '\n') {
1015 ++line;
1016 col = 1;
1017 } else
1018 ++col;
1019
1020 /* Check the cursor at this position, and dump the previous one if we have
1021 * found something new.
1022 */
1023 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1024 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1025 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001026 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001027 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001028 start_line = line;
1029 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001030 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001031 if (c == EOF)
1032 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001033
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001034 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001035 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001036
Ted Kremenek1c6da172009-11-17 19:37:36 +00001037 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001038 clang_disposeTranslationUnit(TU);
1039 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001040 return 0;
1041}
1042
1043/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001044/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001045/******************************************************************************/
1046
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001047/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1048 on failure. If successful, the pointer *filename will contain newly-allocated
1049 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001050int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001051 unsigned *column, unsigned *second_line,
1052 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001053 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001054 const char *last_colon = strrchr(input, ':');
1055 unsigned values[4], i;
1056 unsigned num_values = (second_line && second_column)? 4 : 2;
1057
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001058 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001059 if (!last_colon || last_colon == input) {
1060 if (num_values == 4)
1061 fprintf(stderr, "could not parse filename:line:column:line:column in "
1062 "'%s'\n", input);
1063 else
1064 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001065 return 1;
1066 }
1067
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001068 for (i = 0; i != num_values; ++i) {
1069 const char *prev_colon;
1070
1071 /* Parse the next line or column. */
1072 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1073 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001074 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001075 (i % 2 ? "column" : "line"), input);
1076 return 1;
1077 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001078
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001079 if (i + 1 == num_values)
1080 break;
1081
1082 /* Find the previous colon. */
1083 prev_colon = last_colon - 1;
1084 while (prev_colon != input && *prev_colon != ':')
1085 --prev_colon;
1086 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001087 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001088 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001089 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001090 }
1091
1092 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001093 }
1094
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001095 *line = values[0];
1096 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001097
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001098 if (second_line && second_column) {
1099 *second_line = values[2];
1100 *second_column = values[3];
1101 }
1102
Douglas Gregor88d23952009-11-09 18:19:57 +00001103 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001104 *filename = (char*)malloc(last_colon - input + 1);
1105 memcpy(*filename, input, last_colon - input);
1106 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001107 return 0;
1108}
1109
1110const char *
1111clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1112 switch (Kind) {
1113 case CXCompletionChunk_Optional: return "Optional";
1114 case CXCompletionChunk_TypedText: return "TypedText";
1115 case CXCompletionChunk_Text: return "Text";
1116 case CXCompletionChunk_Placeholder: return "Placeholder";
1117 case CXCompletionChunk_Informative: return "Informative";
1118 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1119 case CXCompletionChunk_LeftParen: return "LeftParen";
1120 case CXCompletionChunk_RightParen: return "RightParen";
1121 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1122 case CXCompletionChunk_RightBracket: return "RightBracket";
1123 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1124 case CXCompletionChunk_RightBrace: return "RightBrace";
1125 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1126 case CXCompletionChunk_RightAngle: return "RightAngle";
1127 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001128 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001129 case CXCompletionChunk_Colon: return "Colon";
1130 case CXCompletionChunk_SemiColon: return "SemiColon";
1131 case CXCompletionChunk_Equal: return "Equal";
1132 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1133 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001134 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001135
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001136 return "Unknown";
1137}
1138
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001139static int checkForErrors(CXTranslationUnit TU) {
1140 unsigned Num, i;
1141 CXDiagnostic Diag;
1142 CXString DiagStr;
1143
1144 if (!getenv("CINDEXTEST_FAILONERROR"))
1145 return 0;
1146
1147 Num = clang_getNumDiagnostics(TU);
1148 for (i = 0; i != Num; ++i) {
1149 Diag = clang_getDiagnostic(TU, i);
1150 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1151 DiagStr = clang_formatDiagnostic(Diag,
1152 clang_defaultDiagnosticDisplayOptions());
1153 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1154 clang_disposeString(DiagStr);
1155 clang_disposeDiagnostic(Diag);
1156 return -1;
1157 }
1158 clang_disposeDiagnostic(Diag);
1159 }
1160
1161 return 0;
1162}
1163
Douglas Gregor3ac73852009-11-09 16:04:45 +00001164void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001165 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001166
Douglas Gregor3ac73852009-11-09 16:04:45 +00001167 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001168 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001169 CXString text;
1170 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001171 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001172 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001173
Douglas Gregor3ac73852009-11-09 16:04:45 +00001174 if (Kind == CXCompletionChunk_Optional) {
1175 fprintf(file, "{Optional ");
1176 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001177 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001178 file);
1179 fprintf(file, "}");
1180 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001181 }
1182
1183 if (Kind == CXCompletionChunk_VerticalSpace) {
1184 fprintf(file, "{VerticalSpace }");
1185 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001186 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001187
Douglas Gregord5a20892009-11-09 17:05:28 +00001188 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001189 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001190 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001191 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001192 cstr ? cstr : "");
1193 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001194 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001195
Douglas Gregor3ac73852009-11-09 16:04:45 +00001196}
1197
1198void print_completion_result(CXCompletionResult *completion_result,
1199 CXClientData client_data) {
1200 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001201 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001202 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001203 enum CXCursorKind ParentKind;
1204 CXString ParentName;
1205
Ted Kremeneke68fff62010-02-17 00:41:32 +00001206 fprintf(file, "%s:", clang_getCString(ks));
1207 clang_disposeString(ks);
1208
Douglas Gregor3ac73852009-11-09 16:04:45 +00001209 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001210 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001211 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001212 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1213 case CXAvailability_Available:
1214 break;
1215
1216 case CXAvailability_Deprecated:
1217 fprintf(file, " (deprecated)");
1218 break;
1219
1220 case CXAvailability_NotAvailable:
1221 fprintf(file, " (unavailable)");
1222 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001223
1224 case CXAvailability_NotAccessible:
1225 fprintf(file, " (inaccessible)");
1226 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001227 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001228
1229 annotationCount = clang_getCompletionNumAnnotations(
1230 completion_result->CompletionString);
1231 if (annotationCount) {
1232 unsigned i;
1233 fprintf(file, " (");
1234 for (i = 0; i < annotationCount; ++i) {
1235 if (i != 0)
1236 fprintf(file, ", ");
1237 fprintf(file, "\"%s\"",
1238 clang_getCString(clang_getCompletionAnnotation(
1239 completion_result->CompletionString, i)));
1240 }
1241 fprintf(file, ")");
1242 }
1243
Douglas Gregorba103062012-03-27 23:34:16 +00001244 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1245 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1246 &ParentKind);
1247 if (ParentKind != CXCursor_NotImplemented) {
1248 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1249 fprintf(file, " (parent: %s '%s')",
1250 clang_getCString(KindSpelling),
1251 clang_getCString(ParentName));
1252 clang_disposeString(KindSpelling);
1253 }
1254 clang_disposeString(ParentName);
1255 }
1256
Douglas Gregor58ddb602010-08-23 23:00:57 +00001257 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001258}
1259
Douglas Gregor3da626b2011-07-07 16:03:39 +00001260void print_completion_contexts(unsigned long long contexts, FILE *file) {
1261 fprintf(file, "Completion contexts:\n");
1262 if (contexts == CXCompletionContext_Unknown) {
1263 fprintf(file, "Unknown\n");
1264 }
1265 if (contexts & CXCompletionContext_AnyType) {
1266 fprintf(file, "Any type\n");
1267 }
1268 if (contexts & CXCompletionContext_AnyValue) {
1269 fprintf(file, "Any value\n");
1270 }
1271 if (contexts & CXCompletionContext_ObjCObjectValue) {
1272 fprintf(file, "Objective-C object value\n");
1273 }
1274 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1275 fprintf(file, "Objective-C selector value\n");
1276 }
1277 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1278 fprintf(file, "C++ class type value\n");
1279 }
1280 if (contexts & CXCompletionContext_DotMemberAccess) {
1281 fprintf(file, "Dot member access\n");
1282 }
1283 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1284 fprintf(file, "Arrow member access\n");
1285 }
1286 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1287 fprintf(file, "Objective-C property access\n");
1288 }
1289 if (contexts & CXCompletionContext_EnumTag) {
1290 fprintf(file, "Enum tag\n");
1291 }
1292 if (contexts & CXCompletionContext_UnionTag) {
1293 fprintf(file, "Union tag\n");
1294 }
1295 if (contexts & CXCompletionContext_StructTag) {
1296 fprintf(file, "Struct tag\n");
1297 }
1298 if (contexts & CXCompletionContext_ClassTag) {
1299 fprintf(file, "Class name\n");
1300 }
1301 if (contexts & CXCompletionContext_Namespace) {
1302 fprintf(file, "Namespace or namespace alias\n");
1303 }
1304 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1305 fprintf(file, "Nested name specifier\n");
1306 }
1307 if (contexts & CXCompletionContext_ObjCInterface) {
1308 fprintf(file, "Objective-C interface\n");
1309 }
1310 if (contexts & CXCompletionContext_ObjCProtocol) {
1311 fprintf(file, "Objective-C protocol\n");
1312 }
1313 if (contexts & CXCompletionContext_ObjCCategory) {
1314 fprintf(file, "Objective-C category\n");
1315 }
1316 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1317 fprintf(file, "Objective-C instance method\n");
1318 }
1319 if (contexts & CXCompletionContext_ObjCClassMessage) {
1320 fprintf(file, "Objective-C class method\n");
1321 }
1322 if (contexts & CXCompletionContext_ObjCSelectorName) {
1323 fprintf(file, "Objective-C selector name\n");
1324 }
1325 if (contexts & CXCompletionContext_MacroName) {
1326 fprintf(file, "Macro name\n");
1327 }
1328 if (contexts & CXCompletionContext_NaturalLanguage) {
1329 fprintf(file, "Natural language\n");
1330 }
1331}
1332
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001333int my_stricmp(const char *s1, const char *s2) {
1334 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001335 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001336 if (c1 < c2)
1337 return -1;
1338 else if (c1 > c2)
1339 return 1;
1340
1341 ++s1;
1342 ++s2;
1343 }
1344
1345 if (*s1)
1346 return 1;
1347 else if (*s2)
1348 return -1;
1349 return 0;
1350}
1351
Douglas Gregor1982c182010-07-12 18:38:41 +00001352int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001353 const char *input = argv[1];
1354 char *filename = 0;
1355 unsigned line;
1356 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001357 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001358 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001359 struct CXUnsavedFile *unsaved_files = 0;
1360 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001361 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001362 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001363 unsigned I, Repeats = 1;
1364 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1365
1366 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1367 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001368
Douglas Gregor1982c182010-07-12 18:38:41 +00001369 if (timing_only)
1370 input += strlen("-code-completion-timing=");
1371 else
1372 input += strlen("-code-completion-at=");
1373
Ted Kremeneke68fff62010-02-17 00:41:32 +00001374 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001375 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001376 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001377
Douglas Gregor735df882009-12-02 09:21:34 +00001378 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1379 return -1;
1380
Douglas Gregor32be4a52010-10-11 21:37:58 +00001381 CIdx = clang_createIndex(0, 0);
1382
1383 if (getenv("CINDEXTEST_EDITING"))
1384 Repeats = 5;
1385
1386 TU = clang_parseTranslationUnit(CIdx, 0,
1387 argv + num_unsaved_files + 2,
1388 argc - num_unsaved_files - 2,
1389 0, 0, getDefaultParsingOptions());
1390 if (!TU) {
1391 fprintf(stderr, "Unable to load translation unit!\n");
1392 return 1;
1393 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001394
1395 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1396 fprintf(stderr, "Unable to reparse translation init!\n");
1397 return 1;
1398 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001399
1400 for (I = 0; I != Repeats; ++I) {
1401 results = clang_codeCompleteAt(TU, filename, line, column,
1402 unsaved_files, num_unsaved_files,
1403 completionOptions);
1404 if (!results) {
1405 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001406 return 1;
1407 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001408 if (I != Repeats-1)
1409 clang_disposeCodeCompleteResults(results);
1410 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001411
Douglas Gregorec6762c2009-12-18 16:20:58 +00001412 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001413 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001414 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001415 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001416 CXString objCSelector;
1417 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001418 if (!timing_only) {
1419 /* Sort the code-completion results based on the typed text. */
1420 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1421
Douglas Gregor1982c182010-07-12 18:38:41 +00001422 for (i = 0; i != n; ++i)
1423 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001424 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001425 n = clang_codeCompleteGetNumDiagnostics(results);
1426 for (i = 0; i != n; ++i) {
1427 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1428 PrintDiagnostic(diag);
1429 clang_disposeDiagnostic(diag);
1430 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001431
1432 contexts = clang_codeCompleteGetContexts(results);
1433 print_completion_contexts(contexts, stdout);
1434
Douglas Gregor0a47d692011-07-26 15:24:30 +00001435 containerKind = clang_codeCompleteGetContainerKind(results,
1436 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001437
1438 if (containerKind != CXCursor_InvalidCode) {
1439 /* We have found a container */
1440 CXString containerUSR, containerKindSpelling;
1441 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1442 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1443 clang_disposeString(containerKindSpelling);
1444
1445 if (containerIsIncomplete) {
1446 printf("Container is incomplete\n");
1447 }
1448 else {
1449 printf("Container is complete\n");
1450 }
1451
1452 containerUSR = clang_codeCompleteGetContainerUSR(results);
1453 printf("Container USR: %s\n", clang_getCString(containerUSR));
1454 clang_disposeString(containerUSR);
1455 }
1456
Douglas Gregor0a47d692011-07-26 15:24:30 +00001457 objCSelector = clang_codeCompleteGetObjCSelector(results);
1458 selectorString = clang_getCString(objCSelector);
1459 if (selectorString && strlen(selectorString) > 0) {
1460 printf("Objective-C selector: %s\n", selectorString);
1461 }
1462 clang_disposeString(objCSelector);
1463
Douglas Gregorec6762c2009-12-18 16:20:58 +00001464 clang_disposeCodeCompleteResults(results);
1465 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001466 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001467 clang_disposeIndex(CIdx);
1468 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001469
Douglas Gregor735df882009-12-02 09:21:34 +00001470 free_remapped_files(unsaved_files, num_unsaved_files);
1471
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001472 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001473}
1474
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001475typedef struct {
1476 char *filename;
1477 unsigned line;
1478 unsigned column;
1479} CursorSourceLocation;
1480
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001481static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001482 CXIndex CIdx;
1483 int errorCode;
1484 struct CXUnsavedFile *unsaved_files = 0;
1485 int num_unsaved_files = 0;
1486 CXTranslationUnit TU;
1487 CXCursor Cursor;
1488 CursorSourceLocation *Locations = 0;
1489 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001490 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001491 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001492
Ted Kremeneke68fff62010-02-17 00:41:32 +00001493 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001494 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1495 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001496
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001497 /* Parse the locations. */
1498 assert(NumLocations > 0 && "Unable to count locations?");
1499 Locations = (CursorSourceLocation *)malloc(
1500 NumLocations * sizeof(CursorSourceLocation));
1501 for (Loc = 0; Loc < NumLocations; ++Loc) {
1502 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001503 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1504 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001505 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001506 return errorCode;
1507 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001508
1509 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001510 &num_unsaved_files))
1511 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001512
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001513 if (getenv("CINDEXTEST_EDITING"))
1514 Repeats = 5;
1515
1516 /* Parse the translation unit. When we're testing clang_getCursor() after
1517 reparsing, don't remap unsaved files until the second parse. */
1518 CIdx = clang_createIndex(1, 1);
1519 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1520 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001521 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001522 unsaved_files,
1523 Repeats > 1? 0 : num_unsaved_files,
1524 getDefaultParsingOptions());
1525
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001526 if (!TU) {
1527 fprintf(stderr, "unable to parse input\n");
1528 return -1;
1529 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001530
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001531 if (checkForErrors(TU) != 0)
1532 return -1;
1533
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001534 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001535 if (Repeats > 1 &&
1536 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1537 clang_defaultReparseOptions(TU))) {
1538 clang_disposeTranslationUnit(TU);
1539 return 1;
1540 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001541
1542 if (checkForErrors(TU) != 0)
1543 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001544
1545 for (Loc = 0; Loc < NumLocations; ++Loc) {
1546 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1547 if (!file)
1548 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001549
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001550 Cursor = clang_getCursor(TU,
1551 clang_getLocation(TU, file, Locations[Loc].line,
1552 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001553
1554 if (checkForErrors(TU) != 0)
1555 return -1;
1556
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001557 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001558 CXCompletionString completionString = clang_getCursorCompletionString(
1559 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001560 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1561 CXString Spelling;
1562 const char *cspell;
1563 unsigned line, column;
1564 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1565 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001566 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001567 PrintCursorExtent(Cursor);
1568 Spelling = clang_getCursorSpelling(Cursor);
1569 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001570 if (cspell && strlen(cspell) != 0) {
1571 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001572 printf(" Spelling=%s (", cspell);
1573 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001574 CXSourceRange range =
1575 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001576 if (clang_Range_isNull(range))
1577 break;
1578 PrintRange(range, 0);
1579 }
1580 printf(")");
1581 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001582 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001583 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1584 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001585 if (completionString != NULL) {
1586 printf("\nCompletion string: ");
1587 print_completion_string(completionString, stdout);
1588 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001589 printf("\n");
1590 free(Locations[Loc].filename);
1591 }
1592 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001593 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001594
Douglas Gregora88084b2010-02-18 18:08:43 +00001595 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001596 clang_disposeTranslationUnit(TU);
1597 clang_disposeIndex(CIdx);
1598 free(Locations);
1599 free_remapped_files(unsaved_files, num_unsaved_files);
1600 return 0;
1601}
1602
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001603static enum CXVisitorResult findFileRefsVisit(void *context,
1604 CXCursor cursor, CXSourceRange range) {
1605 if (clang_Range_isNull(range))
1606 return CXVisit_Continue;
1607
1608 PrintCursor(cursor);
1609 PrintRange(range, "");
1610 printf("\n");
1611 return CXVisit_Continue;
1612}
1613
1614static int find_file_refs_at(int argc, const char **argv) {
1615 CXIndex CIdx;
1616 int errorCode;
1617 struct CXUnsavedFile *unsaved_files = 0;
1618 int num_unsaved_files = 0;
1619 CXTranslationUnit TU;
1620 CXCursor Cursor;
1621 CursorSourceLocation *Locations = 0;
1622 unsigned NumLocations = 0, Loc;
1623 unsigned Repeats = 1;
1624 unsigned I;
1625
1626 /* Count the number of locations. */
1627 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1628 ++NumLocations;
1629
1630 /* Parse the locations. */
1631 assert(NumLocations > 0 && "Unable to count locations?");
1632 Locations = (CursorSourceLocation *)malloc(
1633 NumLocations * sizeof(CursorSourceLocation));
1634 for (Loc = 0; Loc < NumLocations; ++Loc) {
1635 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1636 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1637 &Locations[Loc].line,
1638 &Locations[Loc].column, 0, 0)))
1639 return errorCode;
1640 }
1641
1642 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1643 &num_unsaved_files))
1644 return -1;
1645
1646 if (getenv("CINDEXTEST_EDITING"))
1647 Repeats = 5;
1648
1649 /* Parse the translation unit. When we're testing clang_getCursor() after
1650 reparsing, don't remap unsaved files until the second parse. */
1651 CIdx = clang_createIndex(1, 1);
1652 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1653 argv + num_unsaved_files + 1 + NumLocations,
1654 argc - num_unsaved_files - 2 - NumLocations,
1655 unsaved_files,
1656 Repeats > 1? 0 : num_unsaved_files,
1657 getDefaultParsingOptions());
1658
1659 if (!TU) {
1660 fprintf(stderr, "unable to parse input\n");
1661 return -1;
1662 }
1663
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001664 if (checkForErrors(TU) != 0)
1665 return -1;
1666
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001667 for (I = 0; I != Repeats; ++I) {
1668 if (Repeats > 1 &&
1669 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1670 clang_defaultReparseOptions(TU))) {
1671 clang_disposeTranslationUnit(TU);
1672 return 1;
1673 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001674
1675 if (checkForErrors(TU) != 0)
1676 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001677
1678 for (Loc = 0; Loc < NumLocations; ++Loc) {
1679 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1680 if (!file)
1681 continue;
1682
1683 Cursor = clang_getCursor(TU,
1684 clang_getLocation(TU, file, Locations[Loc].line,
1685 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001686
1687 if (checkForErrors(TU) != 0)
1688 return -1;
1689
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001690 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001691 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001692 PrintCursor(Cursor);
1693 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001694 clang_findReferencesInFile(Cursor, file, visitor);
1695 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001696
1697 if (checkForErrors(TU) != 0)
1698 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001699 }
1700 }
1701 }
1702
1703 PrintDiagnostics(TU);
1704 clang_disposeTranslationUnit(TU);
1705 clang_disposeIndex(CIdx);
1706 free(Locations);
1707 free_remapped_files(unsaved_files, num_unsaved_files);
1708 return 0;
1709}
1710
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001711typedef struct {
1712 const char *check_prefix;
1713 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001714 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001715 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001716 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001717} IndexData;
1718
1719static void printCheck(IndexData *data) {
1720 if (data->check_prefix) {
1721 if (data->first_check_printed) {
1722 printf("// %s-NEXT: ", data->check_prefix);
1723 } else {
1724 printf("// %s : ", data->check_prefix);
1725 data->first_check_printed = 1;
1726 }
1727 }
1728}
1729
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001730static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001731 CXString filename = clang_getFileName((CXFile)file);
1732 printf("%s", clang_getCString(filename));
1733 clang_disposeString(filename);
1734}
1735
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001736static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1737 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001738 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001739 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001740 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001741 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001742 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001743
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001744 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001745 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1746 if (line == 0) {
1747 printf("<null loc>");
1748 return;
1749 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001750 if (!file) {
1751 printf("<no idxfile>");
1752 return;
1753 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001754 filename = clang_getFileName((CXFile)file);
1755 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001756 if (strcmp(cname, index_data->main_filename) == 0)
1757 isMainFile = 1;
1758 else
1759 isMainFile = 0;
1760 clang_disposeString(filename);
1761
1762 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001763 printCXIndexFile(file);
1764 printf(":");
1765 }
1766 printf("%d:%d", line, column);
1767}
1768
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001769static unsigned digitCount(unsigned val) {
1770 unsigned c = 1;
1771 while (1) {
1772 if (val < 10)
1773 return c;
1774 ++c;
1775 val /= 10;
1776 }
1777}
1778
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001779static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1780 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001781 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001782 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001783 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001784 unsigned line, column;
1785
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001786 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001787 if (!name)
1788 name = "<anon-tag>";
1789
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001790 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001791 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001792 newStr = (char *)malloc(strlen(name) +
1793 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001794 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001795 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001796}
1797
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001798static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1799 CXIdxClientContainer container;
1800 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001801 if (!container)
1802 printf("[<<NULL>>]");
1803 else
1804 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001805}
1806
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001807static const char *getEntityKindString(CXIdxEntityKind kind) {
1808 switch (kind) {
1809 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1810 case CXIdxEntity_Typedef: return "typedef";
1811 case CXIdxEntity_Function: return "function";
1812 case CXIdxEntity_Variable: return "variable";
1813 case CXIdxEntity_Field: return "field";
1814 case CXIdxEntity_EnumConstant: return "enumerator";
1815 case CXIdxEntity_ObjCClass: return "objc-class";
1816 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1817 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001818 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1819 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001820 case CXIdxEntity_ObjCProperty: return "objc-property";
1821 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1822 case CXIdxEntity_Enum: return "enum";
1823 case CXIdxEntity_Struct: return "struct";
1824 case CXIdxEntity_Union: return "union";
1825 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001826 case CXIdxEntity_CXXNamespace: return "namespace";
1827 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1828 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1829 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1830 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1831 case CXIdxEntity_CXXConstructor: return "constructor";
1832 case CXIdxEntity_CXXDestructor: return "destructor";
1833 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1834 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1835 }
1836 assert(0 && "Garbage entity kind");
1837 return 0;
1838}
1839
1840static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1841 switch (kind) {
1842 case CXIdxEntity_NonTemplate: return "";
1843 case CXIdxEntity_Template: return "-template";
1844 case CXIdxEntity_TemplatePartialSpecialization:
1845 return "-template-partial-spec";
1846 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001847 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001848 assert(0 && "Garbage entity kind");
1849 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001850}
1851
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001852static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1853 switch (kind) {
1854 case CXIdxEntityLang_None: return "<none>";
1855 case CXIdxEntityLang_C: return "C";
1856 case CXIdxEntityLang_ObjC: return "ObjC";
1857 case CXIdxEntityLang_CXX: return "C++";
1858 }
1859 assert(0 && "Garbage language kind");
1860 return 0;
1861}
1862
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001863static void printEntityInfo(const char *cb,
1864 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001865 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001866 const char *name;
1867 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001868 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001869 index_data = (IndexData *)client_data;
1870 printCheck(index_data);
1871
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001872 if (!info) {
1873 printf("%s: <<NULL>>", cb);
1874 return;
1875 }
1876
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001877 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001878 if (!name)
1879 name = "<anon-tag>";
1880
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001881 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1882 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001883 printf(" | name: %s", name);
1884 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001885 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001886
1887 for (i = 0; i != info->numAttributes; ++i) {
1888 const CXIdxAttrInfo *Attr = info->attributes[i];
1889 printf(" <attribute>: ");
1890 PrintCursor(Attr->cursor);
1891 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001892}
1893
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001894static void printBaseClassInfo(CXClientData client_data,
1895 const CXIdxBaseClassInfo *info) {
1896 printEntityInfo(" <base>", client_data, info->base);
1897 printf(" | cursor: ");
1898 PrintCursor(info->cursor);
1899 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001900 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001901}
1902
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001903static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1904 CXClientData client_data) {
1905 unsigned i;
1906 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1907 printEntityInfo(" <protocol>", client_data,
1908 ProtoInfo->protocols[i]->protocol);
1909 printf(" | cursor: ");
1910 PrintCursor(ProtoInfo->protocols[i]->cursor);
1911 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001912 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001913 printf("\n");
1914 }
1915}
1916
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001917static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001918 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001919 CXString str;
1920 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001921 unsigned numDiags, i;
1922 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001923 IndexData *index_data;
1924 index_data = (IndexData *)client_data;
1925 printCheck(index_data);
1926
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001927 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1928 for (i = 0; i != numDiags; ++i) {
1929 diag = clang_getDiagnosticInSet(diagSet, i);
1930 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1931 cstr = clang_getCString(str);
1932 printf("[diagnostic]: %s\n", cstr);
1933 clang_disposeString(str);
1934
1935 if (getenv("CINDEXTEST_FAILONERROR") &&
1936 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1937 index_data->fail_for_error = 1;
1938 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001939 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001940}
1941
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001942static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1943 CXFile file, void *reserved) {
1944 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001945 CXString filename;
1946
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001947 index_data = (IndexData *)client_data;
1948 printCheck(index_data);
1949
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001950 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001951 index_data->main_filename = clang_getCString(filename);
1952 clang_disposeString(filename);
1953
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001954 printf("[enteredMainFile]: ");
1955 printCXIndexFile((CXIdxClientFile)file);
1956 printf("\n");
1957
1958 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001959}
1960
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001961static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001962 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001963 IndexData *index_data;
1964 index_data = (IndexData *)client_data;
1965 printCheck(index_data);
1966
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001967 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001968 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001969 printf(" | name: \"%s\"", info->filename);
1970 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001971 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001972 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001973
1974 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001975}
1976
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001977static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001978 void *reserved) {
1979 IndexData *index_data;
1980 index_data = (IndexData *)client_data;
1981 printCheck(index_data);
1982
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001983 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001984 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001985}
1986
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001987static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001988 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001989 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001990 const CXIdxObjCCategoryDeclInfo *CatInfo;
1991 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001992 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001993 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001994 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001995 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001996 index_data = (IndexData *)client_data;
1997
1998 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1999 printf(" | cursor: ");
2000 PrintCursor(info->cursor);
2001 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002002 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002003 printf(" | semantic-container: ");
2004 printCXIndexContainer(info->semanticContainer);
2005 printf(" | lexical-container: ");
2006 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002007 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002008 printf(" | isDef: %d", info->isDefinition);
2009 printf(" | isContainer: %d", info->isContainer);
2010 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002011
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002012 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002013 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002014 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002015 PrintCursor(Attr->cursor);
2016 printf("\n");
2017 }
2018
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002019 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2020 const char *kindName = 0;
2021 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2022 switch (K) {
2023 case CXIdxObjCContainer_ForwardRef:
2024 kindName = "forward-ref"; break;
2025 case CXIdxObjCContainer_Interface:
2026 kindName = "interface"; break;
2027 case CXIdxObjCContainer_Implementation:
2028 kindName = "implementation"; break;
2029 }
2030 printCheck(index_data);
2031 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2032 }
2033
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002034 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002035 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2036 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002037 printf(" | cursor: ");
2038 PrintCursor(CatInfo->classCursor);
2039 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002040 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002041 printf("\n");
2042 }
2043
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002044 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2045 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002046 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002047 printf("\n");
2048 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002049 }
2050
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002051 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2052 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002053 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002054
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002055 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2056 if (PropInfo->getter) {
2057 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2058 printf("\n");
2059 }
2060 if (PropInfo->setter) {
2061 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2062 printf("\n");
2063 }
2064 }
2065
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002066 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2067 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2068 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2069 printf("\n");
2070 }
2071 }
2072
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002073 if (info->declAsContainer)
2074 clang_index_setClientContainer(info->declAsContainer,
2075 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002076}
2077
2078static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002079 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002080 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002081 printf(" | cursor: ");
2082 PrintCursor(info->cursor);
2083 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002084 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002085 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002086 printf(" | container: ");
2087 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002088 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002089 switch (info->kind) {
2090 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002091 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002092 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002093 printf("\n");
2094}
2095
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002096static int index_abortQuery(CXClientData client_data, void *reserved) {
2097 IndexData *index_data;
2098 index_data = (IndexData *)client_data;
2099 return index_data->abort;
2100}
2101
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002102static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002103 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002104 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002105 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002106 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002107 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002108 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002109 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002110 index_indexEntityReference
2111};
2112
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002113static unsigned getIndexOptions(void) {
2114 unsigned index_opts;
2115 index_opts = 0;
2116 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2117 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2118 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2119 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2120
2121 return index_opts;
2122}
2123
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002124static int index_file(int argc, const char **argv) {
2125 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002126 CXIndex Idx;
2127 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002128 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002129 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002130 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002131
2132 check_prefix = 0;
2133 if (argc > 0) {
2134 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2135 check_prefix = argv[0] + strlen("-check-prefix=");
2136 ++argv;
2137 --argc;
2138 }
2139 }
2140
2141 if (argc == 0) {
2142 fprintf(stderr, "no compiler arguments\n");
2143 return -1;
2144 }
2145
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002146 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2147 /* displayDiagnosics=*/1))) {
2148 fprintf(stderr, "Could not create Index\n");
2149 return 1;
2150 }
2151 idxAction = 0;
2152 result = 1;
2153
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002154 index_data.check_prefix = check_prefix;
2155 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002156 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002157 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002158
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002159 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002160 idxAction = clang_IndexAction_create(Idx);
2161 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002162 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002163 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002164 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002165 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002166
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002167 clang_IndexAction_dispose(idxAction);
2168 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002169 return result;
2170}
2171
2172static int index_tu(int argc, const char **argv) {
2173 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002174 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002175 CXTranslationUnit TU;
2176 const char *check_prefix;
2177 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002178 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002179 int result;
2180
2181 check_prefix = 0;
2182 if (argc > 0) {
2183 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2184 check_prefix = argv[0] + strlen("-check-prefix=");
2185 ++argv;
2186 --argc;
2187 }
2188 }
2189
2190 if (argc == 0) {
2191 fprintf(stderr, "no ast file\n");
2192 return -1;
2193 }
2194
2195 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2196 /* displayDiagnosics=*/1))) {
2197 fprintf(stderr, "Could not create Index\n");
2198 return 1;
2199 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002200 idxAction = 0;
2201 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002202
2203 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002204 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002205
2206 index_data.check_prefix = check_prefix;
2207 index_data.first_check_printed = 0;
2208 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002209 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002210
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002211 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002212 idxAction = clang_IndexAction_create(Idx);
2213 result = clang_indexTranslationUnit(idxAction, &index_data,
2214 &IndexCB,sizeof(IndexCB),
2215 index_opts, TU);
2216 if (index_data.fail_for_error)
2217 goto finished;
2218
2219 finished:
2220 clang_IndexAction_dispose(idxAction);
2221 clang_disposeIndex(Idx);
2222
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002223 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002224}
2225
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002226int perform_token_annotation(int argc, const char **argv) {
2227 const char *input = argv[1];
2228 char *filename = 0;
2229 unsigned line, second_line;
2230 unsigned column, second_column;
2231 CXIndex CIdx;
2232 CXTranslationUnit TU = 0;
2233 int errorCode;
2234 struct CXUnsavedFile *unsaved_files = 0;
2235 int num_unsaved_files = 0;
2236 CXToken *tokens;
2237 unsigned num_tokens;
2238 CXSourceRange range;
2239 CXSourceLocation startLoc, endLoc;
2240 CXFile file = 0;
2241 CXCursor *cursors = 0;
2242 unsigned i;
2243
2244 input += strlen("-test-annotate-tokens=");
2245 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2246 &second_line, &second_column)))
2247 return errorCode;
2248
2249 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2250 return -1;
2251
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002252 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002253 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2254 argv + num_unsaved_files + 2,
2255 argc - num_unsaved_files - 3,
2256 unsaved_files,
2257 num_unsaved_files,
2258 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002259 if (!TU) {
2260 fprintf(stderr, "unable to parse input\n");
2261 clang_disposeIndex(CIdx);
2262 free(filename);
2263 free_remapped_files(unsaved_files, num_unsaved_files);
2264 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002265 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002266 errorCode = 0;
2267
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002268 if (checkForErrors(TU) != 0)
2269 return -1;
2270
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002271 if (getenv("CINDEXTEST_EDITING")) {
2272 for (i = 0; i < 5; ++i) {
2273 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2274 clang_defaultReparseOptions(TU))) {
2275 fprintf(stderr, "Unable to reparse translation unit!\n");
2276 errorCode = -1;
2277 goto teardown;
2278 }
2279 }
2280 }
2281
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002282 if (checkForErrors(TU) != 0) {
2283 errorCode = -1;
2284 goto teardown;
2285 }
2286
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002287 file = clang_getFile(TU, filename);
2288 if (!file) {
2289 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2290 errorCode = -1;
2291 goto teardown;
2292 }
2293
2294 startLoc = clang_getLocation(TU, file, line, column);
2295 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002296 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002297 column);
2298 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002299 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002300 }
2301
2302 endLoc = clang_getLocation(TU, file, second_line, second_column);
2303 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002304 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002305 second_line, second_column);
2306 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002307 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002308 }
2309
2310 range = clang_getRange(startLoc, endLoc);
2311 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002312
2313 if (checkForErrors(TU) != 0) {
2314 errorCode = -1;
2315 goto teardown;
2316 }
2317
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002318 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2319 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002320
2321 if (checkForErrors(TU) != 0) {
2322 errorCode = -1;
2323 goto teardown;
2324 }
2325
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002326 for (i = 0; i != num_tokens; ++i) {
2327 const char *kind = "<unknown>";
2328 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2329 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2330 unsigned start_line, start_column, end_line, end_column;
2331
2332 switch (clang_getTokenKind(tokens[i])) {
2333 case CXToken_Punctuation: kind = "Punctuation"; break;
2334 case CXToken_Keyword: kind = "Keyword"; break;
2335 case CXToken_Identifier: kind = "Identifier"; break;
2336 case CXToken_Literal: kind = "Literal"; break;
2337 case CXToken_Comment: kind = "Comment"; break;
2338 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002339 clang_getSpellingLocation(clang_getRangeStart(extent),
2340 0, &start_line, &start_column, 0);
2341 clang_getSpellingLocation(clang_getRangeEnd(extent),
2342 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002343 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002344 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002345 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002346 if (!clang_isInvalid(cursors[i].kind)) {
2347 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002348 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002349 }
2350 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002351 }
2352 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002353 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002354
2355 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002356 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002357 clang_disposeTranslationUnit(TU);
2358 clang_disposeIndex(CIdx);
2359 free(filename);
2360 free_remapped_files(unsaved_files, num_unsaved_files);
2361 return errorCode;
2362}
2363
Ted Kremenek0d435192009-11-17 18:13:31 +00002364/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002365/* USR printing. */
2366/******************************************************************************/
2367
2368static int insufficient_usr(const char *kind, const char *usage) {
2369 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2370 return 1;
2371}
2372
2373static unsigned isUSR(const char *s) {
2374 return s[0] == 'c' && s[1] == ':';
2375}
2376
2377static int not_usr(const char *s, const char *arg) {
2378 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2379 return 1;
2380}
2381
2382static void print_usr(CXString usr) {
2383 const char *s = clang_getCString(usr);
2384 printf("%s\n", s);
2385 clang_disposeString(usr);
2386}
2387
2388static void display_usrs() {
2389 fprintf(stderr, "-print-usrs options:\n"
2390 " ObjCCategory <class name> <category name>\n"
2391 " ObjCClass <class name>\n"
2392 " ObjCIvar <ivar name> <class USR>\n"
2393 " ObjCMethod <selector> [0=class method|1=instance method] "
2394 "<class USR>\n"
2395 " ObjCProperty <property name> <class USR>\n"
2396 " ObjCProtocol <protocol name>\n");
2397}
2398
2399int print_usrs(const char **I, const char **E) {
2400 while (I != E) {
2401 const char *kind = *I;
2402 unsigned len = strlen(kind);
2403 switch (len) {
2404 case 8:
2405 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2406 if (I + 2 >= E)
2407 return insufficient_usr(kind, "<ivar name> <class USR>");
2408 if (!isUSR(I[2]))
2409 return not_usr("<class USR>", I[2]);
2410 else {
2411 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002412 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002413 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002414 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2415 }
2416
2417 I += 3;
2418 continue;
2419 }
2420 break;
2421 case 9:
2422 if (memcmp(kind, "ObjCClass", 9) == 0) {
2423 if (I + 1 >= E)
2424 return insufficient_usr(kind, "<class name>");
2425 print_usr(clang_constructUSR_ObjCClass(I[1]));
2426 I += 2;
2427 continue;
2428 }
2429 break;
2430 case 10:
2431 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2432 if (I + 3 >= E)
2433 return insufficient_usr(kind, "<method selector> "
2434 "[0=class method|1=instance method] <class USR>");
2435 if (!isUSR(I[3]))
2436 return not_usr("<class USR>", I[3]);
2437 else {
2438 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002439 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002440 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002441 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2442 }
2443 I += 4;
2444 continue;
2445 }
2446 break;
2447 case 12:
2448 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2449 if (I + 2 >= E)
2450 return insufficient_usr(kind, "<class name> <category name>");
2451 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2452 I += 3;
2453 continue;
2454 }
2455 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2456 if (I + 1 >= E)
2457 return insufficient_usr(kind, "<protocol name>");
2458 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2459 I += 2;
2460 continue;
2461 }
2462 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2463 if (I + 2 >= E)
2464 return insufficient_usr(kind, "<property name> <class USR>");
2465 if (!isUSR(I[2]))
2466 return not_usr("<class USR>", I[2]);
2467 else {
2468 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002469 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002470 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002471 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2472 }
2473 I += 3;
2474 continue;
2475 }
2476 break;
2477 default:
2478 break;
2479 }
2480 break;
2481 }
2482
2483 if (I != E) {
2484 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2485 display_usrs();
2486 return 1;
2487 }
2488 return 0;
2489}
2490
2491int print_usrs_file(const char *file_name) {
2492 char line[2048];
2493 const char *args[128];
2494 unsigned numChars = 0;
2495
2496 FILE *fp = fopen(file_name, "r");
2497 if (!fp) {
2498 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2499 return 1;
2500 }
2501
2502 /* This code is not really all that safe, but it works fine for testing. */
2503 while (!feof(fp)) {
2504 char c = fgetc(fp);
2505 if (c == '\n') {
2506 unsigned i = 0;
2507 const char *s = 0;
2508
2509 if (numChars == 0)
2510 continue;
2511
2512 line[numChars] = '\0';
2513 numChars = 0;
2514
2515 if (line[0] == '/' && line[1] == '/')
2516 continue;
2517
2518 s = strtok(line, " ");
2519 while (s) {
2520 args[i] = s;
2521 ++i;
2522 s = strtok(0, " ");
2523 }
2524 if (print_usrs(&args[0], &args[i]))
2525 return 1;
2526 }
2527 else
2528 line[numChars++] = c;
2529 }
2530
2531 fclose(fp);
2532 return 0;
2533}
2534
2535/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002536/* Command line processing. */
2537/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002538int write_pch_file(const char *filename, int argc, const char *argv[]) {
2539 CXIndex Idx;
2540 CXTranslationUnit TU;
2541 struct CXUnsavedFile *unsaved_files = 0;
2542 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002543 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002544
2545 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2546
2547 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2548 clang_disposeIndex(Idx);
2549 return -1;
2550 }
2551
2552 TU = clang_parseTranslationUnit(Idx, 0,
2553 argv + num_unsaved_files,
2554 argc - num_unsaved_files,
2555 unsaved_files,
2556 num_unsaved_files,
2557 CXTranslationUnit_Incomplete);
2558 if (!TU) {
2559 fprintf(stderr, "Unable to load translation unit!\n");
2560 free_remapped_files(unsaved_files, num_unsaved_files);
2561 clang_disposeIndex(Idx);
2562 return 1;
2563 }
2564
Douglas Gregor39c411f2011-07-06 16:43:36 +00002565 switch (clang_saveTranslationUnit(TU, filename,
2566 clang_defaultSaveOptions(TU))) {
2567 case CXSaveError_None:
2568 break;
2569
2570 case CXSaveError_TranslationErrors:
2571 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2572 filename);
2573 result = 2;
2574 break;
2575
2576 case CXSaveError_InvalidTU:
2577 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2578 filename);
2579 result = 3;
2580 break;
2581
2582 case CXSaveError_Unknown:
2583 default:
2584 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2585 result = 1;
2586 break;
2587 }
2588
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002589 clang_disposeTranslationUnit(TU);
2590 free_remapped_files(unsaved_files, num_unsaved_files);
2591 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002592 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002593}
2594
2595/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002596/* Serialized diagnostics. */
2597/******************************************************************************/
2598
2599static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2600 switch (error) {
2601 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2602 case CXLoadDiag_None: break;
2603 case CXLoadDiag_Unknown: return "Unknown";
2604 case CXLoadDiag_InvalidFile: return "Invalid File";
2605 }
2606 return "None";
2607}
2608
2609static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2610 switch (severity) {
2611 case CXDiagnostic_Note: return "note";
2612 case CXDiagnostic_Error: return "error";
2613 case CXDiagnostic_Fatal: return "fatal";
2614 case CXDiagnostic_Ignored: return "ignored";
2615 case CXDiagnostic_Warning: return "warning";
2616 }
2617 return "unknown";
2618}
2619
2620static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002621 if (indent == 0)
2622 return;
2623 fprintf(stderr, "+");
2624 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002625 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002626 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002627 --indent;
2628 }
2629}
2630
2631static void printLocation(CXSourceLocation L) {
2632 CXFile File;
2633 CXString FileName;
2634 unsigned line, column, offset;
2635
2636 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2637 FileName = clang_getFileName(File);
2638
2639 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2640 clang_disposeString(FileName);
2641}
2642
2643static void printRanges(CXDiagnostic D, unsigned indent) {
2644 unsigned i, n = clang_getDiagnosticNumRanges(D);
2645
2646 for (i = 0; i < n; ++i) {
2647 CXSourceLocation Start, End;
2648 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2649 Start = clang_getRangeStart(SR);
2650 End = clang_getRangeEnd(SR);
2651
2652 printIndent(indent);
2653 fprintf(stderr, "Range: ");
2654 printLocation(Start);
2655 fprintf(stderr, " ");
2656 printLocation(End);
2657 fprintf(stderr, "\n");
2658 }
2659}
2660
2661static void printFixIts(CXDiagnostic D, unsigned indent) {
2662 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002663 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002664 for (i = 0 ; i < n; ++i) {
2665 CXSourceRange ReplacementRange;
2666 CXString text;
2667 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2668
2669 printIndent(indent);
2670 fprintf(stderr, "FIXIT: (");
2671 printLocation(clang_getRangeStart(ReplacementRange));
2672 fprintf(stderr, " - ");
2673 printLocation(clang_getRangeEnd(ReplacementRange));
2674 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2675 clang_disposeString(text);
2676 }
2677}
2678
2679static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002680 unsigned i, n;
2681
Ted Kremenek15322172011-11-10 08:43:12 +00002682 if (!Diags)
2683 return;
2684
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002685 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002686 for (i = 0; i < n; ++i) {
2687 CXSourceLocation DiagLoc;
2688 CXDiagnostic D;
2689 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002690 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002691 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002692 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002693
2694 D = clang_getDiagnosticInSet(Diags, i);
2695 DiagLoc = clang_getDiagnosticLocation(D);
2696 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2697 FileName = clang_getFileName(File);
2698 DiagSpelling = clang_getDiagnosticSpelling(D);
2699
2700 printIndent(indent);
2701
2702 fprintf(stderr, "%s:%d:%d: %s: %s",
2703 clang_getCString(FileName),
2704 line,
2705 column,
2706 getSeverityString(clang_getDiagnosticSeverity(D)),
2707 clang_getCString(DiagSpelling));
2708
2709 DiagOption = clang_getDiagnosticOption(D, 0);
2710 DiagOptionStr = clang_getCString(DiagOption);
2711 if (DiagOptionStr) {
2712 fprintf(stderr, " [%s]", DiagOptionStr);
2713 }
2714
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002715 DiagCat = clang_getDiagnosticCategoryText(D);
2716 DiagCatStr = clang_getCString(DiagCat);
2717 if (DiagCatStr) {
2718 fprintf(stderr, " [%s]", DiagCatStr);
2719 }
2720
Ted Kremenek15322172011-11-10 08:43:12 +00002721 fprintf(stderr, "\n");
2722
2723 printRanges(D, indent);
2724 printFixIts(D, indent);
2725
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002726 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002727 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2728
2729 clang_disposeString(FileName);
2730 clang_disposeString(DiagSpelling);
2731 clang_disposeString(DiagOption);
2732 }
2733}
2734
2735static int read_diagnostics(const char *filename) {
2736 enum CXLoadDiag_Error error;
2737 CXString errorString;
2738 CXDiagnosticSet Diags = 0;
2739
2740 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2741 if (!Diags) {
2742 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2743 getDiagnosticCodeStr(error),
2744 clang_getCString(errorString));
2745 clang_disposeString(errorString);
2746 return 1;
2747 }
2748
2749 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002750 fprintf(stderr, "Number of diagnostics: %d\n",
2751 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002752 clang_disposeDiagnosticSet(Diags);
2753 return 0;
2754}
2755
2756/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002757/* Command line processing. */
2758/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002759
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002760static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002761 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002762 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002763 if (strcmp(s, "-usrs") == 0)
2764 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002765 if (strncmp(s, "-memory-usage", 13) == 0)
2766 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002767 return NULL;
2768}
2769
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002770static void print_usage(void) {
2771 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002772 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002773 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002774 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002775 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002776 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002777 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002778 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002779 "[FileCheck prefix]\n");
2780 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002781 " c-index-test -test-load-tu <AST file> <symbol filter> "
2782 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002783 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2784 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002785 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002786 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002787 " c-index-test -test-load-source-memory-usage "
2788 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002789 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2790 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002791 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002792 " c-index-test -test-load-source-usrs-memory-usage "
2793 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002794 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2795 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002796 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002797 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002798 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002799 " c-index-test -test-print-typekind {<args>}*\n"
2800 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002801 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002802 " c-index-test -write-pch <file> <compiler arguments>\n");
2803 fprintf(stderr,
2804 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002805 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002806 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002807 " all - load all symbols, including those from PCH\n"
2808 " local - load all symbols except those in PCH\n"
2809 " category - only load ObjC categories (non-PCH)\n"
2810 " interface - only load ObjC interfaces (non-PCH)\n"
2811 " protocol - only load ObjC protocols (non-PCH)\n"
2812 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002813 " typedef - only load typdefs (non-PCH)\n"
2814 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002815}
2816
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002817/***/
2818
2819int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002820 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002821 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2822 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002823 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002824 return perform_code_completion(argc, argv, 0);
2825 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2826 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002827 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2828 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002829 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2830 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002831 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2832 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002833 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2834 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002835 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002836 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002837 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002838 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2839 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002840 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002841 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2842 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2843 if (I) {
2844 int trials = atoi(argv[2]);
2845 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2846 NULL);
2847 }
2848 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002849 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002850 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002851
2852 PostVisitTU postVisit = 0;
2853 if (strstr(argv[1], "-memory-usage"))
2854 postVisit = PrintMemoryUsage;
2855
Ted Kremenek7d405622010-01-12 23:34:26 +00002856 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002857 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2858 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002859 }
2860 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002861 return perform_file_scan(argv[2], argv[3],
2862 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002863 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2864 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002865 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2866 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2867 PrintInclusionStack);
2868 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2869 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2870 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002871 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2872 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2873 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002874 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2875 return perform_test_load_source(argc - 2, argv + 2, "all",
2876 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002877 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2878 if (argc > 2)
2879 return print_usrs(argv + 2, argv + argc);
2880 else {
2881 display_usrs();
2882 return 1;
2883 }
2884 }
2885 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2886 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002887 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2888 return write_pch_file(argv[2], argc - 3, argv + 3);
2889
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002890 print_usage();
2891 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002892}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002893
2894/***/
2895
2896/* We intentionally run in a separate thread to ensure we at least minimal
2897 * testing of a multithreaded environment (for example, having a reduced stack
2898 * size). */
2899
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002900typedef struct thread_info {
2901 int argc;
2902 const char **argv;
2903 int result;
2904} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002905void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002906 thread_info *client_data = client_data_v;
2907 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00002908#ifdef __CYGWIN__
2909 fflush(stdout); /* stdout is not flushed on Cygwin. */
2910#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002911}
2912
2913int main(int argc, const char **argv) {
2914 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002915
Douglas Gregor61605982010-10-27 16:00:01 +00002916 if (getenv("CINDEXTEST_NOTHREADS"))
2917 return cindextest_main(argc, argv);
2918
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002919 client_data.argc = argc;
2920 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002921 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002922 return client_data.result;
2923}