blob: 79a3c573b9633b957beda200b771de386c51636f [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"
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00004#include "clang-c/CXCompilationDatabase.h"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00005#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00006#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00007#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00009#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +000010
Ted Kremenek0d435192009-11-17 18:13:31 +000011/******************************************************************************/
12/* Utility functions. */
13/******************************************************************************/
14
John Thompson2e06fc82009-10-27 13:42:56 +000015#ifdef _MSC_VER
16char *basename(const char* path)
17{
18 char* base1 = (char*)strrchr(path, '/');
19 char* base2 = (char*)strrchr(path, '\\');
20 if (base1 && base2)
21 return((base1 > base2) ? base1 + 1 : base2 + 1);
22 else if (base1)
23 return(base1 + 1);
24 else if (base2)
25 return(base2 + 1);
26
27 return((char*)path);
28}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000029char *dirname(char* path)
30{
31 char* base1 = (char*)strrchr(path, '/');
32 char* base2 = (char*)strrchr(path, '\\');
33 if (base1 && base2)
34 if (base1 > base2)
35 *base1 = 0;
36 else
37 *base2 = 0;
38 else if (base1)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000039 *base1 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000040 else if (base2)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000041 *base2 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000042
43 return path;
44}
John Thompson2e06fc82009-10-27 13:42:56 +000045#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000046extern char *basename(const char *);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000047extern char *dirname(char *);
John Thompson2e06fc82009-10-27 13:42:56 +000048#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000049
Douglas Gregor45ba9a12010-07-25 17:39:21 +000050/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000051static unsigned getDefaultParsingOptions() {
52 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
53
54 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000055 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000056 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
57 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000058 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
59 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000060 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
61 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +000062 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
63 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregor44c181a2010-07-23 00:33:23 +000064
65 return options;
66}
67
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000068static int checkForErrors(CXTranslationUnit TU);
69
Daniel Dunbar51b058c2010-02-14 08:32:24 +000070static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
71 unsigned end_line, unsigned end_column) {
72 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000073 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000074}
75
Ted Kremenek1c6da172009-11-17 19:37:36 +000076static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
77 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000078
Douglas Gregora88084b2010-02-18 18:08:43 +000079 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000080 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000081 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
82 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000083 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000084 return 1;
85}
86
Douglas Gregor4db64a42010-01-23 00:14:00 +000087void free_remapped_files(struct CXUnsavedFile *unsaved_files,
88 int num_unsaved_files) {
89 int i;
90 for (i = 0; i != num_unsaved_files; ++i) {
91 free((char *)unsaved_files[i].Filename);
92 free((char *)unsaved_files[i].Contents);
93 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000094 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000095}
96
97int parse_remapped_files(int argc, const char **argv, int start_arg,
98 struct CXUnsavedFile **unsaved_files,
99 int *num_unsaved_files) {
100 int i;
101 int arg;
102 int prefix_len = strlen("-remap-file=");
103 *unsaved_files = 0;
104 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000105
Douglas Gregor4db64a42010-01-23 00:14:00 +0000106 /* Count the number of remapped files. */
107 for (arg = start_arg; arg < argc; ++arg) {
108 if (strncmp(argv[arg], "-remap-file=", prefix_len))
109 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000110
Douglas Gregor4db64a42010-01-23 00:14:00 +0000111 ++*num_unsaved_files;
112 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000113
Douglas Gregor4db64a42010-01-23 00:14:00 +0000114 if (*num_unsaved_files == 0)
115 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +0000118 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
119 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000120 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
121 struct CXUnsavedFile *unsaved = *unsaved_files + i;
122 const char *arg_string = argv[arg] + prefix_len;
123 int filename_len;
124 char *filename;
125 char *contents;
126 FILE *to_file;
127 const char *semi = strchr(arg_string, ';');
128 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000129 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000130 "error: -remap-file=from;to argument is missing semicolon\n");
131 free_remapped_files(*unsaved_files, i);
132 *unsaved_files = 0;
133 *num_unsaved_files = 0;
134 return -1;
135 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000136
Douglas Gregor4db64a42010-01-23 00:14:00 +0000137 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000138 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000139 if (!to_file) {
140 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
141 semi + 1);
142 free_remapped_files(*unsaved_files, i);
143 *unsaved_files = 0;
144 *num_unsaved_files = 0;
145 return -1;
146 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000147
Douglas Gregor4db64a42010-01-23 00:14:00 +0000148 /* Determine the length of the file we're remapping to. */
149 fseek(to_file, 0, SEEK_END);
150 unsaved->Length = ftell(to_file);
151 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000152
Douglas Gregor4db64a42010-01-23 00:14:00 +0000153 /* Read the contents of the file we're remapping to. */
154 contents = (char *)malloc(unsaved->Length + 1);
155 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
156 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
157 (feof(to_file) ? "EOF" : "error"), semi + 1);
158 fclose(to_file);
159 free_remapped_files(*unsaved_files, i);
160 *unsaved_files = 0;
161 *num_unsaved_files = 0;
162 return -1;
163 }
164 contents[unsaved->Length] = 0;
165 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000166
Douglas Gregor4db64a42010-01-23 00:14:00 +0000167 /* Close the file. */
168 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000169
Douglas Gregor4db64a42010-01-23 00:14:00 +0000170 /* Copy the file name that we're remapping from. */
171 filename_len = semi - arg_string;
172 filename = (char *)malloc(filename_len + 1);
173 memcpy(filename, arg_string, filename_len);
174 filename[filename_len] = 0;
175 unsaved->Filename = filename;
176 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000177
Douglas Gregor4db64a42010-01-23 00:14:00 +0000178 return 0;
179}
180
Ted Kremenek0d435192009-11-17 18:13:31 +0000181/******************************************************************************/
182/* Pretty-printing. */
183/******************************************************************************/
184
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000185static void PrintCString(const char *Prefix, const char *CStr) {
186 printf(" %s=[", Prefix);
187 if (CStr != NULL && CStr[0] != '\0') {
188 for ( ; *CStr; ++CStr) {
189 const char C = *CStr;
190 switch (C) {
191 case '\n': printf("\\n"); break;
192 case '\r': printf("\\r"); break;
193 case '\t': printf("\\t"); break;
194 case '\v': printf("\\v"); break;
195 case '\f': printf("\\f"); break;
196 default: putchar(C); break;
197 }
198 }
199 }
200 printf("]");
201}
202
Douglas Gregor430d7a12011-07-25 17:48:11 +0000203static void PrintRange(CXSourceRange R, const char *str) {
204 CXFile begin_file, end_file;
205 unsigned begin_line, begin_column, end_line, end_column;
206
207 clang_getSpellingLocation(clang_getRangeStart(R),
208 &begin_file, &begin_line, &begin_column, 0);
209 clang_getSpellingLocation(clang_getRangeEnd(R),
210 &end_file, &end_line, &end_column, 0);
211 if (!begin_file || !end_file)
212 return;
213
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000214 if (str)
215 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000216 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
217}
218
Douglas Gregor358559d2010-10-02 22:49:11 +0000219int want_display_name = 0;
220
Douglas Gregorcc889662012-05-08 00:14:45 +0000221static void printVersion(const char *Prefix, CXVersion Version) {
222 if (Version.Major < 0)
223 return;
224 printf("%s%d", Prefix, Version.Major);
225
226 if (Version.Minor < 0)
227 return;
228 printf(".%d", Version.Minor);
229
230 if (Version.Subminor < 0)
231 return;
232 printf(".%d", Version.Subminor);
233}
234
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000235static void PrintCursor(CXCursor Cursor) {
236 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000237 if (clang_isInvalid(Cursor.kind)) {
238 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
239 printf("Invalid Cursor => %s", clang_getCString(ks));
240 clang_disposeString(ks);
241 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000242 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000243 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000244 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000245 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000246 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000247 CXCursor *overridden;
248 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000249 unsigned RefNameRangeNr;
250 CXSourceRange CursorExtent;
251 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000252 int AlwaysUnavailable;
253 int AlwaysDeprecated;
254 CXString UnavailableMessage;
255 CXString DeprecatedMessage;
256 CXPlatformAvailability PlatformAvailability[2];
257 int NumPlatformAvailability;
258 int I;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000259 CXString RawComment;
260 const char *RawCommentCString;
261 CXString BriefComment;
262 const char *BriefCommentCString;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000263
Ted Kremeneke68fff62010-02-17 00:41:32 +0000264 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000265 string = want_display_name? clang_getCursorDisplayName(Cursor)
266 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000267 printf("%s=%s", clang_getCString(ks),
268 clang_getCString(string));
269 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000270 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000271
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000272 Referenced = clang_getCursorReferenced(Cursor);
273 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000274 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
275 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
276 printf("[");
277 for (I = 0; I != N; ++I) {
278 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000279 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000280 if (I)
281 printf(", ");
282
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000283 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000284 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000285 printf("%d:%d", line, column);
286 }
287 printf("]");
288 } else {
289 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000290 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000291 printf(":%d:%d", line, column);
292 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000293 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000294
295 if (clang_isCursorDefinition(Cursor))
296 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000297
298 switch (clang_getCursorAvailability(Cursor)) {
299 case CXAvailability_Available:
300 break;
301
302 case CXAvailability_Deprecated:
303 printf(" (deprecated)");
304 break;
305
306 case CXAvailability_NotAvailable:
307 printf(" (unavailable)");
308 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000309
310 case CXAvailability_NotAccessible:
311 printf(" (inaccessible)");
312 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000313 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000314
Douglas Gregorcc889662012-05-08 00:14:45 +0000315 NumPlatformAvailability
316 = clang_getCursorPlatformAvailability(Cursor,
317 &AlwaysDeprecated,
318 &DeprecatedMessage,
319 &AlwaysUnavailable,
320 &UnavailableMessage,
321 PlatformAvailability, 2);
322 if (AlwaysUnavailable) {
323 printf(" (always unavailable: \"%s\")",
324 clang_getCString(UnavailableMessage));
325 } else if (AlwaysDeprecated) {
326 printf(" (always deprecated: \"%s\")",
327 clang_getCString(DeprecatedMessage));
328 } else {
329 for (I = 0; I != NumPlatformAvailability; ++I) {
330 if (I >= 2)
331 break;
332
333 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
334 if (PlatformAvailability[I].Unavailable)
335 printf(", unavailable");
336 else {
337 printVersion(", introduced=", PlatformAvailability[I].Introduced);
338 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
339 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
340 }
341 if (clang_getCString(PlatformAvailability[I].Message)[0])
342 printf(", message=\"%s\"",
343 clang_getCString(PlatformAvailability[I].Message));
344 printf(")");
345 }
346 }
347 for (I = 0; I != NumPlatformAvailability; ++I) {
348 if (I >= 2)
349 break;
350 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
351 }
352
353 clang_disposeString(DeprecatedMessage);
354 clang_disposeString(UnavailableMessage);
355
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000356 if (clang_CXXMethod_isStatic(Cursor))
357 printf(" (static)");
358 if (clang_CXXMethod_isVirtual(Cursor))
359 printf(" (virtual)");
360
Ted Kremenek95f33552010-08-26 01:42:22 +0000361 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
362 CXType T =
363 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
364 CXString S = clang_getTypeKindSpelling(T.kind);
365 printf(" [IBOutletCollection=%s]", clang_getCString(S));
366 clang_disposeString(S);
367 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000368
369 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
370 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
371 unsigned isVirtual = clang_isVirtualBase(Cursor);
372 const char *accessStr = 0;
373
374 switch (access) {
375 case CX_CXXInvalidAccessSpecifier:
376 accessStr = "invalid"; break;
377 case CX_CXXPublic:
378 accessStr = "public"; break;
379 case CX_CXXProtected:
380 accessStr = "protected"; break;
381 case CX_CXXPrivate:
382 accessStr = "private"; break;
383 }
384
385 printf(" [access=%s isVirtual=%s]", accessStr,
386 isVirtual ? "true" : "false");
387 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000388
389 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
390 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
391 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
392 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000393 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000394 printf(" [Specialization of %s:%d:%d]",
395 clang_getCString(Name), line, column);
396 clang_disposeString(Name);
397 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000398
399 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
400 if (num_overridden) {
401 unsigned I;
402 printf(" [Overrides ");
403 for (I = 0; I != num_overridden; ++I) {
404 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000405 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000406 if (I)
407 printf(", ");
408 printf("@%d:%d", line, column);
409 }
410 printf("]");
411 clang_disposeOverriddenCursors(overridden);
412 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000413
414 if (Cursor.kind == CXCursor_InclusionDirective) {
415 CXFile File = clang_getIncludedFile(Cursor);
416 CXString Included = clang_getFileName(File);
417 printf(" (%s)", clang_getCString(Included));
418 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000419
420 if (clang_isFileMultipleIncludeGuarded(TU, File))
421 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000422 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000423
424 CursorExtent = clang_getCursorExtent(Cursor);
425 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
426 CXNameRange_WantQualifier
427 | CXNameRange_WantSinglePiece
428 | CXNameRange_WantTemplateArgs,
429 0);
430 if (!clang_equalRanges(CursorExtent, RefNameRange))
431 PrintRange(RefNameRange, "SingleRefName");
432
433 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
434 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
435 CXNameRange_WantQualifier
436 | CXNameRange_WantTemplateArgs,
437 RefNameRangeNr);
438 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
439 break;
440 if (!clang_equalRanges(CursorExtent, RefNameRange))
441 PrintRange(RefNameRange, "RefName");
442 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000443
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000444 RawComment = clang_Cursor_getRawCommentText(Cursor);
445 RawCommentCString = clang_getCString(RawComment);
446 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
447 PrintCString("RawComment", RawCommentCString);
448 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000449
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000450 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
451 BriefCommentCString = clang_getCString(BriefComment);
452 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
453 PrintCString("BriefComment", BriefCommentCString);
454 clang_disposeString(BriefComment);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000455 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000456 clang_disposeString(RawComment);
Steve Naroff699a07d2009-09-25 21:32:34 +0000457 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000458}
Steve Naroff89922f82009-08-31 00:59:03 +0000459
Ted Kremeneke68fff62010-02-17 00:41:32 +0000460static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000461 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000462 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000463 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000464 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000465 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000466 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000467 clang_disposeString(source);
468 return "<invalid loc>";
469 }
470 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000471 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000472 clang_disposeString(source);
473 return b;
474 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000475}
476
Ted Kremenek0d435192009-11-17 18:13:31 +0000477/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000478/* Callbacks. */
479/******************************************************************************/
480
481typedef void (*PostVisitTU)(CXTranslationUnit);
482
Douglas Gregora88084b2010-02-18 18:08:43 +0000483void PrintDiagnostic(CXDiagnostic Diagnostic) {
484 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000485 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000486 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000487 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000488 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
489 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000490 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000491
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000492 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000493 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000494
Douglas Gregor274f1902010-02-22 23:17:23 +0000495 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
496 fprintf(stderr, "%s\n", clang_getCString(Msg));
497 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000498
Douglas Gregora9b06d42010-11-09 06:24:54 +0000499 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
500 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000501 if (!file)
502 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000503
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000504 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000505 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000506 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000507 CXSourceRange range;
508 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
509 CXSourceLocation start = clang_getRangeStart(range);
510 CXSourceLocation end = clang_getRangeEnd(range);
511 unsigned start_line, start_column, end_line, end_column;
512 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000513 clang_getSpellingLocation(start, &start_file, &start_line,
514 &start_column, 0);
515 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000516 if (clang_equalLocations(start, end)) {
517 /* Insertion. */
518 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000519 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000520 clang_getCString(insertion_text), start_line, start_column);
521 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
522 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000523 if (start_file == file && end_file == file) {
524 fprintf(out, "FIX-IT: Remove ");
525 PrintExtent(out, start_line, start_column, end_line, end_column);
526 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000527 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000528 } else {
529 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000530 if (start_file == end_file) {
531 fprintf(out, "FIX-IT: Replace ");
532 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000533 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000534 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000535 break;
536 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000537 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000538 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000539}
540
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000541void PrintDiagnosticSet(CXDiagnosticSet Set) {
542 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
543 for ( ; i != n ; ++i) {
544 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
545 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000546 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000547 if (ChildDiags)
548 PrintDiagnosticSet(ChildDiags);
549 }
550}
551
552void PrintDiagnostics(CXTranslationUnit TU) {
553 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
554 PrintDiagnosticSet(TUSet);
555 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000556}
557
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000558void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000559 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000560 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000561 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000562 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000563 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000564 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000565 unsigned long amount = usage.entries[i].amount;
566 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000567 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000568 ((double) amount)/(1024*1024));
569 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000570 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000571 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000572 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000573}
574
Ted Kremenekce2ae882010-01-26 17:59:48 +0000575/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000576/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000577/******************************************************************************/
578
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000579static const char *FileCheckPrefix = "CHECK";
580
Douglas Gregora7bde202010-01-19 00:34:46 +0000581static void PrintCursorExtent(CXCursor C) {
582 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000583 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000584}
585
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000586/* Data used by all of the visitors. */
587typedef struct {
588 CXTranslationUnit TU;
589 enum CXCursorKind *Filter;
590} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000591
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000592
Ted Kremeneke68fff62010-02-17 00:41:32 +0000593enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000594 CXCursor Parent,
595 CXClientData ClientData) {
596 VisitorData *Data = (VisitorData *)ClientData;
597 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000598 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000599 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000600 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000601 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000602 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000603 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000604 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000605 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000606 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000607 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000608
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000609 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000610}
Steve Naroff50398192009-08-28 15:28:48 +0000611
Ted Kremeneke68fff62010-02-17 00:41:32 +0000612static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000613 CXCursor Parent,
614 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000615 const char *startBuf, *endBuf;
616 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
617 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000618 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000619
Douglas Gregorb6998662010-01-19 19:34:47 +0000620 if (Cursor.kind != CXCursor_FunctionDecl ||
621 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000622 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000623
624 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
625 &startLine, &startColumn,
626 &endLine, &endColumn);
627 /* Probe the entire body, looking for both decls and refs. */
628 curLine = startLine;
629 curColumn = startColumn;
630
631 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000632 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000633 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000634 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000635
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000636 if (*startBuf == '\n') {
637 startBuf++;
638 curLine++;
639 curColumn = 1;
640 } else if (*startBuf != '\t')
641 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000642
Douglas Gregor98258af2010-01-18 22:46:11 +0000643 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000644 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000645
Douglas Gregor1db19de2010-01-19 21:36:55 +0000646 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000647 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000648 CXSourceLocation RefLoc
649 = clang_getLocation(Data->TU, file, curLine, curColumn);
650 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000651 if (Ref.kind == CXCursor_NoDeclFound) {
652 /* Nothing found here; that's fine. */
653 } else if (Ref.kind != CXCursor_FunctionDecl) {
654 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
655 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000656 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000657 printf("\n");
658 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000659 }
Ted Kremenek74844072010-02-17 00:41:20 +0000660 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000661 startBuf++;
662 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000663
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000664 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000665}
666
Ted Kremenek7d405622010-01-12 23:34:26 +0000667/******************************************************************************/
668/* USR testing. */
669/******************************************************************************/
670
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000671enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
672 CXClientData ClientData) {
673 VisitorData *Data = (VisitorData *)ClientData;
674 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000675 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000676 const char *cstr = clang_getCString(USR);
677 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000678 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000679 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000680 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000681 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
682
Douglas Gregora7bde202010-01-19 00:34:46 +0000683 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000684 printf("\n");
685 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000686
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000687 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000688 }
689
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000690 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000691}
692
693/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000694/* Inclusion stack testing. */
695/******************************************************************************/
696
697void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
698 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000699
Ted Kremenek16b55a72010-01-26 19:31:51 +0000700 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000701 CXString fname;
702
703 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000704 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000705 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000706
Ted Kremenek16b55a72010-01-26 19:31:51 +0000707 for (i = 0; i < includeStackLen; ++i) {
708 CXFile includingFile;
709 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000710 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
711 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000712 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000713 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000714 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000715 }
716 printf("\n");
717}
718
719void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000720 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000721}
722
723/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000724/* Linkage testing. */
725/******************************************************************************/
726
727static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
728 CXClientData d) {
729 const char *linkage = 0;
730
731 if (clang_isInvalid(clang_getCursorKind(cursor)))
732 return CXChildVisit_Recurse;
733
734 switch (clang_getCursorLinkage(cursor)) {
735 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000736 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
737 case CXLinkage_Internal: linkage = "Internal"; break;
738 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
739 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000740 }
741
742 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000743 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000744 printf("linkage=%s\n", linkage);
745 }
746
747 return CXChildVisit_Recurse;
748}
749
750/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000751/* Typekind testing. */
752/******************************************************************************/
753
754static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
755 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000756 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
757 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000758 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000759 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000760 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000761 if (clang_isConstQualifiedType(T))
762 printf(" const");
763 if (clang_isVolatileQualifiedType(T))
764 printf(" volatile");
765 if (clang_isRestrictQualifiedType(T))
766 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000767 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000768 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000769 {
770 CXType CT = clang_getCanonicalType(T);
771 if (!clang_equalTypes(T, CT)) {
772 CXString CS = clang_getTypeKindSpelling(CT.kind);
773 printf(" [canonical=%s]", clang_getCString(CS));
774 clang_disposeString(CS);
775 }
776 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000777 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000778 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000779 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000780 if (RT.kind != CXType_Invalid) {
781 CXString RS = clang_getTypeKindSpelling(RT.kind);
782 printf(" [result=%s]", clang_getCString(RS));
783 clang_disposeString(RS);
784 }
785 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000786 /* Print the argument types if they exist. */
787 {
788 int numArgs = clang_Cursor_getNumArguments(cursor);
789 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000790 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000791 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000792 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000793 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
794 if (T.kind != CXType_Invalid) {
795 CXString S = clang_getTypeKindSpelling(T.kind);
796 printf(" %s", clang_getCString(S));
797 clang_disposeString(S);
798 }
799 }
800 printf("]");
801 }
802 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000803 /* Print if this is a non-POD type. */
804 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000805
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000806 printf("\n");
807 }
808 return CXChildVisit_Recurse;
809}
810
811
812/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000813/* Loading ASTs/source. */
814/******************************************************************************/
815
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000816static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000817 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000818 CXCursorVisitor Visitor,
819 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000820
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000821 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000822 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000823
824 if (Visitor) {
825 enum CXCursorKind K = CXCursor_NotImplemented;
826 enum CXCursorKind *ck = &K;
827 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000828
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000829 /* Perform some simple filtering. */
830 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000831 else if (!strcmp(filter, "all-display") ||
832 !strcmp(filter, "local-display")) {
833 ck = NULL;
834 want_display_name = 1;
835 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000836 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000837 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
838 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
839 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
840 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
841 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
842 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
843 else {
844 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
845 return 1;
846 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000847
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000848 Data.TU = TU;
849 Data.Filter = ck;
850 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000851 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000852
Ted Kremenekce2ae882010-01-26 17:59:48 +0000853 if (PV)
854 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000855
Douglas Gregora88084b2010-02-18 18:08:43 +0000856 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000857 if (checkForErrors(TU) != 0) {
858 clang_disposeTranslationUnit(TU);
859 return -1;
860 }
861
Ted Kremenek0d435192009-11-17 18:13:31 +0000862 clang_disposeTranslationUnit(TU);
863 return 0;
864}
865
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000866int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000867 const char *prefix, CXCursorVisitor Visitor,
868 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000869 CXIndex Idx;
870 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000871 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000872 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000873 !strcmp(filter, "local") ? 1 : 0,
874 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875
Ted Kremenek020a0952010-02-11 07:41:25 +0000876 if (!CreateTranslationUnit(Idx, file, &TU)) {
877 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000878 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000879 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000880
Ted Kremenek020a0952010-02-11 07:41:25 +0000881 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
882 clang_disposeIndex(Idx);
883 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000884}
885
Ted Kremenekce2ae882010-01-26 17:59:48 +0000886int perform_test_load_source(int argc, const char **argv,
887 const char *filter, CXCursorVisitor Visitor,
888 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000889 CXIndex Idx;
890 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000891 struct CXUnsavedFile *unsaved_files = 0;
892 int num_unsaved_files = 0;
893 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000894
Daniel Dunbarada487d2009-12-01 02:03:10 +0000895 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000896 (!strcmp(filter, "local") ||
897 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000898 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000899
Ted Kremenek020a0952010-02-11 07:41:25 +0000900 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
901 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000902 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000903 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000904
Douglas Gregordca8ee82011-05-06 16:33:08 +0000905 TU = clang_parseTranslationUnit(Idx, 0,
906 argv + num_unsaved_files,
907 argc - num_unsaved_files,
908 unsaved_files, num_unsaved_files,
909 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000910 if (!TU) {
911 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000912 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000913 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000914 return 1;
915 }
916
Ted Kremenekce2ae882010-01-26 17:59:48 +0000917 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000918 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000919 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000920 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000921}
922
Douglas Gregorabc563f2010-07-19 21:46:24 +0000923int perform_test_reparse_source(int argc, const char **argv, int trials,
924 const char *filter, CXCursorVisitor Visitor,
925 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000926 CXIndex Idx;
927 CXTranslationUnit TU;
928 struct CXUnsavedFile *unsaved_files = 0;
929 int num_unsaved_files = 0;
930 int result;
931 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000932 int remap_after_trial = 0;
933 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000934
935 Idx = clang_createIndex(/* excludeDeclsFromPCH */
936 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000937 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000938
Douglas Gregorabc563f2010-07-19 21:46:24 +0000939 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
940 clang_disposeIndex(Idx);
941 return -1;
942 }
943
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000944 /* Load the initial translation unit -- we do this without honoring remapped
945 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000946 TU = clang_parseTranslationUnit(Idx, 0,
947 argv + num_unsaved_files,
948 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000949 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000950 if (!TU) {
951 fprintf(stderr, "Unable to load translation unit!\n");
952 free_remapped_files(unsaved_files, num_unsaved_files);
953 clang_disposeIndex(Idx);
954 return 1;
955 }
956
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000957 if (checkForErrors(TU) != 0)
958 return -1;
959
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000960 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
961 remap_after_trial =
962 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
963 }
964
Douglas Gregorabc563f2010-07-19 21:46:24 +0000965 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000966 if (clang_reparseTranslationUnit(TU,
967 trial >= remap_after_trial ? num_unsaved_files : 0,
968 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000969 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000970 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000971 clang_disposeTranslationUnit(TU);
972 free_remapped_files(unsaved_files, num_unsaved_files);
973 clang_disposeIndex(Idx);
974 return -1;
975 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000976
977 if (checkForErrors(TU) != 0)
978 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000979 }
980
981 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000982
Douglas Gregorabc563f2010-07-19 21:46:24 +0000983 free_remapped_files(unsaved_files, num_unsaved_files);
984 clang_disposeIndex(Idx);
985 return result;
986}
987
Ted Kremenek0d435192009-11-17 18:13:31 +0000988/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000989/* Logic for testing clang_getCursor(). */
990/******************************************************************************/
991
Douglas Gregordd3e5542011-05-04 00:14:37 +0000992static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000993 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000994 unsigned end_line, unsigned end_col,
995 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000996 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000997 if (prefix)
998 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000999 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1000 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001001 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001002 printf("\n");
1003}
1004
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001005static int perform_file_scan(const char *ast_file, const char *source_file,
1006 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001007 CXIndex Idx;
1008 CXTranslationUnit TU;
1009 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001010 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001011 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001012 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001013 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001014
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001015 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1016 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001017 fprintf(stderr, "Could not create Index\n");
1018 return 1;
1019 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001020
Ted Kremenek1c6da172009-11-17 19:37:36 +00001021 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1022 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001023
Ted Kremenek1c6da172009-11-17 19:37:36 +00001024 if ((fp = fopen(source_file, "r")) == NULL) {
1025 fprintf(stderr, "Could not open '%s'\n", source_file);
1026 return 1;
1027 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001028
Douglas Gregorb9790342010-01-22 21:44:22 +00001029 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001030 for (;;) {
1031 CXCursor cursor;
1032 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001033
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001034 if (c == '\n') {
1035 ++line;
1036 col = 1;
1037 } else
1038 ++col;
1039
1040 /* Check the cursor at this position, and dump the previous one if we have
1041 * found something new.
1042 */
1043 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1044 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1045 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001046 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001047 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001048 start_line = line;
1049 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001050 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001051 if (c == EOF)
1052 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001053
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001054 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001055 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001056
Ted Kremenek1c6da172009-11-17 19:37:36 +00001057 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001058 clang_disposeTranslationUnit(TU);
1059 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001060 return 0;
1061}
1062
1063/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001064/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001065/******************************************************************************/
1066
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001067/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1068 on failure. If successful, the pointer *filename will contain newly-allocated
1069 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001070int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001071 unsigned *column, unsigned *second_line,
1072 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001073 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001074 const char *last_colon = strrchr(input, ':');
1075 unsigned values[4], i;
1076 unsigned num_values = (second_line && second_column)? 4 : 2;
1077
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001078 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001079 if (!last_colon || last_colon == input) {
1080 if (num_values == 4)
1081 fprintf(stderr, "could not parse filename:line:column:line:column in "
1082 "'%s'\n", input);
1083 else
1084 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001085 return 1;
1086 }
1087
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001088 for (i = 0; i != num_values; ++i) {
1089 const char *prev_colon;
1090
1091 /* Parse the next line or column. */
1092 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1093 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001094 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001095 (i % 2 ? "column" : "line"), input);
1096 return 1;
1097 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001098
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001099 if (i + 1 == num_values)
1100 break;
1101
1102 /* Find the previous colon. */
1103 prev_colon = last_colon - 1;
1104 while (prev_colon != input && *prev_colon != ':')
1105 --prev_colon;
1106 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001107 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001108 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001109 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001110 }
1111
1112 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001113 }
1114
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001115 *line = values[0];
1116 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001117
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001118 if (second_line && second_column) {
1119 *second_line = values[2];
1120 *second_column = values[3];
1121 }
1122
Douglas Gregor88d23952009-11-09 18:19:57 +00001123 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001124 *filename = (char*)malloc(last_colon - input + 1);
1125 memcpy(*filename, input, last_colon - input);
1126 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001127 return 0;
1128}
1129
1130const char *
1131clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1132 switch (Kind) {
1133 case CXCompletionChunk_Optional: return "Optional";
1134 case CXCompletionChunk_TypedText: return "TypedText";
1135 case CXCompletionChunk_Text: return "Text";
1136 case CXCompletionChunk_Placeholder: return "Placeholder";
1137 case CXCompletionChunk_Informative: return "Informative";
1138 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1139 case CXCompletionChunk_LeftParen: return "LeftParen";
1140 case CXCompletionChunk_RightParen: return "RightParen";
1141 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1142 case CXCompletionChunk_RightBracket: return "RightBracket";
1143 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1144 case CXCompletionChunk_RightBrace: return "RightBrace";
1145 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1146 case CXCompletionChunk_RightAngle: return "RightAngle";
1147 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001148 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001149 case CXCompletionChunk_Colon: return "Colon";
1150 case CXCompletionChunk_SemiColon: return "SemiColon";
1151 case CXCompletionChunk_Equal: return "Equal";
1152 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1153 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001154 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001155
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001156 return "Unknown";
1157}
1158
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001159static int checkForErrors(CXTranslationUnit TU) {
1160 unsigned Num, i;
1161 CXDiagnostic Diag;
1162 CXString DiagStr;
1163
1164 if (!getenv("CINDEXTEST_FAILONERROR"))
1165 return 0;
1166
1167 Num = clang_getNumDiagnostics(TU);
1168 for (i = 0; i != Num; ++i) {
1169 Diag = clang_getDiagnostic(TU, i);
1170 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1171 DiagStr = clang_formatDiagnostic(Diag,
1172 clang_defaultDiagnosticDisplayOptions());
1173 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1174 clang_disposeString(DiagStr);
1175 clang_disposeDiagnostic(Diag);
1176 return -1;
1177 }
1178 clang_disposeDiagnostic(Diag);
1179 }
1180
1181 return 0;
1182}
1183
Douglas Gregor3ac73852009-11-09 16:04:45 +00001184void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001185 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001186
Douglas Gregor3ac73852009-11-09 16:04:45 +00001187 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001188 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001189 CXString text;
1190 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001191 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001192 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001193
Douglas Gregor3ac73852009-11-09 16:04:45 +00001194 if (Kind == CXCompletionChunk_Optional) {
1195 fprintf(file, "{Optional ");
1196 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001197 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001198 file);
1199 fprintf(file, "}");
1200 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001201 }
1202
1203 if (Kind == CXCompletionChunk_VerticalSpace) {
1204 fprintf(file, "{VerticalSpace }");
1205 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001206 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001207
Douglas Gregord5a20892009-11-09 17:05:28 +00001208 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001209 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001210 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001211 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001212 cstr ? cstr : "");
1213 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001214 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001215
Douglas Gregor3ac73852009-11-09 16:04:45 +00001216}
1217
1218void print_completion_result(CXCompletionResult *completion_result,
1219 CXClientData client_data) {
1220 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001221 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001222 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001223 enum CXCursorKind ParentKind;
1224 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001225 CXString BriefComment;
1226 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001227
Ted Kremeneke68fff62010-02-17 00:41:32 +00001228 fprintf(file, "%s:", clang_getCString(ks));
1229 clang_disposeString(ks);
1230
Douglas Gregor3ac73852009-11-09 16:04:45 +00001231 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001232 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001233 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001234 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1235 case CXAvailability_Available:
1236 break;
1237
1238 case CXAvailability_Deprecated:
1239 fprintf(file, " (deprecated)");
1240 break;
1241
1242 case CXAvailability_NotAvailable:
1243 fprintf(file, " (unavailable)");
1244 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001245
1246 case CXAvailability_NotAccessible:
1247 fprintf(file, " (inaccessible)");
1248 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001249 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001250
1251 annotationCount = clang_getCompletionNumAnnotations(
1252 completion_result->CompletionString);
1253 if (annotationCount) {
1254 unsigned i;
1255 fprintf(file, " (");
1256 for (i = 0; i < annotationCount; ++i) {
1257 if (i != 0)
1258 fprintf(file, ", ");
1259 fprintf(file, "\"%s\"",
1260 clang_getCString(clang_getCompletionAnnotation(
1261 completion_result->CompletionString, i)));
1262 }
1263 fprintf(file, ")");
1264 }
1265
Douglas Gregorba103062012-03-27 23:34:16 +00001266 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1267 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1268 &ParentKind);
1269 if (ParentKind != CXCursor_NotImplemented) {
1270 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1271 fprintf(file, " (parent: %s '%s')",
1272 clang_getCString(KindSpelling),
1273 clang_getCString(ParentName));
1274 clang_disposeString(KindSpelling);
1275 }
1276 clang_disposeString(ParentName);
1277 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001278
1279 BriefComment = clang_getCompletionBriefComment(
1280 completion_result->CompletionString);
1281 BriefCommentCString = clang_getCString(BriefComment);
1282 if (BriefCommentCString && *BriefCommentCString != '\0') {
1283 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1284 }
1285 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001286
Douglas Gregor58ddb602010-08-23 23:00:57 +00001287 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001288}
1289
Douglas Gregor3da626b2011-07-07 16:03:39 +00001290void print_completion_contexts(unsigned long long contexts, FILE *file) {
1291 fprintf(file, "Completion contexts:\n");
1292 if (contexts == CXCompletionContext_Unknown) {
1293 fprintf(file, "Unknown\n");
1294 }
1295 if (contexts & CXCompletionContext_AnyType) {
1296 fprintf(file, "Any type\n");
1297 }
1298 if (contexts & CXCompletionContext_AnyValue) {
1299 fprintf(file, "Any value\n");
1300 }
1301 if (contexts & CXCompletionContext_ObjCObjectValue) {
1302 fprintf(file, "Objective-C object value\n");
1303 }
1304 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1305 fprintf(file, "Objective-C selector value\n");
1306 }
1307 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1308 fprintf(file, "C++ class type value\n");
1309 }
1310 if (contexts & CXCompletionContext_DotMemberAccess) {
1311 fprintf(file, "Dot member access\n");
1312 }
1313 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1314 fprintf(file, "Arrow member access\n");
1315 }
1316 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1317 fprintf(file, "Objective-C property access\n");
1318 }
1319 if (contexts & CXCompletionContext_EnumTag) {
1320 fprintf(file, "Enum tag\n");
1321 }
1322 if (contexts & CXCompletionContext_UnionTag) {
1323 fprintf(file, "Union tag\n");
1324 }
1325 if (contexts & CXCompletionContext_StructTag) {
1326 fprintf(file, "Struct tag\n");
1327 }
1328 if (contexts & CXCompletionContext_ClassTag) {
1329 fprintf(file, "Class name\n");
1330 }
1331 if (contexts & CXCompletionContext_Namespace) {
1332 fprintf(file, "Namespace or namespace alias\n");
1333 }
1334 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1335 fprintf(file, "Nested name specifier\n");
1336 }
1337 if (contexts & CXCompletionContext_ObjCInterface) {
1338 fprintf(file, "Objective-C interface\n");
1339 }
1340 if (contexts & CXCompletionContext_ObjCProtocol) {
1341 fprintf(file, "Objective-C protocol\n");
1342 }
1343 if (contexts & CXCompletionContext_ObjCCategory) {
1344 fprintf(file, "Objective-C category\n");
1345 }
1346 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1347 fprintf(file, "Objective-C instance method\n");
1348 }
1349 if (contexts & CXCompletionContext_ObjCClassMessage) {
1350 fprintf(file, "Objective-C class method\n");
1351 }
1352 if (contexts & CXCompletionContext_ObjCSelectorName) {
1353 fprintf(file, "Objective-C selector name\n");
1354 }
1355 if (contexts & CXCompletionContext_MacroName) {
1356 fprintf(file, "Macro name\n");
1357 }
1358 if (contexts & CXCompletionContext_NaturalLanguage) {
1359 fprintf(file, "Natural language\n");
1360 }
1361}
1362
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001363int my_stricmp(const char *s1, const char *s2) {
1364 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001365 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001366 if (c1 < c2)
1367 return -1;
1368 else if (c1 > c2)
1369 return 1;
1370
1371 ++s1;
1372 ++s2;
1373 }
1374
1375 if (*s1)
1376 return 1;
1377 else if (*s2)
1378 return -1;
1379 return 0;
1380}
1381
Douglas Gregor1982c182010-07-12 18:38:41 +00001382int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001383 const char *input = argv[1];
1384 char *filename = 0;
1385 unsigned line;
1386 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001387 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001388 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001389 struct CXUnsavedFile *unsaved_files = 0;
1390 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001391 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001392 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001393 unsigned I, Repeats = 1;
1394 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1395
1396 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1397 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001398 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1399 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001400
Douglas Gregor1982c182010-07-12 18:38:41 +00001401 if (timing_only)
1402 input += strlen("-code-completion-timing=");
1403 else
1404 input += strlen("-code-completion-at=");
1405
Ted Kremeneke68fff62010-02-17 00:41:32 +00001406 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001407 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001408 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001409
Douglas Gregor735df882009-12-02 09:21:34 +00001410 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1411 return -1;
1412
Douglas Gregor32be4a52010-10-11 21:37:58 +00001413 CIdx = clang_createIndex(0, 0);
1414
1415 if (getenv("CINDEXTEST_EDITING"))
1416 Repeats = 5;
1417
1418 TU = clang_parseTranslationUnit(CIdx, 0,
1419 argv + num_unsaved_files + 2,
1420 argc - num_unsaved_files - 2,
1421 0, 0, getDefaultParsingOptions());
1422 if (!TU) {
1423 fprintf(stderr, "Unable to load translation unit!\n");
1424 return 1;
1425 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001426
1427 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1428 fprintf(stderr, "Unable to reparse translation init!\n");
1429 return 1;
1430 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001431
1432 for (I = 0; I != Repeats; ++I) {
1433 results = clang_codeCompleteAt(TU, filename, line, column,
1434 unsaved_files, num_unsaved_files,
1435 completionOptions);
1436 if (!results) {
1437 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001438 return 1;
1439 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001440 if (I != Repeats-1)
1441 clang_disposeCodeCompleteResults(results);
1442 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001443
Douglas Gregorec6762c2009-12-18 16:20:58 +00001444 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001445 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001446 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001447 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001448 CXString objCSelector;
1449 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001450 if (!timing_only) {
1451 /* Sort the code-completion results based on the typed text. */
1452 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1453
Douglas Gregor1982c182010-07-12 18:38:41 +00001454 for (i = 0; i != n; ++i)
1455 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001456 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001457 n = clang_codeCompleteGetNumDiagnostics(results);
1458 for (i = 0; i != n; ++i) {
1459 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1460 PrintDiagnostic(diag);
1461 clang_disposeDiagnostic(diag);
1462 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001463
1464 contexts = clang_codeCompleteGetContexts(results);
1465 print_completion_contexts(contexts, stdout);
1466
Douglas Gregor0a47d692011-07-26 15:24:30 +00001467 containerKind = clang_codeCompleteGetContainerKind(results,
1468 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001469
1470 if (containerKind != CXCursor_InvalidCode) {
1471 /* We have found a container */
1472 CXString containerUSR, containerKindSpelling;
1473 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1474 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1475 clang_disposeString(containerKindSpelling);
1476
1477 if (containerIsIncomplete) {
1478 printf("Container is incomplete\n");
1479 }
1480 else {
1481 printf("Container is complete\n");
1482 }
1483
1484 containerUSR = clang_codeCompleteGetContainerUSR(results);
1485 printf("Container USR: %s\n", clang_getCString(containerUSR));
1486 clang_disposeString(containerUSR);
1487 }
1488
Douglas Gregor0a47d692011-07-26 15:24:30 +00001489 objCSelector = clang_codeCompleteGetObjCSelector(results);
1490 selectorString = clang_getCString(objCSelector);
1491 if (selectorString && strlen(selectorString) > 0) {
1492 printf("Objective-C selector: %s\n", selectorString);
1493 }
1494 clang_disposeString(objCSelector);
1495
Douglas Gregorec6762c2009-12-18 16:20:58 +00001496 clang_disposeCodeCompleteResults(results);
1497 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001498 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001499 clang_disposeIndex(CIdx);
1500 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001501
Douglas Gregor735df882009-12-02 09:21:34 +00001502 free_remapped_files(unsaved_files, num_unsaved_files);
1503
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001504 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001505}
1506
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001507typedef struct {
1508 char *filename;
1509 unsigned line;
1510 unsigned column;
1511} CursorSourceLocation;
1512
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001513static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001514 CXIndex CIdx;
1515 int errorCode;
1516 struct CXUnsavedFile *unsaved_files = 0;
1517 int num_unsaved_files = 0;
1518 CXTranslationUnit TU;
1519 CXCursor Cursor;
1520 CursorSourceLocation *Locations = 0;
1521 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001522 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001523 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001524
Ted Kremeneke68fff62010-02-17 00:41:32 +00001525 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001526 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1527 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001528
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001529 /* Parse the locations. */
1530 assert(NumLocations > 0 && "Unable to count locations?");
1531 Locations = (CursorSourceLocation *)malloc(
1532 NumLocations * sizeof(CursorSourceLocation));
1533 for (Loc = 0; Loc < NumLocations; ++Loc) {
1534 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001535 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1536 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001537 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001538 return errorCode;
1539 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001540
1541 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001542 &num_unsaved_files))
1543 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001544
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001545 if (getenv("CINDEXTEST_EDITING"))
1546 Repeats = 5;
1547
1548 /* Parse the translation unit. When we're testing clang_getCursor() after
1549 reparsing, don't remap unsaved files until the second parse. */
1550 CIdx = clang_createIndex(1, 1);
1551 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1552 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001553 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001554 unsaved_files,
1555 Repeats > 1? 0 : num_unsaved_files,
1556 getDefaultParsingOptions());
1557
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001558 if (!TU) {
1559 fprintf(stderr, "unable to parse input\n");
1560 return -1;
1561 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001562
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001563 if (checkForErrors(TU) != 0)
1564 return -1;
1565
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001566 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001567 if (Repeats > 1 &&
1568 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1569 clang_defaultReparseOptions(TU))) {
1570 clang_disposeTranslationUnit(TU);
1571 return 1;
1572 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001573
1574 if (checkForErrors(TU) != 0)
1575 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001576
1577 for (Loc = 0; Loc < NumLocations; ++Loc) {
1578 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1579 if (!file)
1580 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001581
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001582 Cursor = clang_getCursor(TU,
1583 clang_getLocation(TU, file, Locations[Loc].line,
1584 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001585
1586 if (checkForErrors(TU) != 0)
1587 return -1;
1588
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001589 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001590 CXCompletionString completionString = clang_getCursorCompletionString(
1591 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001592 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1593 CXString Spelling;
1594 const char *cspell;
1595 unsigned line, column;
1596 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1597 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001598 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001599 PrintCursorExtent(Cursor);
1600 Spelling = clang_getCursorSpelling(Cursor);
1601 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001602 if (cspell && strlen(cspell) != 0) {
1603 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001604 printf(" Spelling=%s (", cspell);
1605 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001606 CXSourceRange range =
1607 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001608 if (clang_Range_isNull(range))
1609 break;
1610 PrintRange(range, 0);
1611 }
1612 printf(")");
1613 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001614 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001615 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1616 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001617 if (clang_Cursor_isDynamicCall(Cursor))
1618 printf(" Dynamic-call");
1619
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001620 if (completionString != NULL) {
1621 printf("\nCompletion string: ");
1622 print_completion_string(completionString, stdout);
1623 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001624 printf("\n");
1625 free(Locations[Loc].filename);
1626 }
1627 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001628 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001629
Douglas Gregora88084b2010-02-18 18:08:43 +00001630 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001631 clang_disposeTranslationUnit(TU);
1632 clang_disposeIndex(CIdx);
1633 free(Locations);
1634 free_remapped_files(unsaved_files, num_unsaved_files);
1635 return 0;
1636}
1637
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001638static enum CXVisitorResult findFileRefsVisit(void *context,
1639 CXCursor cursor, CXSourceRange range) {
1640 if (clang_Range_isNull(range))
1641 return CXVisit_Continue;
1642
1643 PrintCursor(cursor);
1644 PrintRange(range, "");
1645 printf("\n");
1646 return CXVisit_Continue;
1647}
1648
1649static int find_file_refs_at(int argc, const char **argv) {
1650 CXIndex CIdx;
1651 int errorCode;
1652 struct CXUnsavedFile *unsaved_files = 0;
1653 int num_unsaved_files = 0;
1654 CXTranslationUnit TU;
1655 CXCursor Cursor;
1656 CursorSourceLocation *Locations = 0;
1657 unsigned NumLocations = 0, Loc;
1658 unsigned Repeats = 1;
1659 unsigned I;
1660
1661 /* Count the number of locations. */
1662 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1663 ++NumLocations;
1664
1665 /* Parse the locations. */
1666 assert(NumLocations > 0 && "Unable to count locations?");
1667 Locations = (CursorSourceLocation *)malloc(
1668 NumLocations * sizeof(CursorSourceLocation));
1669 for (Loc = 0; Loc < NumLocations; ++Loc) {
1670 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1671 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1672 &Locations[Loc].line,
1673 &Locations[Loc].column, 0, 0)))
1674 return errorCode;
1675 }
1676
1677 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1678 &num_unsaved_files))
1679 return -1;
1680
1681 if (getenv("CINDEXTEST_EDITING"))
1682 Repeats = 5;
1683
1684 /* Parse the translation unit. When we're testing clang_getCursor() after
1685 reparsing, don't remap unsaved files until the second parse. */
1686 CIdx = clang_createIndex(1, 1);
1687 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1688 argv + num_unsaved_files + 1 + NumLocations,
1689 argc - num_unsaved_files - 2 - NumLocations,
1690 unsaved_files,
1691 Repeats > 1? 0 : num_unsaved_files,
1692 getDefaultParsingOptions());
1693
1694 if (!TU) {
1695 fprintf(stderr, "unable to parse input\n");
1696 return -1;
1697 }
1698
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001699 if (checkForErrors(TU) != 0)
1700 return -1;
1701
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001702 for (I = 0; I != Repeats; ++I) {
1703 if (Repeats > 1 &&
1704 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1705 clang_defaultReparseOptions(TU))) {
1706 clang_disposeTranslationUnit(TU);
1707 return 1;
1708 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001709
1710 if (checkForErrors(TU) != 0)
1711 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001712
1713 for (Loc = 0; Loc < NumLocations; ++Loc) {
1714 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1715 if (!file)
1716 continue;
1717
1718 Cursor = clang_getCursor(TU,
1719 clang_getLocation(TU, file, Locations[Loc].line,
1720 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001721
1722 if (checkForErrors(TU) != 0)
1723 return -1;
1724
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001725 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001726 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001727 PrintCursor(Cursor);
1728 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001729 clang_findReferencesInFile(Cursor, file, visitor);
1730 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001731
1732 if (checkForErrors(TU) != 0)
1733 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001734 }
1735 }
1736 }
1737
1738 PrintDiagnostics(TU);
1739 clang_disposeTranslationUnit(TU);
1740 clang_disposeIndex(CIdx);
1741 free(Locations);
1742 free_remapped_files(unsaved_files, num_unsaved_files);
1743 return 0;
1744}
1745
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001746typedef struct {
1747 const char *check_prefix;
1748 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001749 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001750 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001751 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001752} IndexData;
1753
1754static void printCheck(IndexData *data) {
1755 if (data->check_prefix) {
1756 if (data->first_check_printed) {
1757 printf("// %s-NEXT: ", data->check_prefix);
1758 } else {
1759 printf("// %s : ", data->check_prefix);
1760 data->first_check_printed = 1;
1761 }
1762 }
1763}
1764
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001765static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001766 CXString filename = clang_getFileName((CXFile)file);
1767 printf("%s", clang_getCString(filename));
1768 clang_disposeString(filename);
1769}
1770
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001771static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1772 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001773 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001774 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001775 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001776 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001777 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001778
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001779 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001780 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1781 if (line == 0) {
1782 printf("<null loc>");
1783 return;
1784 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001785 if (!file) {
1786 printf("<no idxfile>");
1787 return;
1788 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001789 filename = clang_getFileName((CXFile)file);
1790 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001791 if (strcmp(cname, index_data->main_filename) == 0)
1792 isMainFile = 1;
1793 else
1794 isMainFile = 0;
1795 clang_disposeString(filename);
1796
1797 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001798 printCXIndexFile(file);
1799 printf(":");
1800 }
1801 printf("%d:%d", line, column);
1802}
1803
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001804static unsigned digitCount(unsigned val) {
1805 unsigned c = 1;
1806 while (1) {
1807 if (val < 10)
1808 return c;
1809 ++c;
1810 val /= 10;
1811 }
1812}
1813
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001814static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1815 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001816 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001817 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001818 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001819 unsigned line, column;
1820
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001821 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001822 if (!name)
1823 name = "<anon-tag>";
1824
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001825 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001826 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001827 newStr = (char *)malloc(strlen(name) +
1828 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001829 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001830 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001831}
1832
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001833static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1834 CXIdxClientContainer container;
1835 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001836 if (!container)
1837 printf("[<<NULL>>]");
1838 else
1839 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001840}
1841
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001842static const char *getEntityKindString(CXIdxEntityKind kind) {
1843 switch (kind) {
1844 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1845 case CXIdxEntity_Typedef: return "typedef";
1846 case CXIdxEntity_Function: return "function";
1847 case CXIdxEntity_Variable: return "variable";
1848 case CXIdxEntity_Field: return "field";
1849 case CXIdxEntity_EnumConstant: return "enumerator";
1850 case CXIdxEntity_ObjCClass: return "objc-class";
1851 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1852 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001853 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1854 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001855 case CXIdxEntity_ObjCProperty: return "objc-property";
1856 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1857 case CXIdxEntity_Enum: return "enum";
1858 case CXIdxEntity_Struct: return "struct";
1859 case CXIdxEntity_Union: return "union";
1860 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001861 case CXIdxEntity_CXXNamespace: return "namespace";
1862 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1863 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1864 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1865 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1866 case CXIdxEntity_CXXConstructor: return "constructor";
1867 case CXIdxEntity_CXXDestructor: return "destructor";
1868 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1869 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1870 }
1871 assert(0 && "Garbage entity kind");
1872 return 0;
1873}
1874
1875static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1876 switch (kind) {
1877 case CXIdxEntity_NonTemplate: return "";
1878 case CXIdxEntity_Template: return "-template";
1879 case CXIdxEntity_TemplatePartialSpecialization:
1880 return "-template-partial-spec";
1881 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001882 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001883 assert(0 && "Garbage entity kind");
1884 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001885}
1886
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001887static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1888 switch (kind) {
1889 case CXIdxEntityLang_None: return "<none>";
1890 case CXIdxEntityLang_C: return "C";
1891 case CXIdxEntityLang_ObjC: return "ObjC";
1892 case CXIdxEntityLang_CXX: return "C++";
1893 }
1894 assert(0 && "Garbage language kind");
1895 return 0;
1896}
1897
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001898static void printEntityInfo(const char *cb,
1899 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001900 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001901 const char *name;
1902 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001903 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001904 index_data = (IndexData *)client_data;
1905 printCheck(index_data);
1906
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001907 if (!info) {
1908 printf("%s: <<NULL>>", cb);
1909 return;
1910 }
1911
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001912 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001913 if (!name)
1914 name = "<anon-tag>";
1915
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001916 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1917 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001918 printf(" | name: %s", name);
1919 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001920 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001921
1922 for (i = 0; i != info->numAttributes; ++i) {
1923 const CXIdxAttrInfo *Attr = info->attributes[i];
1924 printf(" <attribute>: ");
1925 PrintCursor(Attr->cursor);
1926 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001927}
1928
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001929static void printBaseClassInfo(CXClientData client_data,
1930 const CXIdxBaseClassInfo *info) {
1931 printEntityInfo(" <base>", client_data, info->base);
1932 printf(" | cursor: ");
1933 PrintCursor(info->cursor);
1934 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001935 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001936}
1937
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001938static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1939 CXClientData client_data) {
1940 unsigned i;
1941 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1942 printEntityInfo(" <protocol>", client_data,
1943 ProtoInfo->protocols[i]->protocol);
1944 printf(" | cursor: ");
1945 PrintCursor(ProtoInfo->protocols[i]->cursor);
1946 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001947 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001948 printf("\n");
1949 }
1950}
1951
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001952static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001953 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001954 CXString str;
1955 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001956 unsigned numDiags, i;
1957 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001958 IndexData *index_data;
1959 index_data = (IndexData *)client_data;
1960 printCheck(index_data);
1961
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001962 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1963 for (i = 0; i != numDiags; ++i) {
1964 diag = clang_getDiagnosticInSet(diagSet, i);
1965 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1966 cstr = clang_getCString(str);
1967 printf("[diagnostic]: %s\n", cstr);
1968 clang_disposeString(str);
1969
1970 if (getenv("CINDEXTEST_FAILONERROR") &&
1971 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1972 index_data->fail_for_error = 1;
1973 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001974 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001975}
1976
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001977static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1978 CXFile file, void *reserved) {
1979 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001980 CXString filename;
1981
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001982 index_data = (IndexData *)client_data;
1983 printCheck(index_data);
1984
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001985 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001986 index_data->main_filename = clang_getCString(filename);
1987 clang_disposeString(filename);
1988
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001989 printf("[enteredMainFile]: ");
1990 printCXIndexFile((CXIdxClientFile)file);
1991 printf("\n");
1992
1993 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001994}
1995
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001996static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001997 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001998 IndexData *index_data;
1999 index_data = (IndexData *)client_data;
2000 printCheck(index_data);
2001
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002002 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002003 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002004 printf(" | name: \"%s\"", info->filename);
2005 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002006 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002007 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002008
2009 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002010}
2011
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002012static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002013 void *reserved) {
2014 IndexData *index_data;
2015 index_data = (IndexData *)client_data;
2016 printCheck(index_data);
2017
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002018 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002019 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002020}
2021
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002022static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002023 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002024 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002025 const CXIdxObjCCategoryDeclInfo *CatInfo;
2026 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002027 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002028 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002029 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002030 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002031 index_data = (IndexData *)client_data;
2032
2033 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2034 printf(" | cursor: ");
2035 PrintCursor(info->cursor);
2036 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002037 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002038 printf(" | semantic-container: ");
2039 printCXIndexContainer(info->semanticContainer);
2040 printf(" | lexical-container: ");
2041 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002042 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002043 printf(" | isDef: %d", info->isDefinition);
2044 printf(" | isContainer: %d", info->isContainer);
2045 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002046
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002047 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002048 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002049 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002050 PrintCursor(Attr->cursor);
2051 printf("\n");
2052 }
2053
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002054 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2055 const char *kindName = 0;
2056 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2057 switch (K) {
2058 case CXIdxObjCContainer_ForwardRef:
2059 kindName = "forward-ref"; break;
2060 case CXIdxObjCContainer_Interface:
2061 kindName = "interface"; break;
2062 case CXIdxObjCContainer_Implementation:
2063 kindName = "implementation"; break;
2064 }
2065 printCheck(index_data);
2066 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2067 }
2068
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002069 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002070 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2071 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002072 printf(" | cursor: ");
2073 PrintCursor(CatInfo->classCursor);
2074 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002075 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002076 printf("\n");
2077 }
2078
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002079 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2080 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002081 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002082 printf("\n");
2083 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002084 }
2085
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002086 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2087 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002088 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002089
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002090 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2091 if (PropInfo->getter) {
2092 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2093 printf("\n");
2094 }
2095 if (PropInfo->setter) {
2096 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2097 printf("\n");
2098 }
2099 }
2100
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002101 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2102 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2103 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2104 printf("\n");
2105 }
2106 }
2107
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002108 if (info->declAsContainer)
2109 clang_index_setClientContainer(info->declAsContainer,
2110 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002111}
2112
2113static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002114 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002115 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002116 printf(" | cursor: ");
2117 PrintCursor(info->cursor);
2118 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002119 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002120 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002121 printf(" | container: ");
2122 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002123 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002124 switch (info->kind) {
2125 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002126 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002127 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002128 printf("\n");
2129}
2130
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002131static int index_abortQuery(CXClientData client_data, void *reserved) {
2132 IndexData *index_data;
2133 index_data = (IndexData *)client_data;
2134 return index_data->abort;
2135}
2136
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002137static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002138 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002139 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002140 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002141 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002142 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002143 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002144 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002145 index_indexEntityReference
2146};
2147
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002148static unsigned getIndexOptions(void) {
2149 unsigned index_opts;
2150 index_opts = 0;
2151 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2152 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2153 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2154 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2155
2156 return index_opts;
2157}
2158
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002159static int index_file(int argc, const char **argv) {
2160 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002161 CXIndex Idx;
2162 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002163 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002164 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002165 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002166
2167 check_prefix = 0;
2168 if (argc > 0) {
2169 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2170 check_prefix = argv[0] + strlen("-check-prefix=");
2171 ++argv;
2172 --argc;
2173 }
2174 }
2175
2176 if (argc == 0) {
2177 fprintf(stderr, "no compiler arguments\n");
2178 return -1;
2179 }
2180
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002181 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2182 /* displayDiagnosics=*/1))) {
2183 fprintf(stderr, "Could not create Index\n");
2184 return 1;
2185 }
2186 idxAction = 0;
2187 result = 1;
2188
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002189 index_data.check_prefix = check_prefix;
2190 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002191 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002192 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002193
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002194 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002195 idxAction = clang_IndexAction_create(Idx);
2196 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002197 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002198 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002199 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002200 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002201
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002202 clang_IndexAction_dispose(idxAction);
2203 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002204 return result;
2205}
2206
2207static int index_tu(int argc, const char **argv) {
2208 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002209 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002210 CXTranslationUnit TU;
2211 const char *check_prefix;
2212 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002213 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002214 int result;
2215
2216 check_prefix = 0;
2217 if (argc > 0) {
2218 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2219 check_prefix = argv[0] + strlen("-check-prefix=");
2220 ++argv;
2221 --argc;
2222 }
2223 }
2224
2225 if (argc == 0) {
2226 fprintf(stderr, "no ast file\n");
2227 return -1;
2228 }
2229
2230 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2231 /* displayDiagnosics=*/1))) {
2232 fprintf(stderr, "Could not create Index\n");
2233 return 1;
2234 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002235 idxAction = 0;
2236 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002237
2238 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002239 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002240
2241 index_data.check_prefix = check_prefix;
2242 index_data.first_check_printed = 0;
2243 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002244 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002245
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002246 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002247 idxAction = clang_IndexAction_create(Idx);
2248 result = clang_indexTranslationUnit(idxAction, &index_data,
2249 &IndexCB,sizeof(IndexCB),
2250 index_opts, TU);
2251 if (index_data.fail_for_error)
2252 goto finished;
2253
2254 finished:
2255 clang_IndexAction_dispose(idxAction);
2256 clang_disposeIndex(Idx);
2257
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002258 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002259}
2260
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002261int perform_token_annotation(int argc, const char **argv) {
2262 const char *input = argv[1];
2263 char *filename = 0;
2264 unsigned line, second_line;
2265 unsigned column, second_column;
2266 CXIndex CIdx;
2267 CXTranslationUnit TU = 0;
2268 int errorCode;
2269 struct CXUnsavedFile *unsaved_files = 0;
2270 int num_unsaved_files = 0;
2271 CXToken *tokens;
2272 unsigned num_tokens;
2273 CXSourceRange range;
2274 CXSourceLocation startLoc, endLoc;
2275 CXFile file = 0;
2276 CXCursor *cursors = 0;
2277 unsigned i;
2278
2279 input += strlen("-test-annotate-tokens=");
2280 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2281 &second_line, &second_column)))
2282 return errorCode;
2283
2284 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2285 return -1;
2286
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002287 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002288 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2289 argv + num_unsaved_files + 2,
2290 argc - num_unsaved_files - 3,
2291 unsaved_files,
2292 num_unsaved_files,
2293 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002294 if (!TU) {
2295 fprintf(stderr, "unable to parse input\n");
2296 clang_disposeIndex(CIdx);
2297 free(filename);
2298 free_remapped_files(unsaved_files, num_unsaved_files);
2299 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002300 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002301 errorCode = 0;
2302
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002303 if (checkForErrors(TU) != 0)
2304 return -1;
2305
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002306 if (getenv("CINDEXTEST_EDITING")) {
2307 for (i = 0; i < 5; ++i) {
2308 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2309 clang_defaultReparseOptions(TU))) {
2310 fprintf(stderr, "Unable to reparse translation unit!\n");
2311 errorCode = -1;
2312 goto teardown;
2313 }
2314 }
2315 }
2316
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002317 if (checkForErrors(TU) != 0) {
2318 errorCode = -1;
2319 goto teardown;
2320 }
2321
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002322 file = clang_getFile(TU, filename);
2323 if (!file) {
2324 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2325 errorCode = -1;
2326 goto teardown;
2327 }
2328
2329 startLoc = clang_getLocation(TU, file, line, column);
2330 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002331 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002332 column);
2333 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002334 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002335 }
2336
2337 endLoc = clang_getLocation(TU, file, second_line, second_column);
2338 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002339 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002340 second_line, second_column);
2341 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002342 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002343 }
2344
2345 range = clang_getRange(startLoc, endLoc);
2346 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002347
2348 if (checkForErrors(TU) != 0) {
2349 errorCode = -1;
2350 goto teardown;
2351 }
2352
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002353 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2354 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002355
2356 if (checkForErrors(TU) != 0) {
2357 errorCode = -1;
2358 goto teardown;
2359 }
2360
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002361 for (i = 0; i != num_tokens; ++i) {
2362 const char *kind = "<unknown>";
2363 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2364 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2365 unsigned start_line, start_column, end_line, end_column;
2366
2367 switch (clang_getTokenKind(tokens[i])) {
2368 case CXToken_Punctuation: kind = "Punctuation"; break;
2369 case CXToken_Keyword: kind = "Keyword"; break;
2370 case CXToken_Identifier: kind = "Identifier"; break;
2371 case CXToken_Literal: kind = "Literal"; break;
2372 case CXToken_Comment: kind = "Comment"; break;
2373 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002374 clang_getSpellingLocation(clang_getRangeStart(extent),
2375 0, &start_line, &start_column, 0);
2376 clang_getSpellingLocation(clang_getRangeEnd(extent),
2377 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002378 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002379 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002380 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002381 if (!clang_isInvalid(cursors[i].kind)) {
2382 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002383 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002384 }
2385 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002386 }
2387 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002388 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002389
2390 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002391 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002392 clang_disposeTranslationUnit(TU);
2393 clang_disposeIndex(CIdx);
2394 free(filename);
2395 free_remapped_files(unsaved_files, num_unsaved_files);
2396 return errorCode;
2397}
2398
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002399static int
2400perform_test_compilation_db(const char *database, int argc, const char **argv) {
2401 CXCompilationDatabase db;
2402 CXCompileCommands CCmds;
2403 CXCompileCommand CCmd;
2404 CXCompilationDatabase_Error ec;
2405 CXString wd;
2406 CXString arg;
2407 int errorCode = 0;
2408 char *tmp;
2409 unsigned len;
2410 char *buildDir;
2411 int i, j, a, numCmds, numArgs;
2412
2413 len = strlen(database);
2414 tmp = (char *) malloc(len+1);
2415 memcpy(tmp, database, len+1);
2416 buildDir = dirname(tmp);
2417
2418 db = clang_tooling_CompilationDatabase_fromDirectory(buildDir, &ec);
2419
2420 if (db) {
2421
2422 if (ec!=CXCompilationDatabase_NoError) {
2423 printf("unexpected error %d code while loading compilation database\n", ec);
2424 errorCode = -1;
2425 goto cdb_end;
2426 }
2427
2428 for (i=0; i<argc && errorCode==0; ) {
2429 if (strcmp(argv[i],"lookup")==0){
2430 CCmds = clang_tooling_CompilationDatabase_getCompileCommands(db, argv[i+1]);
2431
2432 if (!CCmds) {
2433 printf("file %s not found in compilation db\n", argv[i+1]);
2434 errorCode = -1;
2435 break;
2436 }
2437
2438 numCmds = clang_tooling_CompileCommands_getSize(CCmds);
2439
2440 if (numCmds==0) {
2441 fprintf(stderr, "should not get an empty compileCommand set for file"
2442 " '%s'\n", argv[i+1]);
2443 errorCode = -1;
2444 break;
2445 }
2446
2447 for (j=0; j<numCmds; ++j) {
2448 CCmd = clang_tooling_CompileCommands_getCommand(CCmds, j);
2449
2450 wd = clang_tooling_CompileCommand_getDirectory(CCmd);
2451 printf("workdir:'%s'", clang_getCString(wd));
2452 clang_disposeString(wd);
2453
2454 printf(" cmdline:'");
2455 numArgs = clang_tooling_CompileCommand_getNumArgs(CCmd);
2456 for (a=0; a<numArgs; ++a) {
2457 if (a) printf(" ");
2458 arg = clang_tooling_CompileCommand_getArg(CCmd, a);
2459 printf("%s", clang_getCString(arg));
2460 clang_disposeString(arg);
2461 }
2462 printf("'\n");
2463 }
2464
2465 clang_tooling_CompileCommands_dispose(CCmds);
2466
2467 i += 2;
2468 }
2469 }
2470 clang_tooling_CompilationDatabase_dispose(db);
2471 } else {
2472 printf("database loading failed with error code %d.\n", ec);
2473 errorCode = -1;
2474 }
2475
2476cdb_end:
2477 free(tmp);
2478
2479 return errorCode;
2480}
2481
Ted Kremenek0d435192009-11-17 18:13:31 +00002482/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002483/* USR printing. */
2484/******************************************************************************/
2485
2486static int insufficient_usr(const char *kind, const char *usage) {
2487 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2488 return 1;
2489}
2490
2491static unsigned isUSR(const char *s) {
2492 return s[0] == 'c' && s[1] == ':';
2493}
2494
2495static int not_usr(const char *s, const char *arg) {
2496 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2497 return 1;
2498}
2499
2500static void print_usr(CXString usr) {
2501 const char *s = clang_getCString(usr);
2502 printf("%s\n", s);
2503 clang_disposeString(usr);
2504}
2505
2506static void display_usrs() {
2507 fprintf(stderr, "-print-usrs options:\n"
2508 " ObjCCategory <class name> <category name>\n"
2509 " ObjCClass <class name>\n"
2510 " ObjCIvar <ivar name> <class USR>\n"
2511 " ObjCMethod <selector> [0=class method|1=instance method] "
2512 "<class USR>\n"
2513 " ObjCProperty <property name> <class USR>\n"
2514 " ObjCProtocol <protocol name>\n");
2515}
2516
2517int print_usrs(const char **I, const char **E) {
2518 while (I != E) {
2519 const char *kind = *I;
2520 unsigned len = strlen(kind);
2521 switch (len) {
2522 case 8:
2523 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2524 if (I + 2 >= E)
2525 return insufficient_usr(kind, "<ivar name> <class USR>");
2526 if (!isUSR(I[2]))
2527 return not_usr("<class USR>", I[2]);
2528 else {
2529 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002530 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002531 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002532 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2533 }
2534
2535 I += 3;
2536 continue;
2537 }
2538 break;
2539 case 9:
2540 if (memcmp(kind, "ObjCClass", 9) == 0) {
2541 if (I + 1 >= E)
2542 return insufficient_usr(kind, "<class name>");
2543 print_usr(clang_constructUSR_ObjCClass(I[1]));
2544 I += 2;
2545 continue;
2546 }
2547 break;
2548 case 10:
2549 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2550 if (I + 3 >= E)
2551 return insufficient_usr(kind, "<method selector> "
2552 "[0=class method|1=instance method] <class USR>");
2553 if (!isUSR(I[3]))
2554 return not_usr("<class USR>", I[3]);
2555 else {
2556 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002557 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002558 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002559 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2560 }
2561 I += 4;
2562 continue;
2563 }
2564 break;
2565 case 12:
2566 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2567 if (I + 2 >= E)
2568 return insufficient_usr(kind, "<class name> <category name>");
2569 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2570 I += 3;
2571 continue;
2572 }
2573 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2574 if (I + 1 >= E)
2575 return insufficient_usr(kind, "<protocol name>");
2576 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2577 I += 2;
2578 continue;
2579 }
2580 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2581 if (I + 2 >= E)
2582 return insufficient_usr(kind, "<property name> <class USR>");
2583 if (!isUSR(I[2]))
2584 return not_usr("<class USR>", I[2]);
2585 else {
2586 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002587 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002588 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002589 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2590 }
2591 I += 3;
2592 continue;
2593 }
2594 break;
2595 default:
2596 break;
2597 }
2598 break;
2599 }
2600
2601 if (I != E) {
2602 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2603 display_usrs();
2604 return 1;
2605 }
2606 return 0;
2607}
2608
2609int print_usrs_file(const char *file_name) {
2610 char line[2048];
2611 const char *args[128];
2612 unsigned numChars = 0;
2613
2614 FILE *fp = fopen(file_name, "r");
2615 if (!fp) {
2616 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2617 return 1;
2618 }
2619
2620 /* This code is not really all that safe, but it works fine for testing. */
2621 while (!feof(fp)) {
2622 char c = fgetc(fp);
2623 if (c == '\n') {
2624 unsigned i = 0;
2625 const char *s = 0;
2626
2627 if (numChars == 0)
2628 continue;
2629
2630 line[numChars] = '\0';
2631 numChars = 0;
2632
2633 if (line[0] == '/' && line[1] == '/')
2634 continue;
2635
2636 s = strtok(line, " ");
2637 while (s) {
2638 args[i] = s;
2639 ++i;
2640 s = strtok(0, " ");
2641 }
2642 if (print_usrs(&args[0], &args[i]))
2643 return 1;
2644 }
2645 else
2646 line[numChars++] = c;
2647 }
2648
2649 fclose(fp);
2650 return 0;
2651}
2652
2653/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002654/* Command line processing. */
2655/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002656int write_pch_file(const char *filename, int argc, const char *argv[]) {
2657 CXIndex Idx;
2658 CXTranslationUnit TU;
2659 struct CXUnsavedFile *unsaved_files = 0;
2660 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002661 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002662
2663 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2664
2665 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2666 clang_disposeIndex(Idx);
2667 return -1;
2668 }
2669
2670 TU = clang_parseTranslationUnit(Idx, 0,
2671 argv + num_unsaved_files,
2672 argc - num_unsaved_files,
2673 unsaved_files,
2674 num_unsaved_files,
2675 CXTranslationUnit_Incomplete);
2676 if (!TU) {
2677 fprintf(stderr, "Unable to load translation unit!\n");
2678 free_remapped_files(unsaved_files, num_unsaved_files);
2679 clang_disposeIndex(Idx);
2680 return 1;
2681 }
2682
Douglas Gregor39c411f2011-07-06 16:43:36 +00002683 switch (clang_saveTranslationUnit(TU, filename,
2684 clang_defaultSaveOptions(TU))) {
2685 case CXSaveError_None:
2686 break;
2687
2688 case CXSaveError_TranslationErrors:
2689 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2690 filename);
2691 result = 2;
2692 break;
2693
2694 case CXSaveError_InvalidTU:
2695 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2696 filename);
2697 result = 3;
2698 break;
2699
2700 case CXSaveError_Unknown:
2701 default:
2702 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2703 result = 1;
2704 break;
2705 }
2706
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002707 clang_disposeTranslationUnit(TU);
2708 free_remapped_files(unsaved_files, num_unsaved_files);
2709 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002710 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002711}
2712
2713/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002714/* Serialized diagnostics. */
2715/******************************************************************************/
2716
2717static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2718 switch (error) {
2719 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2720 case CXLoadDiag_None: break;
2721 case CXLoadDiag_Unknown: return "Unknown";
2722 case CXLoadDiag_InvalidFile: return "Invalid File";
2723 }
2724 return "None";
2725}
2726
2727static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2728 switch (severity) {
2729 case CXDiagnostic_Note: return "note";
2730 case CXDiagnostic_Error: return "error";
2731 case CXDiagnostic_Fatal: return "fatal";
2732 case CXDiagnostic_Ignored: return "ignored";
2733 case CXDiagnostic_Warning: return "warning";
2734 }
2735 return "unknown";
2736}
2737
2738static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002739 if (indent == 0)
2740 return;
2741 fprintf(stderr, "+");
2742 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002743 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002744 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002745 --indent;
2746 }
2747}
2748
2749static void printLocation(CXSourceLocation L) {
2750 CXFile File;
2751 CXString FileName;
2752 unsigned line, column, offset;
2753
2754 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2755 FileName = clang_getFileName(File);
2756
2757 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2758 clang_disposeString(FileName);
2759}
2760
2761static void printRanges(CXDiagnostic D, unsigned indent) {
2762 unsigned i, n = clang_getDiagnosticNumRanges(D);
2763
2764 for (i = 0; i < n; ++i) {
2765 CXSourceLocation Start, End;
2766 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2767 Start = clang_getRangeStart(SR);
2768 End = clang_getRangeEnd(SR);
2769
2770 printIndent(indent);
2771 fprintf(stderr, "Range: ");
2772 printLocation(Start);
2773 fprintf(stderr, " ");
2774 printLocation(End);
2775 fprintf(stderr, "\n");
2776 }
2777}
2778
2779static void printFixIts(CXDiagnostic D, unsigned indent) {
2780 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002781 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002782 for (i = 0 ; i < n; ++i) {
2783 CXSourceRange ReplacementRange;
2784 CXString text;
2785 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2786
2787 printIndent(indent);
2788 fprintf(stderr, "FIXIT: (");
2789 printLocation(clang_getRangeStart(ReplacementRange));
2790 fprintf(stderr, " - ");
2791 printLocation(clang_getRangeEnd(ReplacementRange));
2792 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2793 clang_disposeString(text);
2794 }
2795}
2796
2797static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002798 unsigned i, n;
2799
Ted Kremenek15322172011-11-10 08:43:12 +00002800 if (!Diags)
2801 return;
2802
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002803 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002804 for (i = 0; i < n; ++i) {
2805 CXSourceLocation DiagLoc;
2806 CXDiagnostic D;
2807 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002808 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002809 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002810 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002811
2812 D = clang_getDiagnosticInSet(Diags, i);
2813 DiagLoc = clang_getDiagnosticLocation(D);
2814 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2815 FileName = clang_getFileName(File);
2816 DiagSpelling = clang_getDiagnosticSpelling(D);
2817
2818 printIndent(indent);
2819
2820 fprintf(stderr, "%s:%d:%d: %s: %s",
2821 clang_getCString(FileName),
2822 line,
2823 column,
2824 getSeverityString(clang_getDiagnosticSeverity(D)),
2825 clang_getCString(DiagSpelling));
2826
2827 DiagOption = clang_getDiagnosticOption(D, 0);
2828 DiagOptionStr = clang_getCString(DiagOption);
2829 if (DiagOptionStr) {
2830 fprintf(stderr, " [%s]", DiagOptionStr);
2831 }
2832
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002833 DiagCat = clang_getDiagnosticCategoryText(D);
2834 DiagCatStr = clang_getCString(DiagCat);
2835 if (DiagCatStr) {
2836 fprintf(stderr, " [%s]", DiagCatStr);
2837 }
2838
Ted Kremenek15322172011-11-10 08:43:12 +00002839 fprintf(stderr, "\n");
2840
2841 printRanges(D, indent);
2842 printFixIts(D, indent);
2843
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002844 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002845 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2846
2847 clang_disposeString(FileName);
2848 clang_disposeString(DiagSpelling);
2849 clang_disposeString(DiagOption);
2850 }
2851}
2852
2853static int read_diagnostics(const char *filename) {
2854 enum CXLoadDiag_Error error;
2855 CXString errorString;
2856 CXDiagnosticSet Diags = 0;
2857
2858 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2859 if (!Diags) {
2860 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2861 getDiagnosticCodeStr(error),
2862 clang_getCString(errorString));
2863 clang_disposeString(errorString);
2864 return 1;
2865 }
2866
2867 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002868 fprintf(stderr, "Number of diagnostics: %d\n",
2869 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002870 clang_disposeDiagnosticSet(Diags);
2871 return 0;
2872}
2873
2874/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002875/* Command line processing. */
2876/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002877
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002878static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002879 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002880 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002881 if (strcmp(s, "-usrs") == 0)
2882 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002883 if (strncmp(s, "-memory-usage", 13) == 0)
2884 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002885 return NULL;
2886}
2887
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002888static void print_usage(void) {
2889 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002890 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002891 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002892 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002893 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002894 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002895 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002896 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002897 "[FileCheck prefix]\n");
2898 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002899 " c-index-test -test-load-tu <AST file> <symbol filter> "
2900 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002901 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2902 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002903 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002904 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002905 " c-index-test -test-load-source-memory-usage "
2906 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002907 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2908 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002909 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002910 " c-index-test -test-load-source-usrs-memory-usage "
2911 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002912 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2913 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002914 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002915 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002916 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002917 " c-index-test -test-print-typekind {<args>}*\n"
2918 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002919 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002920 " c-index-test -write-pch <file> <compiler arguments>\n");
2921 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002922 " c-index-test -compilation-db [lookup <filename>] database\n");
2923 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00002924 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002925 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002926 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002927 " all - load all symbols, including those from PCH\n"
2928 " local - load all symbols except those in PCH\n"
2929 " category - only load ObjC categories (non-PCH)\n"
2930 " interface - only load ObjC interfaces (non-PCH)\n"
2931 " protocol - only load ObjC protocols (non-PCH)\n"
2932 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002933 " typedef - only load typdefs (non-PCH)\n"
2934 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002935}
2936
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002937/***/
2938
2939int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002940 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002941 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2942 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002943 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002944 return perform_code_completion(argc, argv, 0);
2945 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2946 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002947 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2948 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002949 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2950 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002951 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2952 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002953 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2954 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002955 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002956 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002957 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002958 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2959 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002960 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002961 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2962 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2963 if (I) {
2964 int trials = atoi(argv[2]);
2965 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2966 NULL);
2967 }
2968 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002969 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002970 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002971
2972 PostVisitTU postVisit = 0;
2973 if (strstr(argv[1], "-memory-usage"))
2974 postVisit = PrintMemoryUsage;
2975
Ted Kremenek7d405622010-01-12 23:34:26 +00002976 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002977 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2978 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002979 }
2980 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002981 return perform_file_scan(argv[2], argv[3],
2982 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002983 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2984 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002985 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2986 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2987 PrintInclusionStack);
2988 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2989 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2990 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002991 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2992 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2993 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002994 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2995 return perform_test_load_source(argc - 2, argv + 2, "all",
2996 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002997 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2998 if (argc > 2)
2999 return print_usrs(argv + 2, argv + argc);
3000 else {
3001 display_usrs();
3002 return 1;
3003 }
3004 }
3005 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3006 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003007 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3008 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003009 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3010 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3011
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003012 print_usage();
3013 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003014}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003015
3016/***/
3017
3018/* We intentionally run in a separate thread to ensure we at least minimal
3019 * testing of a multithreaded environment (for example, having a reduced stack
3020 * size). */
3021
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003022typedef struct thread_info {
3023 int argc;
3024 const char **argv;
3025 int result;
3026} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003027void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003028 thread_info *client_data = client_data_v;
3029 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003030#ifdef __CYGWIN__
3031 fflush(stdout); /* stdout is not flushed on Cygwin. */
3032#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003033}
3034
3035int main(int argc, const char **argv) {
3036 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003037
Douglas Gregor61605982010-10-27 16:00:01 +00003038 if (getenv("CINDEXTEST_NOTHREADS"))
3039 return cindextest_main(argc, argv);
3040
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003041 client_data.argc = argc;
3042 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003043 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003044 return client_data.result;
3045}