blob: 8ab7f9410d9092c6700f5c6a7099445ace4b1532 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00004#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00005#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00006#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00007#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00008#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009
Ted Kremenek0d435192009-11-17 18:13:31 +000010/******************************************************************************/
11/* Utility functions. */
12/******************************************************************************/
13
John Thompson2e06fc82009-10-27 13:42:56 +000014#ifdef _MSC_VER
15char *basename(const char* path)
16{
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
25
26 return((char*)path);
27}
28#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000029extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000030#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000031
Douglas Gregor45ba9a12010-07-25 17:39:21 +000032/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000033static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
35
36 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000037 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000038 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
Douglas Gregordca8ee82011-05-06 16:33:08 +000040 if (getenv("CINDEXTEST_NESTED_MACROS"))
Chandler Carruthba7537f2011-07-14 09:02:10 +000041 options |= CXTranslationUnit_NestedMacroExpansions;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000042 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
43 options &= ~CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000044
45 return options;
46}
47
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000048static int checkForErrors(CXTranslationUnit TU);
49
Daniel Dunbar51b058c2010-02-14 08:32:24 +000050static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
51 unsigned end_line, unsigned end_column) {
52 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000053 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000054}
55
Ted Kremenek1c6da172009-11-17 19:37:36 +000056static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
57 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000058
Douglas Gregora88084b2010-02-18 18:08:43 +000059 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000060 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000061 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
62 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000063 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000064 return 1;
65}
66
Douglas Gregor4db64a42010-01-23 00:14:00 +000067void free_remapped_files(struct CXUnsavedFile *unsaved_files,
68 int num_unsaved_files) {
69 int i;
70 for (i = 0; i != num_unsaved_files; ++i) {
71 free((char *)unsaved_files[i].Filename);
72 free((char *)unsaved_files[i].Contents);
73 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000074 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000075}
76
77int parse_remapped_files(int argc, const char **argv, int start_arg,
78 struct CXUnsavedFile **unsaved_files,
79 int *num_unsaved_files) {
80 int i;
81 int arg;
82 int prefix_len = strlen("-remap-file=");
83 *unsaved_files = 0;
84 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000085
Douglas Gregor4db64a42010-01-23 00:14:00 +000086 /* Count the number of remapped files. */
87 for (arg = start_arg; arg < argc; ++arg) {
88 if (strncmp(argv[arg], "-remap-file=", prefix_len))
89 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 ++*num_unsaved_files;
92 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000093
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 if (*num_unsaved_files == 0)
95 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000096
Douglas Gregor4db64a42010-01-23 00:14:00 +000097 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000098 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
99 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
101 struct CXUnsavedFile *unsaved = *unsaved_files + i;
102 const char *arg_string = argv[arg] + prefix_len;
103 int filename_len;
104 char *filename;
105 char *contents;
106 FILE *to_file;
107 const char *semi = strchr(arg_string, ';');
108 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000109 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000110 "error: -remap-file=from;to argument is missing semicolon\n");
111 free_remapped_files(*unsaved_files, i);
112 *unsaved_files = 0;
113 *num_unsaved_files = 0;
114 return -1;
115 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000118 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 if (!to_file) {
120 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
121 semi + 1);
122 free_remapped_files(*unsaved_files, i);
123 *unsaved_files = 0;
124 *num_unsaved_files = 0;
125 return -1;
126 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000127
Douglas Gregor4db64a42010-01-23 00:14:00 +0000128 /* Determine the length of the file we're remapping to. */
129 fseek(to_file, 0, SEEK_END);
130 unsaved->Length = ftell(to_file);
131 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000132
Douglas Gregor4db64a42010-01-23 00:14:00 +0000133 /* Read the contents of the file we're remapping to. */
134 contents = (char *)malloc(unsaved->Length + 1);
135 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
136 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
137 (feof(to_file) ? "EOF" : "error"), semi + 1);
138 fclose(to_file);
139 free_remapped_files(*unsaved_files, i);
140 *unsaved_files = 0;
141 *num_unsaved_files = 0;
142 return -1;
143 }
144 contents[unsaved->Length] = 0;
145 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000146
Douglas Gregor4db64a42010-01-23 00:14:00 +0000147 /* Close the file. */
148 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 /* Copy the file name that we're remapping from. */
151 filename_len = semi - arg_string;
152 filename = (char *)malloc(filename_len + 1);
153 memcpy(filename, arg_string, filename_len);
154 filename[filename_len] = 0;
155 unsaved->Filename = filename;
156 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000157
Douglas Gregor4db64a42010-01-23 00:14:00 +0000158 return 0;
159}
160
Ted Kremenek0d435192009-11-17 18:13:31 +0000161/******************************************************************************/
162/* Pretty-printing. */
163/******************************************************************************/
164
Douglas Gregor430d7a12011-07-25 17:48:11 +0000165static void PrintRange(CXSourceRange R, const char *str) {
166 CXFile begin_file, end_file;
167 unsigned begin_line, begin_column, end_line, end_column;
168
169 clang_getSpellingLocation(clang_getRangeStart(R),
170 &begin_file, &begin_line, &begin_column, 0);
171 clang_getSpellingLocation(clang_getRangeEnd(R),
172 &end_file, &end_line, &end_column, 0);
173 if (!begin_file || !end_file)
174 return;
175
176 printf(" %s=", str);
177 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
178}
179
Douglas Gregor358559d2010-10-02 22:49:11 +0000180int want_display_name = 0;
181
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000182static void PrintCursor(CXCursor Cursor) {
183 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000184 if (clang_isInvalid(Cursor.kind)) {
185 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
186 printf("Invalid Cursor => %s", clang_getCString(ks));
187 clang_disposeString(ks);
188 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000189 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000190 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000191 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000192 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000193 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000194 CXCursor *overridden;
195 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000196 unsigned RefNameRangeNr;
197 CXSourceRange CursorExtent;
198 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000199
Ted Kremeneke68fff62010-02-17 00:41:32 +0000200 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000201 string = want_display_name? clang_getCursorDisplayName(Cursor)
202 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000203 printf("%s=%s", clang_getCString(ks),
204 clang_getCString(string));
205 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000206 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000207
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000208 Referenced = clang_getCursorReferenced(Cursor);
209 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000210 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
211 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
212 printf("[");
213 for (I = 0; I != N; ++I) {
214 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000215 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000216 if (I)
217 printf(", ");
218
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000219 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000220 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000221 printf("%d:%d", line, column);
222 }
223 printf("]");
224 } else {
225 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000226 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000227 printf(":%d:%d", line, column);
228 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000229 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000230
231 if (clang_isCursorDefinition(Cursor))
232 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000233
234 switch (clang_getCursorAvailability(Cursor)) {
235 case CXAvailability_Available:
236 break;
237
238 case CXAvailability_Deprecated:
239 printf(" (deprecated)");
240 break;
241
242 case CXAvailability_NotAvailable:
243 printf(" (unavailable)");
244 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000245
246 case CXAvailability_NotAccessible:
247 printf(" (inaccessible)");
248 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000249 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000250
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000251 if (clang_CXXMethod_isStatic(Cursor))
252 printf(" (static)");
253 if (clang_CXXMethod_isVirtual(Cursor))
254 printf(" (virtual)");
255
Ted Kremenek95f33552010-08-26 01:42:22 +0000256 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
257 CXType T =
258 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
259 CXString S = clang_getTypeKindSpelling(T.kind);
260 printf(" [IBOutletCollection=%s]", clang_getCString(S));
261 clang_disposeString(S);
262 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000263
264 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
265 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
266 unsigned isVirtual = clang_isVirtualBase(Cursor);
267 const char *accessStr = 0;
268
269 switch (access) {
270 case CX_CXXInvalidAccessSpecifier:
271 accessStr = "invalid"; break;
272 case CX_CXXPublic:
273 accessStr = "public"; break;
274 case CX_CXXProtected:
275 accessStr = "protected"; break;
276 case CX_CXXPrivate:
277 accessStr = "private"; break;
278 }
279
280 printf(" [access=%s isVirtual=%s]", accessStr,
281 isVirtual ? "true" : "false");
282 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000283
284 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
285 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
286 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
287 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000288 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000289 printf(" [Specialization of %s:%d:%d]",
290 clang_getCString(Name), line, column);
291 clang_disposeString(Name);
292 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000293
294 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
295 if (num_overridden) {
296 unsigned I;
297 printf(" [Overrides ");
298 for (I = 0; I != num_overridden; ++I) {
299 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000300 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000301 if (I)
302 printf(", ");
303 printf("@%d:%d", line, column);
304 }
305 printf("]");
306 clang_disposeOverriddenCursors(overridden);
307 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000308
309 if (Cursor.kind == CXCursor_InclusionDirective) {
310 CXFile File = clang_getIncludedFile(Cursor);
311 CXString Included = clang_getFileName(File);
312 printf(" (%s)", clang_getCString(Included));
313 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000314
315 if (clang_isFileMultipleIncludeGuarded(TU, File))
316 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000317 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000318
319 CursorExtent = clang_getCursorExtent(Cursor);
320 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
321 CXNameRange_WantQualifier
322 | CXNameRange_WantSinglePiece
323 | CXNameRange_WantTemplateArgs,
324 0);
325 if (!clang_equalRanges(CursorExtent, RefNameRange))
326 PrintRange(RefNameRange, "SingleRefName");
327
328 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
329 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
330 CXNameRange_WantQualifier
331 | CXNameRange_WantTemplateArgs,
332 RefNameRangeNr);
333 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
334 break;
335 if (!clang_equalRanges(CursorExtent, RefNameRange))
336 PrintRange(RefNameRange, "RefName");
337 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000338 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000339}
Steve Naroff89922f82009-08-31 00:59:03 +0000340
Ted Kremeneke68fff62010-02-17 00:41:32 +0000341static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000343 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000345 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000346 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000347 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000348 clang_disposeString(source);
349 return "<invalid loc>";
350 }
351 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000352 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000353 clang_disposeString(source);
354 return b;
355 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000356}
357
Ted Kremenek0d435192009-11-17 18:13:31 +0000358/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000359/* Callbacks. */
360/******************************************************************************/
361
362typedef void (*PostVisitTU)(CXTranslationUnit);
363
Douglas Gregora88084b2010-02-18 18:08:43 +0000364void PrintDiagnostic(CXDiagnostic Diagnostic) {
365 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000366 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000367 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000368 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000369 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
370 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000371 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000372
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000373 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000374 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000375
Douglas Gregor274f1902010-02-22 23:17:23 +0000376 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
377 fprintf(stderr, "%s\n", clang_getCString(Msg));
378 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000379
Douglas Gregora9b06d42010-11-09 06:24:54 +0000380 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
381 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000382 if (!file)
383 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000384
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000385 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
386 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000387 CXSourceRange range;
388 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
389 CXSourceLocation start = clang_getRangeStart(range);
390 CXSourceLocation end = clang_getRangeEnd(range);
391 unsigned start_line, start_column, end_line, end_column;
392 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000393 clang_getSpellingLocation(start, &start_file, &start_line,
394 &start_column, 0);
395 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000396 if (clang_equalLocations(start, end)) {
397 /* Insertion. */
398 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000399 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000400 clang_getCString(insertion_text), start_line, start_column);
401 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
402 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000403 if (start_file == file && end_file == file) {
404 fprintf(out, "FIX-IT: Remove ");
405 PrintExtent(out, start_line, start_column, end_line, end_column);
406 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000407 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000408 } else {
409 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000410 if (start_file == end_file) {
411 fprintf(out, "FIX-IT: Replace ");
412 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000413 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000414 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000415 break;
416 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000417 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000418 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000419}
420
Douglas Gregora88084b2010-02-18 18:08:43 +0000421void PrintDiagnostics(CXTranslationUnit TU) {
422 int i, n = clang_getNumDiagnostics(TU);
423 for (i = 0; i != n; ++i) {
424 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
425 PrintDiagnostic(Diag);
426 clang_disposeDiagnostic(Diag);
427 }
428}
429
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000430void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000431 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000432 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000433 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000434 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000435 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000436 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000437 unsigned long amount = usage.entries[i].amount;
438 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000439 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000440 ((double) amount)/(1024*1024));
441 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000442 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000443 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000444 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000445}
446
Ted Kremenekce2ae882010-01-26 17:59:48 +0000447/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000448/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000449/******************************************************************************/
450
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000451static const char *FileCheckPrefix = "CHECK";
452
Douglas Gregora7bde202010-01-19 00:34:46 +0000453static void PrintCursorExtent(CXCursor C) {
454 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000455 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000456}
457
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000458/* Data used by all of the visitors. */
459typedef struct {
460 CXTranslationUnit TU;
461 enum CXCursorKind *Filter;
462} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000463
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000464
Ted Kremeneke68fff62010-02-17 00:41:32 +0000465enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000466 CXCursor Parent,
467 CXClientData ClientData) {
468 VisitorData *Data = (VisitorData *)ClientData;
469 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000470 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000471 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000472 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000473 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000474 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000475 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000476 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000477 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000478 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000479 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000480
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000481 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000482}
Steve Naroff50398192009-08-28 15:28:48 +0000483
Ted Kremeneke68fff62010-02-17 00:41:32 +0000484static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000485 CXCursor Parent,
486 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000487 const char *startBuf, *endBuf;
488 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
489 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000490 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000491
Douglas Gregorb6998662010-01-19 19:34:47 +0000492 if (Cursor.kind != CXCursor_FunctionDecl ||
493 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000494 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495
496 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
497 &startLine, &startColumn,
498 &endLine, &endColumn);
499 /* Probe the entire body, looking for both decls and refs. */
500 curLine = startLine;
501 curColumn = startColumn;
502
503 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000504 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000505 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000506 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000507
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000508 if (*startBuf == '\n') {
509 startBuf++;
510 curLine++;
511 curColumn = 1;
512 } else if (*startBuf != '\t')
513 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000514
Douglas Gregor98258af2010-01-18 22:46:11 +0000515 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000516 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517
Douglas Gregor1db19de2010-01-19 21:36:55 +0000518 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000519 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000520 CXSourceLocation RefLoc
521 = clang_getLocation(Data->TU, file, curLine, curColumn);
522 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000523 if (Ref.kind == CXCursor_NoDeclFound) {
524 /* Nothing found here; that's fine. */
525 } else if (Ref.kind != CXCursor_FunctionDecl) {
526 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
527 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000528 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000529 printf("\n");
530 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000531 }
Ted Kremenek74844072010-02-17 00:41:20 +0000532 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000533 startBuf++;
534 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000535
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000536 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000537}
538
Ted Kremenek7d405622010-01-12 23:34:26 +0000539/******************************************************************************/
540/* USR testing. */
541/******************************************************************************/
542
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000543enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
544 CXClientData ClientData) {
545 VisitorData *Data = (VisitorData *)ClientData;
546 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000547 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000548 const char *cstr = clang_getCString(USR);
549 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000550 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000551 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000552 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000553 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
554
Douglas Gregora7bde202010-01-19 00:34:46 +0000555 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000556 printf("\n");
557 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000558
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000559 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000560 }
561
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000562 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000563}
564
565/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000566/* Inclusion stack testing. */
567/******************************************************************************/
568
569void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
570 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000571
Ted Kremenek16b55a72010-01-26 19:31:51 +0000572 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000573 CXString fname;
574
575 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000576 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000577 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578
Ted Kremenek16b55a72010-01-26 19:31:51 +0000579 for (i = 0; i < includeStackLen; ++i) {
580 CXFile includingFile;
581 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000582 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
583 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000584 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000585 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000586 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000587 }
588 printf("\n");
589}
590
591void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000592 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000593}
594
595/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000596/* Linkage testing. */
597/******************************************************************************/
598
599static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
600 CXClientData d) {
601 const char *linkage = 0;
602
603 if (clang_isInvalid(clang_getCursorKind(cursor)))
604 return CXChildVisit_Recurse;
605
606 switch (clang_getCursorLinkage(cursor)) {
607 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000608 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
609 case CXLinkage_Internal: linkage = "Internal"; break;
610 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
611 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000612 }
613
614 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000615 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000616 printf("linkage=%s\n", linkage);
617 }
618
619 return CXChildVisit_Recurse;
620}
621
622/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000623/* Typekind testing. */
624/******************************************************************************/
625
626static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
627 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000628 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
629 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000630 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000631 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000632 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000633 if (clang_isConstQualifiedType(T))
634 printf(" const");
635 if (clang_isVolatileQualifiedType(T))
636 printf(" volatile");
637 if (clang_isRestrictQualifiedType(T))
638 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000639 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000640 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000641 {
642 CXType CT = clang_getCanonicalType(T);
643 if (!clang_equalTypes(T, CT)) {
644 CXString CS = clang_getTypeKindSpelling(CT.kind);
645 printf(" [canonical=%s]", clang_getCString(CS));
646 clang_disposeString(CS);
647 }
648 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000649 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000650 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000651 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000652 if (RT.kind != CXType_Invalid) {
653 CXString RS = clang_getTypeKindSpelling(RT.kind);
654 printf(" [result=%s]", clang_getCString(RS));
655 clang_disposeString(RS);
656 }
657 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000658 /* Print if this is a non-POD type. */
659 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000661 printf("\n");
662 }
663 return CXChildVisit_Recurse;
664}
665
666
667/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000668/* Loading ASTs/source. */
669/******************************************************************************/
670
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000671static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000672 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000673 CXCursorVisitor Visitor,
674 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000676 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000677 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000678
679 if (Visitor) {
680 enum CXCursorKind K = CXCursor_NotImplemented;
681 enum CXCursorKind *ck = &K;
682 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000684 /* Perform some simple filtering. */
685 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000686 else if (!strcmp(filter, "all-display") ||
687 !strcmp(filter, "local-display")) {
688 ck = NULL;
689 want_display_name = 1;
690 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000691 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000692 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
693 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
694 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
695 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
696 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
697 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
698 else {
699 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
700 return 1;
701 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000702
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000703 Data.TU = TU;
704 Data.Filter = ck;
705 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000706 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707
Ted Kremenekce2ae882010-01-26 17:59:48 +0000708 if (PV)
709 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000710
Douglas Gregora88084b2010-02-18 18:08:43 +0000711 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000712 clang_disposeTranslationUnit(TU);
713 return 0;
714}
715
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000716int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000717 const char *prefix, CXCursorVisitor Visitor,
718 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000719 CXIndex Idx;
720 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000721 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000722 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000723 !strcmp(filter, "local") ? 1 : 0,
724 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000725
Ted Kremenek020a0952010-02-11 07:41:25 +0000726 if (!CreateTranslationUnit(Idx, file, &TU)) {
727 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000728 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000729 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000730
Ted Kremenek020a0952010-02-11 07:41:25 +0000731 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
732 clang_disposeIndex(Idx);
733 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000734}
735
Ted Kremenekce2ae882010-01-26 17:59:48 +0000736int perform_test_load_source(int argc, const char **argv,
737 const char *filter, CXCursorVisitor Visitor,
738 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000739 CXIndex Idx;
740 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000741 struct CXUnsavedFile *unsaved_files = 0;
742 int num_unsaved_files = 0;
743 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000744
Daniel Dunbarada487d2009-12-01 02:03:10 +0000745 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000746 (!strcmp(filter, "local") ||
747 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000748 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000749
Ted Kremenek020a0952010-02-11 07:41:25 +0000750 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
751 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000752 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000753 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000754
Douglas Gregordca8ee82011-05-06 16:33:08 +0000755 TU = clang_parseTranslationUnit(Idx, 0,
756 argv + num_unsaved_files,
757 argc - num_unsaved_files,
758 unsaved_files, num_unsaved_files,
759 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000760 if (!TU) {
761 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000762 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000763 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000764 return 1;
765 }
766
Ted Kremenekce2ae882010-01-26 17:59:48 +0000767 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000768 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000769 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000770 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000771}
772
Douglas Gregorabc563f2010-07-19 21:46:24 +0000773int perform_test_reparse_source(int argc, const char **argv, int trials,
774 const char *filter, CXCursorVisitor Visitor,
775 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000776 CXIndex Idx;
777 CXTranslationUnit TU;
778 struct CXUnsavedFile *unsaved_files = 0;
779 int num_unsaved_files = 0;
780 int result;
781 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000782 int remap_after_trial = 0;
783 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000784
785 Idx = clang_createIndex(/* excludeDeclsFromPCH */
786 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000787 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000788
Douglas Gregorabc563f2010-07-19 21:46:24 +0000789 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
790 clang_disposeIndex(Idx);
791 return -1;
792 }
793
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000794 /* Load the initial translation unit -- we do this without honoring remapped
795 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000796 TU = clang_parseTranslationUnit(Idx, 0,
797 argv + num_unsaved_files,
798 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000799 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000800 if (!TU) {
801 fprintf(stderr, "Unable to load translation unit!\n");
802 free_remapped_files(unsaved_files, num_unsaved_files);
803 clang_disposeIndex(Idx);
804 return 1;
805 }
806
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000807 if (checkForErrors(TU) != 0)
808 return -1;
809
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000810 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
811 remap_after_trial =
812 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
813 }
814
Douglas Gregorabc563f2010-07-19 21:46:24 +0000815 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000816 if (clang_reparseTranslationUnit(TU,
817 trial >= remap_after_trial ? num_unsaved_files : 0,
818 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000819 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000820 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000821 clang_disposeTranslationUnit(TU);
822 free_remapped_files(unsaved_files, num_unsaved_files);
823 clang_disposeIndex(Idx);
824 return -1;
825 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000826
827 if (checkForErrors(TU) != 0)
828 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000829 }
830
831 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000832
833 if (checkForErrors(TU) != 0)
834 return -1;
835
Douglas Gregorabc563f2010-07-19 21:46:24 +0000836 free_remapped_files(unsaved_files, num_unsaved_files);
837 clang_disposeIndex(Idx);
838 return result;
839}
840
Ted Kremenek0d435192009-11-17 18:13:31 +0000841/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000842/* Logic for testing clang_getCursor(). */
843/******************************************************************************/
844
Douglas Gregordd3e5542011-05-04 00:14:37 +0000845static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000846 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000847 unsigned end_line, unsigned end_col,
848 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000849 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000850 if (prefix)
851 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000852 PrintExtent(stdout, start_line, start_col, end_line, end_col);
853 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000854 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000855 printf("\n");
856}
857
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000858static int perform_file_scan(const char *ast_file, const char *source_file,
859 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000860 CXIndex Idx;
861 CXTranslationUnit TU;
862 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000863 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000864 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000865 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000866 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000867
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000868 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
869 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000870 fprintf(stderr, "Could not create Index\n");
871 return 1;
872 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000873
Ted Kremenek1c6da172009-11-17 19:37:36 +0000874 if (!CreateTranslationUnit(Idx, ast_file, &TU))
875 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000876
Ted Kremenek1c6da172009-11-17 19:37:36 +0000877 if ((fp = fopen(source_file, "r")) == NULL) {
878 fprintf(stderr, "Could not open '%s'\n", source_file);
879 return 1;
880 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000881
Douglas Gregorb9790342010-01-22 21:44:22 +0000882 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000883 for (;;) {
884 CXCursor cursor;
885 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000886
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000887 if (c == '\n') {
888 ++line;
889 col = 1;
890 } else
891 ++col;
892
893 /* Check the cursor at this position, and dump the previous one if we have
894 * found something new.
895 */
896 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
897 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
898 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000899 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000900 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000901 start_line = line;
902 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000903 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000904 if (c == EOF)
905 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000906
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000907 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000908 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000909
Ted Kremenek1c6da172009-11-17 19:37:36 +0000910 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000911 clang_disposeTranslationUnit(TU);
912 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000913 return 0;
914}
915
916/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000917/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000918/******************************************************************************/
919
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000920/* Parse file:line:column from the input string. Returns 0 on success, non-zero
921 on failure. If successful, the pointer *filename will contain newly-allocated
922 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000923int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000924 unsigned *column, unsigned *second_line,
925 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000926 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000927 const char *last_colon = strrchr(input, ':');
928 unsigned values[4], i;
929 unsigned num_values = (second_line && second_column)? 4 : 2;
930
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000931 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000932 if (!last_colon || last_colon == input) {
933 if (num_values == 4)
934 fprintf(stderr, "could not parse filename:line:column:line:column in "
935 "'%s'\n", input);
936 else
937 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000938 return 1;
939 }
940
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000941 for (i = 0; i != num_values; ++i) {
942 const char *prev_colon;
943
944 /* Parse the next line or column. */
945 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
946 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000947 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000948 (i % 2 ? "column" : "line"), input);
949 return 1;
950 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000951
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000952 if (i + 1 == num_values)
953 break;
954
955 /* Find the previous colon. */
956 prev_colon = last_colon - 1;
957 while (prev_colon != input && *prev_colon != ':')
958 --prev_colon;
959 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000960 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000961 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000963 }
964
965 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000966 }
967
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000968 *line = values[0];
969 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000970
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000971 if (second_line && second_column) {
972 *second_line = values[2];
973 *second_column = values[3];
974 }
975
Douglas Gregor88d23952009-11-09 18:19:57 +0000976 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000977 *filename = (char*)malloc(last_colon - input + 1);
978 memcpy(*filename, input, last_colon - input);
979 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000980 return 0;
981}
982
983const char *
984clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
985 switch (Kind) {
986 case CXCompletionChunk_Optional: return "Optional";
987 case CXCompletionChunk_TypedText: return "TypedText";
988 case CXCompletionChunk_Text: return "Text";
989 case CXCompletionChunk_Placeholder: return "Placeholder";
990 case CXCompletionChunk_Informative: return "Informative";
991 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
992 case CXCompletionChunk_LeftParen: return "LeftParen";
993 case CXCompletionChunk_RightParen: return "RightParen";
994 case CXCompletionChunk_LeftBracket: return "LeftBracket";
995 case CXCompletionChunk_RightBracket: return "RightBracket";
996 case CXCompletionChunk_LeftBrace: return "LeftBrace";
997 case CXCompletionChunk_RightBrace: return "RightBrace";
998 case CXCompletionChunk_LeftAngle: return "LeftAngle";
999 case CXCompletionChunk_RightAngle: return "RightAngle";
1000 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001001 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001002 case CXCompletionChunk_Colon: return "Colon";
1003 case CXCompletionChunk_SemiColon: return "SemiColon";
1004 case CXCompletionChunk_Equal: return "Equal";
1005 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1006 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001007 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001008
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001009 return "Unknown";
1010}
1011
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001012static int checkForErrors(CXTranslationUnit TU) {
1013 unsigned Num, i;
1014 CXDiagnostic Diag;
1015 CXString DiagStr;
1016
1017 if (!getenv("CINDEXTEST_FAILONERROR"))
1018 return 0;
1019
1020 Num = clang_getNumDiagnostics(TU);
1021 for (i = 0; i != Num; ++i) {
1022 Diag = clang_getDiagnostic(TU, i);
1023 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1024 DiagStr = clang_formatDiagnostic(Diag,
1025 clang_defaultDiagnosticDisplayOptions());
1026 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1027 clang_disposeString(DiagStr);
1028 clang_disposeDiagnostic(Diag);
1029 return -1;
1030 }
1031 clang_disposeDiagnostic(Diag);
1032 }
1033
1034 return 0;
1035}
1036
Douglas Gregor3ac73852009-11-09 16:04:45 +00001037void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001038 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001039
Douglas Gregor3ac73852009-11-09 16:04:45 +00001040 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001041 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001042 CXString text;
1043 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001044 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001045 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001046
Douglas Gregor3ac73852009-11-09 16:04:45 +00001047 if (Kind == CXCompletionChunk_Optional) {
1048 fprintf(file, "{Optional ");
1049 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001050 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001051 file);
1052 fprintf(file, "}");
1053 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001054 }
1055
1056 if (Kind == CXCompletionChunk_VerticalSpace) {
1057 fprintf(file, "{VerticalSpace }");
1058 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001059 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001060
Douglas Gregord5a20892009-11-09 17:05:28 +00001061 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001062 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001063 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001064 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001065 cstr ? cstr : "");
1066 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001067 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001068
Douglas Gregor3ac73852009-11-09 16:04:45 +00001069}
1070
1071void print_completion_result(CXCompletionResult *completion_result,
1072 CXClientData client_data) {
1073 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001074 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001075 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001076
1077 fprintf(file, "%s:", clang_getCString(ks));
1078 clang_disposeString(ks);
1079
Douglas Gregor3ac73852009-11-09 16:04:45 +00001080 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001081 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001082 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001083 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1084 case CXAvailability_Available:
1085 break;
1086
1087 case CXAvailability_Deprecated:
1088 fprintf(file, " (deprecated)");
1089 break;
1090
1091 case CXAvailability_NotAvailable:
1092 fprintf(file, " (unavailable)");
1093 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001094
1095 case CXAvailability_NotAccessible:
1096 fprintf(file, " (inaccessible)");
1097 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001098 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001099
1100 annotationCount = clang_getCompletionNumAnnotations(
1101 completion_result->CompletionString);
1102 if (annotationCount) {
1103 unsigned i;
1104 fprintf(file, " (");
1105 for (i = 0; i < annotationCount; ++i) {
1106 if (i != 0)
1107 fprintf(file, ", ");
1108 fprintf(file, "\"%s\"",
1109 clang_getCString(clang_getCompletionAnnotation(
1110 completion_result->CompletionString, i)));
1111 }
1112 fprintf(file, ")");
1113 }
1114
Douglas Gregor58ddb602010-08-23 23:00:57 +00001115 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001116}
1117
Douglas Gregor3da626b2011-07-07 16:03:39 +00001118void print_completion_contexts(unsigned long long contexts, FILE *file) {
1119 fprintf(file, "Completion contexts:\n");
1120 if (contexts == CXCompletionContext_Unknown) {
1121 fprintf(file, "Unknown\n");
1122 }
1123 if (contexts & CXCompletionContext_AnyType) {
1124 fprintf(file, "Any type\n");
1125 }
1126 if (contexts & CXCompletionContext_AnyValue) {
1127 fprintf(file, "Any value\n");
1128 }
1129 if (contexts & CXCompletionContext_ObjCObjectValue) {
1130 fprintf(file, "Objective-C object value\n");
1131 }
1132 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1133 fprintf(file, "Objective-C selector value\n");
1134 }
1135 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1136 fprintf(file, "C++ class type value\n");
1137 }
1138 if (contexts & CXCompletionContext_DotMemberAccess) {
1139 fprintf(file, "Dot member access\n");
1140 }
1141 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1142 fprintf(file, "Arrow member access\n");
1143 }
1144 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1145 fprintf(file, "Objective-C property access\n");
1146 }
1147 if (contexts & CXCompletionContext_EnumTag) {
1148 fprintf(file, "Enum tag\n");
1149 }
1150 if (contexts & CXCompletionContext_UnionTag) {
1151 fprintf(file, "Union tag\n");
1152 }
1153 if (contexts & CXCompletionContext_StructTag) {
1154 fprintf(file, "Struct tag\n");
1155 }
1156 if (contexts & CXCompletionContext_ClassTag) {
1157 fprintf(file, "Class name\n");
1158 }
1159 if (contexts & CXCompletionContext_Namespace) {
1160 fprintf(file, "Namespace or namespace alias\n");
1161 }
1162 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1163 fprintf(file, "Nested name specifier\n");
1164 }
1165 if (contexts & CXCompletionContext_ObjCInterface) {
1166 fprintf(file, "Objective-C interface\n");
1167 }
1168 if (contexts & CXCompletionContext_ObjCProtocol) {
1169 fprintf(file, "Objective-C protocol\n");
1170 }
1171 if (contexts & CXCompletionContext_ObjCCategory) {
1172 fprintf(file, "Objective-C category\n");
1173 }
1174 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1175 fprintf(file, "Objective-C instance method\n");
1176 }
1177 if (contexts & CXCompletionContext_ObjCClassMessage) {
1178 fprintf(file, "Objective-C class method\n");
1179 }
1180 if (contexts & CXCompletionContext_ObjCSelectorName) {
1181 fprintf(file, "Objective-C selector name\n");
1182 }
1183 if (contexts & CXCompletionContext_MacroName) {
1184 fprintf(file, "Macro name\n");
1185 }
1186 if (contexts & CXCompletionContext_NaturalLanguage) {
1187 fprintf(file, "Natural language\n");
1188 }
1189}
1190
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001191int my_stricmp(const char *s1, const char *s2) {
1192 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001193 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001194 if (c1 < c2)
1195 return -1;
1196 else if (c1 > c2)
1197 return 1;
1198
1199 ++s1;
1200 ++s2;
1201 }
1202
1203 if (*s1)
1204 return 1;
1205 else if (*s2)
1206 return -1;
1207 return 0;
1208}
1209
Douglas Gregor1982c182010-07-12 18:38:41 +00001210int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001211 const char *input = argv[1];
1212 char *filename = 0;
1213 unsigned line;
1214 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001215 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001216 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001217 struct CXUnsavedFile *unsaved_files = 0;
1218 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001219 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001220 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001221 unsigned I, Repeats = 1;
1222 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1223
1224 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1225 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001226
Douglas Gregor1982c182010-07-12 18:38:41 +00001227 if (timing_only)
1228 input += strlen("-code-completion-timing=");
1229 else
1230 input += strlen("-code-completion-at=");
1231
Ted Kremeneke68fff62010-02-17 00:41:32 +00001232 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001233 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001234 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001235
Douglas Gregor735df882009-12-02 09:21:34 +00001236 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1237 return -1;
1238
Douglas Gregor32be4a52010-10-11 21:37:58 +00001239 CIdx = clang_createIndex(0, 0);
1240
1241 if (getenv("CINDEXTEST_EDITING"))
1242 Repeats = 5;
1243
1244 TU = clang_parseTranslationUnit(CIdx, 0,
1245 argv + num_unsaved_files + 2,
1246 argc - num_unsaved_files - 2,
1247 0, 0, getDefaultParsingOptions());
1248 if (!TU) {
1249 fprintf(stderr, "Unable to load translation unit!\n");
1250 return 1;
1251 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001252
1253 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1254 fprintf(stderr, "Unable to reparse translation init!\n");
1255 return 1;
1256 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001257
1258 for (I = 0; I != Repeats; ++I) {
1259 results = clang_codeCompleteAt(TU, filename, line, column,
1260 unsaved_files, num_unsaved_files,
1261 completionOptions);
1262 if (!results) {
1263 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001264 return 1;
1265 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001266 if (I != Repeats-1)
1267 clang_disposeCodeCompleteResults(results);
1268 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001269
Douglas Gregorec6762c2009-12-18 16:20:58 +00001270 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001271 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001272 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001273 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001274 CXString objCSelector;
1275 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001276 if (!timing_only) {
1277 /* Sort the code-completion results based on the typed text. */
1278 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1279
Douglas Gregor1982c182010-07-12 18:38:41 +00001280 for (i = 0; i != n; ++i)
1281 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001282 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001283 n = clang_codeCompleteGetNumDiagnostics(results);
1284 for (i = 0; i != n; ++i) {
1285 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1286 PrintDiagnostic(diag);
1287 clang_disposeDiagnostic(diag);
1288 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001289
1290 contexts = clang_codeCompleteGetContexts(results);
1291 print_completion_contexts(contexts, stdout);
1292
Douglas Gregor0a47d692011-07-26 15:24:30 +00001293 containerKind = clang_codeCompleteGetContainerKind(results,
1294 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001295
1296 if (containerKind != CXCursor_InvalidCode) {
1297 /* We have found a container */
1298 CXString containerUSR, containerKindSpelling;
1299 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1300 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1301 clang_disposeString(containerKindSpelling);
1302
1303 if (containerIsIncomplete) {
1304 printf("Container is incomplete\n");
1305 }
1306 else {
1307 printf("Container is complete\n");
1308 }
1309
1310 containerUSR = clang_codeCompleteGetContainerUSR(results);
1311 printf("Container USR: %s\n", clang_getCString(containerUSR));
1312 clang_disposeString(containerUSR);
1313 }
1314
Douglas Gregor0a47d692011-07-26 15:24:30 +00001315 objCSelector = clang_codeCompleteGetObjCSelector(results);
1316 selectorString = clang_getCString(objCSelector);
1317 if (selectorString && strlen(selectorString) > 0) {
1318 printf("Objective-C selector: %s\n", selectorString);
1319 }
1320 clang_disposeString(objCSelector);
1321
Douglas Gregorec6762c2009-12-18 16:20:58 +00001322 clang_disposeCodeCompleteResults(results);
1323 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001324 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001325 clang_disposeIndex(CIdx);
1326 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001327
Douglas Gregor735df882009-12-02 09:21:34 +00001328 free_remapped_files(unsaved_files, num_unsaved_files);
1329
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001330 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001331}
1332
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001333typedef struct {
1334 char *filename;
1335 unsigned line;
1336 unsigned column;
1337} CursorSourceLocation;
1338
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001339static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001340 CXIndex CIdx;
1341 int errorCode;
1342 struct CXUnsavedFile *unsaved_files = 0;
1343 int num_unsaved_files = 0;
1344 CXTranslationUnit TU;
1345 CXCursor Cursor;
1346 CursorSourceLocation *Locations = 0;
1347 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001348 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001349 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001350
Ted Kremeneke68fff62010-02-17 00:41:32 +00001351 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001352 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1353 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001354
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001355 /* Parse the locations. */
1356 assert(NumLocations > 0 && "Unable to count locations?");
1357 Locations = (CursorSourceLocation *)malloc(
1358 NumLocations * sizeof(CursorSourceLocation));
1359 for (Loc = 0; Loc < NumLocations; ++Loc) {
1360 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001361 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1362 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001363 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001364 return errorCode;
1365 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001366
1367 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001368 &num_unsaved_files))
1369 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001370
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001371 if (getenv("CINDEXTEST_EDITING"))
1372 Repeats = 5;
1373
1374 /* Parse the translation unit. When we're testing clang_getCursor() after
1375 reparsing, don't remap unsaved files until the second parse. */
1376 CIdx = clang_createIndex(1, 1);
1377 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1378 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001379 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001380 unsaved_files,
1381 Repeats > 1? 0 : num_unsaved_files,
1382 getDefaultParsingOptions());
1383
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001384 if (!TU) {
1385 fprintf(stderr, "unable to parse input\n");
1386 return -1;
1387 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001388
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001389 if (checkForErrors(TU) != 0)
1390 return -1;
1391
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001392 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001393 if (Repeats > 1 &&
1394 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1395 clang_defaultReparseOptions(TU))) {
1396 clang_disposeTranslationUnit(TU);
1397 return 1;
1398 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001399
1400 if (checkForErrors(TU) != 0)
1401 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001402
1403 for (Loc = 0; Loc < NumLocations; ++Loc) {
1404 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1405 if (!file)
1406 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001407
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001408 Cursor = clang_getCursor(TU,
1409 clang_getLocation(TU, file, Locations[Loc].line,
1410 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001411
1412 if (checkForErrors(TU) != 0)
1413 return -1;
1414
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001415 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001416 CXCompletionString completionString = clang_getCursorCompletionString(
1417 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001418 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001419 if (completionString != NULL) {
1420 printf("\nCompletion string: ");
1421 print_completion_string(completionString, stdout);
1422 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001423 printf("\n");
1424 free(Locations[Loc].filename);
1425 }
1426 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001427 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001428
Douglas Gregora88084b2010-02-18 18:08:43 +00001429 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001430 clang_disposeTranslationUnit(TU);
1431 clang_disposeIndex(CIdx);
1432 free(Locations);
1433 free_remapped_files(unsaved_files, num_unsaved_files);
1434 return 0;
1435}
1436
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001437static enum CXVisitorResult findFileRefsVisit(void *context,
1438 CXCursor cursor, CXSourceRange range) {
1439 if (clang_Range_isNull(range))
1440 return CXVisit_Continue;
1441
1442 PrintCursor(cursor);
1443 PrintRange(range, "");
1444 printf("\n");
1445 return CXVisit_Continue;
1446}
1447
1448static int find_file_refs_at(int argc, const char **argv) {
1449 CXIndex CIdx;
1450 int errorCode;
1451 struct CXUnsavedFile *unsaved_files = 0;
1452 int num_unsaved_files = 0;
1453 CXTranslationUnit TU;
1454 CXCursor Cursor;
1455 CursorSourceLocation *Locations = 0;
1456 unsigned NumLocations = 0, Loc;
1457 unsigned Repeats = 1;
1458 unsigned I;
1459
1460 /* Count the number of locations. */
1461 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1462 ++NumLocations;
1463
1464 /* Parse the locations. */
1465 assert(NumLocations > 0 && "Unable to count locations?");
1466 Locations = (CursorSourceLocation *)malloc(
1467 NumLocations * sizeof(CursorSourceLocation));
1468 for (Loc = 0; Loc < NumLocations; ++Loc) {
1469 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1470 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1471 &Locations[Loc].line,
1472 &Locations[Loc].column, 0, 0)))
1473 return errorCode;
1474 }
1475
1476 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1477 &num_unsaved_files))
1478 return -1;
1479
1480 if (getenv("CINDEXTEST_EDITING"))
1481 Repeats = 5;
1482
1483 /* Parse the translation unit. When we're testing clang_getCursor() after
1484 reparsing, don't remap unsaved files until the second parse. */
1485 CIdx = clang_createIndex(1, 1);
1486 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1487 argv + num_unsaved_files + 1 + NumLocations,
1488 argc - num_unsaved_files - 2 - NumLocations,
1489 unsaved_files,
1490 Repeats > 1? 0 : num_unsaved_files,
1491 getDefaultParsingOptions());
1492
1493 if (!TU) {
1494 fprintf(stderr, "unable to parse input\n");
1495 return -1;
1496 }
1497
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001498 if (checkForErrors(TU) != 0)
1499 return -1;
1500
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001501 for (I = 0; I != Repeats; ++I) {
1502 if (Repeats > 1 &&
1503 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1504 clang_defaultReparseOptions(TU))) {
1505 clang_disposeTranslationUnit(TU);
1506 return 1;
1507 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001508
1509 if (checkForErrors(TU) != 0)
1510 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001511
1512 for (Loc = 0; Loc < NumLocations; ++Loc) {
1513 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1514 if (!file)
1515 continue;
1516
1517 Cursor = clang_getCursor(TU,
1518 clang_getLocation(TU, file, Locations[Loc].line,
1519 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001520
1521 if (checkForErrors(TU) != 0)
1522 return -1;
1523
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001524 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001525 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001526 PrintCursor(Cursor);
1527 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001528 clang_findReferencesInFile(Cursor, file, visitor);
1529 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001530
1531 if (checkForErrors(TU) != 0)
1532 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001533 }
1534 }
1535 }
1536
1537 PrintDiagnostics(TU);
1538 clang_disposeTranslationUnit(TU);
1539 clang_disposeIndex(CIdx);
1540 free(Locations);
1541 free_remapped_files(unsaved_files, num_unsaved_files);
1542 return 0;
1543}
1544
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001545typedef struct {
1546 const char *check_prefix;
1547 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001548 int fail_for_error;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001549} IndexData;
1550
1551static void printCheck(IndexData *data) {
1552 if (data->check_prefix) {
1553 if (data->first_check_printed) {
1554 printf("// %s-NEXT: ", data->check_prefix);
1555 } else {
1556 printf("// %s : ", data->check_prefix);
1557 data->first_check_printed = 1;
1558 }
1559 }
1560}
1561
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001562static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001563 CXString filename = clang_getFileName((CXFile)file);
1564 printf("%s", clang_getCString(filename));
1565 clang_disposeString(filename);
1566}
1567
1568static void printCXIndexLoc(CXIdxLoc loc) {
1569 CXString filename;
1570 const char *cname, *end;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001571 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001572 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001573 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001574
1575 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1576 if (line == 0) {
1577 printf("<null loc>");
1578 return;
1579 }
1580 filename = clang_getFileName((CXFile)file);
1581 cname = clang_getCString(filename);
1582 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001583 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001584
1585 if (isHeader) {
1586 printCXIndexFile(file);
1587 printf(":");
1588 }
1589 printf("%d:%d", line, column);
1590}
1591
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001592static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1593 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001594 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001595 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001596 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001597 unsigned line, column;
1598
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001599 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001600 if (!name)
1601 name = "<anon-tag>";
1602
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001603 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001604 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001605 newStr = (char *)malloc(strlen(name) + 10);
1606 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001607 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001608}
1609
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001610static void printCXIndexContainer(CXIdxClientContainer container) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001611 printf("[%s]", (const char *)container);
1612}
1613
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001614static const char *getEntityKindString(CXIdxEntityKind kind) {
1615 switch (kind) {
1616 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1617 case CXIdxEntity_Typedef: return "typedef";
1618 case CXIdxEntity_Function: return "function";
1619 case CXIdxEntity_Variable: return "variable";
1620 case CXIdxEntity_Field: return "field";
1621 case CXIdxEntity_EnumConstant: return "enumerator";
1622 case CXIdxEntity_ObjCClass: return "objc-class";
1623 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1624 case CXIdxEntity_ObjCCategory: return "objc-category";
1625 case CXIdxEntity_ObjCMethod: return "objc-method";
1626 case CXIdxEntity_ObjCProperty: return "objc-property";
1627 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1628 case CXIdxEntity_Enum: return "enum";
1629 case CXIdxEntity_Struct: return "struct";
1630 case CXIdxEntity_Union: return "union";
1631 case CXIdxEntity_CXXClass: return "c++-class";
1632 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001633 assert(0 && "Garbage entity kind");
1634 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001635}
1636
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001637static void printEntityInfo(const char *cb,
1638 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001639 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001640 const char *name;
1641 IndexData *index_data;
1642 index_data = (IndexData *)client_data;
1643 printCheck(index_data);
1644
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001645 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001646 if (!name)
1647 name = "<anon-tag>";
1648
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001649 printf("%s: kind: %s", cb, getEntityKindString(info->kind));
1650 printf(" | name: %s", name);
1651 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001652}
1653
1654static void index_diagnostic(CXClientData client_data,
1655 CXDiagnostic diag, void *reserved) {
1656 CXString str;
1657 const char *cstr;
1658 IndexData *index_data;
1659 index_data = (IndexData *)client_data;
1660 printCheck(index_data);
1661
1662 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1663 cstr = clang_getCString(str);
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001664 printf("[diagnostic]: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001665 clang_disposeString(str);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001666
1667 if (getenv("CINDEXTEST_FAILONERROR") &&
1668 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1669 index_data->fail_for_error = 1;
1670 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001671}
1672
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001673static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1674 CXFile file, void *reserved) {
1675 IndexData *index_data;
1676 index_data = (IndexData *)client_data;
1677 printCheck(index_data);
1678
1679 printf("[enteredMainFile]: ");
1680 printCXIndexFile((CXIdxClientFile)file);
1681 printf("\n");
1682
1683 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001684}
1685
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001686static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001687 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001688 IndexData *index_data;
1689 index_data = (IndexData *)client_data;
1690 printCheck(index_data);
1691
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001692 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001693 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001694 printf(" | name: \"%s\"", info->filename);
1695 printf(" | hash loc: ");
1696 printCXIndexLoc(info->hashLoc);
1697 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001698
1699 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001700}
1701
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001702static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001703 void *reserved) {
1704 IndexData *index_data;
1705 index_data = (IndexData *)client_data;
1706 printCheck(index_data);
1707
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001708 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001709 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001710}
1711
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001712static void index_indexDeclaration(CXClientData client_data,
1713 const CXIdxDeclInfo *info,
1714 const CXIdxDeclOut *outData) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001715 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001716 const CXIdxObjCCategoryDeclInfo *CatInfo;
1717 const CXIdxObjCInterfaceDeclInfo *InterInfo;
1718 const CXIdxObjCProtocolDeclInfo *ProtoInfo;
1719 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001720 index_data = (IndexData *)client_data;
1721
1722 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1723 printf(" | cursor: ");
1724 PrintCursor(info->cursor);
1725 printf(" | loc: ");
1726 printCXIndexLoc(info->loc);
1727 printf(" | container: ");
1728 printCXIndexContainer(info->container);
1729 printf(" | isRedecl: %d", info->isRedeclaration);
1730 printf(" | isDef: %d\n", info->isDefinition);
1731
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001732 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1733 const char *kindName = 0;
1734 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1735 switch (K) {
1736 case CXIdxObjCContainer_ForwardRef:
1737 kindName = "forward-ref"; break;
1738 case CXIdxObjCContainer_Interface:
1739 kindName = "interface"; break;
1740 case CXIdxObjCContainer_Implementation:
1741 kindName = "implementation"; break;
1742 }
1743 printCheck(index_data);
1744 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1745 }
1746
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001747 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001748 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1749 CatInfo->objcClass);
1750 printf("\n");
1751 }
1752
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001753 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1754 if (InterInfo->superInfo) {
1755 printEntityInfo(" <ObjCInterfaceInfo>: base", client_data,
1756 InterInfo->superInfo->base);
1757 printf(" | cursor: ");
1758 PrintCursor(InterInfo->superInfo->cursor);
1759 printf(" | loc: ");
1760 printCXIndexLoc(InterInfo->superInfo->loc);
1761 printf("\n");
1762 }
1763 for (i = 0; i < InterInfo->numProtocols; ++i) {
1764 printEntityInfo(" <ObjCInterfaceInfo>: protocol", client_data,
1765 InterInfo->protocols[i]->protocol);
1766 printf(" | cursor: ");
1767 PrintCursor(InterInfo->protocols[i]->cursor);
1768 printf(" | loc: ");
1769 printCXIndexLoc(InterInfo->protocols[i]->loc);
1770 printf("\n");
1771 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001772 }
1773
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001774 if ((ProtoInfo = clang_index_getObjCProtocolDeclInfo(info))) {
1775 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1776 printEntityInfo(" <ObjCProtocolInfo>: protocol", client_data,
1777 ProtoInfo->protocols[i]->protocol);
1778 printf(" | cursor: ");
1779 PrintCursor(ProtoInfo->protocols[i]->cursor);
1780 printf(" | loc: ");
1781 printCXIndexLoc(ProtoInfo->protocols[i]->loc);
1782 printf("\n");
1783 }
1784 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001785
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001786 if (outData->outContainer)
1787 *outData->outContainer = makeClientContainer(info->entityInfo, info->loc);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001788}
1789
1790static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001791 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001792 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001793 printf(" | cursor: ");
1794 PrintCursor(info->cursor);
1795 printf(" | loc: ");
1796 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001797 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001798 printf(" | container: ");
1799 printCXIndexContainer(info->container);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001800 printf(" | kind: ");
1801 switch (info->kind) {
1802 case CXIdxEntityRef_Direct: printf("direct"); break;
1803 case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break;
1804 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001805 printf("\n");
1806}
1807
1808static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001809 0, /*abortQuery*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001810 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001811 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001812 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001813 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001814 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001815 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001816 index_indexEntityReference
1817};
1818
1819static int index_file(int argc, const char **argv) {
1820 const char *check_prefix;
1821 CXIndex CIdx;
1822 IndexData index_data;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001823 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001824
1825 check_prefix = 0;
1826 if (argc > 0) {
1827 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1828 check_prefix = argv[0] + strlen("-check-prefix=");
1829 ++argv;
1830 --argc;
1831 }
1832 }
1833
1834 if (argc == 0) {
1835 fprintf(stderr, "no compiler arguments\n");
1836 return -1;
1837 }
1838
1839 CIdx = clang_createIndex(0, 1);
1840 index_data.check_prefix = check_prefix;
1841 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001842 index_data.fail_for_error = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001843
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001844 result = clang_indexTranslationUnit(CIdx, &index_data,
1845 &IndexCB,sizeof(IndexCB),
1846 0, 0, argv, argc, 0, 0, 0, 0);
1847 if (index_data.fail_for_error)
1848 return -1;
1849
1850 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001851}
1852
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001853int perform_token_annotation(int argc, const char **argv) {
1854 const char *input = argv[1];
1855 char *filename = 0;
1856 unsigned line, second_line;
1857 unsigned column, second_column;
1858 CXIndex CIdx;
1859 CXTranslationUnit TU = 0;
1860 int errorCode;
1861 struct CXUnsavedFile *unsaved_files = 0;
1862 int num_unsaved_files = 0;
1863 CXToken *tokens;
1864 unsigned num_tokens;
1865 CXSourceRange range;
1866 CXSourceLocation startLoc, endLoc;
1867 CXFile file = 0;
1868 CXCursor *cursors = 0;
1869 unsigned i;
1870
1871 input += strlen("-test-annotate-tokens=");
1872 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1873 &second_line, &second_column)))
1874 return errorCode;
1875
1876 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1877 return -1;
1878
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001879 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00001880 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1881 argv + num_unsaved_files + 2,
1882 argc - num_unsaved_files - 3,
1883 unsaved_files,
1884 num_unsaved_files,
1885 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001886 if (!TU) {
1887 fprintf(stderr, "unable to parse input\n");
1888 clang_disposeIndex(CIdx);
1889 free(filename);
1890 free_remapped_files(unsaved_files, num_unsaved_files);
1891 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001892 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001893 errorCode = 0;
1894
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001895 if (checkForErrors(TU) != 0)
1896 return -1;
1897
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00001898 if (getenv("CINDEXTEST_EDITING")) {
1899 for (i = 0; i < 5; ++i) {
1900 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1901 clang_defaultReparseOptions(TU))) {
1902 fprintf(stderr, "Unable to reparse translation unit!\n");
1903 errorCode = -1;
1904 goto teardown;
1905 }
1906 }
1907 }
1908
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001909 if (checkForErrors(TU) != 0) {
1910 errorCode = -1;
1911 goto teardown;
1912 }
1913
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001914 file = clang_getFile(TU, filename);
1915 if (!file) {
1916 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1917 errorCode = -1;
1918 goto teardown;
1919 }
1920
1921 startLoc = clang_getLocation(TU, file, line, column);
1922 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001923 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001924 column);
1925 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001926 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001927 }
1928
1929 endLoc = clang_getLocation(TU, file, second_line, second_column);
1930 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001931 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001932 second_line, second_column);
1933 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001934 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001935 }
1936
1937 range = clang_getRange(startLoc, endLoc);
1938 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001939
1940 if (checkForErrors(TU) != 0) {
1941 errorCode = -1;
1942 goto teardown;
1943 }
1944
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001945 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1946 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001947
1948 if (checkForErrors(TU) != 0) {
1949 errorCode = -1;
1950 goto teardown;
1951 }
1952
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001953 for (i = 0; i != num_tokens; ++i) {
1954 const char *kind = "<unknown>";
1955 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1956 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1957 unsigned start_line, start_column, end_line, end_column;
1958
1959 switch (clang_getTokenKind(tokens[i])) {
1960 case CXToken_Punctuation: kind = "Punctuation"; break;
1961 case CXToken_Keyword: kind = "Keyword"; break;
1962 case CXToken_Identifier: kind = "Identifier"; break;
1963 case CXToken_Literal: kind = "Literal"; break;
1964 case CXToken_Comment: kind = "Comment"; break;
1965 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00001966 clang_getSpellingLocation(clang_getRangeStart(extent),
1967 0, &start_line, &start_column, 0);
1968 clang_getSpellingLocation(clang_getRangeEnd(extent),
1969 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001970 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1971 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001972 if (!clang_isInvalid(cursors[i].kind)) {
1973 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001974 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001975 }
1976 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001977 }
1978 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00001979 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001980
1981 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001982 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001983 clang_disposeTranslationUnit(TU);
1984 clang_disposeIndex(CIdx);
1985 free(filename);
1986 free_remapped_files(unsaved_files, num_unsaved_files);
1987 return errorCode;
1988}
1989
Ted Kremenek0d435192009-11-17 18:13:31 +00001990/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001991/* USR printing. */
1992/******************************************************************************/
1993
1994static int insufficient_usr(const char *kind, const char *usage) {
1995 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1996 return 1;
1997}
1998
1999static unsigned isUSR(const char *s) {
2000 return s[0] == 'c' && s[1] == ':';
2001}
2002
2003static int not_usr(const char *s, const char *arg) {
2004 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2005 return 1;
2006}
2007
2008static void print_usr(CXString usr) {
2009 const char *s = clang_getCString(usr);
2010 printf("%s\n", s);
2011 clang_disposeString(usr);
2012}
2013
2014static void display_usrs() {
2015 fprintf(stderr, "-print-usrs options:\n"
2016 " ObjCCategory <class name> <category name>\n"
2017 " ObjCClass <class name>\n"
2018 " ObjCIvar <ivar name> <class USR>\n"
2019 " ObjCMethod <selector> [0=class method|1=instance method] "
2020 "<class USR>\n"
2021 " ObjCProperty <property name> <class USR>\n"
2022 " ObjCProtocol <protocol name>\n");
2023}
2024
2025int print_usrs(const char **I, const char **E) {
2026 while (I != E) {
2027 const char *kind = *I;
2028 unsigned len = strlen(kind);
2029 switch (len) {
2030 case 8:
2031 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2032 if (I + 2 >= E)
2033 return insufficient_usr(kind, "<ivar name> <class USR>");
2034 if (!isUSR(I[2]))
2035 return not_usr("<class USR>", I[2]);
2036 else {
2037 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002038 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002039 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002040 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2041 }
2042
2043 I += 3;
2044 continue;
2045 }
2046 break;
2047 case 9:
2048 if (memcmp(kind, "ObjCClass", 9) == 0) {
2049 if (I + 1 >= E)
2050 return insufficient_usr(kind, "<class name>");
2051 print_usr(clang_constructUSR_ObjCClass(I[1]));
2052 I += 2;
2053 continue;
2054 }
2055 break;
2056 case 10:
2057 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2058 if (I + 3 >= E)
2059 return insufficient_usr(kind, "<method selector> "
2060 "[0=class method|1=instance method] <class USR>");
2061 if (!isUSR(I[3]))
2062 return not_usr("<class USR>", I[3]);
2063 else {
2064 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002065 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002066 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002067 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2068 }
2069 I += 4;
2070 continue;
2071 }
2072 break;
2073 case 12:
2074 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2075 if (I + 2 >= E)
2076 return insufficient_usr(kind, "<class name> <category name>");
2077 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2078 I += 3;
2079 continue;
2080 }
2081 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2082 if (I + 1 >= E)
2083 return insufficient_usr(kind, "<protocol name>");
2084 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2085 I += 2;
2086 continue;
2087 }
2088 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2089 if (I + 2 >= E)
2090 return insufficient_usr(kind, "<property name> <class USR>");
2091 if (!isUSR(I[2]))
2092 return not_usr("<class USR>", I[2]);
2093 else {
2094 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002095 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002096 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002097 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2098 }
2099 I += 3;
2100 continue;
2101 }
2102 break;
2103 default:
2104 break;
2105 }
2106 break;
2107 }
2108
2109 if (I != E) {
2110 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2111 display_usrs();
2112 return 1;
2113 }
2114 return 0;
2115}
2116
2117int print_usrs_file(const char *file_name) {
2118 char line[2048];
2119 const char *args[128];
2120 unsigned numChars = 0;
2121
2122 FILE *fp = fopen(file_name, "r");
2123 if (!fp) {
2124 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2125 return 1;
2126 }
2127
2128 /* This code is not really all that safe, but it works fine for testing. */
2129 while (!feof(fp)) {
2130 char c = fgetc(fp);
2131 if (c == '\n') {
2132 unsigned i = 0;
2133 const char *s = 0;
2134
2135 if (numChars == 0)
2136 continue;
2137
2138 line[numChars] = '\0';
2139 numChars = 0;
2140
2141 if (line[0] == '/' && line[1] == '/')
2142 continue;
2143
2144 s = strtok(line, " ");
2145 while (s) {
2146 args[i] = s;
2147 ++i;
2148 s = strtok(0, " ");
2149 }
2150 if (print_usrs(&args[0], &args[i]))
2151 return 1;
2152 }
2153 else
2154 line[numChars++] = c;
2155 }
2156
2157 fclose(fp);
2158 return 0;
2159}
2160
2161/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002162/* Command line processing. */
2163/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002164int write_pch_file(const char *filename, int argc, const char *argv[]) {
2165 CXIndex Idx;
2166 CXTranslationUnit TU;
2167 struct CXUnsavedFile *unsaved_files = 0;
2168 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002169 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002170
2171 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2172
2173 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2174 clang_disposeIndex(Idx);
2175 return -1;
2176 }
2177
2178 TU = clang_parseTranslationUnit(Idx, 0,
2179 argv + num_unsaved_files,
2180 argc - num_unsaved_files,
2181 unsaved_files,
2182 num_unsaved_files,
2183 CXTranslationUnit_Incomplete);
2184 if (!TU) {
2185 fprintf(stderr, "Unable to load translation unit!\n");
2186 free_remapped_files(unsaved_files, num_unsaved_files);
2187 clang_disposeIndex(Idx);
2188 return 1;
2189 }
2190
Douglas Gregor39c411f2011-07-06 16:43:36 +00002191 switch (clang_saveTranslationUnit(TU, filename,
2192 clang_defaultSaveOptions(TU))) {
2193 case CXSaveError_None:
2194 break;
2195
2196 case CXSaveError_TranslationErrors:
2197 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2198 filename);
2199 result = 2;
2200 break;
2201
2202 case CXSaveError_InvalidTU:
2203 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2204 filename);
2205 result = 3;
2206 break;
2207
2208 case CXSaveError_Unknown:
2209 default:
2210 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2211 result = 1;
2212 break;
2213 }
2214
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002215 clang_disposeTranslationUnit(TU);
2216 free_remapped_files(unsaved_files, num_unsaved_files);
2217 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002218 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002219}
2220
2221/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002222/* Serialized diagnostics. */
2223/******************************************************************************/
2224
2225static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2226 switch (error) {
2227 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2228 case CXLoadDiag_None: break;
2229 case CXLoadDiag_Unknown: return "Unknown";
2230 case CXLoadDiag_InvalidFile: return "Invalid File";
2231 }
2232 return "None";
2233}
2234
2235static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2236 switch (severity) {
2237 case CXDiagnostic_Note: return "note";
2238 case CXDiagnostic_Error: return "error";
2239 case CXDiagnostic_Fatal: return "fatal";
2240 case CXDiagnostic_Ignored: return "ignored";
2241 case CXDiagnostic_Warning: return "warning";
2242 }
2243 return "unknown";
2244}
2245
2246static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002247 if (indent == 0)
2248 return;
2249 fprintf(stderr, "+");
2250 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002251 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002252 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002253 --indent;
2254 }
2255}
2256
2257static void printLocation(CXSourceLocation L) {
2258 CXFile File;
2259 CXString FileName;
2260 unsigned line, column, offset;
2261
2262 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2263 FileName = clang_getFileName(File);
2264
2265 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2266 clang_disposeString(FileName);
2267}
2268
2269static void printRanges(CXDiagnostic D, unsigned indent) {
2270 unsigned i, n = clang_getDiagnosticNumRanges(D);
2271
2272 for (i = 0; i < n; ++i) {
2273 CXSourceLocation Start, End;
2274 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2275 Start = clang_getRangeStart(SR);
2276 End = clang_getRangeEnd(SR);
2277
2278 printIndent(indent);
2279 fprintf(stderr, "Range: ");
2280 printLocation(Start);
2281 fprintf(stderr, " ");
2282 printLocation(End);
2283 fprintf(stderr, "\n");
2284 }
2285}
2286
2287static void printFixIts(CXDiagnostic D, unsigned indent) {
2288 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2289 for (i = 0 ; i < n; ++i) {
2290 CXSourceRange ReplacementRange;
2291 CXString text;
2292 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2293
2294 printIndent(indent);
2295 fprintf(stderr, "FIXIT: (");
2296 printLocation(clang_getRangeStart(ReplacementRange));
2297 fprintf(stderr, " - ");
2298 printLocation(clang_getRangeEnd(ReplacementRange));
2299 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2300 clang_disposeString(text);
2301 }
2302}
2303
2304static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002305 unsigned i, n;
2306
Ted Kremenek15322172011-11-10 08:43:12 +00002307 if (!Diags)
2308 return;
2309
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002310 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002311 for (i = 0; i < n; ++i) {
2312 CXSourceLocation DiagLoc;
2313 CXDiagnostic D;
2314 CXFile File;
2315 CXString FileName, DiagSpelling, DiagOption;
2316 unsigned line, column, offset;
2317 const char *DiagOptionStr = 0;
2318
2319 D = clang_getDiagnosticInSet(Diags, i);
2320 DiagLoc = clang_getDiagnosticLocation(D);
2321 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2322 FileName = clang_getFileName(File);
2323 DiagSpelling = clang_getDiagnosticSpelling(D);
2324
2325 printIndent(indent);
2326
2327 fprintf(stderr, "%s:%d:%d: %s: %s",
2328 clang_getCString(FileName),
2329 line,
2330 column,
2331 getSeverityString(clang_getDiagnosticSeverity(D)),
2332 clang_getCString(DiagSpelling));
2333
2334 DiagOption = clang_getDiagnosticOption(D, 0);
2335 DiagOptionStr = clang_getCString(DiagOption);
2336 if (DiagOptionStr) {
2337 fprintf(stderr, " [%s]", DiagOptionStr);
2338 }
2339
2340 fprintf(stderr, "\n");
2341
2342 printRanges(D, indent);
2343 printFixIts(D, indent);
2344
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002345 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002346 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2347
2348 clang_disposeString(FileName);
2349 clang_disposeString(DiagSpelling);
2350 clang_disposeString(DiagOption);
2351 }
2352}
2353
2354static int read_diagnostics(const char *filename) {
2355 enum CXLoadDiag_Error error;
2356 CXString errorString;
2357 CXDiagnosticSet Diags = 0;
2358
2359 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2360 if (!Diags) {
2361 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2362 getDiagnosticCodeStr(error),
2363 clang_getCString(errorString));
2364 clang_disposeString(errorString);
2365 return 1;
2366 }
2367
2368 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002369 fprintf(stderr, "Number of diagnostics: %d\n",
2370 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002371 clang_disposeDiagnosticSet(Diags);
2372 return 0;
2373}
2374
2375/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002376/* Command line processing. */
2377/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002378
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002379static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002380 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002381 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002382 if (strcmp(s, "-usrs") == 0)
2383 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002384 if (strncmp(s, "-memory-usage", 13) == 0)
2385 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002386 return NULL;
2387}
2388
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002389static void print_usage(void) {
2390 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002391 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002392 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002393 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002394 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002395 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002396 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002397 "[FileCheck prefix]\n");
2398 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002399 " c-index-test -test-load-tu <AST file> <symbol filter> "
2400 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002401 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2402 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002403 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002404 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002405 " c-index-test -test-load-source-memory-usage "
2406 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002407 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2408 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002409 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002410 " c-index-test -test-load-source-usrs-memory-usage "
2411 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002412 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2413 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002414 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002415 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002416 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002417 " c-index-test -test-print-typekind {<args>}*\n"
2418 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002419 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002420 " c-index-test -write-pch <file> <compiler arguments>\n");
2421 fprintf(stderr,
2422 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002423 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002424 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002425 " all - load all symbols, including those from PCH\n"
2426 " local - load all symbols except those in PCH\n"
2427 " category - only load ObjC categories (non-PCH)\n"
2428 " interface - only load ObjC interfaces (non-PCH)\n"
2429 " protocol - only load ObjC protocols (non-PCH)\n"
2430 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002431 " typedef - only load typdefs (non-PCH)\n"
2432 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002433}
2434
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002435/***/
2436
2437int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002438 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002439 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2440 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002441 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002442 return perform_code_completion(argc, argv, 0);
2443 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2444 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002445 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2446 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002447 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2448 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002449 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2450 return index_file(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002451 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002452 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002453 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002454 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2455 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002456 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002457 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2458 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2459 if (I) {
2460 int trials = atoi(argv[2]);
2461 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2462 NULL);
2463 }
2464 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002465 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002466 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002467
2468 PostVisitTU postVisit = 0;
2469 if (strstr(argv[1], "-memory-usage"))
2470 postVisit = PrintMemoryUsage;
2471
Ted Kremenek7d405622010-01-12 23:34:26 +00002472 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002473 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2474 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002475 }
2476 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002477 return perform_file_scan(argv[2], argv[3],
2478 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002479 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2480 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002481 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2482 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2483 PrintInclusionStack);
2484 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2485 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2486 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002487 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2488 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2489 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002490 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2491 return perform_test_load_source(argc - 2, argv + 2, "all",
2492 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002493 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2494 if (argc > 2)
2495 return print_usrs(argv + 2, argv + argc);
2496 else {
2497 display_usrs();
2498 return 1;
2499 }
2500 }
2501 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2502 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002503 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2504 return write_pch_file(argv[2], argc - 3, argv + 3);
2505
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002506 print_usage();
2507 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002508}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002509
2510/***/
2511
2512/* We intentionally run in a separate thread to ensure we at least minimal
2513 * testing of a multithreaded environment (for example, having a reduced stack
2514 * size). */
2515
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002516typedef struct thread_info {
2517 int argc;
2518 const char **argv;
2519 int result;
2520} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002521void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002522 thread_info *client_data = client_data_v;
2523 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002524}
2525
2526int main(int argc, const char **argv) {
2527 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002528
Douglas Gregor61605982010-10-27 16:00:01 +00002529 if (getenv("CINDEXTEST_NOTHREADS"))
2530 return cindextest_main(argc, argv);
2531
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002532 client_data.argc = argc;
2533 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002534 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002535 return client_data.result;
2536}