blob: b3b5b8447ff0a0450e546f4bcdfeb4f9f261e454 [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);
Richard Smithe07c5f82012-07-05 08:20:49 +0000160 free(contents);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000161 *unsaved_files = 0;
162 *num_unsaved_files = 0;
163 return -1;
164 }
165 contents[unsaved->Length] = 0;
166 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000167
Douglas Gregor4db64a42010-01-23 00:14:00 +0000168 /* Close the file. */
169 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000170
Douglas Gregor4db64a42010-01-23 00:14:00 +0000171 /* Copy the file name that we're remapping from. */
172 filename_len = semi - arg_string;
173 filename = (char *)malloc(filename_len + 1);
174 memcpy(filename, arg_string, filename_len);
175 filename[filename_len] = 0;
176 unsaved->Filename = filename;
177 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000178
Douglas Gregor4db64a42010-01-23 00:14:00 +0000179 return 0;
180}
181
Ted Kremenek0d435192009-11-17 18:13:31 +0000182/******************************************************************************/
183/* Pretty-printing. */
184/******************************************************************************/
185
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000186static void PrintCString(const char *Prefix, const char *CStr) {
187 printf(" %s=[", Prefix);
188 if (CStr != NULL && CStr[0] != '\0') {
189 for ( ; *CStr; ++CStr) {
190 const char C = *CStr;
191 switch (C) {
192 case '\n': printf("\\n"); break;
193 case '\r': printf("\\r"); break;
194 case '\t': printf("\\t"); break;
195 case '\v': printf("\\v"); break;
196 case '\f': printf("\\f"); break;
197 default: putchar(C); break;
198 }
199 }
200 }
201 printf("]");
202}
203
Douglas Gregor430d7a12011-07-25 17:48:11 +0000204static void PrintRange(CXSourceRange R, const char *str) {
205 CXFile begin_file, end_file;
206 unsigned begin_line, begin_column, end_line, end_column;
207
208 clang_getSpellingLocation(clang_getRangeStart(R),
209 &begin_file, &begin_line, &begin_column, 0);
210 clang_getSpellingLocation(clang_getRangeEnd(R),
211 &end_file, &end_line, &end_column, 0);
212 if (!begin_file || !end_file)
213 return;
214
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000215 if (str)
216 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000217 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
218}
219
Douglas Gregor358559d2010-10-02 22:49:11 +0000220int want_display_name = 0;
221
Douglas Gregorcc889662012-05-08 00:14:45 +0000222static void printVersion(const char *Prefix, CXVersion Version) {
223 if (Version.Major < 0)
224 return;
225 printf("%s%d", Prefix, Version.Major);
226
227 if (Version.Minor < 0)
228 return;
229 printf(".%d", Version.Minor);
230
231 if (Version.Subminor < 0)
232 return;
233 printf(".%d", Version.Subminor);
234}
235
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000236static void PrintCursor(CXCursor Cursor) {
237 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000238 if (clang_isInvalid(Cursor.kind)) {
239 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
240 printf("Invalid Cursor => %s", clang_getCString(ks));
241 clang_disposeString(ks);
242 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000243 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000244 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000245 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000246 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000247 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000248 CXCursor *overridden;
249 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000250 unsigned RefNameRangeNr;
251 CXSourceRange CursorExtent;
252 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000253 int AlwaysUnavailable;
254 int AlwaysDeprecated;
255 CXString UnavailableMessage;
256 CXString DeprecatedMessage;
257 CXPlatformAvailability PlatformAvailability[2];
258 int NumPlatformAvailability;
259 int I;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000260 CXString RawComment;
261 const char *RawCommentCString;
262 CXString BriefComment;
263 const char *BriefCommentCString;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000264
Ted Kremeneke68fff62010-02-17 00:41:32 +0000265 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000266 string = want_display_name? clang_getCursorDisplayName(Cursor)
267 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000268 printf("%s=%s", clang_getCString(ks),
269 clang_getCString(string));
270 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000271 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000272
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000273 Referenced = clang_getCursorReferenced(Cursor);
274 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000275 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
276 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
277 printf("[");
278 for (I = 0; I != N; ++I) {
279 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000280 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000281 if (I)
282 printf(", ");
283
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000284 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000285 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000286 printf("%d:%d", line, column);
287 }
288 printf("]");
289 } else {
290 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000291 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000292 printf(":%d:%d", line, column);
293 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000294 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000295
296 if (clang_isCursorDefinition(Cursor))
297 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000298
299 switch (clang_getCursorAvailability(Cursor)) {
300 case CXAvailability_Available:
301 break;
302
303 case CXAvailability_Deprecated:
304 printf(" (deprecated)");
305 break;
306
307 case CXAvailability_NotAvailable:
308 printf(" (unavailable)");
309 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000310
311 case CXAvailability_NotAccessible:
312 printf(" (inaccessible)");
313 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000314 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000315
Douglas Gregorcc889662012-05-08 00:14:45 +0000316 NumPlatformAvailability
317 = clang_getCursorPlatformAvailability(Cursor,
318 &AlwaysDeprecated,
319 &DeprecatedMessage,
320 &AlwaysUnavailable,
321 &UnavailableMessage,
322 PlatformAvailability, 2);
323 if (AlwaysUnavailable) {
324 printf(" (always unavailable: \"%s\")",
325 clang_getCString(UnavailableMessage));
326 } else if (AlwaysDeprecated) {
327 printf(" (always deprecated: \"%s\")",
328 clang_getCString(DeprecatedMessage));
329 } else {
330 for (I = 0; I != NumPlatformAvailability; ++I) {
331 if (I >= 2)
332 break;
333
334 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
335 if (PlatformAvailability[I].Unavailable)
336 printf(", unavailable");
337 else {
338 printVersion(", introduced=", PlatformAvailability[I].Introduced);
339 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
340 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
341 }
342 if (clang_getCString(PlatformAvailability[I].Message)[0])
343 printf(", message=\"%s\"",
344 clang_getCString(PlatformAvailability[I].Message));
345 printf(")");
346 }
347 }
348 for (I = 0; I != NumPlatformAvailability; ++I) {
349 if (I >= 2)
350 break;
351 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
352 }
353
354 clang_disposeString(DeprecatedMessage);
355 clang_disposeString(UnavailableMessage);
356
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000357 if (clang_CXXMethod_isStatic(Cursor))
358 printf(" (static)");
359 if (clang_CXXMethod_isVirtual(Cursor))
360 printf(" (virtual)");
361
Ted Kremenek95f33552010-08-26 01:42:22 +0000362 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
363 CXType T =
364 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
365 CXString S = clang_getTypeKindSpelling(T.kind);
366 printf(" [IBOutletCollection=%s]", clang_getCString(S));
367 clang_disposeString(S);
368 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000369
370 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
371 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
372 unsigned isVirtual = clang_isVirtualBase(Cursor);
373 const char *accessStr = 0;
374
375 switch (access) {
376 case CX_CXXInvalidAccessSpecifier:
377 accessStr = "invalid"; break;
378 case CX_CXXPublic:
379 accessStr = "public"; break;
380 case CX_CXXProtected:
381 accessStr = "protected"; break;
382 case CX_CXXPrivate:
383 accessStr = "private"; break;
384 }
385
386 printf(" [access=%s isVirtual=%s]", accessStr,
387 isVirtual ? "true" : "false");
388 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000389
390 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
391 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
392 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
393 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000394 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000395 printf(" [Specialization of %s:%d:%d]",
396 clang_getCString(Name), line, column);
397 clang_disposeString(Name);
398 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000399
400 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
401 if (num_overridden) {
402 unsigned I;
403 printf(" [Overrides ");
404 for (I = 0; I != num_overridden; ++I) {
405 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000406 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000407 if (I)
408 printf(", ");
409 printf("@%d:%d", line, column);
410 }
411 printf("]");
412 clang_disposeOverriddenCursors(overridden);
413 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000414
415 if (Cursor.kind == CXCursor_InclusionDirective) {
416 CXFile File = clang_getIncludedFile(Cursor);
417 CXString Included = clang_getFileName(File);
418 printf(" (%s)", clang_getCString(Included));
419 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000420
421 if (clang_isFileMultipleIncludeGuarded(TU, File))
422 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000423 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000424
425 CursorExtent = clang_getCursorExtent(Cursor);
426 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
427 CXNameRange_WantQualifier
428 | CXNameRange_WantSinglePiece
429 | CXNameRange_WantTemplateArgs,
430 0);
431 if (!clang_equalRanges(CursorExtent, RefNameRange))
432 PrintRange(RefNameRange, "SingleRefName");
433
434 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
435 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
436 CXNameRange_WantQualifier
437 | CXNameRange_WantTemplateArgs,
438 RefNameRangeNr);
439 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
440 break;
441 if (!clang_equalRanges(CursorExtent, RefNameRange))
442 PrintRange(RefNameRange, "RefName");
443 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000444
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000445 RawComment = clang_Cursor_getRawCommentText(Cursor);
446 RawCommentCString = clang_getCString(RawComment);
447 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
448 PrintCString("RawComment", RawCommentCString);
449 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000450
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000451 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
452 BriefCommentCString = clang_getCString(BriefComment);
453 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
454 PrintCString("BriefComment", BriefCommentCString);
455 clang_disposeString(BriefComment);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000456 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000457 clang_disposeString(RawComment);
Steve Naroff699a07d2009-09-25 21:32:34 +0000458 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000459}
Steve Naroff89922f82009-08-31 00:59:03 +0000460
Ted Kremeneke68fff62010-02-17 00:41:32 +0000461static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000462 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000463 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000464 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000465 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000466 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000467 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000468 clang_disposeString(source);
469 return "<invalid loc>";
470 }
471 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000472 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000473 clang_disposeString(source);
474 return b;
475 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000476}
477
Ted Kremenek0d435192009-11-17 18:13:31 +0000478/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000479/* Callbacks. */
480/******************************************************************************/
481
482typedef void (*PostVisitTU)(CXTranslationUnit);
483
Douglas Gregora88084b2010-02-18 18:08:43 +0000484void PrintDiagnostic(CXDiagnostic Diagnostic) {
485 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000486 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000487 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000488 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000489 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
490 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000491 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000492
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000493 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000494 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000495
Douglas Gregor274f1902010-02-22 23:17:23 +0000496 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
497 fprintf(stderr, "%s\n", clang_getCString(Msg));
498 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000499
Douglas Gregora9b06d42010-11-09 06:24:54 +0000500 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
501 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000502 if (!file)
503 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000504
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000505 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000506 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000507 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000508 CXSourceRange range;
509 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
510 CXSourceLocation start = clang_getRangeStart(range);
511 CXSourceLocation end = clang_getRangeEnd(range);
512 unsigned start_line, start_column, end_line, end_column;
513 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000514 clang_getSpellingLocation(start, &start_file, &start_line,
515 &start_column, 0);
516 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000517 if (clang_equalLocations(start, end)) {
518 /* Insertion. */
519 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000520 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000521 clang_getCString(insertion_text), start_line, start_column);
522 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
523 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000524 if (start_file == file && end_file == file) {
525 fprintf(out, "FIX-IT: Remove ");
526 PrintExtent(out, start_line, start_column, end_line, end_column);
527 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000528 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000529 } else {
530 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000531 if (start_file == end_file) {
532 fprintf(out, "FIX-IT: Replace ");
533 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000534 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000535 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000536 break;
537 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000538 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000539 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000540}
541
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000542void PrintDiagnosticSet(CXDiagnosticSet Set) {
543 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
544 for ( ; i != n ; ++i) {
545 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
546 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000547 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000548 if (ChildDiags)
549 PrintDiagnosticSet(ChildDiags);
550 }
551}
552
553void PrintDiagnostics(CXTranslationUnit TU) {
554 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
555 PrintDiagnosticSet(TUSet);
556 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000557}
558
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000559void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000560 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000561 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000562 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000563 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000564 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000565 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000566 unsigned long amount = usage.entries[i].amount;
567 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000568 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000569 ((double) amount)/(1024*1024));
570 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000571 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000572 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000573 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000574}
575
Ted Kremenekce2ae882010-01-26 17:59:48 +0000576/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000577/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000578/******************************************************************************/
579
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000580static const char *FileCheckPrefix = "CHECK";
581
Douglas Gregora7bde202010-01-19 00:34:46 +0000582static void PrintCursorExtent(CXCursor C) {
583 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000584 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000585}
586
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000587/* Data used by all of the visitors. */
588typedef struct {
589 CXTranslationUnit TU;
590 enum CXCursorKind *Filter;
591} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000592
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000593
Ted Kremeneke68fff62010-02-17 00:41:32 +0000594enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000595 CXCursor Parent,
596 CXClientData ClientData) {
597 VisitorData *Data = (VisitorData *)ClientData;
598 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000599 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000600 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000601 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000602 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000603 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000604 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000605 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000606 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000607 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000608 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000609
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000610 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000611}
Steve Naroff50398192009-08-28 15:28:48 +0000612
Ted Kremeneke68fff62010-02-17 00:41:32 +0000613static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000614 CXCursor Parent,
615 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000616 const char *startBuf, *endBuf;
617 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
618 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000619 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000620
Douglas Gregorb6998662010-01-19 19:34:47 +0000621 if (Cursor.kind != CXCursor_FunctionDecl ||
622 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000623 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000624
625 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
626 &startLine, &startColumn,
627 &endLine, &endColumn);
628 /* Probe the entire body, looking for both decls and refs. */
629 curLine = startLine;
630 curColumn = startColumn;
631
632 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000633 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000634 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000635 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000636
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000637 if (*startBuf == '\n') {
638 startBuf++;
639 curLine++;
640 curColumn = 1;
641 } else if (*startBuf != '\t')
642 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000643
Douglas Gregor98258af2010-01-18 22:46:11 +0000644 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000645 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000646
Douglas Gregor1db19de2010-01-19 21:36:55 +0000647 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000648 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000649 CXSourceLocation RefLoc
650 = clang_getLocation(Data->TU, file, curLine, curColumn);
651 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000652 if (Ref.kind == CXCursor_NoDeclFound) {
653 /* Nothing found here; that's fine. */
654 } else if (Ref.kind != CXCursor_FunctionDecl) {
655 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
656 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000657 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000658 printf("\n");
659 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000660 }
Ted Kremenek74844072010-02-17 00:41:20 +0000661 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000662 startBuf++;
663 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000664
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000665 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000666}
667
Ted Kremenek7d405622010-01-12 23:34:26 +0000668/******************************************************************************/
669/* USR testing. */
670/******************************************************************************/
671
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000672enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
673 CXClientData ClientData) {
674 VisitorData *Data = (VisitorData *)ClientData;
675 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000676 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000677 const char *cstr = clang_getCString(USR);
678 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000679 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000680 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000681 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000682 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
683
Douglas Gregora7bde202010-01-19 00:34:46 +0000684 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000685 printf("\n");
686 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000687
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000688 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000689 }
690
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000691 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000692}
693
694/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000695/* Inclusion stack testing. */
696/******************************************************************************/
697
698void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
699 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000700
Ted Kremenek16b55a72010-01-26 19:31:51 +0000701 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000702 CXString fname;
703
704 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000705 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000706 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707
Ted Kremenek16b55a72010-01-26 19:31:51 +0000708 for (i = 0; i < includeStackLen; ++i) {
709 CXFile includingFile;
710 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000711 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
712 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000713 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000714 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000715 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000716 }
717 printf("\n");
718}
719
720void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000721 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000722}
723
724/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000725/* Linkage testing. */
726/******************************************************************************/
727
728static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
729 CXClientData d) {
730 const char *linkage = 0;
731
732 if (clang_isInvalid(clang_getCursorKind(cursor)))
733 return CXChildVisit_Recurse;
734
735 switch (clang_getCursorLinkage(cursor)) {
736 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000737 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
738 case CXLinkage_Internal: linkage = "Internal"; break;
739 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
740 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000741 }
742
743 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000744 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000745 printf("linkage=%s\n", linkage);
746 }
747
748 return CXChildVisit_Recurse;
749}
750
751/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000752/* Typekind testing. */
753/******************************************************************************/
754
755static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
756 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000757 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
758 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000759 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000760 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000761 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000762 if (clang_isConstQualifiedType(T))
763 printf(" const");
764 if (clang_isVolatileQualifiedType(T))
765 printf(" volatile");
766 if (clang_isRestrictQualifiedType(T))
767 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000768 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000769 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000770 {
771 CXType CT = clang_getCanonicalType(T);
772 if (!clang_equalTypes(T, CT)) {
773 CXString CS = clang_getTypeKindSpelling(CT.kind);
774 printf(" [canonical=%s]", clang_getCString(CS));
775 clang_disposeString(CS);
776 }
777 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000778 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000779 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000780 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000781 if (RT.kind != CXType_Invalid) {
782 CXString RS = clang_getTypeKindSpelling(RT.kind);
783 printf(" [result=%s]", clang_getCString(RS));
784 clang_disposeString(RS);
785 }
786 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000787 /* Print the argument types if they exist. */
788 {
789 int numArgs = clang_Cursor_getNumArguments(cursor);
790 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000791 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000792 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000793 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000794 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
795 if (T.kind != CXType_Invalid) {
796 CXString S = clang_getTypeKindSpelling(T.kind);
797 printf(" %s", clang_getCString(S));
798 clang_disposeString(S);
799 }
800 }
801 printf("]");
802 }
803 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000804 /* Print if this is a non-POD type. */
805 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000806
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000807 printf("\n");
808 }
809 return CXChildVisit_Recurse;
810}
811
812
813/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000814/* Loading ASTs/source. */
815/******************************************************************************/
816
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000817static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000818 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000819 CXCursorVisitor Visitor,
820 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000821
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000822 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000823 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000824
825 if (Visitor) {
826 enum CXCursorKind K = CXCursor_NotImplemented;
827 enum CXCursorKind *ck = &K;
828 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000829
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000830 /* Perform some simple filtering. */
831 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000832 else if (!strcmp(filter, "all-display") ||
833 !strcmp(filter, "local-display")) {
834 ck = NULL;
835 want_display_name = 1;
836 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000837 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000838 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
839 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
840 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
841 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
842 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
843 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
844 else {
845 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
846 return 1;
847 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000848
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000849 Data.TU = TU;
850 Data.Filter = ck;
851 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000852 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000853
Ted Kremenekce2ae882010-01-26 17:59:48 +0000854 if (PV)
855 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000856
Douglas Gregora88084b2010-02-18 18:08:43 +0000857 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000858 if (checkForErrors(TU) != 0) {
859 clang_disposeTranslationUnit(TU);
860 return -1;
861 }
862
Ted Kremenek0d435192009-11-17 18:13:31 +0000863 clang_disposeTranslationUnit(TU);
864 return 0;
865}
866
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000867int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000868 const char *prefix, CXCursorVisitor Visitor,
869 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000870 CXIndex Idx;
871 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000872 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000873 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000874 !strcmp(filter, "local") ? 1 : 0,
875 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000876
Ted Kremenek020a0952010-02-11 07:41:25 +0000877 if (!CreateTranslationUnit(Idx, file, &TU)) {
878 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000879 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000880 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000881
Ted Kremenek020a0952010-02-11 07:41:25 +0000882 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
883 clang_disposeIndex(Idx);
884 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000885}
886
Ted Kremenekce2ae882010-01-26 17:59:48 +0000887int perform_test_load_source(int argc, const char **argv,
888 const char *filter, CXCursorVisitor Visitor,
889 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000890 CXIndex Idx;
891 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000892 struct CXUnsavedFile *unsaved_files = 0;
893 int num_unsaved_files = 0;
894 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000895
Daniel Dunbarada487d2009-12-01 02:03:10 +0000896 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000897 (!strcmp(filter, "local") ||
898 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000899 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000900
Ted Kremenek020a0952010-02-11 07:41:25 +0000901 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
902 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000903 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000904 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000905
Douglas Gregordca8ee82011-05-06 16:33:08 +0000906 TU = clang_parseTranslationUnit(Idx, 0,
907 argv + num_unsaved_files,
908 argc - num_unsaved_files,
909 unsaved_files, num_unsaved_files,
910 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000911 if (!TU) {
912 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000913 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000914 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000915 return 1;
916 }
917
Ted Kremenekce2ae882010-01-26 17:59:48 +0000918 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000919 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000920 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000921 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000922}
923
Douglas Gregorabc563f2010-07-19 21:46:24 +0000924int perform_test_reparse_source(int argc, const char **argv, int trials,
925 const char *filter, CXCursorVisitor Visitor,
926 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000927 CXIndex Idx;
928 CXTranslationUnit TU;
929 struct CXUnsavedFile *unsaved_files = 0;
930 int num_unsaved_files = 0;
931 int result;
932 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000933 int remap_after_trial = 0;
934 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000935
936 Idx = clang_createIndex(/* excludeDeclsFromPCH */
937 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000938 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000939
Douglas Gregorabc563f2010-07-19 21:46:24 +0000940 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
941 clang_disposeIndex(Idx);
942 return -1;
943 }
944
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000945 /* Load the initial translation unit -- we do this without honoring remapped
946 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000947 TU = clang_parseTranslationUnit(Idx, 0,
948 argv + num_unsaved_files,
949 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000950 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000951 if (!TU) {
952 fprintf(stderr, "Unable to load translation unit!\n");
953 free_remapped_files(unsaved_files, num_unsaved_files);
954 clang_disposeIndex(Idx);
955 return 1;
956 }
957
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000958 if (checkForErrors(TU) != 0)
959 return -1;
960
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000961 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
962 remap_after_trial =
963 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
964 }
965
Douglas Gregorabc563f2010-07-19 21:46:24 +0000966 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000967 if (clang_reparseTranslationUnit(TU,
968 trial >= remap_after_trial ? num_unsaved_files : 0,
969 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000970 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000971 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000972 clang_disposeTranslationUnit(TU);
973 free_remapped_files(unsaved_files, num_unsaved_files);
974 clang_disposeIndex(Idx);
975 return -1;
976 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000977
978 if (checkForErrors(TU) != 0)
979 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000980 }
981
982 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000983
Douglas Gregorabc563f2010-07-19 21:46:24 +0000984 free_remapped_files(unsaved_files, num_unsaved_files);
985 clang_disposeIndex(Idx);
986 return result;
987}
988
Ted Kremenek0d435192009-11-17 18:13:31 +0000989/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000990/* Logic for testing clang_getCursor(). */
991/******************************************************************************/
992
Douglas Gregordd3e5542011-05-04 00:14:37 +0000993static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000994 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000995 unsigned end_line, unsigned end_col,
996 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000997 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000998 if (prefix)
999 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001000 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1001 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001002 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001003 printf("\n");
1004}
1005
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001006static int perform_file_scan(const char *ast_file, const char *source_file,
1007 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001008 CXIndex Idx;
1009 CXTranslationUnit TU;
1010 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001011 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001012 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001013 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001014 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001015
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001016 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1017 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001018 fprintf(stderr, "Could not create Index\n");
1019 return 1;
1020 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001021
Ted Kremenek1c6da172009-11-17 19:37:36 +00001022 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1023 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001024
Ted Kremenek1c6da172009-11-17 19:37:36 +00001025 if ((fp = fopen(source_file, "r")) == NULL) {
1026 fprintf(stderr, "Could not open '%s'\n", source_file);
1027 return 1;
1028 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001029
Douglas Gregorb9790342010-01-22 21:44:22 +00001030 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001031 for (;;) {
1032 CXCursor cursor;
1033 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001034
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001035 if (c == '\n') {
1036 ++line;
1037 col = 1;
1038 } else
1039 ++col;
1040
1041 /* Check the cursor at this position, and dump the previous one if we have
1042 * found something new.
1043 */
1044 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1045 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1046 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001047 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001048 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001049 start_line = line;
1050 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001051 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001052 if (c == EOF)
1053 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001054
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001055 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001056 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001057
Ted Kremenek1c6da172009-11-17 19:37:36 +00001058 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001059 clang_disposeTranslationUnit(TU);
1060 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001061 return 0;
1062}
1063
1064/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001065/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001066/******************************************************************************/
1067
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001068/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1069 on failure. If successful, the pointer *filename will contain newly-allocated
1070 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001071int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001072 unsigned *column, unsigned *second_line,
1073 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001074 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001075 const char *last_colon = strrchr(input, ':');
1076 unsigned values[4], i;
1077 unsigned num_values = (second_line && second_column)? 4 : 2;
1078
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001079 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001080 if (!last_colon || last_colon == input) {
1081 if (num_values == 4)
1082 fprintf(stderr, "could not parse filename:line:column:line:column in "
1083 "'%s'\n", input);
1084 else
1085 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001086 return 1;
1087 }
1088
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001089 for (i = 0; i != num_values; ++i) {
1090 const char *prev_colon;
1091
1092 /* Parse the next line or column. */
1093 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1094 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001095 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001096 (i % 2 ? "column" : "line"), input);
1097 return 1;
1098 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001099
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001100 if (i + 1 == num_values)
1101 break;
1102
1103 /* Find the previous colon. */
1104 prev_colon = last_colon - 1;
1105 while (prev_colon != input && *prev_colon != ':')
1106 --prev_colon;
1107 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001108 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001109 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001110 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001111 }
1112
1113 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001114 }
1115
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001116 *line = values[0];
1117 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001118
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001119 if (second_line && second_column) {
1120 *second_line = values[2];
1121 *second_column = values[3];
1122 }
1123
Douglas Gregor88d23952009-11-09 18:19:57 +00001124 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001125 *filename = (char*)malloc(last_colon - input + 1);
1126 memcpy(*filename, input, last_colon - input);
1127 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001128 return 0;
1129}
1130
1131const char *
1132clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1133 switch (Kind) {
1134 case CXCompletionChunk_Optional: return "Optional";
1135 case CXCompletionChunk_TypedText: return "TypedText";
1136 case CXCompletionChunk_Text: return "Text";
1137 case CXCompletionChunk_Placeholder: return "Placeholder";
1138 case CXCompletionChunk_Informative: return "Informative";
1139 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1140 case CXCompletionChunk_LeftParen: return "LeftParen";
1141 case CXCompletionChunk_RightParen: return "RightParen";
1142 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1143 case CXCompletionChunk_RightBracket: return "RightBracket";
1144 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1145 case CXCompletionChunk_RightBrace: return "RightBrace";
1146 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1147 case CXCompletionChunk_RightAngle: return "RightAngle";
1148 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001149 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001150 case CXCompletionChunk_Colon: return "Colon";
1151 case CXCompletionChunk_SemiColon: return "SemiColon";
1152 case CXCompletionChunk_Equal: return "Equal";
1153 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1154 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001155 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001156
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001157 return "Unknown";
1158}
1159
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001160static int checkForErrors(CXTranslationUnit TU) {
1161 unsigned Num, i;
1162 CXDiagnostic Diag;
1163 CXString DiagStr;
1164
1165 if (!getenv("CINDEXTEST_FAILONERROR"))
1166 return 0;
1167
1168 Num = clang_getNumDiagnostics(TU);
1169 for (i = 0; i != Num; ++i) {
1170 Diag = clang_getDiagnostic(TU, i);
1171 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1172 DiagStr = clang_formatDiagnostic(Diag,
1173 clang_defaultDiagnosticDisplayOptions());
1174 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1175 clang_disposeString(DiagStr);
1176 clang_disposeDiagnostic(Diag);
1177 return -1;
1178 }
1179 clang_disposeDiagnostic(Diag);
1180 }
1181
1182 return 0;
1183}
1184
Douglas Gregor3ac73852009-11-09 16:04:45 +00001185void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001186 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001187
Douglas Gregor3ac73852009-11-09 16:04:45 +00001188 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001189 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001190 CXString text;
1191 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001192 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001193 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001194
Douglas Gregor3ac73852009-11-09 16:04:45 +00001195 if (Kind == CXCompletionChunk_Optional) {
1196 fprintf(file, "{Optional ");
1197 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001198 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001199 file);
1200 fprintf(file, "}");
1201 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001202 }
1203
1204 if (Kind == CXCompletionChunk_VerticalSpace) {
1205 fprintf(file, "{VerticalSpace }");
1206 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001207 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001208
Douglas Gregord5a20892009-11-09 17:05:28 +00001209 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001210 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001211 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001212 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001213 cstr ? cstr : "");
1214 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001215 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001216
Douglas Gregor3ac73852009-11-09 16:04:45 +00001217}
1218
1219void print_completion_result(CXCompletionResult *completion_result,
1220 CXClientData client_data) {
1221 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001222 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001223 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001224 enum CXCursorKind ParentKind;
1225 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001226 CXString BriefComment;
1227 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001228
Ted Kremeneke68fff62010-02-17 00:41:32 +00001229 fprintf(file, "%s:", clang_getCString(ks));
1230 clang_disposeString(ks);
1231
Douglas Gregor3ac73852009-11-09 16:04:45 +00001232 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001233 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001234 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001235 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1236 case CXAvailability_Available:
1237 break;
1238
1239 case CXAvailability_Deprecated:
1240 fprintf(file, " (deprecated)");
1241 break;
1242
1243 case CXAvailability_NotAvailable:
1244 fprintf(file, " (unavailable)");
1245 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001246
1247 case CXAvailability_NotAccessible:
1248 fprintf(file, " (inaccessible)");
1249 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001250 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001251
1252 annotationCount = clang_getCompletionNumAnnotations(
1253 completion_result->CompletionString);
1254 if (annotationCount) {
1255 unsigned i;
1256 fprintf(file, " (");
1257 for (i = 0; i < annotationCount; ++i) {
1258 if (i != 0)
1259 fprintf(file, ", ");
1260 fprintf(file, "\"%s\"",
1261 clang_getCString(clang_getCompletionAnnotation(
1262 completion_result->CompletionString, i)));
1263 }
1264 fprintf(file, ")");
1265 }
1266
Douglas Gregorba103062012-03-27 23:34:16 +00001267 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1268 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1269 &ParentKind);
1270 if (ParentKind != CXCursor_NotImplemented) {
1271 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1272 fprintf(file, " (parent: %s '%s')",
1273 clang_getCString(KindSpelling),
1274 clang_getCString(ParentName));
1275 clang_disposeString(KindSpelling);
1276 }
1277 clang_disposeString(ParentName);
1278 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001279
1280 BriefComment = clang_getCompletionBriefComment(
1281 completion_result->CompletionString);
1282 BriefCommentCString = clang_getCString(BriefComment);
1283 if (BriefCommentCString && *BriefCommentCString != '\0') {
1284 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1285 }
1286 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001287
Douglas Gregor58ddb602010-08-23 23:00:57 +00001288 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001289}
1290
Douglas Gregor3da626b2011-07-07 16:03:39 +00001291void print_completion_contexts(unsigned long long contexts, FILE *file) {
1292 fprintf(file, "Completion contexts:\n");
1293 if (contexts == CXCompletionContext_Unknown) {
1294 fprintf(file, "Unknown\n");
1295 }
1296 if (contexts & CXCompletionContext_AnyType) {
1297 fprintf(file, "Any type\n");
1298 }
1299 if (contexts & CXCompletionContext_AnyValue) {
1300 fprintf(file, "Any value\n");
1301 }
1302 if (contexts & CXCompletionContext_ObjCObjectValue) {
1303 fprintf(file, "Objective-C object value\n");
1304 }
1305 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1306 fprintf(file, "Objective-C selector value\n");
1307 }
1308 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1309 fprintf(file, "C++ class type value\n");
1310 }
1311 if (contexts & CXCompletionContext_DotMemberAccess) {
1312 fprintf(file, "Dot member access\n");
1313 }
1314 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1315 fprintf(file, "Arrow member access\n");
1316 }
1317 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1318 fprintf(file, "Objective-C property access\n");
1319 }
1320 if (contexts & CXCompletionContext_EnumTag) {
1321 fprintf(file, "Enum tag\n");
1322 }
1323 if (contexts & CXCompletionContext_UnionTag) {
1324 fprintf(file, "Union tag\n");
1325 }
1326 if (contexts & CXCompletionContext_StructTag) {
1327 fprintf(file, "Struct tag\n");
1328 }
1329 if (contexts & CXCompletionContext_ClassTag) {
1330 fprintf(file, "Class name\n");
1331 }
1332 if (contexts & CXCompletionContext_Namespace) {
1333 fprintf(file, "Namespace or namespace alias\n");
1334 }
1335 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1336 fprintf(file, "Nested name specifier\n");
1337 }
1338 if (contexts & CXCompletionContext_ObjCInterface) {
1339 fprintf(file, "Objective-C interface\n");
1340 }
1341 if (contexts & CXCompletionContext_ObjCProtocol) {
1342 fprintf(file, "Objective-C protocol\n");
1343 }
1344 if (contexts & CXCompletionContext_ObjCCategory) {
1345 fprintf(file, "Objective-C category\n");
1346 }
1347 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1348 fprintf(file, "Objective-C instance method\n");
1349 }
1350 if (contexts & CXCompletionContext_ObjCClassMessage) {
1351 fprintf(file, "Objective-C class method\n");
1352 }
1353 if (contexts & CXCompletionContext_ObjCSelectorName) {
1354 fprintf(file, "Objective-C selector name\n");
1355 }
1356 if (contexts & CXCompletionContext_MacroName) {
1357 fprintf(file, "Macro name\n");
1358 }
1359 if (contexts & CXCompletionContext_NaturalLanguage) {
1360 fprintf(file, "Natural language\n");
1361 }
1362}
1363
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001364int my_stricmp(const char *s1, const char *s2) {
1365 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001366 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001367 if (c1 < c2)
1368 return -1;
1369 else if (c1 > c2)
1370 return 1;
1371
1372 ++s1;
1373 ++s2;
1374 }
1375
1376 if (*s1)
1377 return 1;
1378 else if (*s2)
1379 return -1;
1380 return 0;
1381}
1382
Douglas Gregor1982c182010-07-12 18:38:41 +00001383int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001384 const char *input = argv[1];
1385 char *filename = 0;
1386 unsigned line;
1387 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001388 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001389 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001390 struct CXUnsavedFile *unsaved_files = 0;
1391 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001392 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001393 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001394 unsigned I, Repeats = 1;
1395 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1396
1397 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1398 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001399 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1400 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001401
Douglas Gregor1982c182010-07-12 18:38:41 +00001402 if (timing_only)
1403 input += strlen("-code-completion-timing=");
1404 else
1405 input += strlen("-code-completion-at=");
1406
Ted Kremeneke68fff62010-02-17 00:41:32 +00001407 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001408 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001409 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001410
Douglas Gregor735df882009-12-02 09:21:34 +00001411 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1412 return -1;
1413
Douglas Gregor32be4a52010-10-11 21:37:58 +00001414 CIdx = clang_createIndex(0, 0);
1415
1416 if (getenv("CINDEXTEST_EDITING"))
1417 Repeats = 5;
1418
1419 TU = clang_parseTranslationUnit(CIdx, 0,
1420 argv + num_unsaved_files + 2,
1421 argc - num_unsaved_files - 2,
1422 0, 0, getDefaultParsingOptions());
1423 if (!TU) {
1424 fprintf(stderr, "Unable to load translation unit!\n");
1425 return 1;
1426 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001427
1428 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1429 fprintf(stderr, "Unable to reparse translation init!\n");
1430 return 1;
1431 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001432
1433 for (I = 0; I != Repeats; ++I) {
1434 results = clang_codeCompleteAt(TU, filename, line, column,
1435 unsaved_files, num_unsaved_files,
1436 completionOptions);
1437 if (!results) {
1438 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001439 return 1;
1440 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001441 if (I != Repeats-1)
1442 clang_disposeCodeCompleteResults(results);
1443 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001444
Douglas Gregorec6762c2009-12-18 16:20:58 +00001445 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001446 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001447 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001448 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001449 CXString objCSelector;
1450 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001451 if (!timing_only) {
1452 /* Sort the code-completion results based on the typed text. */
1453 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1454
Douglas Gregor1982c182010-07-12 18:38:41 +00001455 for (i = 0; i != n; ++i)
1456 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001457 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001458 n = clang_codeCompleteGetNumDiagnostics(results);
1459 for (i = 0; i != n; ++i) {
1460 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1461 PrintDiagnostic(diag);
1462 clang_disposeDiagnostic(diag);
1463 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001464
1465 contexts = clang_codeCompleteGetContexts(results);
1466 print_completion_contexts(contexts, stdout);
1467
Douglas Gregor0a47d692011-07-26 15:24:30 +00001468 containerKind = clang_codeCompleteGetContainerKind(results,
1469 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001470
1471 if (containerKind != CXCursor_InvalidCode) {
1472 /* We have found a container */
1473 CXString containerUSR, containerKindSpelling;
1474 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1475 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1476 clang_disposeString(containerKindSpelling);
1477
1478 if (containerIsIncomplete) {
1479 printf("Container is incomplete\n");
1480 }
1481 else {
1482 printf("Container is complete\n");
1483 }
1484
1485 containerUSR = clang_codeCompleteGetContainerUSR(results);
1486 printf("Container USR: %s\n", clang_getCString(containerUSR));
1487 clang_disposeString(containerUSR);
1488 }
1489
Douglas Gregor0a47d692011-07-26 15:24:30 +00001490 objCSelector = clang_codeCompleteGetObjCSelector(results);
1491 selectorString = clang_getCString(objCSelector);
1492 if (selectorString && strlen(selectorString) > 0) {
1493 printf("Objective-C selector: %s\n", selectorString);
1494 }
1495 clang_disposeString(objCSelector);
1496
Douglas Gregorec6762c2009-12-18 16:20:58 +00001497 clang_disposeCodeCompleteResults(results);
1498 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001499 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001500 clang_disposeIndex(CIdx);
1501 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001502
Douglas Gregor735df882009-12-02 09:21:34 +00001503 free_remapped_files(unsaved_files, num_unsaved_files);
1504
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001505 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001506}
1507
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001508typedef struct {
1509 char *filename;
1510 unsigned line;
1511 unsigned column;
1512} CursorSourceLocation;
1513
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001514static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001515 CXIndex CIdx;
1516 int errorCode;
1517 struct CXUnsavedFile *unsaved_files = 0;
1518 int num_unsaved_files = 0;
1519 CXTranslationUnit TU;
1520 CXCursor Cursor;
1521 CursorSourceLocation *Locations = 0;
1522 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001523 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001524 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001525
Ted Kremeneke68fff62010-02-17 00:41:32 +00001526 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001527 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1528 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001529
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001530 /* Parse the locations. */
1531 assert(NumLocations > 0 && "Unable to count locations?");
1532 Locations = (CursorSourceLocation *)malloc(
1533 NumLocations * sizeof(CursorSourceLocation));
1534 for (Loc = 0; Loc < NumLocations; ++Loc) {
1535 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001536 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1537 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001538 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001539 return errorCode;
1540 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001541
1542 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001543 &num_unsaved_files))
1544 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001545
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001546 if (getenv("CINDEXTEST_EDITING"))
1547 Repeats = 5;
1548
1549 /* Parse the translation unit. When we're testing clang_getCursor() after
1550 reparsing, don't remap unsaved files until the second parse. */
1551 CIdx = clang_createIndex(1, 1);
1552 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1553 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001554 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001555 unsaved_files,
1556 Repeats > 1? 0 : num_unsaved_files,
1557 getDefaultParsingOptions());
1558
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001559 if (!TU) {
1560 fprintf(stderr, "unable to parse input\n");
1561 return -1;
1562 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001563
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001564 if (checkForErrors(TU) != 0)
1565 return -1;
1566
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001567 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001568 if (Repeats > 1 &&
1569 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1570 clang_defaultReparseOptions(TU))) {
1571 clang_disposeTranslationUnit(TU);
1572 return 1;
1573 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001574
1575 if (checkForErrors(TU) != 0)
1576 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001577
1578 for (Loc = 0; Loc < NumLocations; ++Loc) {
1579 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1580 if (!file)
1581 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001582
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001583 Cursor = clang_getCursor(TU,
1584 clang_getLocation(TU, file, Locations[Loc].line,
1585 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001586
1587 if (checkForErrors(TU) != 0)
1588 return -1;
1589
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001590 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001591 CXCompletionString completionString = clang_getCursorCompletionString(
1592 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001593 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1594 CXString Spelling;
1595 const char *cspell;
1596 unsigned line, column;
1597 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1598 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001599 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001600 PrintCursorExtent(Cursor);
1601 Spelling = clang_getCursorSpelling(Cursor);
1602 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001603 if (cspell && strlen(cspell) != 0) {
1604 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001605 printf(" Spelling=%s (", cspell);
1606 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001607 CXSourceRange range =
1608 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001609 if (clang_Range_isNull(range))
1610 break;
1611 PrintRange(range, 0);
1612 }
1613 printf(")");
1614 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001615 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001616 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1617 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001618 if (clang_Cursor_isDynamicCall(Cursor))
1619 printf(" Dynamic-call");
1620
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001621 if (completionString != NULL) {
1622 printf("\nCompletion string: ");
1623 print_completion_string(completionString, stdout);
1624 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001625 printf("\n");
1626 free(Locations[Loc].filename);
1627 }
1628 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001629 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001630
Douglas Gregora88084b2010-02-18 18:08:43 +00001631 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001632 clang_disposeTranslationUnit(TU);
1633 clang_disposeIndex(CIdx);
1634 free(Locations);
1635 free_remapped_files(unsaved_files, num_unsaved_files);
1636 return 0;
1637}
1638
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001639static enum CXVisitorResult findFileRefsVisit(void *context,
1640 CXCursor cursor, CXSourceRange range) {
1641 if (clang_Range_isNull(range))
1642 return CXVisit_Continue;
1643
1644 PrintCursor(cursor);
1645 PrintRange(range, "");
1646 printf("\n");
1647 return CXVisit_Continue;
1648}
1649
1650static int find_file_refs_at(int argc, const char **argv) {
1651 CXIndex CIdx;
1652 int errorCode;
1653 struct CXUnsavedFile *unsaved_files = 0;
1654 int num_unsaved_files = 0;
1655 CXTranslationUnit TU;
1656 CXCursor Cursor;
1657 CursorSourceLocation *Locations = 0;
1658 unsigned NumLocations = 0, Loc;
1659 unsigned Repeats = 1;
1660 unsigned I;
1661
1662 /* Count the number of locations. */
1663 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1664 ++NumLocations;
1665
1666 /* Parse the locations. */
1667 assert(NumLocations > 0 && "Unable to count locations?");
1668 Locations = (CursorSourceLocation *)malloc(
1669 NumLocations * sizeof(CursorSourceLocation));
1670 for (Loc = 0; Loc < NumLocations; ++Loc) {
1671 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1672 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1673 &Locations[Loc].line,
1674 &Locations[Loc].column, 0, 0)))
1675 return errorCode;
1676 }
1677
1678 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1679 &num_unsaved_files))
1680 return -1;
1681
1682 if (getenv("CINDEXTEST_EDITING"))
1683 Repeats = 5;
1684
1685 /* Parse the translation unit. When we're testing clang_getCursor() after
1686 reparsing, don't remap unsaved files until the second parse. */
1687 CIdx = clang_createIndex(1, 1);
1688 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1689 argv + num_unsaved_files + 1 + NumLocations,
1690 argc - num_unsaved_files - 2 - NumLocations,
1691 unsaved_files,
1692 Repeats > 1? 0 : num_unsaved_files,
1693 getDefaultParsingOptions());
1694
1695 if (!TU) {
1696 fprintf(stderr, "unable to parse input\n");
1697 return -1;
1698 }
1699
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001700 if (checkForErrors(TU) != 0)
1701 return -1;
1702
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001703 for (I = 0; I != Repeats; ++I) {
1704 if (Repeats > 1 &&
1705 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1706 clang_defaultReparseOptions(TU))) {
1707 clang_disposeTranslationUnit(TU);
1708 return 1;
1709 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001710
1711 if (checkForErrors(TU) != 0)
1712 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001713
1714 for (Loc = 0; Loc < NumLocations; ++Loc) {
1715 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1716 if (!file)
1717 continue;
1718
1719 Cursor = clang_getCursor(TU,
1720 clang_getLocation(TU, file, Locations[Loc].line,
1721 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001722
1723 if (checkForErrors(TU) != 0)
1724 return -1;
1725
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001726 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001727 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001728 PrintCursor(Cursor);
1729 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001730 clang_findReferencesInFile(Cursor, file, visitor);
1731 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001732
1733 if (checkForErrors(TU) != 0)
1734 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001735 }
1736 }
1737 }
1738
1739 PrintDiagnostics(TU);
1740 clang_disposeTranslationUnit(TU);
1741 clang_disposeIndex(CIdx);
1742 free(Locations);
1743 free_remapped_files(unsaved_files, num_unsaved_files);
1744 return 0;
1745}
1746
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001747typedef struct {
1748 const char *check_prefix;
1749 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001750 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001751 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001752 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001753} IndexData;
1754
1755static void printCheck(IndexData *data) {
1756 if (data->check_prefix) {
1757 if (data->first_check_printed) {
1758 printf("// %s-NEXT: ", data->check_prefix);
1759 } else {
1760 printf("// %s : ", data->check_prefix);
1761 data->first_check_printed = 1;
1762 }
1763 }
1764}
1765
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001766static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001767 CXString filename = clang_getFileName((CXFile)file);
1768 printf("%s", clang_getCString(filename));
1769 clang_disposeString(filename);
1770}
1771
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001772static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1773 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001774 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001775 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001776 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001777 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001778 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001779
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001780 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001781 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1782 if (line == 0) {
1783 printf("<null loc>");
1784 return;
1785 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001786 if (!file) {
1787 printf("<no idxfile>");
1788 return;
1789 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001790 filename = clang_getFileName((CXFile)file);
1791 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001792 if (strcmp(cname, index_data->main_filename) == 0)
1793 isMainFile = 1;
1794 else
1795 isMainFile = 0;
1796 clang_disposeString(filename);
1797
1798 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001799 printCXIndexFile(file);
1800 printf(":");
1801 }
1802 printf("%d:%d", line, column);
1803}
1804
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001805static unsigned digitCount(unsigned val) {
1806 unsigned c = 1;
1807 while (1) {
1808 if (val < 10)
1809 return c;
1810 ++c;
1811 val /= 10;
1812 }
1813}
1814
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001815static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1816 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001817 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001818 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001819 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001820 unsigned line, column;
1821
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001822 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001823 if (!name)
1824 name = "<anon-tag>";
1825
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001826 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001827 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001828 newStr = (char *)malloc(strlen(name) +
1829 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001830 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001831 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001832}
1833
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001834static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1835 CXIdxClientContainer container;
1836 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001837 if (!container)
1838 printf("[<<NULL>>]");
1839 else
1840 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001841}
1842
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001843static const char *getEntityKindString(CXIdxEntityKind kind) {
1844 switch (kind) {
1845 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1846 case CXIdxEntity_Typedef: return "typedef";
1847 case CXIdxEntity_Function: return "function";
1848 case CXIdxEntity_Variable: return "variable";
1849 case CXIdxEntity_Field: return "field";
1850 case CXIdxEntity_EnumConstant: return "enumerator";
1851 case CXIdxEntity_ObjCClass: return "objc-class";
1852 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1853 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001854 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1855 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001856 case CXIdxEntity_ObjCProperty: return "objc-property";
1857 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1858 case CXIdxEntity_Enum: return "enum";
1859 case CXIdxEntity_Struct: return "struct";
1860 case CXIdxEntity_Union: return "union";
1861 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001862 case CXIdxEntity_CXXNamespace: return "namespace";
1863 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1864 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1865 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1866 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1867 case CXIdxEntity_CXXConstructor: return "constructor";
1868 case CXIdxEntity_CXXDestructor: return "destructor";
1869 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1870 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1871 }
1872 assert(0 && "Garbage entity kind");
1873 return 0;
1874}
1875
1876static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1877 switch (kind) {
1878 case CXIdxEntity_NonTemplate: return "";
1879 case CXIdxEntity_Template: return "-template";
1880 case CXIdxEntity_TemplatePartialSpecialization:
1881 return "-template-partial-spec";
1882 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001883 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001884 assert(0 && "Garbage entity kind");
1885 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001886}
1887
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001888static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1889 switch (kind) {
1890 case CXIdxEntityLang_None: return "<none>";
1891 case CXIdxEntityLang_C: return "C";
1892 case CXIdxEntityLang_ObjC: return "ObjC";
1893 case CXIdxEntityLang_CXX: return "C++";
1894 }
1895 assert(0 && "Garbage language kind");
1896 return 0;
1897}
1898
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001899static void printEntityInfo(const char *cb,
1900 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001901 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001902 const char *name;
1903 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001904 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001905 index_data = (IndexData *)client_data;
1906 printCheck(index_data);
1907
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001908 if (!info) {
1909 printf("%s: <<NULL>>", cb);
1910 return;
1911 }
1912
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001913 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001914 if (!name)
1915 name = "<anon-tag>";
1916
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001917 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1918 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001919 printf(" | name: %s", name);
1920 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001921 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001922
1923 for (i = 0; i != info->numAttributes; ++i) {
1924 const CXIdxAttrInfo *Attr = info->attributes[i];
1925 printf(" <attribute>: ");
1926 PrintCursor(Attr->cursor);
1927 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001928}
1929
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001930static void printBaseClassInfo(CXClientData client_data,
1931 const CXIdxBaseClassInfo *info) {
1932 printEntityInfo(" <base>", client_data, info->base);
1933 printf(" | cursor: ");
1934 PrintCursor(info->cursor);
1935 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001936 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001937}
1938
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001939static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1940 CXClientData client_data) {
1941 unsigned i;
1942 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1943 printEntityInfo(" <protocol>", client_data,
1944 ProtoInfo->protocols[i]->protocol);
1945 printf(" | cursor: ");
1946 PrintCursor(ProtoInfo->protocols[i]->cursor);
1947 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001948 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001949 printf("\n");
1950 }
1951}
1952
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001953static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001954 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001955 CXString str;
1956 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001957 unsigned numDiags, i;
1958 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001959 IndexData *index_data;
1960 index_data = (IndexData *)client_data;
1961 printCheck(index_data);
1962
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001963 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1964 for (i = 0; i != numDiags; ++i) {
1965 diag = clang_getDiagnosticInSet(diagSet, i);
1966 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1967 cstr = clang_getCString(str);
1968 printf("[diagnostic]: %s\n", cstr);
1969 clang_disposeString(str);
1970
1971 if (getenv("CINDEXTEST_FAILONERROR") &&
1972 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1973 index_data->fail_for_error = 1;
1974 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001975 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001976}
1977
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001978static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1979 CXFile file, void *reserved) {
1980 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001981 CXString filename;
1982
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001983 index_data = (IndexData *)client_data;
1984 printCheck(index_data);
1985
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001986 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001987 index_data->main_filename = clang_getCString(filename);
1988 clang_disposeString(filename);
1989
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001990 printf("[enteredMainFile]: ");
1991 printCXIndexFile((CXIdxClientFile)file);
1992 printf("\n");
1993
1994 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001995}
1996
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001997static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001998 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001999 IndexData *index_data;
2000 index_data = (IndexData *)client_data;
2001 printCheck(index_data);
2002
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002003 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002004 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002005 printf(" | name: \"%s\"", info->filename);
2006 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002007 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002008 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002009
2010 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002011}
2012
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002013static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002014 void *reserved) {
2015 IndexData *index_data;
2016 index_data = (IndexData *)client_data;
2017 printCheck(index_data);
2018
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002019 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002020 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002021}
2022
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002023static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002024 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002025 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002026 const CXIdxObjCCategoryDeclInfo *CatInfo;
2027 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002028 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002029 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002030 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002031 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002032 index_data = (IndexData *)client_data;
2033
2034 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2035 printf(" | cursor: ");
2036 PrintCursor(info->cursor);
2037 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002038 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002039 printf(" | semantic-container: ");
2040 printCXIndexContainer(info->semanticContainer);
2041 printf(" | lexical-container: ");
2042 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002043 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002044 printf(" | isDef: %d", info->isDefinition);
2045 printf(" | isContainer: %d", info->isContainer);
2046 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002047
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002048 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002049 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002050 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002051 PrintCursor(Attr->cursor);
2052 printf("\n");
2053 }
2054
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002055 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2056 const char *kindName = 0;
2057 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2058 switch (K) {
2059 case CXIdxObjCContainer_ForwardRef:
2060 kindName = "forward-ref"; break;
2061 case CXIdxObjCContainer_Interface:
2062 kindName = "interface"; break;
2063 case CXIdxObjCContainer_Implementation:
2064 kindName = "implementation"; break;
2065 }
2066 printCheck(index_data);
2067 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2068 }
2069
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002070 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002071 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2072 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002073 printf(" | cursor: ");
2074 PrintCursor(CatInfo->classCursor);
2075 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002076 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002077 printf("\n");
2078 }
2079
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002080 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2081 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002082 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002083 printf("\n");
2084 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002085 }
2086
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002087 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2088 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002089 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002090
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002091 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2092 if (PropInfo->getter) {
2093 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2094 printf("\n");
2095 }
2096 if (PropInfo->setter) {
2097 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2098 printf("\n");
2099 }
2100 }
2101
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002102 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2103 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2104 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2105 printf("\n");
2106 }
2107 }
2108
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002109 if (info->declAsContainer)
2110 clang_index_setClientContainer(info->declAsContainer,
2111 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002112}
2113
2114static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002115 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002116 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002117 printf(" | cursor: ");
2118 PrintCursor(info->cursor);
2119 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002120 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002121 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002122 printf(" | container: ");
2123 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002124 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002125 switch (info->kind) {
2126 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002127 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002128 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002129 printf("\n");
2130}
2131
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002132static int index_abortQuery(CXClientData client_data, void *reserved) {
2133 IndexData *index_data;
2134 index_data = (IndexData *)client_data;
2135 return index_data->abort;
2136}
2137
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002138static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002139 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002140 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002141 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002142 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002143 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002144 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002145 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002146 index_indexEntityReference
2147};
2148
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002149static unsigned getIndexOptions(void) {
2150 unsigned index_opts;
2151 index_opts = 0;
2152 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2153 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2154 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2155 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2156
2157 return index_opts;
2158}
2159
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002160static int index_file(int argc, const char **argv) {
2161 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002162 CXIndex Idx;
2163 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002164 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002165 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002166 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002167
2168 check_prefix = 0;
2169 if (argc > 0) {
2170 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2171 check_prefix = argv[0] + strlen("-check-prefix=");
2172 ++argv;
2173 --argc;
2174 }
2175 }
2176
2177 if (argc == 0) {
2178 fprintf(stderr, "no compiler arguments\n");
2179 return -1;
2180 }
2181
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002182 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2183 /* displayDiagnosics=*/1))) {
2184 fprintf(stderr, "Could not create Index\n");
2185 return 1;
2186 }
2187 idxAction = 0;
2188 result = 1;
2189
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002190 index_data.check_prefix = check_prefix;
2191 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002192 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002193 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002194
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002195 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002196 idxAction = clang_IndexAction_create(Idx);
2197 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002198 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002199 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002200 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002201 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002202
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002203 clang_IndexAction_dispose(idxAction);
2204 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002205 return result;
2206}
2207
2208static int index_tu(int argc, const char **argv) {
2209 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002210 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002211 CXTranslationUnit TU;
2212 const char *check_prefix;
2213 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002214 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002215 int result;
2216
2217 check_prefix = 0;
2218 if (argc > 0) {
2219 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2220 check_prefix = argv[0] + strlen("-check-prefix=");
2221 ++argv;
2222 --argc;
2223 }
2224 }
2225
2226 if (argc == 0) {
2227 fprintf(stderr, "no ast file\n");
2228 return -1;
2229 }
2230
2231 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2232 /* displayDiagnosics=*/1))) {
2233 fprintf(stderr, "Could not create Index\n");
2234 return 1;
2235 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002236 idxAction = 0;
2237 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002238
2239 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002240 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002241
2242 index_data.check_prefix = check_prefix;
2243 index_data.first_check_printed = 0;
2244 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002245 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002246
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002247 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002248 idxAction = clang_IndexAction_create(Idx);
2249 result = clang_indexTranslationUnit(idxAction, &index_data,
2250 &IndexCB,sizeof(IndexCB),
2251 index_opts, TU);
2252 if (index_data.fail_for_error)
2253 goto finished;
2254
2255 finished:
2256 clang_IndexAction_dispose(idxAction);
2257 clang_disposeIndex(Idx);
2258
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002259 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002260}
2261
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002262int perform_token_annotation(int argc, const char **argv) {
2263 const char *input = argv[1];
2264 char *filename = 0;
2265 unsigned line, second_line;
2266 unsigned column, second_column;
2267 CXIndex CIdx;
2268 CXTranslationUnit TU = 0;
2269 int errorCode;
2270 struct CXUnsavedFile *unsaved_files = 0;
2271 int num_unsaved_files = 0;
2272 CXToken *tokens;
2273 unsigned num_tokens;
2274 CXSourceRange range;
2275 CXSourceLocation startLoc, endLoc;
2276 CXFile file = 0;
2277 CXCursor *cursors = 0;
2278 unsigned i;
2279
2280 input += strlen("-test-annotate-tokens=");
2281 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2282 &second_line, &second_column)))
2283 return errorCode;
2284
Richard Smithe07c5f82012-07-05 08:20:49 +00002285 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
2286 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002287 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00002288 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002289
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002290 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002291 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2292 argv + num_unsaved_files + 2,
2293 argc - num_unsaved_files - 3,
2294 unsaved_files,
2295 num_unsaved_files,
2296 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002297 if (!TU) {
2298 fprintf(stderr, "unable to parse input\n");
2299 clang_disposeIndex(CIdx);
2300 free(filename);
2301 free_remapped_files(unsaved_files, num_unsaved_files);
2302 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002303 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002304 errorCode = 0;
2305
Richard Smithe07c5f82012-07-05 08:20:49 +00002306 if (checkForErrors(TU) != 0) {
2307 errorCode = -1;
2308 goto teardown;
2309 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002310
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002311 if (getenv("CINDEXTEST_EDITING")) {
2312 for (i = 0; i < 5; ++i) {
2313 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2314 clang_defaultReparseOptions(TU))) {
2315 fprintf(stderr, "Unable to reparse translation unit!\n");
2316 errorCode = -1;
2317 goto teardown;
2318 }
2319 }
2320 }
2321
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002322 if (checkForErrors(TU) != 0) {
2323 errorCode = -1;
2324 goto teardown;
2325 }
2326
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002327 file = clang_getFile(TU, filename);
2328 if (!file) {
2329 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2330 errorCode = -1;
2331 goto teardown;
2332 }
2333
2334 startLoc = clang_getLocation(TU, file, line, column);
2335 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002336 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002337 column);
2338 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002339 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002340 }
2341
2342 endLoc = clang_getLocation(TU, file, second_line, second_column);
2343 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002344 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002345 second_line, second_column);
2346 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002347 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002348 }
2349
2350 range = clang_getRange(startLoc, endLoc);
2351 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002352
2353 if (checkForErrors(TU) != 0) {
2354 errorCode = -1;
2355 goto teardown;
2356 }
2357
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002358 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2359 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002360
2361 if (checkForErrors(TU) != 0) {
2362 errorCode = -1;
2363 goto teardown;
2364 }
2365
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002366 for (i = 0; i != num_tokens; ++i) {
2367 const char *kind = "<unknown>";
2368 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2369 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2370 unsigned start_line, start_column, end_line, end_column;
2371
2372 switch (clang_getTokenKind(tokens[i])) {
2373 case CXToken_Punctuation: kind = "Punctuation"; break;
2374 case CXToken_Keyword: kind = "Keyword"; break;
2375 case CXToken_Identifier: kind = "Identifier"; break;
2376 case CXToken_Literal: kind = "Literal"; break;
2377 case CXToken_Comment: kind = "Comment"; break;
2378 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002379 clang_getSpellingLocation(clang_getRangeStart(extent),
2380 0, &start_line, &start_column, 0);
2381 clang_getSpellingLocation(clang_getRangeEnd(extent),
2382 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002383 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002384 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002385 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002386 if (!clang_isInvalid(cursors[i].kind)) {
2387 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002388 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002389 }
2390 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002391 }
2392 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002393 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002394
2395 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002396 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002397 clang_disposeTranslationUnit(TU);
2398 clang_disposeIndex(CIdx);
2399 free(filename);
2400 free_remapped_files(unsaved_files, num_unsaved_files);
2401 return errorCode;
2402}
2403
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002404static int
2405perform_test_compilation_db(const char *database, int argc, const char **argv) {
2406 CXCompilationDatabase db;
2407 CXCompileCommands CCmds;
2408 CXCompileCommand CCmd;
2409 CXCompilationDatabase_Error ec;
2410 CXString wd;
2411 CXString arg;
2412 int errorCode = 0;
2413 char *tmp;
2414 unsigned len;
2415 char *buildDir;
2416 int i, j, a, numCmds, numArgs;
2417
2418 len = strlen(database);
2419 tmp = (char *) malloc(len+1);
2420 memcpy(tmp, database, len+1);
2421 buildDir = dirname(tmp);
2422
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002423 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002424
2425 if (db) {
2426
2427 if (ec!=CXCompilationDatabase_NoError) {
2428 printf("unexpected error %d code while loading compilation database\n", ec);
2429 errorCode = -1;
2430 goto cdb_end;
2431 }
2432
2433 for (i=0; i<argc && errorCode==0; ) {
2434 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002435 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002436
2437 if (!CCmds) {
2438 printf("file %s not found in compilation db\n", argv[i+1]);
2439 errorCode = -1;
2440 break;
2441 }
2442
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002443 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002444
2445 if (numCmds==0) {
2446 fprintf(stderr, "should not get an empty compileCommand set for file"
2447 " '%s'\n", argv[i+1]);
2448 errorCode = -1;
2449 break;
2450 }
2451
2452 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002453 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002454
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002455 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002456 printf("workdir:'%s'", clang_getCString(wd));
2457 clang_disposeString(wd);
2458
2459 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002460 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002461 for (a=0; a<numArgs; ++a) {
2462 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002463 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002464 printf("%s", clang_getCString(arg));
2465 clang_disposeString(arg);
2466 }
2467 printf("'\n");
2468 }
2469
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002470 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002471
2472 i += 2;
2473 }
2474 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002475 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002476 } else {
2477 printf("database loading failed with error code %d.\n", ec);
2478 errorCode = -1;
2479 }
2480
2481cdb_end:
2482 free(tmp);
2483
2484 return errorCode;
2485}
2486
Ted Kremenek0d435192009-11-17 18:13:31 +00002487/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002488/* USR printing. */
2489/******************************************************************************/
2490
2491static int insufficient_usr(const char *kind, const char *usage) {
2492 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2493 return 1;
2494}
2495
2496static unsigned isUSR(const char *s) {
2497 return s[0] == 'c' && s[1] == ':';
2498}
2499
2500static int not_usr(const char *s, const char *arg) {
2501 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2502 return 1;
2503}
2504
2505static void print_usr(CXString usr) {
2506 const char *s = clang_getCString(usr);
2507 printf("%s\n", s);
2508 clang_disposeString(usr);
2509}
2510
2511static void display_usrs() {
2512 fprintf(stderr, "-print-usrs options:\n"
2513 " ObjCCategory <class name> <category name>\n"
2514 " ObjCClass <class name>\n"
2515 " ObjCIvar <ivar name> <class USR>\n"
2516 " ObjCMethod <selector> [0=class method|1=instance method] "
2517 "<class USR>\n"
2518 " ObjCProperty <property name> <class USR>\n"
2519 " ObjCProtocol <protocol name>\n");
2520}
2521
2522int print_usrs(const char **I, const char **E) {
2523 while (I != E) {
2524 const char *kind = *I;
2525 unsigned len = strlen(kind);
2526 switch (len) {
2527 case 8:
2528 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2529 if (I + 2 >= E)
2530 return insufficient_usr(kind, "<ivar name> <class USR>");
2531 if (!isUSR(I[2]))
2532 return not_usr("<class USR>", I[2]);
2533 else {
2534 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002535 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002536 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002537 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2538 }
2539
2540 I += 3;
2541 continue;
2542 }
2543 break;
2544 case 9:
2545 if (memcmp(kind, "ObjCClass", 9) == 0) {
2546 if (I + 1 >= E)
2547 return insufficient_usr(kind, "<class name>");
2548 print_usr(clang_constructUSR_ObjCClass(I[1]));
2549 I += 2;
2550 continue;
2551 }
2552 break;
2553 case 10:
2554 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2555 if (I + 3 >= E)
2556 return insufficient_usr(kind, "<method selector> "
2557 "[0=class method|1=instance method] <class USR>");
2558 if (!isUSR(I[3]))
2559 return not_usr("<class USR>", I[3]);
2560 else {
2561 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002562 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002563 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002564 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2565 }
2566 I += 4;
2567 continue;
2568 }
2569 break;
2570 case 12:
2571 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2572 if (I + 2 >= E)
2573 return insufficient_usr(kind, "<class name> <category name>");
2574 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2575 I += 3;
2576 continue;
2577 }
2578 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2579 if (I + 1 >= E)
2580 return insufficient_usr(kind, "<protocol name>");
2581 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2582 I += 2;
2583 continue;
2584 }
2585 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2586 if (I + 2 >= E)
2587 return insufficient_usr(kind, "<property name> <class USR>");
2588 if (!isUSR(I[2]))
2589 return not_usr("<class USR>", I[2]);
2590 else {
2591 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002592 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002593 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002594 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2595 }
2596 I += 3;
2597 continue;
2598 }
2599 break;
2600 default:
2601 break;
2602 }
2603 break;
2604 }
2605
2606 if (I != E) {
2607 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2608 display_usrs();
2609 return 1;
2610 }
2611 return 0;
2612}
2613
2614int print_usrs_file(const char *file_name) {
2615 char line[2048];
2616 const char *args[128];
2617 unsigned numChars = 0;
2618
2619 FILE *fp = fopen(file_name, "r");
2620 if (!fp) {
2621 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2622 return 1;
2623 }
2624
2625 /* This code is not really all that safe, but it works fine for testing. */
2626 while (!feof(fp)) {
2627 char c = fgetc(fp);
2628 if (c == '\n') {
2629 unsigned i = 0;
2630 const char *s = 0;
2631
2632 if (numChars == 0)
2633 continue;
2634
2635 line[numChars] = '\0';
2636 numChars = 0;
2637
2638 if (line[0] == '/' && line[1] == '/')
2639 continue;
2640
2641 s = strtok(line, " ");
2642 while (s) {
2643 args[i] = s;
2644 ++i;
2645 s = strtok(0, " ");
2646 }
2647 if (print_usrs(&args[0], &args[i]))
2648 return 1;
2649 }
2650 else
2651 line[numChars++] = c;
2652 }
2653
2654 fclose(fp);
2655 return 0;
2656}
2657
2658/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002659/* Command line processing. */
2660/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002661int write_pch_file(const char *filename, int argc, const char *argv[]) {
2662 CXIndex Idx;
2663 CXTranslationUnit TU;
2664 struct CXUnsavedFile *unsaved_files = 0;
2665 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002666 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002667
2668 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2669
2670 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2671 clang_disposeIndex(Idx);
2672 return -1;
2673 }
2674
2675 TU = clang_parseTranslationUnit(Idx, 0,
2676 argv + num_unsaved_files,
2677 argc - num_unsaved_files,
2678 unsaved_files,
2679 num_unsaved_files,
2680 CXTranslationUnit_Incomplete);
2681 if (!TU) {
2682 fprintf(stderr, "Unable to load translation unit!\n");
2683 free_remapped_files(unsaved_files, num_unsaved_files);
2684 clang_disposeIndex(Idx);
2685 return 1;
2686 }
2687
Douglas Gregor39c411f2011-07-06 16:43:36 +00002688 switch (clang_saveTranslationUnit(TU, filename,
2689 clang_defaultSaveOptions(TU))) {
2690 case CXSaveError_None:
2691 break;
2692
2693 case CXSaveError_TranslationErrors:
2694 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2695 filename);
2696 result = 2;
2697 break;
2698
2699 case CXSaveError_InvalidTU:
2700 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2701 filename);
2702 result = 3;
2703 break;
2704
2705 case CXSaveError_Unknown:
2706 default:
2707 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2708 result = 1;
2709 break;
2710 }
2711
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002712 clang_disposeTranslationUnit(TU);
2713 free_remapped_files(unsaved_files, num_unsaved_files);
2714 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002715 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002716}
2717
2718/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002719/* Serialized diagnostics. */
2720/******************************************************************************/
2721
2722static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2723 switch (error) {
2724 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2725 case CXLoadDiag_None: break;
2726 case CXLoadDiag_Unknown: return "Unknown";
2727 case CXLoadDiag_InvalidFile: return "Invalid File";
2728 }
2729 return "None";
2730}
2731
2732static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2733 switch (severity) {
2734 case CXDiagnostic_Note: return "note";
2735 case CXDiagnostic_Error: return "error";
2736 case CXDiagnostic_Fatal: return "fatal";
2737 case CXDiagnostic_Ignored: return "ignored";
2738 case CXDiagnostic_Warning: return "warning";
2739 }
2740 return "unknown";
2741}
2742
2743static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002744 if (indent == 0)
2745 return;
2746 fprintf(stderr, "+");
2747 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002748 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002749 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002750 --indent;
2751 }
2752}
2753
2754static void printLocation(CXSourceLocation L) {
2755 CXFile File;
2756 CXString FileName;
2757 unsigned line, column, offset;
2758
2759 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2760 FileName = clang_getFileName(File);
2761
2762 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2763 clang_disposeString(FileName);
2764}
2765
2766static void printRanges(CXDiagnostic D, unsigned indent) {
2767 unsigned i, n = clang_getDiagnosticNumRanges(D);
2768
2769 for (i = 0; i < n; ++i) {
2770 CXSourceLocation Start, End;
2771 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2772 Start = clang_getRangeStart(SR);
2773 End = clang_getRangeEnd(SR);
2774
2775 printIndent(indent);
2776 fprintf(stderr, "Range: ");
2777 printLocation(Start);
2778 fprintf(stderr, " ");
2779 printLocation(End);
2780 fprintf(stderr, "\n");
2781 }
2782}
2783
2784static void printFixIts(CXDiagnostic D, unsigned indent) {
2785 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002786 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002787 for (i = 0 ; i < n; ++i) {
2788 CXSourceRange ReplacementRange;
2789 CXString text;
2790 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2791
2792 printIndent(indent);
2793 fprintf(stderr, "FIXIT: (");
2794 printLocation(clang_getRangeStart(ReplacementRange));
2795 fprintf(stderr, " - ");
2796 printLocation(clang_getRangeEnd(ReplacementRange));
2797 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2798 clang_disposeString(text);
2799 }
2800}
2801
2802static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002803 unsigned i, n;
2804
Ted Kremenek15322172011-11-10 08:43:12 +00002805 if (!Diags)
2806 return;
2807
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002808 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002809 for (i = 0; i < n; ++i) {
2810 CXSourceLocation DiagLoc;
2811 CXDiagnostic D;
2812 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002813 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002814 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002815 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002816
2817 D = clang_getDiagnosticInSet(Diags, i);
2818 DiagLoc = clang_getDiagnosticLocation(D);
2819 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2820 FileName = clang_getFileName(File);
2821 DiagSpelling = clang_getDiagnosticSpelling(D);
2822
2823 printIndent(indent);
2824
2825 fprintf(stderr, "%s:%d:%d: %s: %s",
2826 clang_getCString(FileName),
2827 line,
2828 column,
2829 getSeverityString(clang_getDiagnosticSeverity(D)),
2830 clang_getCString(DiagSpelling));
2831
2832 DiagOption = clang_getDiagnosticOption(D, 0);
2833 DiagOptionStr = clang_getCString(DiagOption);
2834 if (DiagOptionStr) {
2835 fprintf(stderr, " [%s]", DiagOptionStr);
2836 }
2837
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002838 DiagCat = clang_getDiagnosticCategoryText(D);
2839 DiagCatStr = clang_getCString(DiagCat);
2840 if (DiagCatStr) {
2841 fprintf(stderr, " [%s]", DiagCatStr);
2842 }
2843
Ted Kremenek15322172011-11-10 08:43:12 +00002844 fprintf(stderr, "\n");
2845
2846 printRanges(D, indent);
2847 printFixIts(D, indent);
2848
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002849 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002850 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2851
2852 clang_disposeString(FileName);
2853 clang_disposeString(DiagSpelling);
2854 clang_disposeString(DiagOption);
2855 }
2856}
2857
2858static int read_diagnostics(const char *filename) {
2859 enum CXLoadDiag_Error error;
2860 CXString errorString;
2861 CXDiagnosticSet Diags = 0;
2862
2863 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2864 if (!Diags) {
2865 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2866 getDiagnosticCodeStr(error),
2867 clang_getCString(errorString));
2868 clang_disposeString(errorString);
2869 return 1;
2870 }
2871
2872 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002873 fprintf(stderr, "Number of diagnostics: %d\n",
2874 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002875 clang_disposeDiagnosticSet(Diags);
2876 return 0;
2877}
2878
2879/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002880/* Command line processing. */
2881/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002882
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002883static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002884 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002885 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002886 if (strcmp(s, "-usrs") == 0)
2887 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002888 if (strncmp(s, "-memory-usage", 13) == 0)
2889 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002890 return NULL;
2891}
2892
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002893static void print_usage(void) {
2894 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002895 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002896 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002897 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002898 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002899 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002900 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002901 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002902 "[FileCheck prefix]\n");
2903 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002904 " c-index-test -test-load-tu <AST file> <symbol filter> "
2905 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002906 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2907 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002908 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002909 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002910 " c-index-test -test-load-source-memory-usage "
2911 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002912 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2913 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002914 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002915 " c-index-test -test-load-source-usrs-memory-usage "
2916 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002917 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2918 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002919 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002920 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002921 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002922 " c-index-test -test-print-typekind {<args>}*\n"
2923 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002924 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002925 " c-index-test -write-pch <file> <compiler arguments>\n");
2926 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002927 " c-index-test -compilation-db [lookup <filename>] database\n");
2928 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00002929 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002930 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002931 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002932 " all - load all symbols, including those from PCH\n"
2933 " local - load all symbols except those in PCH\n"
2934 " category - only load ObjC categories (non-PCH)\n"
2935 " interface - only load ObjC interfaces (non-PCH)\n"
2936 " protocol - only load ObjC protocols (non-PCH)\n"
2937 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002938 " typedef - only load typdefs (non-PCH)\n"
2939 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002940}
2941
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002942/***/
2943
2944int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002945 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002946 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2947 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002948 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002949 return perform_code_completion(argc, argv, 0);
2950 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2951 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002952 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2953 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002954 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2955 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002956 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2957 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002958 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2959 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002960 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002961 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002962 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002963 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2964 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002965 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002966 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2967 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2968 if (I) {
2969 int trials = atoi(argv[2]);
2970 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2971 NULL);
2972 }
2973 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002974 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002975 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002976
2977 PostVisitTU postVisit = 0;
2978 if (strstr(argv[1], "-memory-usage"))
2979 postVisit = PrintMemoryUsage;
2980
Ted Kremenek7d405622010-01-12 23:34:26 +00002981 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002982 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2983 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002984 }
2985 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002986 return perform_file_scan(argv[2], argv[3],
2987 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002988 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2989 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002990 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2991 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2992 PrintInclusionStack);
2993 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2994 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2995 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002996 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2997 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2998 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002999 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
3000 return perform_test_load_source(argc - 2, argv + 2, "all",
3001 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003002 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3003 if (argc > 2)
3004 return print_usrs(argv + 2, argv + argc);
3005 else {
3006 display_usrs();
3007 return 1;
3008 }
3009 }
3010 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3011 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003012 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3013 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003014 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3015 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3016
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003017 print_usage();
3018 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003019}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003020
3021/***/
3022
3023/* We intentionally run in a separate thread to ensure we at least minimal
3024 * testing of a multithreaded environment (for example, having a reduced stack
3025 * size). */
3026
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003027typedef struct thread_info {
3028 int argc;
3029 const char **argv;
3030 int result;
3031} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003032void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003033 thread_info *client_data = client_data_v;
3034 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003035#ifdef __CYGWIN__
3036 fflush(stdout); /* stdout is not flushed on Cygwin. */
3037#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003038}
3039
3040int main(int argc, const char **argv) {
3041 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003042
Douglas Gregor61605982010-10-27 16:00:01 +00003043 if (getenv("CINDEXTEST_NOTHREADS"))
3044 return cindextest_main(argc, argv);
3045
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003046 client_data.argc = argc;
3047 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003048 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003049 return client_data.result;
3050}