blob: c4792784577e39a608527af5b0714fe5c2cb1d3d [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);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000712 if (checkForErrors(TU) != 0) {
713 clang_disposeTranslationUnit(TU);
714 return -1;
715 }
716
Ted Kremenek0d435192009-11-17 18:13:31 +0000717 clang_disposeTranslationUnit(TU);
718 return 0;
719}
720
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000721int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000722 const char *prefix, CXCursorVisitor Visitor,
723 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000724 CXIndex Idx;
725 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000726 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000727 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000728 !strcmp(filter, "local") ? 1 : 0,
729 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000730
Ted Kremenek020a0952010-02-11 07:41:25 +0000731 if (!CreateTranslationUnit(Idx, file, &TU)) {
732 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000733 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000734 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000735
Ted Kremenek020a0952010-02-11 07:41:25 +0000736 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
737 clang_disposeIndex(Idx);
738 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000739}
740
Ted Kremenekce2ae882010-01-26 17:59:48 +0000741int perform_test_load_source(int argc, const char **argv,
742 const char *filter, CXCursorVisitor Visitor,
743 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000744 CXIndex Idx;
745 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000746 struct CXUnsavedFile *unsaved_files = 0;
747 int num_unsaved_files = 0;
748 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000749
Daniel Dunbarada487d2009-12-01 02:03:10 +0000750 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000751 (!strcmp(filter, "local") ||
752 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000753 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000754
Ted Kremenek020a0952010-02-11 07:41:25 +0000755 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
756 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000757 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000758 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000759
Douglas Gregordca8ee82011-05-06 16:33:08 +0000760 TU = clang_parseTranslationUnit(Idx, 0,
761 argv + num_unsaved_files,
762 argc - num_unsaved_files,
763 unsaved_files, num_unsaved_files,
764 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000765 if (!TU) {
766 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000767 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000768 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000769 return 1;
770 }
771
Ted Kremenekce2ae882010-01-26 17:59:48 +0000772 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000773 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000774 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000775 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000776}
777
Douglas Gregorabc563f2010-07-19 21:46:24 +0000778int perform_test_reparse_source(int argc, const char **argv, int trials,
779 const char *filter, CXCursorVisitor Visitor,
780 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000781 CXIndex Idx;
782 CXTranslationUnit TU;
783 struct CXUnsavedFile *unsaved_files = 0;
784 int num_unsaved_files = 0;
785 int result;
786 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000787 int remap_after_trial = 0;
788 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000789
790 Idx = clang_createIndex(/* excludeDeclsFromPCH */
791 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000792 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000793
Douglas Gregorabc563f2010-07-19 21:46:24 +0000794 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
795 clang_disposeIndex(Idx);
796 return -1;
797 }
798
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000799 /* Load the initial translation unit -- we do this without honoring remapped
800 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000801 TU = clang_parseTranslationUnit(Idx, 0,
802 argv + num_unsaved_files,
803 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000804 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000805 if (!TU) {
806 fprintf(stderr, "Unable to load translation unit!\n");
807 free_remapped_files(unsaved_files, num_unsaved_files);
808 clang_disposeIndex(Idx);
809 return 1;
810 }
811
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000812 if (checkForErrors(TU) != 0)
813 return -1;
814
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000815 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
816 remap_after_trial =
817 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
818 }
819
Douglas Gregorabc563f2010-07-19 21:46:24 +0000820 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000821 if (clang_reparseTranslationUnit(TU,
822 trial >= remap_after_trial ? num_unsaved_files : 0,
823 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000824 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000825 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000826 clang_disposeTranslationUnit(TU);
827 free_remapped_files(unsaved_files, num_unsaved_files);
828 clang_disposeIndex(Idx);
829 return -1;
830 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000831
832 if (checkForErrors(TU) != 0)
833 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000834 }
835
836 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000837
Douglas Gregorabc563f2010-07-19 21:46:24 +0000838 free_remapped_files(unsaved_files, num_unsaved_files);
839 clang_disposeIndex(Idx);
840 return result;
841}
842
Ted Kremenek0d435192009-11-17 18:13:31 +0000843/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000844/* Logic for testing clang_getCursor(). */
845/******************************************************************************/
846
Douglas Gregordd3e5542011-05-04 00:14:37 +0000847static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000848 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000849 unsigned end_line, unsigned end_col,
850 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000851 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000852 if (prefix)
853 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000854 PrintExtent(stdout, start_line, start_col, end_line, end_col);
855 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000856 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000857 printf("\n");
858}
859
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000860static int perform_file_scan(const char *ast_file, const char *source_file,
861 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000862 CXIndex Idx;
863 CXTranslationUnit TU;
864 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000865 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000866 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000867 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000868 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000869
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000870 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
871 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000872 fprintf(stderr, "Could not create Index\n");
873 return 1;
874 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875
Ted Kremenek1c6da172009-11-17 19:37:36 +0000876 if (!CreateTranslationUnit(Idx, ast_file, &TU))
877 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000878
Ted Kremenek1c6da172009-11-17 19:37:36 +0000879 if ((fp = fopen(source_file, "r")) == NULL) {
880 fprintf(stderr, "Could not open '%s'\n", source_file);
881 return 1;
882 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883
Douglas Gregorb9790342010-01-22 21:44:22 +0000884 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000885 for (;;) {
886 CXCursor cursor;
887 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000888
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000889 if (c == '\n') {
890 ++line;
891 col = 1;
892 } else
893 ++col;
894
895 /* Check the cursor at this position, and dump the previous one if we have
896 * found something new.
897 */
898 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
899 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
900 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000901 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000902 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000903 start_line = line;
904 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000905 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000906 if (c == EOF)
907 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000908
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000909 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000910 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000911
Ted Kremenek1c6da172009-11-17 19:37:36 +0000912 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000913 clang_disposeTranslationUnit(TU);
914 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000915 return 0;
916}
917
918/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000919/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000920/******************************************************************************/
921
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000922/* Parse file:line:column from the input string. Returns 0 on success, non-zero
923 on failure. If successful, the pointer *filename will contain newly-allocated
924 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000925int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000926 unsigned *column, unsigned *second_line,
927 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000928 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000929 const char *last_colon = strrchr(input, ':');
930 unsigned values[4], i;
931 unsigned num_values = (second_line && second_column)? 4 : 2;
932
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000933 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000934 if (!last_colon || last_colon == input) {
935 if (num_values == 4)
936 fprintf(stderr, "could not parse filename:line:column:line:column in "
937 "'%s'\n", input);
938 else
939 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000940 return 1;
941 }
942
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000943 for (i = 0; i != num_values; ++i) {
944 const char *prev_colon;
945
946 /* Parse the next line or column. */
947 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
948 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000949 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000950 (i % 2 ? "column" : "line"), input);
951 return 1;
952 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000953
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000954 if (i + 1 == num_values)
955 break;
956
957 /* Find the previous colon. */
958 prev_colon = last_colon - 1;
959 while (prev_colon != input && *prev_colon != ':')
960 --prev_colon;
961 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000963 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000964 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000965 }
966
967 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000968 }
969
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000970 *line = values[0];
971 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000973 if (second_line && second_column) {
974 *second_line = values[2];
975 *second_column = values[3];
976 }
977
Douglas Gregor88d23952009-11-09 18:19:57 +0000978 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000979 *filename = (char*)malloc(last_colon - input + 1);
980 memcpy(*filename, input, last_colon - input);
981 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000982 return 0;
983}
984
985const char *
986clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
987 switch (Kind) {
988 case CXCompletionChunk_Optional: return "Optional";
989 case CXCompletionChunk_TypedText: return "TypedText";
990 case CXCompletionChunk_Text: return "Text";
991 case CXCompletionChunk_Placeholder: return "Placeholder";
992 case CXCompletionChunk_Informative: return "Informative";
993 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
994 case CXCompletionChunk_LeftParen: return "LeftParen";
995 case CXCompletionChunk_RightParen: return "RightParen";
996 case CXCompletionChunk_LeftBracket: return "LeftBracket";
997 case CXCompletionChunk_RightBracket: return "RightBracket";
998 case CXCompletionChunk_LeftBrace: return "LeftBrace";
999 case CXCompletionChunk_RightBrace: return "RightBrace";
1000 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1001 case CXCompletionChunk_RightAngle: return "RightAngle";
1002 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001003 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001004 case CXCompletionChunk_Colon: return "Colon";
1005 case CXCompletionChunk_SemiColon: return "SemiColon";
1006 case CXCompletionChunk_Equal: return "Equal";
1007 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1008 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001009 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001010
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001011 return "Unknown";
1012}
1013
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001014static int checkForErrors(CXTranslationUnit TU) {
1015 unsigned Num, i;
1016 CXDiagnostic Diag;
1017 CXString DiagStr;
1018
1019 if (!getenv("CINDEXTEST_FAILONERROR"))
1020 return 0;
1021
1022 Num = clang_getNumDiagnostics(TU);
1023 for (i = 0; i != Num; ++i) {
1024 Diag = clang_getDiagnostic(TU, i);
1025 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1026 DiagStr = clang_formatDiagnostic(Diag,
1027 clang_defaultDiagnosticDisplayOptions());
1028 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1029 clang_disposeString(DiagStr);
1030 clang_disposeDiagnostic(Diag);
1031 return -1;
1032 }
1033 clang_disposeDiagnostic(Diag);
1034 }
1035
1036 return 0;
1037}
1038
Douglas Gregor3ac73852009-11-09 16:04:45 +00001039void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001040 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001041
Douglas Gregor3ac73852009-11-09 16:04:45 +00001042 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001043 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001044 CXString text;
1045 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001046 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001047 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001048
Douglas Gregor3ac73852009-11-09 16:04:45 +00001049 if (Kind == CXCompletionChunk_Optional) {
1050 fprintf(file, "{Optional ");
1051 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001052 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001053 file);
1054 fprintf(file, "}");
1055 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001056 }
1057
1058 if (Kind == CXCompletionChunk_VerticalSpace) {
1059 fprintf(file, "{VerticalSpace }");
1060 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001061 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001062
Douglas Gregord5a20892009-11-09 17:05:28 +00001063 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001064 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001065 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001066 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001067 cstr ? cstr : "");
1068 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001069 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001070
Douglas Gregor3ac73852009-11-09 16:04:45 +00001071}
1072
1073void print_completion_result(CXCompletionResult *completion_result,
1074 CXClientData client_data) {
1075 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001076 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001077 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001078
1079 fprintf(file, "%s:", clang_getCString(ks));
1080 clang_disposeString(ks);
1081
Douglas Gregor3ac73852009-11-09 16:04:45 +00001082 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001083 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001084 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001085 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1086 case CXAvailability_Available:
1087 break;
1088
1089 case CXAvailability_Deprecated:
1090 fprintf(file, " (deprecated)");
1091 break;
1092
1093 case CXAvailability_NotAvailable:
1094 fprintf(file, " (unavailable)");
1095 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001096
1097 case CXAvailability_NotAccessible:
1098 fprintf(file, " (inaccessible)");
1099 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001100 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001101
1102 annotationCount = clang_getCompletionNumAnnotations(
1103 completion_result->CompletionString);
1104 if (annotationCount) {
1105 unsigned i;
1106 fprintf(file, " (");
1107 for (i = 0; i < annotationCount; ++i) {
1108 if (i != 0)
1109 fprintf(file, ", ");
1110 fprintf(file, "\"%s\"",
1111 clang_getCString(clang_getCompletionAnnotation(
1112 completion_result->CompletionString, i)));
1113 }
1114 fprintf(file, ")");
1115 }
1116
Douglas Gregor58ddb602010-08-23 23:00:57 +00001117 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001118}
1119
Douglas Gregor3da626b2011-07-07 16:03:39 +00001120void print_completion_contexts(unsigned long long contexts, FILE *file) {
1121 fprintf(file, "Completion contexts:\n");
1122 if (contexts == CXCompletionContext_Unknown) {
1123 fprintf(file, "Unknown\n");
1124 }
1125 if (contexts & CXCompletionContext_AnyType) {
1126 fprintf(file, "Any type\n");
1127 }
1128 if (contexts & CXCompletionContext_AnyValue) {
1129 fprintf(file, "Any value\n");
1130 }
1131 if (contexts & CXCompletionContext_ObjCObjectValue) {
1132 fprintf(file, "Objective-C object value\n");
1133 }
1134 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1135 fprintf(file, "Objective-C selector value\n");
1136 }
1137 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1138 fprintf(file, "C++ class type value\n");
1139 }
1140 if (contexts & CXCompletionContext_DotMemberAccess) {
1141 fprintf(file, "Dot member access\n");
1142 }
1143 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1144 fprintf(file, "Arrow member access\n");
1145 }
1146 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1147 fprintf(file, "Objective-C property access\n");
1148 }
1149 if (contexts & CXCompletionContext_EnumTag) {
1150 fprintf(file, "Enum tag\n");
1151 }
1152 if (contexts & CXCompletionContext_UnionTag) {
1153 fprintf(file, "Union tag\n");
1154 }
1155 if (contexts & CXCompletionContext_StructTag) {
1156 fprintf(file, "Struct tag\n");
1157 }
1158 if (contexts & CXCompletionContext_ClassTag) {
1159 fprintf(file, "Class name\n");
1160 }
1161 if (contexts & CXCompletionContext_Namespace) {
1162 fprintf(file, "Namespace or namespace alias\n");
1163 }
1164 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1165 fprintf(file, "Nested name specifier\n");
1166 }
1167 if (contexts & CXCompletionContext_ObjCInterface) {
1168 fprintf(file, "Objective-C interface\n");
1169 }
1170 if (contexts & CXCompletionContext_ObjCProtocol) {
1171 fprintf(file, "Objective-C protocol\n");
1172 }
1173 if (contexts & CXCompletionContext_ObjCCategory) {
1174 fprintf(file, "Objective-C category\n");
1175 }
1176 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1177 fprintf(file, "Objective-C instance method\n");
1178 }
1179 if (contexts & CXCompletionContext_ObjCClassMessage) {
1180 fprintf(file, "Objective-C class method\n");
1181 }
1182 if (contexts & CXCompletionContext_ObjCSelectorName) {
1183 fprintf(file, "Objective-C selector name\n");
1184 }
1185 if (contexts & CXCompletionContext_MacroName) {
1186 fprintf(file, "Macro name\n");
1187 }
1188 if (contexts & CXCompletionContext_NaturalLanguage) {
1189 fprintf(file, "Natural language\n");
1190 }
1191}
1192
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001193int my_stricmp(const char *s1, const char *s2) {
1194 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001195 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001196 if (c1 < c2)
1197 return -1;
1198 else if (c1 > c2)
1199 return 1;
1200
1201 ++s1;
1202 ++s2;
1203 }
1204
1205 if (*s1)
1206 return 1;
1207 else if (*s2)
1208 return -1;
1209 return 0;
1210}
1211
Douglas Gregor1982c182010-07-12 18:38:41 +00001212int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001213 const char *input = argv[1];
1214 char *filename = 0;
1215 unsigned line;
1216 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001217 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001218 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001219 struct CXUnsavedFile *unsaved_files = 0;
1220 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001221 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001222 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001223 unsigned I, Repeats = 1;
1224 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1225
1226 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1227 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001228
Douglas Gregor1982c182010-07-12 18:38:41 +00001229 if (timing_only)
1230 input += strlen("-code-completion-timing=");
1231 else
1232 input += strlen("-code-completion-at=");
1233
Ted Kremeneke68fff62010-02-17 00:41:32 +00001234 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001235 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001236 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001237
Douglas Gregor735df882009-12-02 09:21:34 +00001238 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1239 return -1;
1240
Douglas Gregor32be4a52010-10-11 21:37:58 +00001241 CIdx = clang_createIndex(0, 0);
1242
1243 if (getenv("CINDEXTEST_EDITING"))
1244 Repeats = 5;
1245
1246 TU = clang_parseTranslationUnit(CIdx, 0,
1247 argv + num_unsaved_files + 2,
1248 argc - num_unsaved_files - 2,
1249 0, 0, getDefaultParsingOptions());
1250 if (!TU) {
1251 fprintf(stderr, "Unable to load translation unit!\n");
1252 return 1;
1253 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001254
1255 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1256 fprintf(stderr, "Unable to reparse translation init!\n");
1257 return 1;
1258 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001259
1260 for (I = 0; I != Repeats; ++I) {
1261 results = clang_codeCompleteAt(TU, filename, line, column,
1262 unsaved_files, num_unsaved_files,
1263 completionOptions);
1264 if (!results) {
1265 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001266 return 1;
1267 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001268 if (I != Repeats-1)
1269 clang_disposeCodeCompleteResults(results);
1270 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001271
Douglas Gregorec6762c2009-12-18 16:20:58 +00001272 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001273 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001274 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001275 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001276 CXString objCSelector;
1277 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001278 if (!timing_only) {
1279 /* Sort the code-completion results based on the typed text. */
1280 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1281
Douglas Gregor1982c182010-07-12 18:38:41 +00001282 for (i = 0; i != n; ++i)
1283 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001284 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001285 n = clang_codeCompleteGetNumDiagnostics(results);
1286 for (i = 0; i != n; ++i) {
1287 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1288 PrintDiagnostic(diag);
1289 clang_disposeDiagnostic(diag);
1290 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001291
1292 contexts = clang_codeCompleteGetContexts(results);
1293 print_completion_contexts(contexts, stdout);
1294
Douglas Gregor0a47d692011-07-26 15:24:30 +00001295 containerKind = clang_codeCompleteGetContainerKind(results,
1296 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001297
1298 if (containerKind != CXCursor_InvalidCode) {
1299 /* We have found a container */
1300 CXString containerUSR, containerKindSpelling;
1301 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1302 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1303 clang_disposeString(containerKindSpelling);
1304
1305 if (containerIsIncomplete) {
1306 printf("Container is incomplete\n");
1307 }
1308 else {
1309 printf("Container is complete\n");
1310 }
1311
1312 containerUSR = clang_codeCompleteGetContainerUSR(results);
1313 printf("Container USR: %s\n", clang_getCString(containerUSR));
1314 clang_disposeString(containerUSR);
1315 }
1316
Douglas Gregor0a47d692011-07-26 15:24:30 +00001317 objCSelector = clang_codeCompleteGetObjCSelector(results);
1318 selectorString = clang_getCString(objCSelector);
1319 if (selectorString && strlen(selectorString) > 0) {
1320 printf("Objective-C selector: %s\n", selectorString);
1321 }
1322 clang_disposeString(objCSelector);
1323
Douglas Gregorec6762c2009-12-18 16:20:58 +00001324 clang_disposeCodeCompleteResults(results);
1325 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001326 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001327 clang_disposeIndex(CIdx);
1328 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001329
Douglas Gregor735df882009-12-02 09:21:34 +00001330 free_remapped_files(unsaved_files, num_unsaved_files);
1331
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001332 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001333}
1334
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001335typedef struct {
1336 char *filename;
1337 unsigned line;
1338 unsigned column;
1339} CursorSourceLocation;
1340
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001341static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001342 CXIndex CIdx;
1343 int errorCode;
1344 struct CXUnsavedFile *unsaved_files = 0;
1345 int num_unsaved_files = 0;
1346 CXTranslationUnit TU;
1347 CXCursor Cursor;
1348 CursorSourceLocation *Locations = 0;
1349 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001350 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001351 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001352
Ted Kremeneke68fff62010-02-17 00:41:32 +00001353 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001354 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1355 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001356
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001357 /* Parse the locations. */
1358 assert(NumLocations > 0 && "Unable to count locations?");
1359 Locations = (CursorSourceLocation *)malloc(
1360 NumLocations * sizeof(CursorSourceLocation));
1361 for (Loc = 0; Loc < NumLocations; ++Loc) {
1362 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001363 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1364 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001365 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001366 return errorCode;
1367 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368
1369 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001370 &num_unsaved_files))
1371 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001372
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001373 if (getenv("CINDEXTEST_EDITING"))
1374 Repeats = 5;
1375
1376 /* Parse the translation unit. When we're testing clang_getCursor() after
1377 reparsing, don't remap unsaved files until the second parse. */
1378 CIdx = clang_createIndex(1, 1);
1379 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1380 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001381 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001382 unsaved_files,
1383 Repeats > 1? 0 : num_unsaved_files,
1384 getDefaultParsingOptions());
1385
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001386 if (!TU) {
1387 fprintf(stderr, "unable to parse input\n");
1388 return -1;
1389 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001390
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001391 if (checkForErrors(TU) != 0)
1392 return -1;
1393
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001394 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001395 if (Repeats > 1 &&
1396 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1397 clang_defaultReparseOptions(TU))) {
1398 clang_disposeTranslationUnit(TU);
1399 return 1;
1400 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001401
1402 if (checkForErrors(TU) != 0)
1403 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001404
1405 for (Loc = 0; Loc < NumLocations; ++Loc) {
1406 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1407 if (!file)
1408 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001409
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001410 Cursor = clang_getCursor(TU,
1411 clang_getLocation(TU, file, Locations[Loc].line,
1412 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001413
1414 if (checkForErrors(TU) != 0)
1415 return -1;
1416
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001417 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001418 CXCompletionString completionString = clang_getCursorCompletionString(
1419 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001420 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001421 if (completionString != NULL) {
1422 printf("\nCompletion string: ");
1423 print_completion_string(completionString, stdout);
1424 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001425 printf("\n");
1426 free(Locations[Loc].filename);
1427 }
1428 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001429 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001430
Douglas Gregora88084b2010-02-18 18:08:43 +00001431 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001432 clang_disposeTranslationUnit(TU);
1433 clang_disposeIndex(CIdx);
1434 free(Locations);
1435 free_remapped_files(unsaved_files, num_unsaved_files);
1436 return 0;
1437}
1438
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001439static enum CXVisitorResult findFileRefsVisit(void *context,
1440 CXCursor cursor, CXSourceRange range) {
1441 if (clang_Range_isNull(range))
1442 return CXVisit_Continue;
1443
1444 PrintCursor(cursor);
1445 PrintRange(range, "");
1446 printf("\n");
1447 return CXVisit_Continue;
1448}
1449
1450static int find_file_refs_at(int argc, const char **argv) {
1451 CXIndex CIdx;
1452 int errorCode;
1453 struct CXUnsavedFile *unsaved_files = 0;
1454 int num_unsaved_files = 0;
1455 CXTranslationUnit TU;
1456 CXCursor Cursor;
1457 CursorSourceLocation *Locations = 0;
1458 unsigned NumLocations = 0, Loc;
1459 unsigned Repeats = 1;
1460 unsigned I;
1461
1462 /* Count the number of locations. */
1463 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1464 ++NumLocations;
1465
1466 /* Parse the locations. */
1467 assert(NumLocations > 0 && "Unable to count locations?");
1468 Locations = (CursorSourceLocation *)malloc(
1469 NumLocations * sizeof(CursorSourceLocation));
1470 for (Loc = 0; Loc < NumLocations; ++Loc) {
1471 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1472 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1473 &Locations[Loc].line,
1474 &Locations[Loc].column, 0, 0)))
1475 return errorCode;
1476 }
1477
1478 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1479 &num_unsaved_files))
1480 return -1;
1481
1482 if (getenv("CINDEXTEST_EDITING"))
1483 Repeats = 5;
1484
1485 /* Parse the translation unit. When we're testing clang_getCursor() after
1486 reparsing, don't remap unsaved files until the second parse. */
1487 CIdx = clang_createIndex(1, 1);
1488 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1489 argv + num_unsaved_files + 1 + NumLocations,
1490 argc - num_unsaved_files - 2 - NumLocations,
1491 unsaved_files,
1492 Repeats > 1? 0 : num_unsaved_files,
1493 getDefaultParsingOptions());
1494
1495 if (!TU) {
1496 fprintf(stderr, "unable to parse input\n");
1497 return -1;
1498 }
1499
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001500 if (checkForErrors(TU) != 0)
1501 return -1;
1502
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001503 for (I = 0; I != Repeats; ++I) {
1504 if (Repeats > 1 &&
1505 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1506 clang_defaultReparseOptions(TU))) {
1507 clang_disposeTranslationUnit(TU);
1508 return 1;
1509 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001510
1511 if (checkForErrors(TU) != 0)
1512 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001513
1514 for (Loc = 0; Loc < NumLocations; ++Loc) {
1515 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1516 if (!file)
1517 continue;
1518
1519 Cursor = clang_getCursor(TU,
1520 clang_getLocation(TU, file, Locations[Loc].line,
1521 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001522
1523 if (checkForErrors(TU) != 0)
1524 return -1;
1525
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001526 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001527 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001528 PrintCursor(Cursor);
1529 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001530 clang_findReferencesInFile(Cursor, file, visitor);
1531 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001532
1533 if (checkForErrors(TU) != 0)
1534 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001535 }
1536 }
1537 }
1538
1539 PrintDiagnostics(TU);
1540 clang_disposeTranslationUnit(TU);
1541 clang_disposeIndex(CIdx);
1542 free(Locations);
1543 free_remapped_files(unsaved_files, num_unsaved_files);
1544 return 0;
1545}
1546
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001547typedef struct {
1548 const char *check_prefix;
1549 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001550 int fail_for_error;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001551} IndexData;
1552
1553static void printCheck(IndexData *data) {
1554 if (data->check_prefix) {
1555 if (data->first_check_printed) {
1556 printf("// %s-NEXT: ", data->check_prefix);
1557 } else {
1558 printf("// %s : ", data->check_prefix);
1559 data->first_check_printed = 1;
1560 }
1561 }
1562}
1563
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001564static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001565 CXString filename = clang_getFileName((CXFile)file);
1566 printf("%s", clang_getCString(filename));
1567 clang_disposeString(filename);
1568}
1569
1570static void printCXIndexLoc(CXIdxLoc loc) {
1571 CXString filename;
1572 const char *cname, *end;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001573 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001574 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001575 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001576
1577 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1578 if (line == 0) {
1579 printf("<null loc>");
1580 return;
1581 }
1582 filename = clang_getFileName((CXFile)file);
1583 cname = clang_getCString(filename);
1584 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001585 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001586
1587 if (isHeader) {
1588 printCXIndexFile(file);
1589 printf(":");
1590 }
1591 printf("%d:%d", line, column);
1592}
1593
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001594static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1595 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001596 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001597 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001598 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001599 unsigned line, column;
1600
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001601 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001602 if (!name)
1603 name = "<anon-tag>";
1604
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001605 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001606 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001607 newStr = (char *)malloc(strlen(name) + 10);
1608 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001609 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001610}
1611
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001612static void printCXIndexContainer(CXIdxClientContainer container) {
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001613 if (!container)
1614 printf("[<<NULL>>]");
1615 else
1616 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001617}
1618
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001619static const char *getEntityKindString(CXIdxEntityKind kind) {
1620 switch (kind) {
1621 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1622 case CXIdxEntity_Typedef: return "typedef";
1623 case CXIdxEntity_Function: return "function";
1624 case CXIdxEntity_Variable: return "variable";
1625 case CXIdxEntity_Field: return "field";
1626 case CXIdxEntity_EnumConstant: return "enumerator";
1627 case CXIdxEntity_ObjCClass: return "objc-class";
1628 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1629 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001630 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1631 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001632 case CXIdxEntity_ObjCProperty: return "objc-property";
1633 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1634 case CXIdxEntity_Enum: return "enum";
1635 case CXIdxEntity_Struct: return "struct";
1636 case CXIdxEntity_Union: return "union";
1637 case CXIdxEntity_CXXClass: return "c++-class";
1638 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001639 assert(0 && "Garbage entity kind");
1640 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001641}
1642
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001643static void printEntityInfo(const char *cb,
1644 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001645 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001646 const char *name;
1647 IndexData *index_data;
1648 index_data = (IndexData *)client_data;
1649 printCheck(index_data);
1650
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001651 if (!info) {
1652 printf("%s: <<NULL>>", cb);
1653 return;
1654 }
1655
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001656 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001657 if (!name)
1658 name = "<anon-tag>";
1659
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001660 printf("%s: kind: %s", cb, getEntityKindString(info->kind));
1661 printf(" | name: %s", name);
1662 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001663}
1664
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001665static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1666 CXClientData client_data) {
1667 unsigned i;
1668 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1669 printEntityInfo(" <protocol>", client_data,
1670 ProtoInfo->protocols[i]->protocol);
1671 printf(" | cursor: ");
1672 PrintCursor(ProtoInfo->protocols[i]->cursor);
1673 printf(" | loc: ");
1674 printCXIndexLoc(ProtoInfo->protocols[i]->loc);
1675 printf("\n");
1676 }
1677}
1678
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001679static void index_diagnostic(CXClientData client_data,
1680 CXDiagnostic diag, void *reserved) {
1681 CXString str;
1682 const char *cstr;
1683 IndexData *index_data;
1684 index_data = (IndexData *)client_data;
1685 printCheck(index_data);
1686
1687 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1688 cstr = clang_getCString(str);
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001689 printf("[diagnostic]: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001690 clang_disposeString(str);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001691
1692 if (getenv("CINDEXTEST_FAILONERROR") &&
1693 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1694 index_data->fail_for_error = 1;
1695 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001696}
1697
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001698static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1699 CXFile file, void *reserved) {
1700 IndexData *index_data;
1701 index_data = (IndexData *)client_data;
1702 printCheck(index_data);
1703
1704 printf("[enteredMainFile]: ");
1705 printCXIndexFile((CXIdxClientFile)file);
1706 printf("\n");
1707
1708 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001709}
1710
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001711static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001712 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001713 IndexData *index_data;
1714 index_data = (IndexData *)client_data;
1715 printCheck(index_data);
1716
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001717 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001718 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001719 printf(" | name: \"%s\"", info->filename);
1720 printf(" | hash loc: ");
1721 printCXIndexLoc(info->hashLoc);
1722 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001723
1724 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001725}
1726
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001727static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001728 void *reserved) {
1729 IndexData *index_data;
1730 index_data = (IndexData *)client_data;
1731 printCheck(index_data);
1732
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001733 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001734 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001735}
1736
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001737static void index_indexDeclaration(CXClientData client_data,
1738 const CXIdxDeclInfo *info,
1739 const CXIdxDeclOut *outData) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001740 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001741 const CXIdxObjCCategoryDeclInfo *CatInfo;
1742 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001743 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001744 index_data = (IndexData *)client_data;
1745
1746 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1747 printf(" | cursor: ");
1748 PrintCursor(info->cursor);
1749 printf(" | loc: ");
1750 printCXIndexLoc(info->loc);
1751 printf(" | container: ");
1752 printCXIndexContainer(info->container);
1753 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001754 printf(" | isDef: %d", info->isDefinition);
1755 printf(" | isContainer: %d", info->isContainer);
1756 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001757
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001758 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1759 const char *kindName = 0;
1760 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1761 switch (K) {
1762 case CXIdxObjCContainer_ForwardRef:
1763 kindName = "forward-ref"; break;
1764 case CXIdxObjCContainer_Interface:
1765 kindName = "interface"; break;
1766 case CXIdxObjCContainer_Implementation:
1767 kindName = "implementation"; break;
1768 }
1769 printCheck(index_data);
1770 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1771 }
1772
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001773 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001774 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1775 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001776 printf(" | cursor: ");
1777 PrintCursor(CatInfo->classCursor);
1778 printf(" | loc: ");
1779 printCXIndexLoc(CatInfo->classLoc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001780 printf("\n");
1781 }
1782
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001783 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1784 if (InterInfo->superInfo) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001785 printEntityInfo(" <base>", client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001786 InterInfo->superInfo->base);
1787 printf(" | cursor: ");
1788 PrintCursor(InterInfo->superInfo->cursor);
1789 printf(" | loc: ");
1790 printCXIndexLoc(InterInfo->superInfo->loc);
1791 printf("\n");
1792 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001793 }
1794
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001795 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1796 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001797 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001798
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001799 if (outData->outContainer)
1800 *outData->outContainer = makeClientContainer(info->entityInfo, info->loc);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001801}
1802
1803static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001804 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001805 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001806 printf(" | cursor: ");
1807 PrintCursor(info->cursor);
1808 printf(" | loc: ");
1809 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001810 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001811 printf(" | container: ");
1812 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001813 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001814 switch (info->kind) {
1815 case CXIdxEntityRef_Direct: printf("direct"); break;
1816 case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break;
1817 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001818 printf("\n");
1819}
1820
1821static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001822 0, /*abortQuery*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001823 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001824 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001825 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001826 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001827 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001828 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001829 index_indexEntityReference
1830};
1831
1832static int index_file(int argc, const char **argv) {
1833 const char *check_prefix;
1834 CXIndex CIdx;
1835 IndexData index_data;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001836 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001837
1838 check_prefix = 0;
1839 if (argc > 0) {
1840 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1841 check_prefix = argv[0] + strlen("-check-prefix=");
1842 ++argv;
1843 --argc;
1844 }
1845 }
1846
1847 if (argc == 0) {
1848 fprintf(stderr, "no compiler arguments\n");
1849 return -1;
1850 }
1851
1852 CIdx = clang_createIndex(0, 1);
1853 index_data.check_prefix = check_prefix;
1854 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001855 index_data.fail_for_error = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001856
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001857 result = clang_indexSourceFile(CIdx, &index_data,
1858 &IndexCB,sizeof(IndexCB),
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001859 CXIndexOpt_OneRefPerFile,
1860 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001861 if (index_data.fail_for_error)
1862 return -1;
1863
1864 return result;
1865}
1866
1867static int index_tu(int argc, const char **argv) {
1868 CXIndex Idx;
1869 CXTranslationUnit TU;
1870 const char *check_prefix;
1871 IndexData index_data;
1872 int result;
1873
1874 check_prefix = 0;
1875 if (argc > 0) {
1876 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1877 check_prefix = argv[0] + strlen("-check-prefix=");
1878 ++argv;
1879 --argc;
1880 }
1881 }
1882
1883 if (argc == 0) {
1884 fprintf(stderr, "no ast file\n");
1885 return -1;
1886 }
1887
1888 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1889 /* displayDiagnosics=*/1))) {
1890 fprintf(stderr, "Could not create Index\n");
1891 return 1;
1892 }
1893
1894 if (!CreateTranslationUnit(Idx, argv[0], &TU))
1895 return 1;
1896
1897 index_data.check_prefix = check_prefix;
1898 index_data.first_check_printed = 0;
1899 index_data.fail_for_error = 0;
1900
1901 result = clang_indexTranslationUnit(TU, &index_data,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001902 &IndexCB,sizeof(IndexCB),
1903 CXIndexOpt_OneRefPerFile);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001904 if (index_data.fail_for_error)
1905 return -1;
1906
1907 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001908}
1909
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001910int perform_token_annotation(int argc, const char **argv) {
1911 const char *input = argv[1];
1912 char *filename = 0;
1913 unsigned line, second_line;
1914 unsigned column, second_column;
1915 CXIndex CIdx;
1916 CXTranslationUnit TU = 0;
1917 int errorCode;
1918 struct CXUnsavedFile *unsaved_files = 0;
1919 int num_unsaved_files = 0;
1920 CXToken *tokens;
1921 unsigned num_tokens;
1922 CXSourceRange range;
1923 CXSourceLocation startLoc, endLoc;
1924 CXFile file = 0;
1925 CXCursor *cursors = 0;
1926 unsigned i;
1927
1928 input += strlen("-test-annotate-tokens=");
1929 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1930 &second_line, &second_column)))
1931 return errorCode;
1932
1933 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1934 return -1;
1935
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001936 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00001937 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1938 argv + num_unsaved_files + 2,
1939 argc - num_unsaved_files - 3,
1940 unsaved_files,
1941 num_unsaved_files,
1942 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001943 if (!TU) {
1944 fprintf(stderr, "unable to parse input\n");
1945 clang_disposeIndex(CIdx);
1946 free(filename);
1947 free_remapped_files(unsaved_files, num_unsaved_files);
1948 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001949 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001950 errorCode = 0;
1951
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001952 if (checkForErrors(TU) != 0)
1953 return -1;
1954
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00001955 if (getenv("CINDEXTEST_EDITING")) {
1956 for (i = 0; i < 5; ++i) {
1957 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1958 clang_defaultReparseOptions(TU))) {
1959 fprintf(stderr, "Unable to reparse translation unit!\n");
1960 errorCode = -1;
1961 goto teardown;
1962 }
1963 }
1964 }
1965
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001966 if (checkForErrors(TU) != 0) {
1967 errorCode = -1;
1968 goto teardown;
1969 }
1970
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001971 file = clang_getFile(TU, filename);
1972 if (!file) {
1973 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1974 errorCode = -1;
1975 goto teardown;
1976 }
1977
1978 startLoc = clang_getLocation(TU, file, line, column);
1979 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001980 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001981 column);
1982 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001983 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001984 }
1985
1986 endLoc = clang_getLocation(TU, file, second_line, second_column);
1987 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001988 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001989 second_line, second_column);
1990 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001991 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001992 }
1993
1994 range = clang_getRange(startLoc, endLoc);
1995 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001996
1997 if (checkForErrors(TU) != 0) {
1998 errorCode = -1;
1999 goto teardown;
2000 }
2001
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002002 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2003 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002004
2005 if (checkForErrors(TU) != 0) {
2006 errorCode = -1;
2007 goto teardown;
2008 }
2009
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002010 for (i = 0; i != num_tokens; ++i) {
2011 const char *kind = "<unknown>";
2012 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2013 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2014 unsigned start_line, start_column, end_line, end_column;
2015
2016 switch (clang_getTokenKind(tokens[i])) {
2017 case CXToken_Punctuation: kind = "Punctuation"; break;
2018 case CXToken_Keyword: kind = "Keyword"; break;
2019 case CXToken_Identifier: kind = "Identifier"; break;
2020 case CXToken_Literal: kind = "Literal"; break;
2021 case CXToken_Comment: kind = "Comment"; break;
2022 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002023 clang_getSpellingLocation(clang_getRangeStart(extent),
2024 0, &start_line, &start_column, 0);
2025 clang_getSpellingLocation(clang_getRangeEnd(extent),
2026 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002027 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2028 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002029 if (!clang_isInvalid(cursors[i].kind)) {
2030 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002031 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002032 }
2033 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002034 }
2035 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002036 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002037
2038 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002039 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002040 clang_disposeTranslationUnit(TU);
2041 clang_disposeIndex(CIdx);
2042 free(filename);
2043 free_remapped_files(unsaved_files, num_unsaved_files);
2044 return errorCode;
2045}
2046
Ted Kremenek0d435192009-11-17 18:13:31 +00002047/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002048/* USR printing. */
2049/******************************************************************************/
2050
2051static int insufficient_usr(const char *kind, const char *usage) {
2052 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2053 return 1;
2054}
2055
2056static unsigned isUSR(const char *s) {
2057 return s[0] == 'c' && s[1] == ':';
2058}
2059
2060static int not_usr(const char *s, const char *arg) {
2061 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2062 return 1;
2063}
2064
2065static void print_usr(CXString usr) {
2066 const char *s = clang_getCString(usr);
2067 printf("%s\n", s);
2068 clang_disposeString(usr);
2069}
2070
2071static void display_usrs() {
2072 fprintf(stderr, "-print-usrs options:\n"
2073 " ObjCCategory <class name> <category name>\n"
2074 " ObjCClass <class name>\n"
2075 " ObjCIvar <ivar name> <class USR>\n"
2076 " ObjCMethod <selector> [0=class method|1=instance method] "
2077 "<class USR>\n"
2078 " ObjCProperty <property name> <class USR>\n"
2079 " ObjCProtocol <protocol name>\n");
2080}
2081
2082int print_usrs(const char **I, const char **E) {
2083 while (I != E) {
2084 const char *kind = *I;
2085 unsigned len = strlen(kind);
2086 switch (len) {
2087 case 8:
2088 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2089 if (I + 2 >= E)
2090 return insufficient_usr(kind, "<ivar 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_ObjCIvar(I[1], x));
2098 }
2099
2100 I += 3;
2101 continue;
2102 }
2103 break;
2104 case 9:
2105 if (memcmp(kind, "ObjCClass", 9) == 0) {
2106 if (I + 1 >= E)
2107 return insufficient_usr(kind, "<class name>");
2108 print_usr(clang_constructUSR_ObjCClass(I[1]));
2109 I += 2;
2110 continue;
2111 }
2112 break;
2113 case 10:
2114 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2115 if (I + 3 >= E)
2116 return insufficient_usr(kind, "<method selector> "
2117 "[0=class method|1=instance method] <class USR>");
2118 if (!isUSR(I[3]))
2119 return not_usr("<class USR>", I[3]);
2120 else {
2121 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002122 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002123 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002124 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2125 }
2126 I += 4;
2127 continue;
2128 }
2129 break;
2130 case 12:
2131 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2132 if (I + 2 >= E)
2133 return insufficient_usr(kind, "<class name> <category name>");
2134 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2135 I += 3;
2136 continue;
2137 }
2138 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2139 if (I + 1 >= E)
2140 return insufficient_usr(kind, "<protocol name>");
2141 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2142 I += 2;
2143 continue;
2144 }
2145 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2146 if (I + 2 >= E)
2147 return insufficient_usr(kind, "<property name> <class USR>");
2148 if (!isUSR(I[2]))
2149 return not_usr("<class USR>", I[2]);
2150 else {
2151 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002152 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002153 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002154 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2155 }
2156 I += 3;
2157 continue;
2158 }
2159 break;
2160 default:
2161 break;
2162 }
2163 break;
2164 }
2165
2166 if (I != E) {
2167 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2168 display_usrs();
2169 return 1;
2170 }
2171 return 0;
2172}
2173
2174int print_usrs_file(const char *file_name) {
2175 char line[2048];
2176 const char *args[128];
2177 unsigned numChars = 0;
2178
2179 FILE *fp = fopen(file_name, "r");
2180 if (!fp) {
2181 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2182 return 1;
2183 }
2184
2185 /* This code is not really all that safe, but it works fine for testing. */
2186 while (!feof(fp)) {
2187 char c = fgetc(fp);
2188 if (c == '\n') {
2189 unsigned i = 0;
2190 const char *s = 0;
2191
2192 if (numChars == 0)
2193 continue;
2194
2195 line[numChars] = '\0';
2196 numChars = 0;
2197
2198 if (line[0] == '/' && line[1] == '/')
2199 continue;
2200
2201 s = strtok(line, " ");
2202 while (s) {
2203 args[i] = s;
2204 ++i;
2205 s = strtok(0, " ");
2206 }
2207 if (print_usrs(&args[0], &args[i]))
2208 return 1;
2209 }
2210 else
2211 line[numChars++] = c;
2212 }
2213
2214 fclose(fp);
2215 return 0;
2216}
2217
2218/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002219/* Command line processing. */
2220/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002221int write_pch_file(const char *filename, int argc, const char *argv[]) {
2222 CXIndex Idx;
2223 CXTranslationUnit TU;
2224 struct CXUnsavedFile *unsaved_files = 0;
2225 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002226 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002227
2228 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2229
2230 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2231 clang_disposeIndex(Idx);
2232 return -1;
2233 }
2234
2235 TU = clang_parseTranslationUnit(Idx, 0,
2236 argv + num_unsaved_files,
2237 argc - num_unsaved_files,
2238 unsaved_files,
2239 num_unsaved_files,
2240 CXTranslationUnit_Incomplete);
2241 if (!TU) {
2242 fprintf(stderr, "Unable to load translation unit!\n");
2243 free_remapped_files(unsaved_files, num_unsaved_files);
2244 clang_disposeIndex(Idx);
2245 return 1;
2246 }
2247
Douglas Gregor39c411f2011-07-06 16:43:36 +00002248 switch (clang_saveTranslationUnit(TU, filename,
2249 clang_defaultSaveOptions(TU))) {
2250 case CXSaveError_None:
2251 break;
2252
2253 case CXSaveError_TranslationErrors:
2254 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2255 filename);
2256 result = 2;
2257 break;
2258
2259 case CXSaveError_InvalidTU:
2260 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2261 filename);
2262 result = 3;
2263 break;
2264
2265 case CXSaveError_Unknown:
2266 default:
2267 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2268 result = 1;
2269 break;
2270 }
2271
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002272 clang_disposeTranslationUnit(TU);
2273 free_remapped_files(unsaved_files, num_unsaved_files);
2274 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002275 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002276}
2277
2278/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002279/* Serialized diagnostics. */
2280/******************************************************************************/
2281
2282static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2283 switch (error) {
2284 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2285 case CXLoadDiag_None: break;
2286 case CXLoadDiag_Unknown: return "Unknown";
2287 case CXLoadDiag_InvalidFile: return "Invalid File";
2288 }
2289 return "None";
2290}
2291
2292static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2293 switch (severity) {
2294 case CXDiagnostic_Note: return "note";
2295 case CXDiagnostic_Error: return "error";
2296 case CXDiagnostic_Fatal: return "fatal";
2297 case CXDiagnostic_Ignored: return "ignored";
2298 case CXDiagnostic_Warning: return "warning";
2299 }
2300 return "unknown";
2301}
2302
2303static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002304 if (indent == 0)
2305 return;
2306 fprintf(stderr, "+");
2307 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002308 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002309 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002310 --indent;
2311 }
2312}
2313
2314static void printLocation(CXSourceLocation L) {
2315 CXFile File;
2316 CXString FileName;
2317 unsigned line, column, offset;
2318
2319 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2320 FileName = clang_getFileName(File);
2321
2322 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2323 clang_disposeString(FileName);
2324}
2325
2326static void printRanges(CXDiagnostic D, unsigned indent) {
2327 unsigned i, n = clang_getDiagnosticNumRanges(D);
2328
2329 for (i = 0; i < n; ++i) {
2330 CXSourceLocation Start, End;
2331 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2332 Start = clang_getRangeStart(SR);
2333 End = clang_getRangeEnd(SR);
2334
2335 printIndent(indent);
2336 fprintf(stderr, "Range: ");
2337 printLocation(Start);
2338 fprintf(stderr, " ");
2339 printLocation(End);
2340 fprintf(stderr, "\n");
2341 }
2342}
2343
2344static void printFixIts(CXDiagnostic D, unsigned indent) {
2345 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2346 for (i = 0 ; i < n; ++i) {
2347 CXSourceRange ReplacementRange;
2348 CXString text;
2349 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2350
2351 printIndent(indent);
2352 fprintf(stderr, "FIXIT: (");
2353 printLocation(clang_getRangeStart(ReplacementRange));
2354 fprintf(stderr, " - ");
2355 printLocation(clang_getRangeEnd(ReplacementRange));
2356 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2357 clang_disposeString(text);
2358 }
2359}
2360
2361static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002362 unsigned i, n;
2363
Ted Kremenek15322172011-11-10 08:43:12 +00002364 if (!Diags)
2365 return;
2366
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002367 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002368 for (i = 0; i < n; ++i) {
2369 CXSourceLocation DiagLoc;
2370 CXDiagnostic D;
2371 CXFile File;
2372 CXString FileName, DiagSpelling, DiagOption;
2373 unsigned line, column, offset;
2374 const char *DiagOptionStr = 0;
2375
2376 D = clang_getDiagnosticInSet(Diags, i);
2377 DiagLoc = clang_getDiagnosticLocation(D);
2378 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2379 FileName = clang_getFileName(File);
2380 DiagSpelling = clang_getDiagnosticSpelling(D);
2381
2382 printIndent(indent);
2383
2384 fprintf(stderr, "%s:%d:%d: %s: %s",
2385 clang_getCString(FileName),
2386 line,
2387 column,
2388 getSeverityString(clang_getDiagnosticSeverity(D)),
2389 clang_getCString(DiagSpelling));
2390
2391 DiagOption = clang_getDiagnosticOption(D, 0);
2392 DiagOptionStr = clang_getCString(DiagOption);
2393 if (DiagOptionStr) {
2394 fprintf(stderr, " [%s]", DiagOptionStr);
2395 }
2396
2397 fprintf(stderr, "\n");
2398
2399 printRanges(D, indent);
2400 printFixIts(D, indent);
2401
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002402 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002403 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2404
2405 clang_disposeString(FileName);
2406 clang_disposeString(DiagSpelling);
2407 clang_disposeString(DiagOption);
2408 }
2409}
2410
2411static int read_diagnostics(const char *filename) {
2412 enum CXLoadDiag_Error error;
2413 CXString errorString;
2414 CXDiagnosticSet Diags = 0;
2415
2416 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2417 if (!Diags) {
2418 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2419 getDiagnosticCodeStr(error),
2420 clang_getCString(errorString));
2421 clang_disposeString(errorString);
2422 return 1;
2423 }
2424
2425 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002426 fprintf(stderr, "Number of diagnostics: %d\n",
2427 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002428 clang_disposeDiagnosticSet(Diags);
2429 return 0;
2430}
2431
2432/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002433/* Command line processing. */
2434/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002435
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002436static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002437 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002438 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002439 if (strcmp(s, "-usrs") == 0)
2440 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002441 if (strncmp(s, "-memory-usage", 13) == 0)
2442 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002443 return NULL;
2444}
2445
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002446static void print_usage(void) {
2447 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002448 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002449 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002450 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002451 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002452 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002453 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002454 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002455 "[FileCheck prefix]\n");
2456 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002457 " c-index-test -test-load-tu <AST file> <symbol filter> "
2458 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002459 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2460 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002461 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002462 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002463 " c-index-test -test-load-source-memory-usage "
2464 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002465 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2466 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002467 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002468 " c-index-test -test-load-source-usrs-memory-usage "
2469 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002470 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2471 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002472 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002473 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002474 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002475 " c-index-test -test-print-typekind {<args>}*\n"
2476 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002477 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002478 " c-index-test -write-pch <file> <compiler arguments>\n");
2479 fprintf(stderr,
2480 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002481 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002482 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002483 " all - load all symbols, including those from PCH\n"
2484 " local - load all symbols except those in PCH\n"
2485 " category - only load ObjC categories (non-PCH)\n"
2486 " interface - only load ObjC interfaces (non-PCH)\n"
2487 " protocol - only load ObjC protocols (non-PCH)\n"
2488 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002489 " typedef - only load typdefs (non-PCH)\n"
2490 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002491}
2492
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002493/***/
2494
2495int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002496 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002497 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2498 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002499 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002500 return perform_code_completion(argc, argv, 0);
2501 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2502 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002503 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2504 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002505 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2506 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002507 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2508 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002509 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2510 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002511 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002512 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002513 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002514 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2515 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002516 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002517 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2518 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2519 if (I) {
2520 int trials = atoi(argv[2]);
2521 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2522 NULL);
2523 }
2524 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002525 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002526 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002527
2528 PostVisitTU postVisit = 0;
2529 if (strstr(argv[1], "-memory-usage"))
2530 postVisit = PrintMemoryUsage;
2531
Ted Kremenek7d405622010-01-12 23:34:26 +00002532 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002533 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2534 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002535 }
2536 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002537 return perform_file_scan(argv[2], argv[3],
2538 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002539 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2540 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002541 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2542 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2543 PrintInclusionStack);
2544 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2545 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2546 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002547 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2548 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2549 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002550 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2551 return perform_test_load_source(argc - 2, argv + 2, "all",
2552 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002553 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2554 if (argc > 2)
2555 return print_usrs(argv + 2, argv + argc);
2556 else {
2557 display_usrs();
2558 return 1;
2559 }
2560 }
2561 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2562 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002563 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2564 return write_pch_file(argv[2], argc - 3, argv + 3);
2565
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002566 print_usage();
2567 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002568}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002569
2570/***/
2571
2572/* We intentionally run in a separate thread to ensure we at least minimal
2573 * testing of a multithreaded environment (for example, having a reduced stack
2574 * size). */
2575
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002576typedef struct thread_info {
2577 int argc;
2578 const char **argv;
2579 int result;
2580} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002581void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002582 thread_info *client_data = client_data_v;
2583 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002584}
2585
2586int main(int argc, const char **argv) {
2587 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002588
Douglas Gregor61605982010-10-27 16:00:01 +00002589 if (getenv("CINDEXTEST_NOTHREADS"))
2590 return cindextest_main(argc, argv);
2591
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002592 client_data.argc = argc;
2593 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002594 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002595 return client_data.result;
2596}