blob: 7f60925984188805bc1de7b702aee97eaf6a8634 [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)
39 *base1 = 0
40 else if (base2)
41 *base2 = 0
42
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;
Douglas Gregor44c181a2010-07-23 00:33:23 +000062
63 return options;
64}
65
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000066static int checkForErrors(CXTranslationUnit TU);
67
Daniel Dunbar51b058c2010-02-14 08:32:24 +000068static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
69 unsigned end_line, unsigned end_column) {
70 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000071 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000072}
73
Ted Kremenek1c6da172009-11-17 19:37:36 +000074static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
75 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000076
Douglas Gregora88084b2010-02-18 18:08:43 +000077 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000078 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000079 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
80 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000081 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000082 return 1;
83}
84
Douglas Gregor4db64a42010-01-23 00:14:00 +000085void free_remapped_files(struct CXUnsavedFile *unsaved_files,
86 int num_unsaved_files) {
87 int i;
88 for (i = 0; i != num_unsaved_files; ++i) {
89 free((char *)unsaved_files[i].Filename);
90 free((char *)unsaved_files[i].Contents);
91 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000092 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000093}
94
95int parse_remapped_files(int argc, const char **argv, int start_arg,
96 struct CXUnsavedFile **unsaved_files,
97 int *num_unsaved_files) {
98 int i;
99 int arg;
100 int prefix_len = strlen("-remap-file=");
101 *unsaved_files = 0;
102 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000103
Douglas Gregor4db64a42010-01-23 00:14:00 +0000104 /* Count the number of remapped files. */
105 for (arg = start_arg; arg < argc; ++arg) {
106 if (strncmp(argv[arg], "-remap-file=", prefix_len))
107 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000108
Douglas Gregor4db64a42010-01-23 00:14:00 +0000109 ++*num_unsaved_files;
110 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000111
Douglas Gregor4db64a42010-01-23 00:14:00 +0000112 if (*num_unsaved_files == 0)
113 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000114
Douglas Gregor4db64a42010-01-23 00:14:00 +0000115 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +0000116 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
117 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000118 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
119 struct CXUnsavedFile *unsaved = *unsaved_files + i;
120 const char *arg_string = argv[arg] + prefix_len;
121 int filename_len;
122 char *filename;
123 char *contents;
124 FILE *to_file;
125 const char *semi = strchr(arg_string, ';');
126 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000127 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000128 "error: -remap-file=from;to argument is missing semicolon\n");
129 free_remapped_files(*unsaved_files, i);
130 *unsaved_files = 0;
131 *num_unsaved_files = 0;
132 return -1;
133 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000134
Douglas Gregor4db64a42010-01-23 00:14:00 +0000135 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000136 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000137 if (!to_file) {
138 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
139 semi + 1);
140 free_remapped_files(*unsaved_files, i);
141 *unsaved_files = 0;
142 *num_unsaved_files = 0;
143 return -1;
144 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000145
Douglas Gregor4db64a42010-01-23 00:14:00 +0000146 /* Determine the length of the file we're remapping to. */
147 fseek(to_file, 0, SEEK_END);
148 unsaved->Length = ftell(to_file);
149 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000150
Douglas Gregor4db64a42010-01-23 00:14:00 +0000151 /* Read the contents of the file we're remapping to. */
152 contents = (char *)malloc(unsaved->Length + 1);
153 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
154 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
155 (feof(to_file) ? "EOF" : "error"), semi + 1);
156 fclose(to_file);
157 free_remapped_files(*unsaved_files, i);
158 *unsaved_files = 0;
159 *num_unsaved_files = 0;
160 return -1;
161 }
162 contents[unsaved->Length] = 0;
163 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000164
Douglas Gregor4db64a42010-01-23 00:14:00 +0000165 /* Close the file. */
166 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000167
Douglas Gregor4db64a42010-01-23 00:14:00 +0000168 /* Copy the file name that we're remapping from. */
169 filename_len = semi - arg_string;
170 filename = (char *)malloc(filename_len + 1);
171 memcpy(filename, arg_string, filename_len);
172 filename[filename_len] = 0;
173 unsaved->Filename = filename;
174 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000175
Douglas Gregor4db64a42010-01-23 00:14:00 +0000176 return 0;
177}
178
Ted Kremenek0d435192009-11-17 18:13:31 +0000179/******************************************************************************/
180/* Pretty-printing. */
181/******************************************************************************/
182
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000183static void PrintCString(const char *Prefix, const char *CStr) {
184 printf(" %s=[", Prefix);
185 if (CStr != NULL && CStr[0] != '\0') {
186 for ( ; *CStr; ++CStr) {
187 const char C = *CStr;
188 switch (C) {
189 case '\n': printf("\\n"); break;
190 case '\r': printf("\\r"); break;
191 case '\t': printf("\\t"); break;
192 case '\v': printf("\\v"); break;
193 case '\f': printf("\\f"); break;
194 default: putchar(C); break;
195 }
196 }
197 }
198 printf("]");
199}
200
Douglas Gregor430d7a12011-07-25 17:48:11 +0000201static void PrintRange(CXSourceRange R, const char *str) {
202 CXFile begin_file, end_file;
203 unsigned begin_line, begin_column, end_line, end_column;
204
205 clang_getSpellingLocation(clang_getRangeStart(R),
206 &begin_file, &begin_line, &begin_column, 0);
207 clang_getSpellingLocation(clang_getRangeEnd(R),
208 &end_file, &end_line, &end_column, 0);
209 if (!begin_file || !end_file)
210 return;
211
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000212 if (str)
213 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000214 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
215}
216
Douglas Gregor358559d2010-10-02 22:49:11 +0000217int want_display_name = 0;
218
Douglas Gregorcc889662012-05-08 00:14:45 +0000219static void printVersion(const char *Prefix, CXVersion Version) {
220 if (Version.Major < 0)
221 return;
222 printf("%s%d", Prefix, Version.Major);
223
224 if (Version.Minor < 0)
225 return;
226 printf(".%d", Version.Minor);
227
228 if (Version.Subminor < 0)
229 return;
230 printf(".%d", Version.Subminor);
231}
232
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000233static void PrintCursor(CXCursor Cursor) {
234 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000235 if (clang_isInvalid(Cursor.kind)) {
236 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
237 printf("Invalid Cursor => %s", clang_getCString(ks));
238 clang_disposeString(ks);
239 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000240 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000241 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000242 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000243 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000244 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000245 CXCursor *overridden;
246 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000247 unsigned RefNameRangeNr;
248 CXSourceRange CursorExtent;
249 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000250 int AlwaysUnavailable;
251 int AlwaysDeprecated;
252 CXString UnavailableMessage;
253 CXString DeprecatedMessage;
254 CXPlatformAvailability PlatformAvailability[2];
255 int NumPlatformAvailability;
256 int I;
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000257 CXString RawComment;
258 const char *RawCommentCString;
259 CXString BriefComment;
260 const char *BriefCommentCString;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000261
Ted Kremeneke68fff62010-02-17 00:41:32 +0000262 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000263 string = want_display_name? clang_getCursorDisplayName(Cursor)
264 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000265 printf("%s=%s", clang_getCString(ks),
266 clang_getCString(string));
267 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000268 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000269
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000270 Referenced = clang_getCursorReferenced(Cursor);
271 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000272 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
273 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
274 printf("[");
275 for (I = 0; I != N; ++I) {
276 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000277 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000278 if (I)
279 printf(", ");
280
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000281 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000282 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000283 printf("%d:%d", line, column);
284 }
285 printf("]");
286 } else {
287 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000288 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000289 printf(":%d:%d", line, column);
290 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000291 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000292
293 if (clang_isCursorDefinition(Cursor))
294 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000295
296 switch (clang_getCursorAvailability(Cursor)) {
297 case CXAvailability_Available:
298 break;
299
300 case CXAvailability_Deprecated:
301 printf(" (deprecated)");
302 break;
303
304 case CXAvailability_NotAvailable:
305 printf(" (unavailable)");
306 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000307
308 case CXAvailability_NotAccessible:
309 printf(" (inaccessible)");
310 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000311 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000312
Douglas Gregorcc889662012-05-08 00:14:45 +0000313 NumPlatformAvailability
314 = clang_getCursorPlatformAvailability(Cursor,
315 &AlwaysDeprecated,
316 &DeprecatedMessage,
317 &AlwaysUnavailable,
318 &UnavailableMessage,
319 PlatformAvailability, 2);
320 if (AlwaysUnavailable) {
321 printf(" (always unavailable: \"%s\")",
322 clang_getCString(UnavailableMessage));
323 } else if (AlwaysDeprecated) {
324 printf(" (always deprecated: \"%s\")",
325 clang_getCString(DeprecatedMessage));
326 } else {
327 for (I = 0; I != NumPlatformAvailability; ++I) {
328 if (I >= 2)
329 break;
330
331 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
332 if (PlatformAvailability[I].Unavailable)
333 printf(", unavailable");
334 else {
335 printVersion(", introduced=", PlatformAvailability[I].Introduced);
336 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
337 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
338 }
339 if (clang_getCString(PlatformAvailability[I].Message)[0])
340 printf(", message=\"%s\"",
341 clang_getCString(PlatformAvailability[I].Message));
342 printf(")");
343 }
344 }
345 for (I = 0; I != NumPlatformAvailability; ++I) {
346 if (I >= 2)
347 break;
348 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
349 }
350
351 clang_disposeString(DeprecatedMessage);
352 clang_disposeString(UnavailableMessage);
353
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000354 if (clang_CXXMethod_isStatic(Cursor))
355 printf(" (static)");
356 if (clang_CXXMethod_isVirtual(Cursor))
357 printf(" (virtual)");
358
Ted Kremenek95f33552010-08-26 01:42:22 +0000359 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
360 CXType T =
361 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
362 CXString S = clang_getTypeKindSpelling(T.kind);
363 printf(" [IBOutletCollection=%s]", clang_getCString(S));
364 clang_disposeString(S);
365 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000366
367 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
368 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
369 unsigned isVirtual = clang_isVirtualBase(Cursor);
370 const char *accessStr = 0;
371
372 switch (access) {
373 case CX_CXXInvalidAccessSpecifier:
374 accessStr = "invalid"; break;
375 case CX_CXXPublic:
376 accessStr = "public"; break;
377 case CX_CXXProtected:
378 accessStr = "protected"; break;
379 case CX_CXXPrivate:
380 accessStr = "private"; break;
381 }
382
383 printf(" [access=%s isVirtual=%s]", accessStr,
384 isVirtual ? "true" : "false");
385 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000386
387 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
388 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
389 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
390 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000391 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000392 printf(" [Specialization of %s:%d:%d]",
393 clang_getCString(Name), line, column);
394 clang_disposeString(Name);
395 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000396
397 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
398 if (num_overridden) {
399 unsigned I;
400 printf(" [Overrides ");
401 for (I = 0; I != num_overridden; ++I) {
402 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000403 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000404 if (I)
405 printf(", ");
406 printf("@%d:%d", line, column);
407 }
408 printf("]");
409 clang_disposeOverriddenCursors(overridden);
410 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000411
412 if (Cursor.kind == CXCursor_InclusionDirective) {
413 CXFile File = clang_getIncludedFile(Cursor);
414 CXString Included = clang_getFileName(File);
415 printf(" (%s)", clang_getCString(Included));
416 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000417
418 if (clang_isFileMultipleIncludeGuarded(TU, File))
419 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000420 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000421
422 CursorExtent = clang_getCursorExtent(Cursor);
423 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
424 CXNameRange_WantQualifier
425 | CXNameRange_WantSinglePiece
426 | CXNameRange_WantTemplateArgs,
427 0);
428 if (!clang_equalRanges(CursorExtent, RefNameRange))
429 PrintRange(RefNameRange, "SingleRefName");
430
431 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
432 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
433 CXNameRange_WantQualifier
434 | CXNameRange_WantTemplateArgs,
435 RefNameRangeNr);
436 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
437 break;
438 if (!clang_equalRanges(CursorExtent, RefNameRange))
439 PrintRange(RefNameRange, "RefName");
440 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000441
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000442 RawComment = clang_Cursor_getRawCommentText(Cursor);
443 RawCommentCString = clang_getCString(RawComment);
444 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
445 PrintCString("RawComment", RawCommentCString);
446 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000447
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000448 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
449 BriefCommentCString = clang_getCString(BriefComment);
450 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
451 PrintCString("BriefComment", BriefCommentCString);
452 clang_disposeString(BriefComment);
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000453 }
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000454 clang_disposeString(RawComment);
Steve Naroff699a07d2009-09-25 21:32:34 +0000455 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000456}
Steve Naroff89922f82009-08-31 00:59:03 +0000457
Ted Kremeneke68fff62010-02-17 00:41:32 +0000458static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000459 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000460 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000461 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000462 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000463 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000464 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000465 clang_disposeString(source);
466 return "<invalid loc>";
467 }
468 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000469 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000470 clang_disposeString(source);
471 return b;
472 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000473}
474
Ted Kremenek0d435192009-11-17 18:13:31 +0000475/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000476/* Callbacks. */
477/******************************************************************************/
478
479typedef void (*PostVisitTU)(CXTranslationUnit);
480
Douglas Gregora88084b2010-02-18 18:08:43 +0000481void PrintDiagnostic(CXDiagnostic Diagnostic) {
482 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000483 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000484 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000485 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000486 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
487 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000488 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000489
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000490 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000491 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492
Douglas Gregor274f1902010-02-22 23:17:23 +0000493 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
494 fprintf(stderr, "%s\n", clang_getCString(Msg));
495 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000496
Douglas Gregora9b06d42010-11-09 06:24:54 +0000497 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
498 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000499 if (!file)
500 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000501
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000502 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000503 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000504 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000505 CXSourceRange range;
506 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
507 CXSourceLocation start = clang_getRangeStart(range);
508 CXSourceLocation end = clang_getRangeEnd(range);
509 unsigned start_line, start_column, end_line, end_column;
510 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000511 clang_getSpellingLocation(start, &start_file, &start_line,
512 &start_column, 0);
513 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000514 if (clang_equalLocations(start, end)) {
515 /* Insertion. */
516 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000517 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000518 clang_getCString(insertion_text), start_line, start_column);
519 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
520 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000521 if (start_file == file && end_file == file) {
522 fprintf(out, "FIX-IT: Remove ");
523 PrintExtent(out, start_line, start_column, end_line, end_column);
524 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000525 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000526 } else {
527 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000528 if (start_file == end_file) {
529 fprintf(out, "FIX-IT: Replace ");
530 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000531 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000532 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000533 break;
534 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000535 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000536 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000537}
538
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000539void PrintDiagnosticSet(CXDiagnosticSet Set) {
540 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
541 for ( ; i != n ; ++i) {
542 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
543 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000544 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000545 if (ChildDiags)
546 PrintDiagnosticSet(ChildDiags);
547 }
548}
549
550void PrintDiagnostics(CXTranslationUnit TU) {
551 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
552 PrintDiagnosticSet(TUSet);
553 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000554}
555
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000556void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000557 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000558 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000559 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000560 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000561 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000562 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000563 unsigned long amount = usage.entries[i].amount;
564 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000565 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000566 ((double) amount)/(1024*1024));
567 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000568 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000569 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000570 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000571}
572
Ted Kremenekce2ae882010-01-26 17:59:48 +0000573/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000574/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000575/******************************************************************************/
576
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000577static const char *FileCheckPrefix = "CHECK";
578
Douglas Gregora7bde202010-01-19 00:34:46 +0000579static void PrintCursorExtent(CXCursor C) {
580 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000581 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000582}
583
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000584/* Data used by all of the visitors. */
585typedef struct {
586 CXTranslationUnit TU;
587 enum CXCursorKind *Filter;
588} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000589
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000590
Ted Kremeneke68fff62010-02-17 00:41:32 +0000591enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000592 CXCursor Parent,
593 CXClientData ClientData) {
594 VisitorData *Data = (VisitorData *)ClientData;
595 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000596 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000597 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000598 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000599 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000600 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000601 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000602 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000603 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000604 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000605 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000606
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000607 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000608}
Steve Naroff50398192009-08-28 15:28:48 +0000609
Ted Kremeneke68fff62010-02-17 00:41:32 +0000610static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000611 CXCursor Parent,
612 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000613 const char *startBuf, *endBuf;
614 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
615 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000616 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000617
Douglas Gregorb6998662010-01-19 19:34:47 +0000618 if (Cursor.kind != CXCursor_FunctionDecl ||
619 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000620 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000621
622 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
623 &startLine, &startColumn,
624 &endLine, &endColumn);
625 /* Probe the entire body, looking for both decls and refs. */
626 curLine = startLine;
627 curColumn = startColumn;
628
629 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000630 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000631 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000632 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000633
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000634 if (*startBuf == '\n') {
635 startBuf++;
636 curLine++;
637 curColumn = 1;
638 } else if (*startBuf != '\t')
639 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000640
Douglas Gregor98258af2010-01-18 22:46:11 +0000641 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000642 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000643
Douglas Gregor1db19de2010-01-19 21:36:55 +0000644 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000645 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000646 CXSourceLocation RefLoc
647 = clang_getLocation(Data->TU, file, curLine, curColumn);
648 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000649 if (Ref.kind == CXCursor_NoDeclFound) {
650 /* Nothing found here; that's fine. */
651 } else if (Ref.kind != CXCursor_FunctionDecl) {
652 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
653 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000654 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000655 printf("\n");
656 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000657 }
Ted Kremenek74844072010-02-17 00:41:20 +0000658 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000659 startBuf++;
660 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000661
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000662 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000663}
664
Ted Kremenek7d405622010-01-12 23:34:26 +0000665/******************************************************************************/
666/* USR testing. */
667/******************************************************************************/
668
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000669enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
670 CXClientData ClientData) {
671 VisitorData *Data = (VisitorData *)ClientData;
672 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000673 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000674 const char *cstr = clang_getCString(USR);
675 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000676 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000677 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000678 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000679 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
680
Douglas Gregora7bde202010-01-19 00:34:46 +0000681 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000682 printf("\n");
683 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000684
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000685 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000686 }
687
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000688 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000689}
690
691/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000692/* Inclusion stack testing. */
693/******************************************************************************/
694
695void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
696 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000697
Ted Kremenek16b55a72010-01-26 19:31:51 +0000698 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000699 CXString fname;
700
701 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000702 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000703 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000704
Ted Kremenek16b55a72010-01-26 19:31:51 +0000705 for (i = 0; i < includeStackLen; ++i) {
706 CXFile includingFile;
707 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000708 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
709 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000710 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000711 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000712 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000713 }
714 printf("\n");
715}
716
717void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000718 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000719}
720
721/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000722/* Linkage testing. */
723/******************************************************************************/
724
725static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
726 CXClientData d) {
727 const char *linkage = 0;
728
729 if (clang_isInvalid(clang_getCursorKind(cursor)))
730 return CXChildVisit_Recurse;
731
732 switch (clang_getCursorLinkage(cursor)) {
733 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000734 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
735 case CXLinkage_Internal: linkage = "Internal"; break;
736 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
737 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000738 }
739
740 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000741 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000742 printf("linkage=%s\n", linkage);
743 }
744
745 return CXChildVisit_Recurse;
746}
747
748/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000749/* Typekind testing. */
750/******************************************************************************/
751
752static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
753 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000754 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
755 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000756 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000757 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000758 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000759 if (clang_isConstQualifiedType(T))
760 printf(" const");
761 if (clang_isVolatileQualifiedType(T))
762 printf(" volatile");
763 if (clang_isRestrictQualifiedType(T))
764 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000765 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000766 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000767 {
768 CXType CT = clang_getCanonicalType(T);
769 if (!clang_equalTypes(T, CT)) {
770 CXString CS = clang_getTypeKindSpelling(CT.kind);
771 printf(" [canonical=%s]", clang_getCString(CS));
772 clang_disposeString(CS);
773 }
774 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000775 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000776 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000777 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000778 if (RT.kind != CXType_Invalid) {
779 CXString RS = clang_getTypeKindSpelling(RT.kind);
780 printf(" [result=%s]", clang_getCString(RS));
781 clang_disposeString(RS);
782 }
783 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000784 /* Print the argument types if they exist. */
785 {
786 int numArgs = clang_Cursor_getNumArguments(cursor);
787 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000788 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000789 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000790 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000791 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
792 if (T.kind != CXType_Invalid) {
793 CXString S = clang_getTypeKindSpelling(T.kind);
794 printf(" %s", clang_getCString(S));
795 clang_disposeString(S);
796 }
797 }
798 printf("]");
799 }
800 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000801 /* Print if this is a non-POD type. */
802 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000803
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000804 printf("\n");
805 }
806 return CXChildVisit_Recurse;
807}
808
809
810/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000811/* Loading ASTs/source. */
812/******************************************************************************/
813
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000814static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000815 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000816 CXCursorVisitor Visitor,
817 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000818
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000819 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000820 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000821
822 if (Visitor) {
823 enum CXCursorKind K = CXCursor_NotImplemented;
824 enum CXCursorKind *ck = &K;
825 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000826
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000827 /* Perform some simple filtering. */
828 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000829 else if (!strcmp(filter, "all-display") ||
830 !strcmp(filter, "local-display")) {
831 ck = NULL;
832 want_display_name = 1;
833 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000834 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000835 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
836 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
837 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
838 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
839 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
840 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
841 else {
842 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
843 return 1;
844 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000845
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000846 Data.TU = TU;
847 Data.Filter = ck;
848 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000849 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000850
Ted Kremenekce2ae882010-01-26 17:59:48 +0000851 if (PV)
852 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000853
Douglas Gregora88084b2010-02-18 18:08:43 +0000854 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000855 if (checkForErrors(TU) != 0) {
856 clang_disposeTranslationUnit(TU);
857 return -1;
858 }
859
Ted Kremenek0d435192009-11-17 18:13:31 +0000860 clang_disposeTranslationUnit(TU);
861 return 0;
862}
863
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000864int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000865 const char *prefix, CXCursorVisitor Visitor,
866 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000867 CXIndex Idx;
868 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000869 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000870 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000871 !strcmp(filter, "local") ? 1 : 0,
872 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000873
Ted Kremenek020a0952010-02-11 07:41:25 +0000874 if (!CreateTranslationUnit(Idx, file, &TU)) {
875 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000876 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000877 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000878
Ted Kremenek020a0952010-02-11 07:41:25 +0000879 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
880 clang_disposeIndex(Idx);
881 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000882}
883
Ted Kremenekce2ae882010-01-26 17:59:48 +0000884int perform_test_load_source(int argc, const char **argv,
885 const char *filter, CXCursorVisitor Visitor,
886 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000887 CXIndex Idx;
888 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000889 struct CXUnsavedFile *unsaved_files = 0;
890 int num_unsaved_files = 0;
891 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000892
Daniel Dunbarada487d2009-12-01 02:03:10 +0000893 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000894 (!strcmp(filter, "local") ||
895 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000896 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000897
Ted Kremenek020a0952010-02-11 07:41:25 +0000898 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
899 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000900 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000901 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000902
Douglas Gregordca8ee82011-05-06 16:33:08 +0000903 TU = clang_parseTranslationUnit(Idx, 0,
904 argv + num_unsaved_files,
905 argc - num_unsaved_files,
906 unsaved_files, num_unsaved_files,
907 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000908 if (!TU) {
909 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000910 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000911 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000912 return 1;
913 }
914
Ted Kremenekce2ae882010-01-26 17:59:48 +0000915 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000916 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000917 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000918 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000919}
920
Douglas Gregorabc563f2010-07-19 21:46:24 +0000921int perform_test_reparse_source(int argc, const char **argv, int trials,
922 const char *filter, CXCursorVisitor Visitor,
923 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000924 CXIndex Idx;
925 CXTranslationUnit TU;
926 struct CXUnsavedFile *unsaved_files = 0;
927 int num_unsaved_files = 0;
928 int result;
929 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000930 int remap_after_trial = 0;
931 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000932
933 Idx = clang_createIndex(/* excludeDeclsFromPCH */
934 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000935 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000936
Douglas Gregorabc563f2010-07-19 21:46:24 +0000937 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
938 clang_disposeIndex(Idx);
939 return -1;
940 }
941
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000942 /* Load the initial translation unit -- we do this without honoring remapped
943 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000944 TU = clang_parseTranslationUnit(Idx, 0,
945 argv + num_unsaved_files,
946 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000947 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000948 if (!TU) {
949 fprintf(stderr, "Unable to load translation unit!\n");
950 free_remapped_files(unsaved_files, num_unsaved_files);
951 clang_disposeIndex(Idx);
952 return 1;
953 }
954
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000955 if (checkForErrors(TU) != 0)
956 return -1;
957
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000958 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
959 remap_after_trial =
960 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
961 }
962
Douglas Gregorabc563f2010-07-19 21:46:24 +0000963 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000964 if (clang_reparseTranslationUnit(TU,
965 trial >= remap_after_trial ? num_unsaved_files : 0,
966 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000967 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000968 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000969 clang_disposeTranslationUnit(TU);
970 free_remapped_files(unsaved_files, num_unsaved_files);
971 clang_disposeIndex(Idx);
972 return -1;
973 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000974
975 if (checkForErrors(TU) != 0)
976 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000977 }
978
979 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000980
Douglas Gregorabc563f2010-07-19 21:46:24 +0000981 free_remapped_files(unsaved_files, num_unsaved_files);
982 clang_disposeIndex(Idx);
983 return result;
984}
985
Ted Kremenek0d435192009-11-17 18:13:31 +0000986/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000987/* Logic for testing clang_getCursor(). */
988/******************************************************************************/
989
Douglas Gregordd3e5542011-05-04 00:14:37 +0000990static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000991 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000992 unsigned end_line, unsigned end_col,
993 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000994 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000995 if (prefix)
996 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000997 PrintExtent(stdout, start_line, start_col, end_line, end_col);
998 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000999 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001000 printf("\n");
1001}
1002
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001003static int perform_file_scan(const char *ast_file, const char *source_file,
1004 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001005 CXIndex Idx;
1006 CXTranslationUnit TU;
1007 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001008 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001009 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001010 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001011 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001012
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001013 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1014 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001015 fprintf(stderr, "Could not create Index\n");
1016 return 1;
1017 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001018
Ted Kremenek1c6da172009-11-17 19:37:36 +00001019 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1020 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001021
Ted Kremenek1c6da172009-11-17 19:37:36 +00001022 if ((fp = fopen(source_file, "r")) == NULL) {
1023 fprintf(stderr, "Could not open '%s'\n", source_file);
1024 return 1;
1025 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001026
Douglas Gregorb9790342010-01-22 21:44:22 +00001027 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001028 for (;;) {
1029 CXCursor cursor;
1030 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001031
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001032 if (c == '\n') {
1033 ++line;
1034 col = 1;
1035 } else
1036 ++col;
1037
1038 /* Check the cursor at this position, and dump the previous one if we have
1039 * found something new.
1040 */
1041 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1042 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1043 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001044 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001045 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001046 start_line = line;
1047 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001048 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001049 if (c == EOF)
1050 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001051
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001052 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001053 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001054
Ted Kremenek1c6da172009-11-17 19:37:36 +00001055 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001056 clang_disposeTranslationUnit(TU);
1057 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001058 return 0;
1059}
1060
1061/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001062/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001063/******************************************************************************/
1064
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001065/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1066 on failure. If successful, the pointer *filename will contain newly-allocated
1067 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001068int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001069 unsigned *column, unsigned *second_line,
1070 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001071 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001072 const char *last_colon = strrchr(input, ':');
1073 unsigned values[4], i;
1074 unsigned num_values = (second_line && second_column)? 4 : 2;
1075
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001076 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001077 if (!last_colon || last_colon == input) {
1078 if (num_values == 4)
1079 fprintf(stderr, "could not parse filename:line:column:line:column in "
1080 "'%s'\n", input);
1081 else
1082 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001083 return 1;
1084 }
1085
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001086 for (i = 0; i != num_values; ++i) {
1087 const char *prev_colon;
1088
1089 /* Parse the next line or column. */
1090 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1091 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001092 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001093 (i % 2 ? "column" : "line"), input);
1094 return 1;
1095 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001096
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001097 if (i + 1 == num_values)
1098 break;
1099
1100 /* Find the previous colon. */
1101 prev_colon = last_colon - 1;
1102 while (prev_colon != input && *prev_colon != ':')
1103 --prev_colon;
1104 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001105 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001106 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001107 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001108 }
1109
1110 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001111 }
1112
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001113 *line = values[0];
1114 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001115
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001116 if (second_line && second_column) {
1117 *second_line = values[2];
1118 *second_column = values[3];
1119 }
1120
Douglas Gregor88d23952009-11-09 18:19:57 +00001121 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001122 *filename = (char*)malloc(last_colon - input + 1);
1123 memcpy(*filename, input, last_colon - input);
1124 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001125 return 0;
1126}
1127
1128const char *
1129clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1130 switch (Kind) {
1131 case CXCompletionChunk_Optional: return "Optional";
1132 case CXCompletionChunk_TypedText: return "TypedText";
1133 case CXCompletionChunk_Text: return "Text";
1134 case CXCompletionChunk_Placeholder: return "Placeholder";
1135 case CXCompletionChunk_Informative: return "Informative";
1136 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1137 case CXCompletionChunk_LeftParen: return "LeftParen";
1138 case CXCompletionChunk_RightParen: return "RightParen";
1139 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1140 case CXCompletionChunk_RightBracket: return "RightBracket";
1141 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1142 case CXCompletionChunk_RightBrace: return "RightBrace";
1143 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1144 case CXCompletionChunk_RightAngle: return "RightAngle";
1145 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001146 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001147 case CXCompletionChunk_Colon: return "Colon";
1148 case CXCompletionChunk_SemiColon: return "SemiColon";
1149 case CXCompletionChunk_Equal: return "Equal";
1150 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1151 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001152 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001153
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001154 return "Unknown";
1155}
1156
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001157static int checkForErrors(CXTranslationUnit TU) {
1158 unsigned Num, i;
1159 CXDiagnostic Diag;
1160 CXString DiagStr;
1161
1162 if (!getenv("CINDEXTEST_FAILONERROR"))
1163 return 0;
1164
1165 Num = clang_getNumDiagnostics(TU);
1166 for (i = 0; i != Num; ++i) {
1167 Diag = clang_getDiagnostic(TU, i);
1168 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1169 DiagStr = clang_formatDiagnostic(Diag,
1170 clang_defaultDiagnosticDisplayOptions());
1171 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1172 clang_disposeString(DiagStr);
1173 clang_disposeDiagnostic(Diag);
1174 return -1;
1175 }
1176 clang_disposeDiagnostic(Diag);
1177 }
1178
1179 return 0;
1180}
1181
Douglas Gregor3ac73852009-11-09 16:04:45 +00001182void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001183 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001184
Douglas Gregor3ac73852009-11-09 16:04:45 +00001185 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001186 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001187 CXString text;
1188 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001189 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001190 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001191
Douglas Gregor3ac73852009-11-09 16:04:45 +00001192 if (Kind == CXCompletionChunk_Optional) {
1193 fprintf(file, "{Optional ");
1194 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001195 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001196 file);
1197 fprintf(file, "}");
1198 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001199 }
1200
1201 if (Kind == CXCompletionChunk_VerticalSpace) {
1202 fprintf(file, "{VerticalSpace }");
1203 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001204 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001205
Douglas Gregord5a20892009-11-09 17:05:28 +00001206 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001207 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001208 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001209 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001210 cstr ? cstr : "");
1211 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001212 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001213
Douglas Gregor3ac73852009-11-09 16:04:45 +00001214}
1215
1216void print_completion_result(CXCompletionResult *completion_result,
1217 CXClientData client_data) {
1218 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001219 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001220 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001221 enum CXCursorKind ParentKind;
1222 CXString ParentName;
1223
Ted Kremeneke68fff62010-02-17 00:41:32 +00001224 fprintf(file, "%s:", clang_getCString(ks));
1225 clang_disposeString(ks);
1226
Douglas Gregor3ac73852009-11-09 16:04:45 +00001227 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001228 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001229 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001230 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1231 case CXAvailability_Available:
1232 break;
1233
1234 case CXAvailability_Deprecated:
1235 fprintf(file, " (deprecated)");
1236 break;
1237
1238 case CXAvailability_NotAvailable:
1239 fprintf(file, " (unavailable)");
1240 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001241
1242 case CXAvailability_NotAccessible:
1243 fprintf(file, " (inaccessible)");
1244 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001245 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001246
1247 annotationCount = clang_getCompletionNumAnnotations(
1248 completion_result->CompletionString);
1249 if (annotationCount) {
1250 unsigned i;
1251 fprintf(file, " (");
1252 for (i = 0; i < annotationCount; ++i) {
1253 if (i != 0)
1254 fprintf(file, ", ");
1255 fprintf(file, "\"%s\"",
1256 clang_getCString(clang_getCompletionAnnotation(
1257 completion_result->CompletionString, i)));
1258 }
1259 fprintf(file, ")");
1260 }
1261
Douglas Gregorba103062012-03-27 23:34:16 +00001262 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1263 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1264 &ParentKind);
1265 if (ParentKind != CXCursor_NotImplemented) {
1266 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1267 fprintf(file, " (parent: %s '%s')",
1268 clang_getCString(KindSpelling),
1269 clang_getCString(ParentName));
1270 clang_disposeString(KindSpelling);
1271 }
1272 clang_disposeString(ParentName);
1273 }
1274
Douglas Gregor58ddb602010-08-23 23:00:57 +00001275 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001276}
1277
Douglas Gregor3da626b2011-07-07 16:03:39 +00001278void print_completion_contexts(unsigned long long contexts, FILE *file) {
1279 fprintf(file, "Completion contexts:\n");
1280 if (contexts == CXCompletionContext_Unknown) {
1281 fprintf(file, "Unknown\n");
1282 }
1283 if (contexts & CXCompletionContext_AnyType) {
1284 fprintf(file, "Any type\n");
1285 }
1286 if (contexts & CXCompletionContext_AnyValue) {
1287 fprintf(file, "Any value\n");
1288 }
1289 if (contexts & CXCompletionContext_ObjCObjectValue) {
1290 fprintf(file, "Objective-C object value\n");
1291 }
1292 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1293 fprintf(file, "Objective-C selector value\n");
1294 }
1295 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1296 fprintf(file, "C++ class type value\n");
1297 }
1298 if (contexts & CXCompletionContext_DotMemberAccess) {
1299 fprintf(file, "Dot member access\n");
1300 }
1301 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1302 fprintf(file, "Arrow member access\n");
1303 }
1304 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1305 fprintf(file, "Objective-C property access\n");
1306 }
1307 if (contexts & CXCompletionContext_EnumTag) {
1308 fprintf(file, "Enum tag\n");
1309 }
1310 if (contexts & CXCompletionContext_UnionTag) {
1311 fprintf(file, "Union tag\n");
1312 }
1313 if (contexts & CXCompletionContext_StructTag) {
1314 fprintf(file, "Struct tag\n");
1315 }
1316 if (contexts & CXCompletionContext_ClassTag) {
1317 fprintf(file, "Class name\n");
1318 }
1319 if (contexts & CXCompletionContext_Namespace) {
1320 fprintf(file, "Namespace or namespace alias\n");
1321 }
1322 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1323 fprintf(file, "Nested name specifier\n");
1324 }
1325 if (contexts & CXCompletionContext_ObjCInterface) {
1326 fprintf(file, "Objective-C interface\n");
1327 }
1328 if (contexts & CXCompletionContext_ObjCProtocol) {
1329 fprintf(file, "Objective-C protocol\n");
1330 }
1331 if (contexts & CXCompletionContext_ObjCCategory) {
1332 fprintf(file, "Objective-C category\n");
1333 }
1334 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1335 fprintf(file, "Objective-C instance method\n");
1336 }
1337 if (contexts & CXCompletionContext_ObjCClassMessage) {
1338 fprintf(file, "Objective-C class method\n");
1339 }
1340 if (contexts & CXCompletionContext_ObjCSelectorName) {
1341 fprintf(file, "Objective-C selector name\n");
1342 }
1343 if (contexts & CXCompletionContext_MacroName) {
1344 fprintf(file, "Macro name\n");
1345 }
1346 if (contexts & CXCompletionContext_NaturalLanguage) {
1347 fprintf(file, "Natural language\n");
1348 }
1349}
1350
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001351int my_stricmp(const char *s1, const char *s2) {
1352 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001353 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001354 if (c1 < c2)
1355 return -1;
1356 else if (c1 > c2)
1357 return 1;
1358
1359 ++s1;
1360 ++s2;
1361 }
1362
1363 if (*s1)
1364 return 1;
1365 else if (*s2)
1366 return -1;
1367 return 0;
1368}
1369
Douglas Gregor1982c182010-07-12 18:38:41 +00001370int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001371 const char *input = argv[1];
1372 char *filename = 0;
1373 unsigned line;
1374 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001375 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001376 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001377 struct CXUnsavedFile *unsaved_files = 0;
1378 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001379 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001380 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001381 unsigned I, Repeats = 1;
1382 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1383
1384 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1385 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001386
Douglas Gregor1982c182010-07-12 18:38:41 +00001387 if (timing_only)
1388 input += strlen("-code-completion-timing=");
1389 else
1390 input += strlen("-code-completion-at=");
1391
Ted Kremeneke68fff62010-02-17 00:41:32 +00001392 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001393 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001394 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001395
Douglas Gregor735df882009-12-02 09:21:34 +00001396 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1397 return -1;
1398
Douglas Gregor32be4a52010-10-11 21:37:58 +00001399 CIdx = clang_createIndex(0, 0);
1400
1401 if (getenv("CINDEXTEST_EDITING"))
1402 Repeats = 5;
1403
1404 TU = clang_parseTranslationUnit(CIdx, 0,
1405 argv + num_unsaved_files + 2,
1406 argc - num_unsaved_files - 2,
1407 0, 0, getDefaultParsingOptions());
1408 if (!TU) {
1409 fprintf(stderr, "Unable to load translation unit!\n");
1410 return 1;
1411 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001412
1413 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1414 fprintf(stderr, "Unable to reparse translation init!\n");
1415 return 1;
1416 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001417
1418 for (I = 0; I != Repeats; ++I) {
1419 results = clang_codeCompleteAt(TU, filename, line, column,
1420 unsaved_files, num_unsaved_files,
1421 completionOptions);
1422 if (!results) {
1423 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001424 return 1;
1425 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001426 if (I != Repeats-1)
1427 clang_disposeCodeCompleteResults(results);
1428 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001429
Douglas Gregorec6762c2009-12-18 16:20:58 +00001430 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001431 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001432 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001433 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001434 CXString objCSelector;
1435 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001436 if (!timing_only) {
1437 /* Sort the code-completion results based on the typed text. */
1438 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1439
Douglas Gregor1982c182010-07-12 18:38:41 +00001440 for (i = 0; i != n; ++i)
1441 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001442 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001443 n = clang_codeCompleteGetNumDiagnostics(results);
1444 for (i = 0; i != n; ++i) {
1445 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1446 PrintDiagnostic(diag);
1447 clang_disposeDiagnostic(diag);
1448 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001449
1450 contexts = clang_codeCompleteGetContexts(results);
1451 print_completion_contexts(contexts, stdout);
1452
Douglas Gregor0a47d692011-07-26 15:24:30 +00001453 containerKind = clang_codeCompleteGetContainerKind(results,
1454 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001455
1456 if (containerKind != CXCursor_InvalidCode) {
1457 /* We have found a container */
1458 CXString containerUSR, containerKindSpelling;
1459 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1460 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1461 clang_disposeString(containerKindSpelling);
1462
1463 if (containerIsIncomplete) {
1464 printf("Container is incomplete\n");
1465 }
1466 else {
1467 printf("Container is complete\n");
1468 }
1469
1470 containerUSR = clang_codeCompleteGetContainerUSR(results);
1471 printf("Container USR: %s\n", clang_getCString(containerUSR));
1472 clang_disposeString(containerUSR);
1473 }
1474
Douglas Gregor0a47d692011-07-26 15:24:30 +00001475 objCSelector = clang_codeCompleteGetObjCSelector(results);
1476 selectorString = clang_getCString(objCSelector);
1477 if (selectorString && strlen(selectorString) > 0) {
1478 printf("Objective-C selector: %s\n", selectorString);
1479 }
1480 clang_disposeString(objCSelector);
1481
Douglas Gregorec6762c2009-12-18 16:20:58 +00001482 clang_disposeCodeCompleteResults(results);
1483 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001484 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001485 clang_disposeIndex(CIdx);
1486 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001487
Douglas Gregor735df882009-12-02 09:21:34 +00001488 free_remapped_files(unsaved_files, num_unsaved_files);
1489
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001490 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001491}
1492
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001493typedef struct {
1494 char *filename;
1495 unsigned line;
1496 unsigned column;
1497} CursorSourceLocation;
1498
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001499static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001500 CXIndex CIdx;
1501 int errorCode;
1502 struct CXUnsavedFile *unsaved_files = 0;
1503 int num_unsaved_files = 0;
1504 CXTranslationUnit TU;
1505 CXCursor Cursor;
1506 CursorSourceLocation *Locations = 0;
1507 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001508 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001509 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001510
Ted Kremeneke68fff62010-02-17 00:41:32 +00001511 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001512 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1513 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001514
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001515 /* Parse the locations. */
1516 assert(NumLocations > 0 && "Unable to count locations?");
1517 Locations = (CursorSourceLocation *)malloc(
1518 NumLocations * sizeof(CursorSourceLocation));
1519 for (Loc = 0; Loc < NumLocations; ++Loc) {
1520 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001521 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1522 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001523 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001524 return errorCode;
1525 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001526
1527 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001528 &num_unsaved_files))
1529 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001530
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001531 if (getenv("CINDEXTEST_EDITING"))
1532 Repeats = 5;
1533
1534 /* Parse the translation unit. When we're testing clang_getCursor() after
1535 reparsing, don't remap unsaved files until the second parse. */
1536 CIdx = clang_createIndex(1, 1);
1537 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1538 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001539 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001540 unsaved_files,
1541 Repeats > 1? 0 : num_unsaved_files,
1542 getDefaultParsingOptions());
1543
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001544 if (!TU) {
1545 fprintf(stderr, "unable to parse input\n");
1546 return -1;
1547 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001548
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001549 if (checkForErrors(TU) != 0)
1550 return -1;
1551
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001552 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001553 if (Repeats > 1 &&
1554 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1555 clang_defaultReparseOptions(TU))) {
1556 clang_disposeTranslationUnit(TU);
1557 return 1;
1558 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001559
1560 if (checkForErrors(TU) != 0)
1561 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001562
1563 for (Loc = 0; Loc < NumLocations; ++Loc) {
1564 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1565 if (!file)
1566 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001567
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001568 Cursor = clang_getCursor(TU,
1569 clang_getLocation(TU, file, Locations[Loc].line,
1570 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001571
1572 if (checkForErrors(TU) != 0)
1573 return -1;
1574
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001575 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001576 CXCompletionString completionString = clang_getCursorCompletionString(
1577 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001578 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1579 CXString Spelling;
1580 const char *cspell;
1581 unsigned line, column;
1582 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1583 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001584 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001585 PrintCursorExtent(Cursor);
1586 Spelling = clang_getCursorSpelling(Cursor);
1587 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001588 if (cspell && strlen(cspell) != 0) {
1589 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001590 printf(" Spelling=%s (", cspell);
1591 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001592 CXSourceRange range =
1593 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001594 if (clang_Range_isNull(range))
1595 break;
1596 PrintRange(range, 0);
1597 }
1598 printf(")");
1599 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001600 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001601 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1602 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001603 if (completionString != NULL) {
1604 printf("\nCompletion string: ");
1605 print_completion_string(completionString, stdout);
1606 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001607 printf("\n");
1608 free(Locations[Loc].filename);
1609 }
1610 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001611 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001612
Douglas Gregora88084b2010-02-18 18:08:43 +00001613 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001614 clang_disposeTranslationUnit(TU);
1615 clang_disposeIndex(CIdx);
1616 free(Locations);
1617 free_remapped_files(unsaved_files, num_unsaved_files);
1618 return 0;
1619}
1620
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001621static enum CXVisitorResult findFileRefsVisit(void *context,
1622 CXCursor cursor, CXSourceRange range) {
1623 if (clang_Range_isNull(range))
1624 return CXVisit_Continue;
1625
1626 PrintCursor(cursor);
1627 PrintRange(range, "");
1628 printf("\n");
1629 return CXVisit_Continue;
1630}
1631
1632static int find_file_refs_at(int argc, const char **argv) {
1633 CXIndex CIdx;
1634 int errorCode;
1635 struct CXUnsavedFile *unsaved_files = 0;
1636 int num_unsaved_files = 0;
1637 CXTranslationUnit TU;
1638 CXCursor Cursor;
1639 CursorSourceLocation *Locations = 0;
1640 unsigned NumLocations = 0, Loc;
1641 unsigned Repeats = 1;
1642 unsigned I;
1643
1644 /* Count the number of locations. */
1645 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1646 ++NumLocations;
1647
1648 /* Parse the locations. */
1649 assert(NumLocations > 0 && "Unable to count locations?");
1650 Locations = (CursorSourceLocation *)malloc(
1651 NumLocations * sizeof(CursorSourceLocation));
1652 for (Loc = 0; Loc < NumLocations; ++Loc) {
1653 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1654 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1655 &Locations[Loc].line,
1656 &Locations[Loc].column, 0, 0)))
1657 return errorCode;
1658 }
1659
1660 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1661 &num_unsaved_files))
1662 return -1;
1663
1664 if (getenv("CINDEXTEST_EDITING"))
1665 Repeats = 5;
1666
1667 /* Parse the translation unit. When we're testing clang_getCursor() after
1668 reparsing, don't remap unsaved files until the second parse. */
1669 CIdx = clang_createIndex(1, 1);
1670 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1671 argv + num_unsaved_files + 1 + NumLocations,
1672 argc - num_unsaved_files - 2 - NumLocations,
1673 unsaved_files,
1674 Repeats > 1? 0 : num_unsaved_files,
1675 getDefaultParsingOptions());
1676
1677 if (!TU) {
1678 fprintf(stderr, "unable to parse input\n");
1679 return -1;
1680 }
1681
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001682 if (checkForErrors(TU) != 0)
1683 return -1;
1684
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001685 for (I = 0; I != Repeats; ++I) {
1686 if (Repeats > 1 &&
1687 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1688 clang_defaultReparseOptions(TU))) {
1689 clang_disposeTranslationUnit(TU);
1690 return 1;
1691 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001692
1693 if (checkForErrors(TU) != 0)
1694 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001695
1696 for (Loc = 0; Loc < NumLocations; ++Loc) {
1697 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1698 if (!file)
1699 continue;
1700
1701 Cursor = clang_getCursor(TU,
1702 clang_getLocation(TU, file, Locations[Loc].line,
1703 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001704
1705 if (checkForErrors(TU) != 0)
1706 return -1;
1707
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001708 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001709 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001710 PrintCursor(Cursor);
1711 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001712 clang_findReferencesInFile(Cursor, file, visitor);
1713 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001714
1715 if (checkForErrors(TU) != 0)
1716 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001717 }
1718 }
1719 }
1720
1721 PrintDiagnostics(TU);
1722 clang_disposeTranslationUnit(TU);
1723 clang_disposeIndex(CIdx);
1724 free(Locations);
1725 free_remapped_files(unsaved_files, num_unsaved_files);
1726 return 0;
1727}
1728
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001729typedef struct {
1730 const char *check_prefix;
1731 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001732 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001733 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001734 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001735} IndexData;
1736
1737static void printCheck(IndexData *data) {
1738 if (data->check_prefix) {
1739 if (data->first_check_printed) {
1740 printf("// %s-NEXT: ", data->check_prefix);
1741 } else {
1742 printf("// %s : ", data->check_prefix);
1743 data->first_check_printed = 1;
1744 }
1745 }
1746}
1747
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001748static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001749 CXString filename = clang_getFileName((CXFile)file);
1750 printf("%s", clang_getCString(filename));
1751 clang_disposeString(filename);
1752}
1753
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001754static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1755 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001756 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001757 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001758 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001759 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001760 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001761
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001762 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001763 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1764 if (line == 0) {
1765 printf("<null loc>");
1766 return;
1767 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001768 if (!file) {
1769 printf("<no idxfile>");
1770 return;
1771 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001772 filename = clang_getFileName((CXFile)file);
1773 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001774 if (strcmp(cname, index_data->main_filename) == 0)
1775 isMainFile = 1;
1776 else
1777 isMainFile = 0;
1778 clang_disposeString(filename);
1779
1780 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001781 printCXIndexFile(file);
1782 printf(":");
1783 }
1784 printf("%d:%d", line, column);
1785}
1786
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001787static unsigned digitCount(unsigned val) {
1788 unsigned c = 1;
1789 while (1) {
1790 if (val < 10)
1791 return c;
1792 ++c;
1793 val /= 10;
1794 }
1795}
1796
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001797static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1798 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001799 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001800 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001801 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001802 unsigned line, column;
1803
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001804 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001805 if (!name)
1806 name = "<anon-tag>";
1807
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001808 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001809 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001810 newStr = (char *)malloc(strlen(name) +
1811 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001812 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001813 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001814}
1815
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001816static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1817 CXIdxClientContainer container;
1818 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001819 if (!container)
1820 printf("[<<NULL>>]");
1821 else
1822 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001823}
1824
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001825static const char *getEntityKindString(CXIdxEntityKind kind) {
1826 switch (kind) {
1827 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1828 case CXIdxEntity_Typedef: return "typedef";
1829 case CXIdxEntity_Function: return "function";
1830 case CXIdxEntity_Variable: return "variable";
1831 case CXIdxEntity_Field: return "field";
1832 case CXIdxEntity_EnumConstant: return "enumerator";
1833 case CXIdxEntity_ObjCClass: return "objc-class";
1834 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1835 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001836 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1837 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001838 case CXIdxEntity_ObjCProperty: return "objc-property";
1839 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1840 case CXIdxEntity_Enum: return "enum";
1841 case CXIdxEntity_Struct: return "struct";
1842 case CXIdxEntity_Union: return "union";
1843 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001844 case CXIdxEntity_CXXNamespace: return "namespace";
1845 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1846 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1847 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1848 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1849 case CXIdxEntity_CXXConstructor: return "constructor";
1850 case CXIdxEntity_CXXDestructor: return "destructor";
1851 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1852 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1853 }
1854 assert(0 && "Garbage entity kind");
1855 return 0;
1856}
1857
1858static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1859 switch (kind) {
1860 case CXIdxEntity_NonTemplate: return "";
1861 case CXIdxEntity_Template: return "-template";
1862 case CXIdxEntity_TemplatePartialSpecialization:
1863 return "-template-partial-spec";
1864 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001865 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001866 assert(0 && "Garbage entity kind");
1867 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001868}
1869
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001870static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1871 switch (kind) {
1872 case CXIdxEntityLang_None: return "<none>";
1873 case CXIdxEntityLang_C: return "C";
1874 case CXIdxEntityLang_ObjC: return "ObjC";
1875 case CXIdxEntityLang_CXX: return "C++";
1876 }
1877 assert(0 && "Garbage language kind");
1878 return 0;
1879}
1880
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001881static void printEntityInfo(const char *cb,
1882 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001883 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001884 const char *name;
1885 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001886 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001887 index_data = (IndexData *)client_data;
1888 printCheck(index_data);
1889
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001890 if (!info) {
1891 printf("%s: <<NULL>>", cb);
1892 return;
1893 }
1894
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001895 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001896 if (!name)
1897 name = "<anon-tag>";
1898
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001899 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1900 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001901 printf(" | name: %s", name);
1902 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001903 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001904
1905 for (i = 0; i != info->numAttributes; ++i) {
1906 const CXIdxAttrInfo *Attr = info->attributes[i];
1907 printf(" <attribute>: ");
1908 PrintCursor(Attr->cursor);
1909 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001910}
1911
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001912static void printBaseClassInfo(CXClientData client_data,
1913 const CXIdxBaseClassInfo *info) {
1914 printEntityInfo(" <base>", client_data, info->base);
1915 printf(" | cursor: ");
1916 PrintCursor(info->cursor);
1917 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001918 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001919}
1920
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001921static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1922 CXClientData client_data) {
1923 unsigned i;
1924 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1925 printEntityInfo(" <protocol>", client_data,
1926 ProtoInfo->protocols[i]->protocol);
1927 printf(" | cursor: ");
1928 PrintCursor(ProtoInfo->protocols[i]->cursor);
1929 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001930 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001931 printf("\n");
1932 }
1933}
1934
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001935static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001936 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001937 CXString str;
1938 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001939 unsigned numDiags, i;
1940 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001941 IndexData *index_data;
1942 index_data = (IndexData *)client_data;
1943 printCheck(index_data);
1944
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001945 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1946 for (i = 0; i != numDiags; ++i) {
1947 diag = clang_getDiagnosticInSet(diagSet, i);
1948 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1949 cstr = clang_getCString(str);
1950 printf("[diagnostic]: %s\n", cstr);
1951 clang_disposeString(str);
1952
1953 if (getenv("CINDEXTEST_FAILONERROR") &&
1954 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1955 index_data->fail_for_error = 1;
1956 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001957 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001958}
1959
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001960static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1961 CXFile file, void *reserved) {
1962 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001963 CXString filename;
1964
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001965 index_data = (IndexData *)client_data;
1966 printCheck(index_data);
1967
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001968 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001969 index_data->main_filename = clang_getCString(filename);
1970 clang_disposeString(filename);
1971
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001972 printf("[enteredMainFile]: ");
1973 printCXIndexFile((CXIdxClientFile)file);
1974 printf("\n");
1975
1976 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001977}
1978
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001979static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001980 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001981 IndexData *index_data;
1982 index_data = (IndexData *)client_data;
1983 printCheck(index_data);
1984
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001985 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001986 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001987 printf(" | name: \"%s\"", info->filename);
1988 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001989 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001990 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001991
1992 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001993}
1994
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001995static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001996 void *reserved) {
1997 IndexData *index_data;
1998 index_data = (IndexData *)client_data;
1999 printCheck(index_data);
2000
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002001 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002002 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002003}
2004
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002005static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002006 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002007 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002008 const CXIdxObjCCategoryDeclInfo *CatInfo;
2009 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002010 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002011 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002012 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002013 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002014 index_data = (IndexData *)client_data;
2015
2016 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2017 printf(" | cursor: ");
2018 PrintCursor(info->cursor);
2019 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002020 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002021 printf(" | semantic-container: ");
2022 printCXIndexContainer(info->semanticContainer);
2023 printf(" | lexical-container: ");
2024 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002025 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002026 printf(" | isDef: %d", info->isDefinition);
2027 printf(" | isContainer: %d", info->isContainer);
2028 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002029
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002030 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002031 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002032 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002033 PrintCursor(Attr->cursor);
2034 printf("\n");
2035 }
2036
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002037 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2038 const char *kindName = 0;
2039 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2040 switch (K) {
2041 case CXIdxObjCContainer_ForwardRef:
2042 kindName = "forward-ref"; break;
2043 case CXIdxObjCContainer_Interface:
2044 kindName = "interface"; break;
2045 case CXIdxObjCContainer_Implementation:
2046 kindName = "implementation"; break;
2047 }
2048 printCheck(index_data);
2049 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2050 }
2051
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002052 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002053 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2054 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002055 printf(" | cursor: ");
2056 PrintCursor(CatInfo->classCursor);
2057 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002058 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002059 printf("\n");
2060 }
2061
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002062 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2063 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002064 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002065 printf("\n");
2066 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002067 }
2068
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002069 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2070 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002071 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002072
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002073 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2074 if (PropInfo->getter) {
2075 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2076 printf("\n");
2077 }
2078 if (PropInfo->setter) {
2079 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2080 printf("\n");
2081 }
2082 }
2083
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002084 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2085 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2086 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2087 printf("\n");
2088 }
2089 }
2090
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002091 if (info->declAsContainer)
2092 clang_index_setClientContainer(info->declAsContainer,
2093 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002094}
2095
2096static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002097 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002098 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002099 printf(" | cursor: ");
2100 PrintCursor(info->cursor);
2101 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002102 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002103 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002104 printf(" | container: ");
2105 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002106 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002107 switch (info->kind) {
2108 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002109 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002110 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002111 printf("\n");
2112}
2113
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002114static int index_abortQuery(CXClientData client_data, void *reserved) {
2115 IndexData *index_data;
2116 index_data = (IndexData *)client_data;
2117 return index_data->abort;
2118}
2119
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002120static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002121 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002122 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002123 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002124 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002125 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002126 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002127 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002128 index_indexEntityReference
2129};
2130
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002131static unsigned getIndexOptions(void) {
2132 unsigned index_opts;
2133 index_opts = 0;
2134 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2135 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2136 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2137 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2138
2139 return index_opts;
2140}
2141
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002142static int index_file(int argc, const char **argv) {
2143 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002144 CXIndex Idx;
2145 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002146 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002147 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002148 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002149
2150 check_prefix = 0;
2151 if (argc > 0) {
2152 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2153 check_prefix = argv[0] + strlen("-check-prefix=");
2154 ++argv;
2155 --argc;
2156 }
2157 }
2158
2159 if (argc == 0) {
2160 fprintf(stderr, "no compiler arguments\n");
2161 return -1;
2162 }
2163
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002164 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2165 /* displayDiagnosics=*/1))) {
2166 fprintf(stderr, "Could not create Index\n");
2167 return 1;
2168 }
2169 idxAction = 0;
2170 result = 1;
2171
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002172 index_data.check_prefix = check_prefix;
2173 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002174 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002175 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002176
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002177 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002178 idxAction = clang_IndexAction_create(Idx);
2179 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002180 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002181 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002182 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002183 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002184
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002185 clang_IndexAction_dispose(idxAction);
2186 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002187 return result;
2188}
2189
2190static int index_tu(int argc, const char **argv) {
2191 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002192 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002193 CXTranslationUnit TU;
2194 const char *check_prefix;
2195 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002196 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002197 int result;
2198
2199 check_prefix = 0;
2200 if (argc > 0) {
2201 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2202 check_prefix = argv[0] + strlen("-check-prefix=");
2203 ++argv;
2204 --argc;
2205 }
2206 }
2207
2208 if (argc == 0) {
2209 fprintf(stderr, "no ast file\n");
2210 return -1;
2211 }
2212
2213 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2214 /* displayDiagnosics=*/1))) {
2215 fprintf(stderr, "Could not create Index\n");
2216 return 1;
2217 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002218 idxAction = 0;
2219 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002220
2221 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002222 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002223
2224 index_data.check_prefix = check_prefix;
2225 index_data.first_check_printed = 0;
2226 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002227 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002228
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002229 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002230 idxAction = clang_IndexAction_create(Idx);
2231 result = clang_indexTranslationUnit(idxAction, &index_data,
2232 &IndexCB,sizeof(IndexCB),
2233 index_opts, TU);
2234 if (index_data.fail_for_error)
2235 goto finished;
2236
2237 finished:
2238 clang_IndexAction_dispose(idxAction);
2239 clang_disposeIndex(Idx);
2240
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002241 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002242}
2243
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002244int perform_token_annotation(int argc, const char **argv) {
2245 const char *input = argv[1];
2246 char *filename = 0;
2247 unsigned line, second_line;
2248 unsigned column, second_column;
2249 CXIndex CIdx;
2250 CXTranslationUnit TU = 0;
2251 int errorCode;
2252 struct CXUnsavedFile *unsaved_files = 0;
2253 int num_unsaved_files = 0;
2254 CXToken *tokens;
2255 unsigned num_tokens;
2256 CXSourceRange range;
2257 CXSourceLocation startLoc, endLoc;
2258 CXFile file = 0;
2259 CXCursor *cursors = 0;
2260 unsigned i;
2261
2262 input += strlen("-test-annotate-tokens=");
2263 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2264 &second_line, &second_column)))
2265 return errorCode;
2266
2267 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2268 return -1;
2269
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002270 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002271 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2272 argv + num_unsaved_files + 2,
2273 argc - num_unsaved_files - 3,
2274 unsaved_files,
2275 num_unsaved_files,
2276 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002277 if (!TU) {
2278 fprintf(stderr, "unable to parse input\n");
2279 clang_disposeIndex(CIdx);
2280 free(filename);
2281 free_remapped_files(unsaved_files, num_unsaved_files);
2282 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002283 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002284 errorCode = 0;
2285
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002286 if (checkForErrors(TU) != 0)
2287 return -1;
2288
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002289 if (getenv("CINDEXTEST_EDITING")) {
2290 for (i = 0; i < 5; ++i) {
2291 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2292 clang_defaultReparseOptions(TU))) {
2293 fprintf(stderr, "Unable to reparse translation unit!\n");
2294 errorCode = -1;
2295 goto teardown;
2296 }
2297 }
2298 }
2299
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002300 if (checkForErrors(TU) != 0) {
2301 errorCode = -1;
2302 goto teardown;
2303 }
2304
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002305 file = clang_getFile(TU, filename);
2306 if (!file) {
2307 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2308 errorCode = -1;
2309 goto teardown;
2310 }
2311
2312 startLoc = clang_getLocation(TU, file, line, column);
2313 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002314 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002315 column);
2316 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002317 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002318 }
2319
2320 endLoc = clang_getLocation(TU, file, second_line, second_column);
2321 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002322 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002323 second_line, second_column);
2324 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002325 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002326 }
2327
2328 range = clang_getRange(startLoc, endLoc);
2329 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002330
2331 if (checkForErrors(TU) != 0) {
2332 errorCode = -1;
2333 goto teardown;
2334 }
2335
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002336 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2337 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002338
2339 if (checkForErrors(TU) != 0) {
2340 errorCode = -1;
2341 goto teardown;
2342 }
2343
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002344 for (i = 0; i != num_tokens; ++i) {
2345 const char *kind = "<unknown>";
2346 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2347 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2348 unsigned start_line, start_column, end_line, end_column;
2349
2350 switch (clang_getTokenKind(tokens[i])) {
2351 case CXToken_Punctuation: kind = "Punctuation"; break;
2352 case CXToken_Keyword: kind = "Keyword"; break;
2353 case CXToken_Identifier: kind = "Identifier"; break;
2354 case CXToken_Literal: kind = "Literal"; break;
2355 case CXToken_Comment: kind = "Comment"; break;
2356 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002357 clang_getSpellingLocation(clang_getRangeStart(extent),
2358 0, &start_line, &start_column, 0);
2359 clang_getSpellingLocation(clang_getRangeEnd(extent),
2360 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002361 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002362 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002363 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002364 if (!clang_isInvalid(cursors[i].kind)) {
2365 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002366 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002367 }
2368 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002369 }
2370 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002371 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002372
2373 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002374 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002375 clang_disposeTranslationUnit(TU);
2376 clang_disposeIndex(CIdx);
2377 free(filename);
2378 free_remapped_files(unsaved_files, num_unsaved_files);
2379 return errorCode;
2380}
2381
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002382static int
2383perform_test_compilation_db(const char *database, int argc, const char **argv) {
2384 CXCompilationDatabase db;
2385 CXCompileCommands CCmds;
2386 CXCompileCommand CCmd;
2387 CXCompilationDatabase_Error ec;
2388 CXString wd;
2389 CXString arg;
2390 int errorCode = 0;
2391 char *tmp;
2392 unsigned len;
2393 char *buildDir;
2394 int i, j, a, numCmds, numArgs;
2395
2396 len = strlen(database);
2397 tmp = (char *) malloc(len+1);
2398 memcpy(tmp, database, len+1);
2399 buildDir = dirname(tmp);
2400
2401 db = clang_tooling_CompilationDatabase_fromDirectory(buildDir, &ec);
2402
2403 if (db) {
2404
2405 if (ec!=CXCompilationDatabase_NoError) {
2406 printf("unexpected error %d code while loading compilation database\n", ec);
2407 errorCode = -1;
2408 goto cdb_end;
2409 }
2410
2411 for (i=0; i<argc && errorCode==0; ) {
2412 if (strcmp(argv[i],"lookup")==0){
2413 CCmds = clang_tooling_CompilationDatabase_getCompileCommands(db, argv[i+1]);
2414
2415 if (!CCmds) {
2416 printf("file %s not found in compilation db\n", argv[i+1]);
2417 errorCode = -1;
2418 break;
2419 }
2420
2421 numCmds = clang_tooling_CompileCommands_getSize(CCmds);
2422
2423 if (numCmds==0) {
2424 fprintf(stderr, "should not get an empty compileCommand set for file"
2425 " '%s'\n", argv[i+1]);
2426 errorCode = -1;
2427 break;
2428 }
2429
2430 for (j=0; j<numCmds; ++j) {
2431 CCmd = clang_tooling_CompileCommands_getCommand(CCmds, j);
2432
2433 wd = clang_tooling_CompileCommand_getDirectory(CCmd);
2434 printf("workdir:'%s'", clang_getCString(wd));
2435 clang_disposeString(wd);
2436
2437 printf(" cmdline:'");
2438 numArgs = clang_tooling_CompileCommand_getNumArgs(CCmd);
2439 for (a=0; a<numArgs; ++a) {
2440 if (a) printf(" ");
2441 arg = clang_tooling_CompileCommand_getArg(CCmd, a);
2442 printf("%s", clang_getCString(arg));
2443 clang_disposeString(arg);
2444 }
2445 printf("'\n");
2446 }
2447
2448 clang_tooling_CompileCommands_dispose(CCmds);
2449
2450 i += 2;
2451 }
2452 }
2453 clang_tooling_CompilationDatabase_dispose(db);
2454 } else {
2455 printf("database loading failed with error code %d.\n", ec);
2456 errorCode = -1;
2457 }
2458
2459cdb_end:
2460 free(tmp);
2461
2462 return errorCode;
2463}
2464
Ted Kremenek0d435192009-11-17 18:13:31 +00002465/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002466/* USR printing. */
2467/******************************************************************************/
2468
2469static int insufficient_usr(const char *kind, const char *usage) {
2470 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2471 return 1;
2472}
2473
2474static unsigned isUSR(const char *s) {
2475 return s[0] == 'c' && s[1] == ':';
2476}
2477
2478static int not_usr(const char *s, const char *arg) {
2479 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2480 return 1;
2481}
2482
2483static void print_usr(CXString usr) {
2484 const char *s = clang_getCString(usr);
2485 printf("%s\n", s);
2486 clang_disposeString(usr);
2487}
2488
2489static void display_usrs() {
2490 fprintf(stderr, "-print-usrs options:\n"
2491 " ObjCCategory <class name> <category name>\n"
2492 " ObjCClass <class name>\n"
2493 " ObjCIvar <ivar name> <class USR>\n"
2494 " ObjCMethod <selector> [0=class method|1=instance method] "
2495 "<class USR>\n"
2496 " ObjCProperty <property name> <class USR>\n"
2497 " ObjCProtocol <protocol name>\n");
2498}
2499
2500int print_usrs(const char **I, const char **E) {
2501 while (I != E) {
2502 const char *kind = *I;
2503 unsigned len = strlen(kind);
2504 switch (len) {
2505 case 8:
2506 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2507 if (I + 2 >= E)
2508 return insufficient_usr(kind, "<ivar name> <class USR>");
2509 if (!isUSR(I[2]))
2510 return not_usr("<class USR>", I[2]);
2511 else {
2512 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002513 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002514 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002515 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2516 }
2517
2518 I += 3;
2519 continue;
2520 }
2521 break;
2522 case 9:
2523 if (memcmp(kind, "ObjCClass", 9) == 0) {
2524 if (I + 1 >= E)
2525 return insufficient_usr(kind, "<class name>");
2526 print_usr(clang_constructUSR_ObjCClass(I[1]));
2527 I += 2;
2528 continue;
2529 }
2530 break;
2531 case 10:
2532 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2533 if (I + 3 >= E)
2534 return insufficient_usr(kind, "<method selector> "
2535 "[0=class method|1=instance method] <class USR>");
2536 if (!isUSR(I[3]))
2537 return not_usr("<class USR>", I[3]);
2538 else {
2539 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002540 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002541 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002542 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2543 }
2544 I += 4;
2545 continue;
2546 }
2547 break;
2548 case 12:
2549 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2550 if (I + 2 >= E)
2551 return insufficient_usr(kind, "<class name> <category name>");
2552 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2553 I += 3;
2554 continue;
2555 }
2556 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2557 if (I + 1 >= E)
2558 return insufficient_usr(kind, "<protocol name>");
2559 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2560 I += 2;
2561 continue;
2562 }
2563 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2564 if (I + 2 >= E)
2565 return insufficient_usr(kind, "<property name> <class USR>");
2566 if (!isUSR(I[2]))
2567 return not_usr("<class USR>", I[2]);
2568 else {
2569 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002570 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002571 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002572 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2573 }
2574 I += 3;
2575 continue;
2576 }
2577 break;
2578 default:
2579 break;
2580 }
2581 break;
2582 }
2583
2584 if (I != E) {
2585 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2586 display_usrs();
2587 return 1;
2588 }
2589 return 0;
2590}
2591
2592int print_usrs_file(const char *file_name) {
2593 char line[2048];
2594 const char *args[128];
2595 unsigned numChars = 0;
2596
2597 FILE *fp = fopen(file_name, "r");
2598 if (!fp) {
2599 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2600 return 1;
2601 }
2602
2603 /* This code is not really all that safe, but it works fine for testing. */
2604 while (!feof(fp)) {
2605 char c = fgetc(fp);
2606 if (c == '\n') {
2607 unsigned i = 0;
2608 const char *s = 0;
2609
2610 if (numChars == 0)
2611 continue;
2612
2613 line[numChars] = '\0';
2614 numChars = 0;
2615
2616 if (line[0] == '/' && line[1] == '/')
2617 continue;
2618
2619 s = strtok(line, " ");
2620 while (s) {
2621 args[i] = s;
2622 ++i;
2623 s = strtok(0, " ");
2624 }
2625 if (print_usrs(&args[0], &args[i]))
2626 return 1;
2627 }
2628 else
2629 line[numChars++] = c;
2630 }
2631
2632 fclose(fp);
2633 return 0;
2634}
2635
2636/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002637/* Command line processing. */
2638/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002639int write_pch_file(const char *filename, int argc, const char *argv[]) {
2640 CXIndex Idx;
2641 CXTranslationUnit TU;
2642 struct CXUnsavedFile *unsaved_files = 0;
2643 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002644 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002645
2646 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2647
2648 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2649 clang_disposeIndex(Idx);
2650 return -1;
2651 }
2652
2653 TU = clang_parseTranslationUnit(Idx, 0,
2654 argv + num_unsaved_files,
2655 argc - num_unsaved_files,
2656 unsaved_files,
2657 num_unsaved_files,
2658 CXTranslationUnit_Incomplete);
2659 if (!TU) {
2660 fprintf(stderr, "Unable to load translation unit!\n");
2661 free_remapped_files(unsaved_files, num_unsaved_files);
2662 clang_disposeIndex(Idx);
2663 return 1;
2664 }
2665
Douglas Gregor39c411f2011-07-06 16:43:36 +00002666 switch (clang_saveTranslationUnit(TU, filename,
2667 clang_defaultSaveOptions(TU))) {
2668 case CXSaveError_None:
2669 break;
2670
2671 case CXSaveError_TranslationErrors:
2672 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2673 filename);
2674 result = 2;
2675 break;
2676
2677 case CXSaveError_InvalidTU:
2678 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2679 filename);
2680 result = 3;
2681 break;
2682
2683 case CXSaveError_Unknown:
2684 default:
2685 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2686 result = 1;
2687 break;
2688 }
2689
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002690 clang_disposeTranslationUnit(TU);
2691 free_remapped_files(unsaved_files, num_unsaved_files);
2692 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002693 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002694}
2695
2696/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002697/* Serialized diagnostics. */
2698/******************************************************************************/
2699
2700static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2701 switch (error) {
2702 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2703 case CXLoadDiag_None: break;
2704 case CXLoadDiag_Unknown: return "Unknown";
2705 case CXLoadDiag_InvalidFile: return "Invalid File";
2706 }
2707 return "None";
2708}
2709
2710static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2711 switch (severity) {
2712 case CXDiagnostic_Note: return "note";
2713 case CXDiagnostic_Error: return "error";
2714 case CXDiagnostic_Fatal: return "fatal";
2715 case CXDiagnostic_Ignored: return "ignored";
2716 case CXDiagnostic_Warning: return "warning";
2717 }
2718 return "unknown";
2719}
2720
2721static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002722 if (indent == 0)
2723 return;
2724 fprintf(stderr, "+");
2725 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002726 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002727 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002728 --indent;
2729 }
2730}
2731
2732static void printLocation(CXSourceLocation L) {
2733 CXFile File;
2734 CXString FileName;
2735 unsigned line, column, offset;
2736
2737 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2738 FileName = clang_getFileName(File);
2739
2740 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2741 clang_disposeString(FileName);
2742}
2743
2744static void printRanges(CXDiagnostic D, unsigned indent) {
2745 unsigned i, n = clang_getDiagnosticNumRanges(D);
2746
2747 for (i = 0; i < n; ++i) {
2748 CXSourceLocation Start, End;
2749 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2750 Start = clang_getRangeStart(SR);
2751 End = clang_getRangeEnd(SR);
2752
2753 printIndent(indent);
2754 fprintf(stderr, "Range: ");
2755 printLocation(Start);
2756 fprintf(stderr, " ");
2757 printLocation(End);
2758 fprintf(stderr, "\n");
2759 }
2760}
2761
2762static void printFixIts(CXDiagnostic D, unsigned indent) {
2763 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002764 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002765 for (i = 0 ; i < n; ++i) {
2766 CXSourceRange ReplacementRange;
2767 CXString text;
2768 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2769
2770 printIndent(indent);
2771 fprintf(stderr, "FIXIT: (");
2772 printLocation(clang_getRangeStart(ReplacementRange));
2773 fprintf(stderr, " - ");
2774 printLocation(clang_getRangeEnd(ReplacementRange));
2775 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2776 clang_disposeString(text);
2777 }
2778}
2779
2780static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002781 unsigned i, n;
2782
Ted Kremenek15322172011-11-10 08:43:12 +00002783 if (!Diags)
2784 return;
2785
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002786 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002787 for (i = 0; i < n; ++i) {
2788 CXSourceLocation DiagLoc;
2789 CXDiagnostic D;
2790 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002791 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002792 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002793 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002794
2795 D = clang_getDiagnosticInSet(Diags, i);
2796 DiagLoc = clang_getDiagnosticLocation(D);
2797 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2798 FileName = clang_getFileName(File);
2799 DiagSpelling = clang_getDiagnosticSpelling(D);
2800
2801 printIndent(indent);
2802
2803 fprintf(stderr, "%s:%d:%d: %s: %s",
2804 clang_getCString(FileName),
2805 line,
2806 column,
2807 getSeverityString(clang_getDiagnosticSeverity(D)),
2808 clang_getCString(DiagSpelling));
2809
2810 DiagOption = clang_getDiagnosticOption(D, 0);
2811 DiagOptionStr = clang_getCString(DiagOption);
2812 if (DiagOptionStr) {
2813 fprintf(stderr, " [%s]", DiagOptionStr);
2814 }
2815
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002816 DiagCat = clang_getDiagnosticCategoryText(D);
2817 DiagCatStr = clang_getCString(DiagCat);
2818 if (DiagCatStr) {
2819 fprintf(stderr, " [%s]", DiagCatStr);
2820 }
2821
Ted Kremenek15322172011-11-10 08:43:12 +00002822 fprintf(stderr, "\n");
2823
2824 printRanges(D, indent);
2825 printFixIts(D, indent);
2826
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002827 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002828 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2829
2830 clang_disposeString(FileName);
2831 clang_disposeString(DiagSpelling);
2832 clang_disposeString(DiagOption);
2833 }
2834}
2835
2836static int read_diagnostics(const char *filename) {
2837 enum CXLoadDiag_Error error;
2838 CXString errorString;
2839 CXDiagnosticSet Diags = 0;
2840
2841 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2842 if (!Diags) {
2843 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2844 getDiagnosticCodeStr(error),
2845 clang_getCString(errorString));
2846 clang_disposeString(errorString);
2847 return 1;
2848 }
2849
2850 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002851 fprintf(stderr, "Number of diagnostics: %d\n",
2852 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002853 clang_disposeDiagnosticSet(Diags);
2854 return 0;
2855}
2856
2857/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002858/* Command line processing. */
2859/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002860
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002861static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002862 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002863 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002864 if (strcmp(s, "-usrs") == 0)
2865 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002866 if (strncmp(s, "-memory-usage", 13) == 0)
2867 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002868 return NULL;
2869}
2870
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002871static void print_usage(void) {
2872 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002873 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002874 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002875 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002876 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002877 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002878 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002879 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002880 "[FileCheck prefix]\n");
2881 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002882 " c-index-test -test-load-tu <AST file> <symbol filter> "
2883 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002884 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2885 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002886 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002887 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002888 " c-index-test -test-load-source-memory-usage "
2889 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002890 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2891 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002892 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002893 " c-index-test -test-load-source-usrs-memory-usage "
2894 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002895 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2896 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002897 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002898 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002899 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002900 " c-index-test -test-print-typekind {<args>}*\n"
2901 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002902 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002903 " c-index-test -write-pch <file> <compiler arguments>\n");
2904 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002905 " c-index-test -compilation-db [lookup <filename>] database\n");
2906 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00002907 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002908 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002909 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002910 " all - load all symbols, including those from PCH\n"
2911 " local - load all symbols except those in PCH\n"
2912 " category - only load ObjC categories (non-PCH)\n"
2913 " interface - only load ObjC interfaces (non-PCH)\n"
2914 " protocol - only load ObjC protocols (non-PCH)\n"
2915 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002916 " typedef - only load typdefs (non-PCH)\n"
2917 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002918}
2919
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002920/***/
2921
2922int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002923 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002924 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2925 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002926 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002927 return perform_code_completion(argc, argv, 0);
2928 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2929 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002930 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2931 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002932 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2933 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002934 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2935 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002936 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2937 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002938 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002939 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002940 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002941 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2942 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002943 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002944 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2945 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2946 if (I) {
2947 int trials = atoi(argv[2]);
2948 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2949 NULL);
2950 }
2951 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002952 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002953 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002954
2955 PostVisitTU postVisit = 0;
2956 if (strstr(argv[1], "-memory-usage"))
2957 postVisit = PrintMemoryUsage;
2958
Ted Kremenek7d405622010-01-12 23:34:26 +00002959 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002960 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2961 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002962 }
2963 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002964 return perform_file_scan(argv[2], argv[3],
2965 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002966 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2967 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002968 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2969 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2970 PrintInclusionStack);
2971 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2972 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2973 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002974 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2975 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2976 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002977 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2978 return perform_test_load_source(argc - 2, argv + 2, "all",
2979 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002980 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2981 if (argc > 2)
2982 return print_usrs(argv + 2, argv + argc);
2983 else {
2984 display_usrs();
2985 return 1;
2986 }
2987 }
2988 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2989 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002990 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2991 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002992 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
2993 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
2994
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002995 print_usage();
2996 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002997}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002998
2999/***/
3000
3001/* We intentionally run in a separate thread to ensure we at least minimal
3002 * testing of a multithreaded environment (for example, having a reduced stack
3003 * size). */
3004
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003005typedef struct thread_info {
3006 int argc;
3007 const char **argv;
3008 int result;
3009} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003010void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003011 thread_info *client_data = client_data_v;
3012 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003013#ifdef __CYGWIN__
3014 fflush(stdout); /* stdout is not flushed on Cygwin. */
3015#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003016}
3017
3018int main(int argc, const char **argv) {
3019 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003020
Douglas Gregor61605982010-10-27 16:00:01 +00003021 if (getenv("CINDEXTEST_NOTHREADS"))
3022 return cindextest_main(argc, argv);
3023
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003024 client_data.argc = argc;
3025 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003026 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003027 return client_data.result;
3028}