blob: d0d5b2bce2f74c40af178172926964b2bd0d7977 [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
Daniel Dunbar51b058c2010-02-14 08:32:24 +000048static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
49 unsigned end_line, unsigned end_column) {
50 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000051 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000052}
53
Ted Kremenek1c6da172009-11-17 19:37:36 +000054static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
55 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000056
Douglas Gregora88084b2010-02-18 18:08:43 +000057 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000058 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000059 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
60 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000061 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000062 return 1;
63}
64
Douglas Gregor4db64a42010-01-23 00:14:00 +000065void free_remapped_files(struct CXUnsavedFile *unsaved_files,
66 int num_unsaved_files) {
67 int i;
68 for (i = 0; i != num_unsaved_files; ++i) {
69 free((char *)unsaved_files[i].Filename);
70 free((char *)unsaved_files[i].Contents);
71 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000072 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000073}
74
75int parse_remapped_files(int argc, const char **argv, int start_arg,
76 struct CXUnsavedFile **unsaved_files,
77 int *num_unsaved_files) {
78 int i;
79 int arg;
80 int prefix_len = strlen("-remap-file=");
81 *unsaved_files = 0;
82 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000083
Douglas Gregor4db64a42010-01-23 00:14:00 +000084 /* Count the number of remapped files. */
85 for (arg = start_arg; arg < argc; ++arg) {
86 if (strncmp(argv[arg], "-remap-file=", prefix_len))
87 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000088
Douglas Gregor4db64a42010-01-23 00:14:00 +000089 ++*num_unsaved_files;
90 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000091
Douglas Gregor4db64a42010-01-23 00:14:00 +000092 if (*num_unsaved_files == 0)
93 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000094
Douglas Gregor4db64a42010-01-23 00:14:00 +000095 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000096 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
97 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000098 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
99 struct CXUnsavedFile *unsaved = *unsaved_files + i;
100 const char *arg_string = argv[arg] + prefix_len;
101 int filename_len;
102 char *filename;
103 char *contents;
104 FILE *to_file;
105 const char *semi = strchr(arg_string, ';');
106 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000107 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000108 "error: -remap-file=from;to argument is missing semicolon\n");
109 free_remapped_files(*unsaved_files, i);
110 *unsaved_files = 0;
111 *num_unsaved_files = 0;
112 return -1;
113 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000114
Douglas Gregor4db64a42010-01-23 00:14:00 +0000115 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000116 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 if (!to_file) {
118 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
119 semi + 1);
120 free_remapped_files(*unsaved_files, i);
121 *unsaved_files = 0;
122 *num_unsaved_files = 0;
123 return -1;
124 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000125
Douglas Gregor4db64a42010-01-23 00:14:00 +0000126 /* Determine the length of the file we're remapping to. */
127 fseek(to_file, 0, SEEK_END);
128 unsaved->Length = ftell(to_file);
129 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000130
Douglas Gregor4db64a42010-01-23 00:14:00 +0000131 /* Read the contents of the file we're remapping to. */
132 contents = (char *)malloc(unsaved->Length + 1);
133 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
134 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
135 (feof(to_file) ? "EOF" : "error"), semi + 1);
136 fclose(to_file);
137 free_remapped_files(*unsaved_files, i);
138 *unsaved_files = 0;
139 *num_unsaved_files = 0;
140 return -1;
141 }
142 contents[unsaved->Length] = 0;
143 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000144
Douglas Gregor4db64a42010-01-23 00:14:00 +0000145 /* Close the file. */
146 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000147
Douglas Gregor4db64a42010-01-23 00:14:00 +0000148 /* Copy the file name that we're remapping from. */
149 filename_len = semi - arg_string;
150 filename = (char *)malloc(filename_len + 1);
151 memcpy(filename, arg_string, filename_len);
152 filename[filename_len] = 0;
153 unsaved->Filename = filename;
154 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000155
Douglas Gregor4db64a42010-01-23 00:14:00 +0000156 return 0;
157}
158
Ted Kremenek0d435192009-11-17 18:13:31 +0000159/******************************************************************************/
160/* Pretty-printing. */
161/******************************************************************************/
162
Douglas Gregor430d7a12011-07-25 17:48:11 +0000163static void PrintRange(CXSourceRange R, const char *str) {
164 CXFile begin_file, end_file;
165 unsigned begin_line, begin_column, end_line, end_column;
166
167 clang_getSpellingLocation(clang_getRangeStart(R),
168 &begin_file, &begin_line, &begin_column, 0);
169 clang_getSpellingLocation(clang_getRangeEnd(R),
170 &end_file, &end_line, &end_column, 0);
171 if (!begin_file || !end_file)
172 return;
173
174 printf(" %s=", str);
175 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
176}
177
Douglas Gregor358559d2010-10-02 22:49:11 +0000178int want_display_name = 0;
179
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000180static void PrintCursor(CXCursor Cursor) {
181 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000182 if (clang_isInvalid(Cursor.kind)) {
183 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
184 printf("Invalid Cursor => %s", clang_getCString(ks));
185 clang_disposeString(ks);
186 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000187 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000188 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000189 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000190 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000191 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000192 CXCursor *overridden;
193 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000194 unsigned RefNameRangeNr;
195 CXSourceRange CursorExtent;
196 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000197
Ted Kremeneke68fff62010-02-17 00:41:32 +0000198 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000199 string = want_display_name? clang_getCursorDisplayName(Cursor)
200 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000201 printf("%s=%s", clang_getCString(ks),
202 clang_getCString(string));
203 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000204 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000205
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000206 Referenced = clang_getCursorReferenced(Cursor);
207 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000208 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
209 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
210 printf("[");
211 for (I = 0; I != N; ++I) {
212 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000213 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000214 if (I)
215 printf(", ");
216
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000217 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000218 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000219 printf("%d:%d", line, column);
220 }
221 printf("]");
222 } else {
223 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000224 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000225 printf(":%d:%d", line, column);
226 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000227 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000228
229 if (clang_isCursorDefinition(Cursor))
230 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000231
232 switch (clang_getCursorAvailability(Cursor)) {
233 case CXAvailability_Available:
234 break;
235
236 case CXAvailability_Deprecated:
237 printf(" (deprecated)");
238 break;
239
240 case CXAvailability_NotAvailable:
241 printf(" (unavailable)");
242 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000243
244 case CXAvailability_NotAccessible:
245 printf(" (inaccessible)");
246 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000247 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000248
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000249 if (clang_CXXMethod_isStatic(Cursor))
250 printf(" (static)");
251 if (clang_CXXMethod_isVirtual(Cursor))
252 printf(" (virtual)");
253
Ted Kremenek95f33552010-08-26 01:42:22 +0000254 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
255 CXType T =
256 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
257 CXString S = clang_getTypeKindSpelling(T.kind);
258 printf(" [IBOutletCollection=%s]", clang_getCString(S));
259 clang_disposeString(S);
260 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000261
262 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
263 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
264 unsigned isVirtual = clang_isVirtualBase(Cursor);
265 const char *accessStr = 0;
266
267 switch (access) {
268 case CX_CXXInvalidAccessSpecifier:
269 accessStr = "invalid"; break;
270 case CX_CXXPublic:
271 accessStr = "public"; break;
272 case CX_CXXProtected:
273 accessStr = "protected"; break;
274 case CX_CXXPrivate:
275 accessStr = "private"; break;
276 }
277
278 printf(" [access=%s isVirtual=%s]", accessStr,
279 isVirtual ? "true" : "false");
280 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000281
282 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
283 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
284 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
285 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000286 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000287 printf(" [Specialization of %s:%d:%d]",
288 clang_getCString(Name), line, column);
289 clang_disposeString(Name);
290 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000291
292 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
293 if (num_overridden) {
294 unsigned I;
295 printf(" [Overrides ");
296 for (I = 0; I != num_overridden; ++I) {
297 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000298 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000299 if (I)
300 printf(", ");
301 printf("@%d:%d", line, column);
302 }
303 printf("]");
304 clang_disposeOverriddenCursors(overridden);
305 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000306
307 if (Cursor.kind == CXCursor_InclusionDirective) {
308 CXFile File = clang_getIncludedFile(Cursor);
309 CXString Included = clang_getFileName(File);
310 printf(" (%s)", clang_getCString(Included));
311 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000312
313 if (clang_isFileMultipleIncludeGuarded(TU, File))
314 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000315 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000316
317 CursorExtent = clang_getCursorExtent(Cursor);
318 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
319 CXNameRange_WantQualifier
320 | CXNameRange_WantSinglePiece
321 | CXNameRange_WantTemplateArgs,
322 0);
323 if (!clang_equalRanges(CursorExtent, RefNameRange))
324 PrintRange(RefNameRange, "SingleRefName");
325
326 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
327 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
328 CXNameRange_WantQualifier
329 | CXNameRange_WantTemplateArgs,
330 RefNameRangeNr);
331 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
332 break;
333 if (!clang_equalRanges(CursorExtent, RefNameRange))
334 PrintRange(RefNameRange, "RefName");
335 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000336 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000337}
Steve Naroff89922f82009-08-31 00:59:03 +0000338
Ted Kremeneke68fff62010-02-17 00:41:32 +0000339static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000340 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000341 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000343 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000345 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000346 clang_disposeString(source);
347 return "<invalid loc>";
348 }
349 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000350 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000351 clang_disposeString(source);
352 return b;
353 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000354}
355
Ted Kremenek0d435192009-11-17 18:13:31 +0000356/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000357/* Callbacks. */
358/******************************************************************************/
359
360typedef void (*PostVisitTU)(CXTranslationUnit);
361
Douglas Gregora88084b2010-02-18 18:08:43 +0000362void PrintDiagnostic(CXDiagnostic Diagnostic) {
363 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000364 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000365 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000366 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000367 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
368 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000369 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000370
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000371 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000372 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000373
Douglas Gregor274f1902010-02-22 23:17:23 +0000374 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
375 fprintf(stderr, "%s\n", clang_getCString(Msg));
376 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000377
Douglas Gregora9b06d42010-11-09 06:24:54 +0000378 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
379 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000380 if (!file)
381 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000382
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000383 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
384 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000385 CXSourceRange range;
386 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
387 CXSourceLocation start = clang_getRangeStart(range);
388 CXSourceLocation end = clang_getRangeEnd(range);
389 unsigned start_line, start_column, end_line, end_column;
390 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000391 clang_getSpellingLocation(start, &start_file, &start_line,
392 &start_column, 0);
393 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000394 if (clang_equalLocations(start, end)) {
395 /* Insertion. */
396 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000397 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000398 clang_getCString(insertion_text), start_line, start_column);
399 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
400 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000401 if (start_file == file && end_file == file) {
402 fprintf(out, "FIX-IT: Remove ");
403 PrintExtent(out, start_line, start_column, end_line, end_column);
404 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000405 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000406 } else {
407 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000408 if (start_file == end_file) {
409 fprintf(out, "FIX-IT: Replace ");
410 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000411 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000412 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000413 break;
414 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000415 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000416 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000417}
418
Douglas Gregora88084b2010-02-18 18:08:43 +0000419void PrintDiagnostics(CXTranslationUnit TU) {
420 int i, n = clang_getNumDiagnostics(TU);
421 for (i = 0; i != n; ++i) {
422 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
423 PrintDiagnostic(Diag);
424 clang_disposeDiagnostic(Diag);
425 }
426}
427
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000428void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000429 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000430 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000431 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000432 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000433 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000434 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000435 unsigned long amount = usage.entries[i].amount;
436 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000437 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000438 ((double) amount)/(1024*1024));
439 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000440 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000441 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000442 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000443}
444
Ted Kremenekce2ae882010-01-26 17:59:48 +0000445/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000446/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000447/******************************************************************************/
448
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000449static const char *FileCheckPrefix = "CHECK";
450
Douglas Gregora7bde202010-01-19 00:34:46 +0000451static void PrintCursorExtent(CXCursor C) {
452 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000453 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000454}
455
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000456/* Data used by all of the visitors. */
457typedef struct {
458 CXTranslationUnit TU;
459 enum CXCursorKind *Filter;
460} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000461
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000462
Ted Kremeneke68fff62010-02-17 00:41:32 +0000463enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000464 CXCursor Parent,
465 CXClientData ClientData) {
466 VisitorData *Data = (VisitorData *)ClientData;
467 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000468 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000469 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000470 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000471 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000472 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000473 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000474 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000475 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000476 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000477 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000478
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000479 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000480}
Steve Naroff50398192009-08-28 15:28:48 +0000481
Ted Kremeneke68fff62010-02-17 00:41:32 +0000482static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000483 CXCursor Parent,
484 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000485 const char *startBuf, *endBuf;
486 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
487 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000488 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000489
Douglas Gregorb6998662010-01-19 19:34:47 +0000490 if (Cursor.kind != CXCursor_FunctionDecl ||
491 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000492 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000493
494 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
495 &startLine, &startColumn,
496 &endLine, &endColumn);
497 /* Probe the entire body, looking for both decls and refs. */
498 curLine = startLine;
499 curColumn = startColumn;
500
501 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000502 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000503 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000504 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000505
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000506 if (*startBuf == '\n') {
507 startBuf++;
508 curLine++;
509 curColumn = 1;
510 } else if (*startBuf != '\t')
511 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000512
Douglas Gregor98258af2010-01-18 22:46:11 +0000513 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000514 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515
Douglas Gregor1db19de2010-01-19 21:36:55 +0000516 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000518 CXSourceLocation RefLoc
519 = clang_getLocation(Data->TU, file, curLine, curColumn);
520 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000521 if (Ref.kind == CXCursor_NoDeclFound) {
522 /* Nothing found here; that's fine. */
523 } else if (Ref.kind != CXCursor_FunctionDecl) {
524 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
525 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000526 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000527 printf("\n");
528 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000529 }
Ted Kremenek74844072010-02-17 00:41:20 +0000530 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000531 startBuf++;
532 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000533
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000534 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000535}
536
Ted Kremenek7d405622010-01-12 23:34:26 +0000537/******************************************************************************/
538/* USR testing. */
539/******************************************************************************/
540
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000541enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
542 CXClientData ClientData) {
543 VisitorData *Data = (VisitorData *)ClientData;
544 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000545 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000546 const char *cstr = clang_getCString(USR);
547 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000548 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000549 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000550 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000551 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
552
Douglas Gregora7bde202010-01-19 00:34:46 +0000553 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000554 printf("\n");
555 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000556
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000557 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000558 }
559
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000560 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000561}
562
563/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000564/* Inclusion stack testing. */
565/******************************************************************************/
566
567void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
568 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000569
Ted Kremenek16b55a72010-01-26 19:31:51 +0000570 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000571 CXString fname;
572
573 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000574 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000575 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000576
Ted Kremenek16b55a72010-01-26 19:31:51 +0000577 for (i = 0; i < includeStackLen; ++i) {
578 CXFile includingFile;
579 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000580 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
581 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000582 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000583 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000584 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000585 }
586 printf("\n");
587}
588
589void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000590 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000591}
592
593/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000594/* Linkage testing. */
595/******************************************************************************/
596
597static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
598 CXClientData d) {
599 const char *linkage = 0;
600
601 if (clang_isInvalid(clang_getCursorKind(cursor)))
602 return CXChildVisit_Recurse;
603
604 switch (clang_getCursorLinkage(cursor)) {
605 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000606 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
607 case CXLinkage_Internal: linkage = "Internal"; break;
608 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
609 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000610 }
611
612 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000613 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000614 printf("linkage=%s\n", linkage);
615 }
616
617 return CXChildVisit_Recurse;
618}
619
620/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000621/* Typekind testing. */
622/******************************************************************************/
623
624static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
625 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000626 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
627 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000628 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000629 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000630 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000631 if (clang_isConstQualifiedType(T))
632 printf(" const");
633 if (clang_isVolatileQualifiedType(T))
634 printf(" volatile");
635 if (clang_isRestrictQualifiedType(T))
636 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000637 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000638 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000639 {
640 CXType CT = clang_getCanonicalType(T);
641 if (!clang_equalTypes(T, CT)) {
642 CXString CS = clang_getTypeKindSpelling(CT.kind);
643 printf(" [canonical=%s]", clang_getCString(CS));
644 clang_disposeString(CS);
645 }
646 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000647 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000648 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000649 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000650 if (RT.kind != CXType_Invalid) {
651 CXString RS = clang_getTypeKindSpelling(RT.kind);
652 printf(" [result=%s]", clang_getCString(RS));
653 clang_disposeString(RS);
654 }
655 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000656 /* Print if this is a non-POD type. */
657 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000658
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000659 printf("\n");
660 }
661 return CXChildVisit_Recurse;
662}
663
664
665/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000666/* Loading ASTs/source. */
667/******************************************************************************/
668
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000669static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000670 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000671 CXCursorVisitor Visitor,
672 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000673
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000674 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000676
677 if (Visitor) {
678 enum CXCursorKind K = CXCursor_NotImplemented;
679 enum CXCursorKind *ck = &K;
680 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000681
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000682 /* Perform some simple filtering. */
683 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000684 else if (!strcmp(filter, "all-display") ||
685 !strcmp(filter, "local-display")) {
686 ck = NULL;
687 want_display_name = 1;
688 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000689 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000690 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
691 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
692 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
693 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
694 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
695 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
696 else {
697 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
698 return 1;
699 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000700
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000701 Data.TU = TU;
702 Data.Filter = ck;
703 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000704 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000705
Ted Kremenekce2ae882010-01-26 17:59:48 +0000706 if (PV)
707 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000708
Douglas Gregora88084b2010-02-18 18:08:43 +0000709 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000710 clang_disposeTranslationUnit(TU);
711 return 0;
712}
713
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000714int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000715 const char *prefix, CXCursorVisitor Visitor,
716 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000717 CXIndex Idx;
718 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000719 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000720 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000721 !strcmp(filter, "local") ? 1 : 0,
722 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000723
Ted Kremenek020a0952010-02-11 07:41:25 +0000724 if (!CreateTranslationUnit(Idx, file, &TU)) {
725 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000726 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000727 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000728
Ted Kremenek020a0952010-02-11 07:41:25 +0000729 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
730 clang_disposeIndex(Idx);
731 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000732}
733
Ted Kremenekce2ae882010-01-26 17:59:48 +0000734int perform_test_load_source(int argc, const char **argv,
735 const char *filter, CXCursorVisitor Visitor,
736 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000737 CXIndex Idx;
738 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000739 struct CXUnsavedFile *unsaved_files = 0;
740 int num_unsaved_files = 0;
741 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000742
Daniel Dunbarada487d2009-12-01 02:03:10 +0000743 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000744 (!strcmp(filter, "local") ||
745 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000746 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000747
Ted Kremenek020a0952010-02-11 07:41:25 +0000748 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
749 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000750 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000751 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000752
Douglas Gregordca8ee82011-05-06 16:33:08 +0000753 TU = clang_parseTranslationUnit(Idx, 0,
754 argv + num_unsaved_files,
755 argc - num_unsaved_files,
756 unsaved_files, num_unsaved_files,
757 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000758 if (!TU) {
759 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000760 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000761 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000762 return 1;
763 }
764
Ted Kremenekce2ae882010-01-26 17:59:48 +0000765 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000766 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000767 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000768 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000769}
770
Douglas Gregorabc563f2010-07-19 21:46:24 +0000771int perform_test_reparse_source(int argc, const char **argv, int trials,
772 const char *filter, CXCursorVisitor Visitor,
773 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000774 CXIndex Idx;
775 CXTranslationUnit TU;
776 struct CXUnsavedFile *unsaved_files = 0;
777 int num_unsaved_files = 0;
778 int result;
779 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000780 int remap_after_trial = 0;
781 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000782
783 Idx = clang_createIndex(/* excludeDeclsFromPCH */
784 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000785 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000786
Douglas Gregorabc563f2010-07-19 21:46:24 +0000787 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
788 clang_disposeIndex(Idx);
789 return -1;
790 }
791
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000792 /* Load the initial translation unit -- we do this without honoring remapped
793 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000794 TU = clang_parseTranslationUnit(Idx, 0,
795 argv + num_unsaved_files,
796 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000797 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000798 if (!TU) {
799 fprintf(stderr, "Unable to load translation unit!\n");
800 free_remapped_files(unsaved_files, num_unsaved_files);
801 clang_disposeIndex(Idx);
802 return 1;
803 }
804
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000805 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
806 remap_after_trial =
807 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
808 }
809
Douglas Gregorabc563f2010-07-19 21:46:24 +0000810 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000811 if (clang_reparseTranslationUnit(TU,
812 trial >= remap_after_trial ? num_unsaved_files : 0,
813 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000814 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000815 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000816 clang_disposeTranslationUnit(TU);
817 free_remapped_files(unsaved_files, num_unsaved_files);
818 clang_disposeIndex(Idx);
819 return -1;
820 }
821 }
822
823 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
824 free_remapped_files(unsaved_files, num_unsaved_files);
825 clang_disposeIndex(Idx);
826 return result;
827}
828
Ted Kremenek0d435192009-11-17 18:13:31 +0000829/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000830/* Logic for testing clang_getCursor(). */
831/******************************************************************************/
832
Douglas Gregordd3e5542011-05-04 00:14:37 +0000833static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000834 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000835 unsigned end_line, unsigned end_col,
836 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000837 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000838 if (prefix)
839 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000840 PrintExtent(stdout, start_line, start_col, end_line, end_col);
841 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000842 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000843 printf("\n");
844}
845
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000846static int perform_file_scan(const char *ast_file, const char *source_file,
847 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000848 CXIndex Idx;
849 CXTranslationUnit TU;
850 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000851 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000852 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000853 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000854 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000855
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000856 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
857 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000858 fprintf(stderr, "Could not create Index\n");
859 return 1;
860 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000861
Ted Kremenek1c6da172009-11-17 19:37:36 +0000862 if (!CreateTranslationUnit(Idx, ast_file, &TU))
863 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000864
Ted Kremenek1c6da172009-11-17 19:37:36 +0000865 if ((fp = fopen(source_file, "r")) == NULL) {
866 fprintf(stderr, "Could not open '%s'\n", source_file);
867 return 1;
868 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000869
Douglas Gregorb9790342010-01-22 21:44:22 +0000870 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000871 for (;;) {
872 CXCursor cursor;
873 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000874
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000875 if (c == '\n') {
876 ++line;
877 col = 1;
878 } else
879 ++col;
880
881 /* Check the cursor at this position, and dump the previous one if we have
882 * found something new.
883 */
884 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
885 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
886 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000887 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000888 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000889 start_line = line;
890 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000891 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000892 if (c == EOF)
893 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000894
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000895 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000896 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000897
Ted Kremenek1c6da172009-11-17 19:37:36 +0000898 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000899 clang_disposeTranslationUnit(TU);
900 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000901 return 0;
902}
903
904/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000905/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000906/******************************************************************************/
907
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000908/* Parse file:line:column from the input string. Returns 0 on success, non-zero
909 on failure. If successful, the pointer *filename will contain newly-allocated
910 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000911int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000912 unsigned *column, unsigned *second_line,
913 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000914 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000915 const char *last_colon = strrchr(input, ':');
916 unsigned values[4], i;
917 unsigned num_values = (second_line && second_column)? 4 : 2;
918
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000919 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000920 if (!last_colon || last_colon == input) {
921 if (num_values == 4)
922 fprintf(stderr, "could not parse filename:line:column:line:column in "
923 "'%s'\n", input);
924 else
925 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000926 return 1;
927 }
928
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000929 for (i = 0; i != num_values; ++i) {
930 const char *prev_colon;
931
932 /* Parse the next line or column. */
933 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
934 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000935 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000936 (i % 2 ? "column" : "line"), input);
937 return 1;
938 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000939
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000940 if (i + 1 == num_values)
941 break;
942
943 /* Find the previous colon. */
944 prev_colon = last_colon - 1;
945 while (prev_colon != input && *prev_colon != ':')
946 --prev_colon;
947 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000948 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000949 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000950 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 }
952
953 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000954 }
955
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000956 *line = values[0];
957 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000958
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000959 if (second_line && second_column) {
960 *second_line = values[2];
961 *second_column = values[3];
962 }
963
Douglas Gregor88d23952009-11-09 18:19:57 +0000964 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000965 *filename = (char*)malloc(last_colon - input + 1);
966 memcpy(*filename, input, last_colon - input);
967 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000968 return 0;
969}
970
971const char *
972clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
973 switch (Kind) {
974 case CXCompletionChunk_Optional: return "Optional";
975 case CXCompletionChunk_TypedText: return "TypedText";
976 case CXCompletionChunk_Text: return "Text";
977 case CXCompletionChunk_Placeholder: return "Placeholder";
978 case CXCompletionChunk_Informative: return "Informative";
979 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
980 case CXCompletionChunk_LeftParen: return "LeftParen";
981 case CXCompletionChunk_RightParen: return "RightParen";
982 case CXCompletionChunk_LeftBracket: return "LeftBracket";
983 case CXCompletionChunk_RightBracket: return "RightBracket";
984 case CXCompletionChunk_LeftBrace: return "LeftBrace";
985 case CXCompletionChunk_RightBrace: return "RightBrace";
986 case CXCompletionChunk_LeftAngle: return "LeftAngle";
987 case CXCompletionChunk_RightAngle: return "RightAngle";
988 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000989 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000990 case CXCompletionChunk_Colon: return "Colon";
991 case CXCompletionChunk_SemiColon: return "SemiColon";
992 case CXCompletionChunk_Equal: return "Equal";
993 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
994 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000995 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000996
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000997 return "Unknown";
998}
999
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001000static int checkForErrors(CXTranslationUnit TU) {
1001 unsigned Num, i;
1002 CXDiagnostic Diag;
1003 CXString DiagStr;
1004
1005 if (!getenv("CINDEXTEST_FAILONERROR"))
1006 return 0;
1007
1008 Num = clang_getNumDiagnostics(TU);
1009 for (i = 0; i != Num; ++i) {
1010 Diag = clang_getDiagnostic(TU, i);
1011 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1012 DiagStr = clang_formatDiagnostic(Diag,
1013 clang_defaultDiagnosticDisplayOptions());
1014 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1015 clang_disposeString(DiagStr);
1016 clang_disposeDiagnostic(Diag);
1017 return -1;
1018 }
1019 clang_disposeDiagnostic(Diag);
1020 }
1021
1022 return 0;
1023}
1024
Douglas Gregor3ac73852009-11-09 16:04:45 +00001025void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001026 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001027
Douglas Gregor3ac73852009-11-09 16:04:45 +00001028 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001029 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001030 CXString text;
1031 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001032 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001033 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001034
Douglas Gregor3ac73852009-11-09 16:04:45 +00001035 if (Kind == CXCompletionChunk_Optional) {
1036 fprintf(file, "{Optional ");
1037 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001038 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001039 file);
1040 fprintf(file, "}");
1041 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001042 }
1043
1044 if (Kind == CXCompletionChunk_VerticalSpace) {
1045 fprintf(file, "{VerticalSpace }");
1046 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001047 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001048
Douglas Gregord5a20892009-11-09 17:05:28 +00001049 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001050 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001051 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001052 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001053 cstr ? cstr : "");
1054 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001055 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001056
Douglas Gregor3ac73852009-11-09 16:04:45 +00001057}
1058
1059void print_completion_result(CXCompletionResult *completion_result,
1060 CXClientData client_data) {
1061 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001062 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001063 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001064
1065 fprintf(file, "%s:", clang_getCString(ks));
1066 clang_disposeString(ks);
1067
Douglas Gregor3ac73852009-11-09 16:04:45 +00001068 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001069 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001070 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001071 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1072 case CXAvailability_Available:
1073 break;
1074
1075 case CXAvailability_Deprecated:
1076 fprintf(file, " (deprecated)");
1077 break;
1078
1079 case CXAvailability_NotAvailable:
1080 fprintf(file, " (unavailable)");
1081 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001082
1083 case CXAvailability_NotAccessible:
1084 fprintf(file, " (inaccessible)");
1085 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001086 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001087
1088 annotationCount = clang_getCompletionNumAnnotations(
1089 completion_result->CompletionString);
1090 if (annotationCount) {
1091 unsigned i;
1092 fprintf(file, " (");
1093 for (i = 0; i < annotationCount; ++i) {
1094 if (i != 0)
1095 fprintf(file, ", ");
1096 fprintf(file, "\"%s\"",
1097 clang_getCString(clang_getCompletionAnnotation(
1098 completion_result->CompletionString, i)));
1099 }
1100 fprintf(file, ")");
1101 }
1102
Douglas Gregor58ddb602010-08-23 23:00:57 +00001103 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001104}
1105
Douglas Gregor3da626b2011-07-07 16:03:39 +00001106void print_completion_contexts(unsigned long long contexts, FILE *file) {
1107 fprintf(file, "Completion contexts:\n");
1108 if (contexts == CXCompletionContext_Unknown) {
1109 fprintf(file, "Unknown\n");
1110 }
1111 if (contexts & CXCompletionContext_AnyType) {
1112 fprintf(file, "Any type\n");
1113 }
1114 if (contexts & CXCompletionContext_AnyValue) {
1115 fprintf(file, "Any value\n");
1116 }
1117 if (contexts & CXCompletionContext_ObjCObjectValue) {
1118 fprintf(file, "Objective-C object value\n");
1119 }
1120 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1121 fprintf(file, "Objective-C selector value\n");
1122 }
1123 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1124 fprintf(file, "C++ class type value\n");
1125 }
1126 if (contexts & CXCompletionContext_DotMemberAccess) {
1127 fprintf(file, "Dot member access\n");
1128 }
1129 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1130 fprintf(file, "Arrow member access\n");
1131 }
1132 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1133 fprintf(file, "Objective-C property access\n");
1134 }
1135 if (contexts & CXCompletionContext_EnumTag) {
1136 fprintf(file, "Enum tag\n");
1137 }
1138 if (contexts & CXCompletionContext_UnionTag) {
1139 fprintf(file, "Union tag\n");
1140 }
1141 if (contexts & CXCompletionContext_StructTag) {
1142 fprintf(file, "Struct tag\n");
1143 }
1144 if (contexts & CXCompletionContext_ClassTag) {
1145 fprintf(file, "Class name\n");
1146 }
1147 if (contexts & CXCompletionContext_Namespace) {
1148 fprintf(file, "Namespace or namespace alias\n");
1149 }
1150 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1151 fprintf(file, "Nested name specifier\n");
1152 }
1153 if (contexts & CXCompletionContext_ObjCInterface) {
1154 fprintf(file, "Objective-C interface\n");
1155 }
1156 if (contexts & CXCompletionContext_ObjCProtocol) {
1157 fprintf(file, "Objective-C protocol\n");
1158 }
1159 if (contexts & CXCompletionContext_ObjCCategory) {
1160 fprintf(file, "Objective-C category\n");
1161 }
1162 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1163 fprintf(file, "Objective-C instance method\n");
1164 }
1165 if (contexts & CXCompletionContext_ObjCClassMessage) {
1166 fprintf(file, "Objective-C class method\n");
1167 }
1168 if (contexts & CXCompletionContext_ObjCSelectorName) {
1169 fprintf(file, "Objective-C selector name\n");
1170 }
1171 if (contexts & CXCompletionContext_MacroName) {
1172 fprintf(file, "Macro name\n");
1173 }
1174 if (contexts & CXCompletionContext_NaturalLanguage) {
1175 fprintf(file, "Natural language\n");
1176 }
1177}
1178
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001179int my_stricmp(const char *s1, const char *s2) {
1180 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001181 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001182 if (c1 < c2)
1183 return -1;
1184 else if (c1 > c2)
1185 return 1;
1186
1187 ++s1;
1188 ++s2;
1189 }
1190
1191 if (*s1)
1192 return 1;
1193 else if (*s2)
1194 return -1;
1195 return 0;
1196}
1197
Douglas Gregor1982c182010-07-12 18:38:41 +00001198int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001199 const char *input = argv[1];
1200 char *filename = 0;
1201 unsigned line;
1202 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001203 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001204 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001205 struct CXUnsavedFile *unsaved_files = 0;
1206 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001207 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001208 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001209 unsigned I, Repeats = 1;
1210 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1211
1212 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1213 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001214
Douglas Gregor1982c182010-07-12 18:38:41 +00001215 if (timing_only)
1216 input += strlen("-code-completion-timing=");
1217 else
1218 input += strlen("-code-completion-at=");
1219
Ted Kremeneke68fff62010-02-17 00:41:32 +00001220 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001221 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001222 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001223
Douglas Gregor735df882009-12-02 09:21:34 +00001224 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1225 return -1;
1226
Douglas Gregor32be4a52010-10-11 21:37:58 +00001227 CIdx = clang_createIndex(0, 0);
1228
1229 if (getenv("CINDEXTEST_EDITING"))
1230 Repeats = 5;
1231
1232 TU = clang_parseTranslationUnit(CIdx, 0,
1233 argv + num_unsaved_files + 2,
1234 argc - num_unsaved_files - 2,
1235 0, 0, getDefaultParsingOptions());
1236 if (!TU) {
1237 fprintf(stderr, "Unable to load translation unit!\n");
1238 return 1;
1239 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001240
1241 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1242 fprintf(stderr, "Unable to reparse translation init!\n");
1243 return 1;
1244 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001245
1246 for (I = 0; I != Repeats; ++I) {
1247 results = clang_codeCompleteAt(TU, filename, line, column,
1248 unsaved_files, num_unsaved_files,
1249 completionOptions);
1250 if (!results) {
1251 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001252 return 1;
1253 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001254 if (I != Repeats-1)
1255 clang_disposeCodeCompleteResults(results);
1256 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001257
Douglas Gregorec6762c2009-12-18 16:20:58 +00001258 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001259 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001260 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001261 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001262 CXString objCSelector;
1263 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001264 if (!timing_only) {
1265 /* Sort the code-completion results based on the typed text. */
1266 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1267
Douglas Gregor1982c182010-07-12 18:38:41 +00001268 for (i = 0; i != n; ++i)
1269 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001270 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001271 n = clang_codeCompleteGetNumDiagnostics(results);
1272 for (i = 0; i != n; ++i) {
1273 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1274 PrintDiagnostic(diag);
1275 clang_disposeDiagnostic(diag);
1276 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001277
1278 contexts = clang_codeCompleteGetContexts(results);
1279 print_completion_contexts(contexts, stdout);
1280
Douglas Gregor0a47d692011-07-26 15:24:30 +00001281 containerKind = clang_codeCompleteGetContainerKind(results,
1282 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001283
1284 if (containerKind != CXCursor_InvalidCode) {
1285 /* We have found a container */
1286 CXString containerUSR, containerKindSpelling;
1287 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1288 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1289 clang_disposeString(containerKindSpelling);
1290
1291 if (containerIsIncomplete) {
1292 printf("Container is incomplete\n");
1293 }
1294 else {
1295 printf("Container is complete\n");
1296 }
1297
1298 containerUSR = clang_codeCompleteGetContainerUSR(results);
1299 printf("Container USR: %s\n", clang_getCString(containerUSR));
1300 clang_disposeString(containerUSR);
1301 }
1302
Douglas Gregor0a47d692011-07-26 15:24:30 +00001303 objCSelector = clang_codeCompleteGetObjCSelector(results);
1304 selectorString = clang_getCString(objCSelector);
1305 if (selectorString && strlen(selectorString) > 0) {
1306 printf("Objective-C selector: %s\n", selectorString);
1307 }
1308 clang_disposeString(objCSelector);
1309
Douglas Gregorec6762c2009-12-18 16:20:58 +00001310 clang_disposeCodeCompleteResults(results);
1311 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001312 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001313 clang_disposeIndex(CIdx);
1314 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001315
Douglas Gregor735df882009-12-02 09:21:34 +00001316 free_remapped_files(unsaved_files, num_unsaved_files);
1317
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001318 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001319}
1320
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001321typedef struct {
1322 char *filename;
1323 unsigned line;
1324 unsigned column;
1325} CursorSourceLocation;
1326
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001327static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001328 CXIndex CIdx;
1329 int errorCode;
1330 struct CXUnsavedFile *unsaved_files = 0;
1331 int num_unsaved_files = 0;
1332 CXTranslationUnit TU;
1333 CXCursor Cursor;
1334 CursorSourceLocation *Locations = 0;
1335 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001336 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001337 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001338
Ted Kremeneke68fff62010-02-17 00:41:32 +00001339 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001340 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1341 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001342
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001343 /* Parse the locations. */
1344 assert(NumLocations > 0 && "Unable to count locations?");
1345 Locations = (CursorSourceLocation *)malloc(
1346 NumLocations * sizeof(CursorSourceLocation));
1347 for (Loc = 0; Loc < NumLocations; ++Loc) {
1348 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001349 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1350 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001351 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001352 return errorCode;
1353 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001354
1355 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001356 &num_unsaved_files))
1357 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001358
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001359 if (getenv("CINDEXTEST_EDITING"))
1360 Repeats = 5;
1361
1362 /* Parse the translation unit. When we're testing clang_getCursor() after
1363 reparsing, don't remap unsaved files until the second parse. */
1364 CIdx = clang_createIndex(1, 1);
1365 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1366 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001367 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001368 unsaved_files,
1369 Repeats > 1? 0 : num_unsaved_files,
1370 getDefaultParsingOptions());
1371
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001372 if (!TU) {
1373 fprintf(stderr, "unable to parse input\n");
1374 return -1;
1375 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001376
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001377 if (checkForErrors(TU) != 0)
1378 return -1;
1379
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001380 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001381 if (Repeats > 1 &&
1382 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1383 clang_defaultReparseOptions(TU))) {
1384 clang_disposeTranslationUnit(TU);
1385 return 1;
1386 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001387
1388 if (checkForErrors(TU) != 0)
1389 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001390
1391 for (Loc = 0; Loc < NumLocations; ++Loc) {
1392 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1393 if (!file)
1394 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001395
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001396 Cursor = clang_getCursor(TU,
1397 clang_getLocation(TU, file, Locations[Loc].line,
1398 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001399
1400 if (checkForErrors(TU) != 0)
1401 return -1;
1402
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001403 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001404 CXCompletionString completionString = clang_getCursorCompletionString(
1405 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001406 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001407 if (completionString != NULL) {
1408 printf("\nCompletion string: ");
1409 print_completion_string(completionString, stdout);
1410 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001411 printf("\n");
1412 free(Locations[Loc].filename);
1413 }
1414 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001415 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001416
Douglas Gregora88084b2010-02-18 18:08:43 +00001417 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001418 clang_disposeTranslationUnit(TU);
1419 clang_disposeIndex(CIdx);
1420 free(Locations);
1421 free_remapped_files(unsaved_files, num_unsaved_files);
1422 return 0;
1423}
1424
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001425static enum CXVisitorResult findFileRefsVisit(void *context,
1426 CXCursor cursor, CXSourceRange range) {
1427 if (clang_Range_isNull(range))
1428 return CXVisit_Continue;
1429
1430 PrintCursor(cursor);
1431 PrintRange(range, "");
1432 printf("\n");
1433 return CXVisit_Continue;
1434}
1435
1436static int find_file_refs_at(int argc, const char **argv) {
1437 CXIndex CIdx;
1438 int errorCode;
1439 struct CXUnsavedFile *unsaved_files = 0;
1440 int num_unsaved_files = 0;
1441 CXTranslationUnit TU;
1442 CXCursor Cursor;
1443 CursorSourceLocation *Locations = 0;
1444 unsigned NumLocations = 0, Loc;
1445 unsigned Repeats = 1;
1446 unsigned I;
1447
1448 /* Count the number of locations. */
1449 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1450 ++NumLocations;
1451
1452 /* Parse the locations. */
1453 assert(NumLocations > 0 && "Unable to count locations?");
1454 Locations = (CursorSourceLocation *)malloc(
1455 NumLocations * sizeof(CursorSourceLocation));
1456 for (Loc = 0; Loc < NumLocations; ++Loc) {
1457 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1458 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1459 &Locations[Loc].line,
1460 &Locations[Loc].column, 0, 0)))
1461 return errorCode;
1462 }
1463
1464 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1465 &num_unsaved_files))
1466 return -1;
1467
1468 if (getenv("CINDEXTEST_EDITING"))
1469 Repeats = 5;
1470
1471 /* Parse the translation unit. When we're testing clang_getCursor() after
1472 reparsing, don't remap unsaved files until the second parse. */
1473 CIdx = clang_createIndex(1, 1);
1474 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1475 argv + num_unsaved_files + 1 + NumLocations,
1476 argc - num_unsaved_files - 2 - NumLocations,
1477 unsaved_files,
1478 Repeats > 1? 0 : num_unsaved_files,
1479 getDefaultParsingOptions());
1480
1481 if (!TU) {
1482 fprintf(stderr, "unable to parse input\n");
1483 return -1;
1484 }
1485
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001486 if (checkForErrors(TU) != 0)
1487 return -1;
1488
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001489 for (I = 0; I != Repeats; ++I) {
1490 if (Repeats > 1 &&
1491 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1492 clang_defaultReparseOptions(TU))) {
1493 clang_disposeTranslationUnit(TU);
1494 return 1;
1495 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001496
1497 if (checkForErrors(TU) != 0)
1498 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001499
1500 for (Loc = 0; Loc < NumLocations; ++Loc) {
1501 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1502 if (!file)
1503 continue;
1504
1505 Cursor = clang_getCursor(TU,
1506 clang_getLocation(TU, file, Locations[Loc].line,
1507 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001508
1509 if (checkForErrors(TU) != 0)
1510 return -1;
1511
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001512 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001513 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001514 PrintCursor(Cursor);
1515 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001516 clang_findReferencesInFile(Cursor, file, visitor);
1517 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001518
1519 if (checkForErrors(TU) != 0)
1520 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001521 }
1522 }
1523 }
1524
1525 PrintDiagnostics(TU);
1526 clang_disposeTranslationUnit(TU);
1527 clang_disposeIndex(CIdx);
1528 free(Locations);
1529 free_remapped_files(unsaved_files, num_unsaved_files);
1530 return 0;
1531}
1532
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001533typedef struct {
1534 const char *check_prefix;
1535 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001536 int fail_for_error;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001537} IndexData;
1538
1539static void printCheck(IndexData *data) {
1540 if (data->check_prefix) {
1541 if (data->first_check_printed) {
1542 printf("// %s-NEXT: ", data->check_prefix);
1543 } else {
1544 printf("// %s : ", data->check_prefix);
1545 data->first_check_printed = 1;
1546 }
1547 }
1548}
1549
1550static void printCXIndexFile(CXIdxFile file) {
1551 CXString filename = clang_getFileName((CXFile)file);
1552 printf("%s", clang_getCString(filename));
1553 clang_disposeString(filename);
1554}
1555
1556static void printCXIndexLoc(CXIdxLoc loc) {
1557 CXString filename;
1558 const char *cname, *end;
1559 CXIdxFile file;
1560 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001561 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001562
1563 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1564 if (line == 0) {
1565 printf("<null loc>");
1566 return;
1567 }
1568 filename = clang_getFileName((CXFile)file);
1569 cname = clang_getCString(filename);
1570 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001571 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001572
1573 if (isHeader) {
1574 printCXIndexFile(file);
1575 printf(":");
1576 }
1577 printf("%d:%d", line, column);
1578}
1579
1580static CXIdxEntity makeCXIndexEntity(CXIdxIndexedEntityInfo *info) {
1581 const char *name;
1582 CXIdxLoc loc;
1583 char *newStr;
1584 CXIdxFile file;
1585 unsigned line, column;
1586
1587 name = info->entityInfo->name;
1588 if (!name)
1589 name = "<anon-tag>";
1590
1591 loc = info->declInfo->loc;
1592 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001593 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001594 newStr = (char *)malloc(strlen(name) + 10);
1595 sprintf(newStr, "%s:%d:%d", name, line, column);
1596 return (CXIdxEntity)newStr;
1597}
1598
1599static CXIdxContainer makeCXIndexContainer(CXIdxEntity entity) {
1600 return (CXIdxContainer)entity;
1601}
1602
1603static void printCXIndexEntity(CXIdxEntity entity) {
1604 printf("{%s}", (const char *)entity);
1605}
1606
1607static void printCXIndexContainer(CXIdxContainer container) {
1608 printf("[%s]", (const char *)container);
1609}
1610
1611static void printIndexedDeclInfo(CXIdxIndexedDeclInfo *info) {
1612 printf(" | cursor: ");
1613 PrintCursor(info->cursor);
1614 printf(" | loc: ");
1615 printCXIndexLoc(info->loc);
1616 printf(" | container: ");
1617 printCXIndexContainer(info->container);
1618}
1619
1620static void printIndexedEntityInfo(const char *cb,
1621 CXClientData client_data,
1622 CXIdxIndexedEntityInfo *info) {
1623 const char *name;
1624 IndexData *index_data;
1625 index_data = (IndexData *)client_data;
1626 printCheck(index_data);
1627
1628 name = info->entityInfo->name;
1629 if (!name)
1630 name = "<anon-tag>";
1631
1632 printf("%s: %s", cb, info->entityInfo->name);
1633 printIndexedDeclInfo(info->declInfo);
1634 printf(" | USR: %s", info->entityInfo->USR);
1635}
1636
1637static void printIndexedRedeclInfo(const char *cb,
1638 CXClientData client_data,
1639 CXIdxIndexedRedeclInfo *info) {
1640 IndexData *index_data;
1641 index_data = (IndexData *)client_data;
1642 printCheck(index_data);
1643
1644 printf("%s redeclaration: ", cb);
1645 printCXIndexEntity(info->entity);
1646 printIndexedDeclInfo(info->declInfo);
1647}
1648
1649static void printStartedContainerInfo(const char *cb,
1650 CXClientData client_data,
1651 CXIdxContainerInfo *info) {
1652 IndexData *index_data;
1653 index_data = (IndexData *)client_data;
1654 printCheck(index_data);
1655
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001656 printf("%s: ", cb);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001657 printCXIndexEntity(info->entity);
1658 printf(" | cursor: ");
1659 PrintCursor(info->cursor);
1660 printf(" | loc: ");
1661 printCXIndexLoc(info->loc);
1662}
1663
1664static void index_diagnostic(CXClientData client_data,
1665 CXDiagnostic diag, void *reserved) {
1666 CXString str;
1667 const char *cstr;
1668 IndexData *index_data;
1669 index_data = (IndexData *)client_data;
1670 printCheck(index_data);
1671
1672 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1673 cstr = clang_getCString(str);
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001674 printf("[diagnostic]: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001675 clang_disposeString(str);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001676
1677 if (getenv("CINDEXTEST_FAILONERROR") &&
1678 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1679 index_data->fail_for_error = 1;
1680 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001681}
1682
1683static CXIdxFile index_recordFile(CXClientData client_data,
1684 CXFile file, void *reserved) {
1685 return (CXIdxFile)file;
1686}
1687
1688static void index_ppIncludedFile(CXClientData client_data,
1689 CXIdxIncludedFileInfo *info) {
1690 IndexData *index_data;
1691 index_data = (IndexData *)client_data;
1692 printCheck(index_data);
1693
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001694 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001695 printCXIndexFile(info->file);
1696 printf(" | name: \"%s\"", info->filename);
1697 printf(" | hash loc: ");
1698 printCXIndexLoc(info->hashLoc);
1699 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
1700}
1701
1702static CXIdxMacro index_ppMacroDefined(CXClientData client_data,
1703 CXIdxMacroDefinedInfo *info) {
1704 IndexData *index_data;
1705 index_data = (IndexData *)client_data;
1706 printCheck(index_data);
1707
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001708 printf("[ppMacroDefined]: %s", info->macroInfo->name);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001709 printf(" | loc: ");
1710 printCXIndexLoc(info->macroInfo->loc);
1711 printf(" | defBegin: ");
1712 printCXIndexLoc(info->defBegin);
1713 printf(" | length: %d\n", info->defLength);
1714
1715 return (CXIdxMacro)info->macroInfo->name;
1716}
1717
1718static void index_ppMacroUndefined(CXClientData client_data,
1719 CXIdxMacroUndefinedInfo *info) {
1720 IndexData *index_data;
1721 index_data = (IndexData *)client_data;
1722 printCheck(index_data);
1723
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001724 printf("[ppMacroUndefined]: %s", info->name);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001725 printf(" | loc: ");
1726 printCXIndexLoc(info->loc);
1727 printf("\n");
1728}
1729
1730static void index_ppMacroExpanded(CXClientData client_data,
1731 CXIdxMacroExpandedInfo *info) {
1732 IndexData *index_data;
1733 index_data = (IndexData *)client_data;
1734 printCheck(index_data);
1735
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001736 printf("[ppMacroExpanded]: %s", info->name);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001737 printf(" | loc: ");
1738 printCXIndexLoc(info->loc);
1739 printf("\n");
1740}
1741
1742static CXIdxEntity index_importedEntity(CXClientData client_data,
1743 CXIdxImportedEntityInfo *info) {
1744 IndexData *index_data;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001745 CXIdxIndexedDeclInfo DeclInfo;
1746 CXIdxIndexedEntityInfo EntityInfo;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001747 const char *name;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001748 DeclInfo.cursor = info->cursor;
1749 DeclInfo.loc = info->loc;
1750 DeclInfo.container = 0;
1751 EntityInfo.entityInfo = info->entityInfo;
1752 EntityInfo.declInfo = &DeclInfo;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001753 index_data = (IndexData *)client_data;
1754 printCheck(index_data);
1755
1756 name = info->entityInfo->name;
1757 if (!name)
1758 name = "<anon-tag>";
1759
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001760 printf("[importedEntity]: %s", name);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001761 printf(" | cursor: ");
1762 PrintCursor(info->cursor);
1763 printf(" | loc: ");
1764 printCXIndexLoc(info->loc);
1765 printf("\n");
1766
1767 return makeCXIndexEntity(&EntityInfo);
1768}
1769
1770static CXIdxContainer index_startedTranslationUnit(CXClientData client_data,
1771 void *reserved) {
1772 IndexData *index_data;
1773 index_data = (IndexData *)client_data;
1774 printCheck(index_data);
1775
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001776 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001777 return (CXIdxContainer)"TU";
1778}
1779
1780static CXIdxEntity index_indexTypedef(CXClientData client_data,
1781 CXIdxTypedefInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001782 printIndexedEntityInfo("[indexTypedef]", client_data, info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001783 printf("\n");
1784
1785 return makeCXIndexEntity(info->indexedEntityInfo);
1786}
1787
1788static CXIdxEntity index_indexFunction(CXClientData client_data,
1789 CXIdxFunctionInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001790 printIndexedEntityInfo("[indexFunction]", client_data, info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001791 printf(" | isDefinition: %d\n", info->isDefinition);
1792
1793 return makeCXIndexEntity(info->indexedEntityInfo);
1794}
1795
1796static void index_indexFunctionRedeclaration(CXClientData client_data,
1797 CXIdxFunctionRedeclInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001798 printIndexedRedeclInfo("[indexFunctionRedeclaration]", client_data,
1799 info->indexedRedeclInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001800 printf(" | isDefinition: %d\n", info->isDefinition);
1801}
1802
1803static CXIdxEntity index_indexVariable(CXClientData client_data,
1804 CXIdxVariableInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001805 printIndexedEntityInfo("[indexVariable]", client_data, info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001806 printf(" | isDefinition: %d\n", info->isDefinition);
1807
1808 return makeCXIndexEntity(info->indexedEntityInfo);
1809}
1810
1811static void index_indexVariableRedeclaration(CXClientData client_data,
1812 CXIdxVariableRedeclInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001813 printIndexedRedeclInfo("[indexVariableRedeclaration]", client_data,
1814 info->indexedRedeclInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001815 printf(" | isDefinition: %d\n", info->isDefinition);
1816}
1817
1818static CXIdxEntity index_indexTagType(CXClientData client_data,
1819 CXIdxTagTypeInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001820 printIndexedEntityInfo("[indexTagType]", client_data, info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001821 printf(" | isDefinition: %d | anon: %d\n",
1822 info->isDefinition, info->isAnonymous);
1823
1824 return makeCXIndexEntity(info->indexedEntityInfo);
1825}
1826
1827static void index_indexTagTypeRedeclaration(CXClientData client_data,
1828 CXIdxTagTypeRedeclInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001829 printIndexedRedeclInfo("[indexTagTypeRedeclaration]", client_data,
1830 info->indexedRedeclInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001831 printf(" | isDefinition: %d\n", info->isDefinition);
1832}
1833
1834static CXIdxEntity index_indexField(CXClientData client_data,
1835 CXIdxFieldInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001836 printIndexedEntityInfo("[indexField]", client_data, info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001837 printf("\n");
1838
1839 return makeCXIndexEntity(info->indexedEntityInfo);
1840}
1841
1842static CXIdxEntity index_indexEnumerator(CXClientData client_data,
1843 CXIdxEnumeratorInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001844 printIndexedEntityInfo("[indexEnumerator]", client_data,
1845 info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001846 printf("\n");
1847
1848 return makeCXIndexEntity(info->indexedEntityInfo);
1849}
1850
1851static CXIdxContainer
1852index_startedTagTypeDefinition(CXClientData client_data,
1853 CXIdxTagTypeDefinitionInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001854 printStartedContainerInfo("[startedTagTypeDefinition]", client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001855 info->containerInfo);
1856 printf("\n");
1857
1858 return makeCXIndexContainer(info->containerInfo->entity);
1859}
1860
1861static CXIdxEntity index_indexObjCClass(CXClientData client_data,
1862 CXIdxObjCClassInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001863 printIndexedEntityInfo("[indexObjCClass]", client_data,
1864 info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001865 printf(" | forward ref: %d\n", info->isForwardRef);
1866
1867 return makeCXIndexEntity(info->indexedEntityInfo);
1868}
1869
1870static CXIdxEntity index_indexObjCProtocol(CXClientData client_data,
1871 CXIdxObjCProtocolInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001872 printIndexedEntityInfo("[indexObjCProtocol]", client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001873 info->indexedEntityInfo);
1874 printf(" | forward ref: %d\n", info->isForwardRef);
1875
1876 return makeCXIndexEntity(info->indexedEntityInfo);
1877}
1878
1879static CXIdxEntity index_indexObjCCategory(CXClientData client_data,
1880 CXIdxObjCCategoryInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001881 printIndexedEntityInfo("[indexObjCCategory]", client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001882 info->indexedEntityInfo);
1883 printf(" | class: ");
1884 printCXIndexEntity(info->objcClass);
1885 printf("\n");
1886
1887 return makeCXIndexEntity(info->indexedEntityInfo);
1888}
1889
1890static CXIdxEntity index_indexObjCMethod(CXClientData client_data,
1891 CXIdxObjCMethodInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001892 printIndexedEntityInfo("[indexObjCMethod]", client_data,
1893 info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001894 printf(" | isDefinition: %d\n", info->isDefinition);
1895
1896 return makeCXIndexEntity(info->indexedEntityInfo);
1897}
1898
1899static CXIdxEntity index_indexObjCProperty(CXClientData client_data,
1900 CXIdxObjCPropertyInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001901 printIndexedEntityInfo("[indexObjCProperty]", client_data,
1902 info->indexedEntityInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001903 printf("\n");
1904
1905 return makeCXIndexEntity(info->indexedEntityInfo);
1906}
1907
1908static void index_indexObjCMethodRedeclaration(CXClientData client_data,
1909 CXIdxObjCMethodRedeclInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001910 printIndexedRedeclInfo("[indexObjCMethodRedeclaration]", client_data,
1911 info->indexedRedeclInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001912 printf(" | isDefinition: %d\n", info->isDefinition);
1913}
1914
1915static CXIdxContainer
1916index_startedStatementBody(CXClientData client_data,
1917 CXIdxStmtBodyInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001918 printStartedContainerInfo("[startedStatementBody]", client_data,
1919 info->containerInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001920 printf(" | body: ");
1921 printCXIndexLoc(info->bodyBegin);
1922 printf("\n");
1923
1924 return makeCXIndexContainer(info->containerInfo->entity);
1925}
1926
1927static CXIdxContainer
1928index_startedObjCContainer(CXClientData client_data,
1929 CXIdxObjCContainerInfo *info) {
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001930 printStartedContainerInfo("[startedObjCContainer]", client_data,
1931 info->containerInfo);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001932 printf("\n");
1933
1934 return makeCXIndexContainer(info->containerInfo->entity);
1935}
1936
1937static void index_defineObjCClass(CXClientData client_data,
1938 CXIdxObjCClassDefineInfo *info) {
1939 IndexData *index_data;
1940 index_data = (IndexData *)client_data;
1941 printCheck(index_data);
1942
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001943 printf("[defineObjCClass]: ");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001944 printCXIndexEntity(info->objcClass);
1945 printf(" | cursor: ");
1946 PrintCursor(info->cursor);
1947 printf(" | container: ");
1948 printCXIndexContainer(info->container);
1949
1950 if (info->baseInfo) {
1951 printf(" | base: ");
1952 printCXIndexEntity(info->baseInfo->objcClass);
1953 printf(" | base loc: ");
1954 printCXIndexLoc(info->baseInfo->loc);
1955 }
1956
1957 printf("\n");
1958}
1959
1960static void index_endedContainer(CXClientData client_data,
1961 CXIdxEndContainerInfo *info) {
1962 IndexData *index_data;
1963 index_data = (IndexData *)client_data;
1964 printCheck(index_data);
1965
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001966 printf("[endedContainer]: ");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001967 printCXIndexContainer(info->container);
1968 printf(" | end: ");
1969 printCXIndexLoc(info->endLoc);
1970 printf("\n");
1971}
1972
1973static void index_indexEntityReference(CXClientData client_data,
1974 CXIdxEntityRefInfo *info) {
1975 IndexData *index_data;
1976 index_data = (IndexData *)client_data;
1977 printCheck(index_data);
1978
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001979 printf("[indexEntityReference]: ");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001980 printCXIndexEntity(info->referencedEntity);
1981 printf(" | cursor: ");
1982 PrintCursor(info->cursor);
1983 printf(" | loc: ");
1984 printCXIndexLoc(info->loc);
1985 printf(" | parent: ");
1986 printCXIndexEntity(info->parentEntity);
1987 printf(" | container: ");
1988 printCXIndexContainer(info->container);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001989 printf(" | kind: ");
1990 switch (info->kind) {
1991 case CXIdxEntityRef_Direct: printf("direct"); break;
1992 case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break;
1993 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001994 printf("\n");
1995}
1996
1997static IndexerCallbacks IndexCB = {
1998 index_diagnostic,
1999 index_recordFile,
2000 index_ppIncludedFile,
2001 index_ppMacroDefined,
2002 index_ppMacroUndefined,
2003 index_ppMacroExpanded,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002004 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002005 index_importedEntity,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002006 0,/*index_importedMacro,*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002007 index_startedTranslationUnit,
2008 index_indexTypedef,
2009 index_indexFunction,
2010 index_indexFunctionRedeclaration,
2011 index_indexVariable,
2012 index_indexVariableRedeclaration,
2013 index_indexTagType,
2014 index_indexTagTypeRedeclaration,
2015 index_indexField,
2016 index_indexEnumerator,
2017 index_startedTagTypeDefinition,
2018 index_indexObjCClass,
2019 index_indexObjCProtocol,
2020 index_indexObjCCategory,
2021 index_indexObjCMethod,
2022 index_indexObjCProperty,
2023 index_indexObjCMethodRedeclaration,
2024 index_startedStatementBody,
2025 index_startedObjCContainer,
2026 index_defineObjCClass,
2027 index_endedContainer,
2028 index_indexEntityReference
2029};
2030
2031static int index_file(int argc, const char **argv) {
2032 const char *check_prefix;
2033 CXIndex CIdx;
2034 IndexData index_data;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002035 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002036
2037 check_prefix = 0;
2038 if (argc > 0) {
2039 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2040 check_prefix = argv[0] + strlen("-check-prefix=");
2041 ++argv;
2042 --argc;
2043 }
2044 }
2045
2046 if (argc == 0) {
2047 fprintf(stderr, "no compiler arguments\n");
2048 return -1;
2049 }
2050
2051 CIdx = clang_createIndex(0, 1);
2052 index_data.check_prefix = check_prefix;
2053 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002054 index_data.fail_for_error = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002055
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002056 result = clang_indexTranslationUnit(CIdx, &index_data,
2057 &IndexCB,sizeof(IndexCB),
2058 0, 0, argv, argc, 0, 0, 0, 0);
2059 if (index_data.fail_for_error)
2060 return -1;
2061
2062 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002063}
2064
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002065int perform_token_annotation(int argc, const char **argv) {
2066 const char *input = argv[1];
2067 char *filename = 0;
2068 unsigned line, second_line;
2069 unsigned column, second_column;
2070 CXIndex CIdx;
2071 CXTranslationUnit TU = 0;
2072 int errorCode;
2073 struct CXUnsavedFile *unsaved_files = 0;
2074 int num_unsaved_files = 0;
2075 CXToken *tokens;
2076 unsigned num_tokens;
2077 CXSourceRange range;
2078 CXSourceLocation startLoc, endLoc;
2079 CXFile file = 0;
2080 CXCursor *cursors = 0;
2081 unsigned i;
2082
2083 input += strlen("-test-annotate-tokens=");
2084 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2085 &second_line, &second_column)))
2086 return errorCode;
2087
2088 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2089 return -1;
2090
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002091 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002092 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2093 argv + num_unsaved_files + 2,
2094 argc - num_unsaved_files - 3,
2095 unsaved_files,
2096 num_unsaved_files,
2097 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002098 if (!TU) {
2099 fprintf(stderr, "unable to parse input\n");
2100 clang_disposeIndex(CIdx);
2101 free(filename);
2102 free_remapped_files(unsaved_files, num_unsaved_files);
2103 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002104 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002105 errorCode = 0;
2106
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002107 if (checkForErrors(TU) != 0)
2108 return -1;
2109
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002110 if (getenv("CINDEXTEST_EDITING")) {
2111 for (i = 0; i < 5; ++i) {
2112 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2113 clang_defaultReparseOptions(TU))) {
2114 fprintf(stderr, "Unable to reparse translation unit!\n");
2115 errorCode = -1;
2116 goto teardown;
2117 }
2118 }
2119 }
2120
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002121 if (checkForErrors(TU) != 0) {
2122 errorCode = -1;
2123 goto teardown;
2124 }
2125
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002126 file = clang_getFile(TU, filename);
2127 if (!file) {
2128 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2129 errorCode = -1;
2130 goto teardown;
2131 }
2132
2133 startLoc = clang_getLocation(TU, file, line, column);
2134 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002135 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002136 column);
2137 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002138 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002139 }
2140
2141 endLoc = clang_getLocation(TU, file, second_line, second_column);
2142 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002143 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002144 second_line, second_column);
2145 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002146 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002147 }
2148
2149 range = clang_getRange(startLoc, endLoc);
2150 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002151
2152 if (checkForErrors(TU) != 0) {
2153 errorCode = -1;
2154 goto teardown;
2155 }
2156
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002157 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2158 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002159
2160 if (checkForErrors(TU) != 0) {
2161 errorCode = -1;
2162 goto teardown;
2163 }
2164
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002165 for (i = 0; i != num_tokens; ++i) {
2166 const char *kind = "<unknown>";
2167 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2168 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2169 unsigned start_line, start_column, end_line, end_column;
2170
2171 switch (clang_getTokenKind(tokens[i])) {
2172 case CXToken_Punctuation: kind = "Punctuation"; break;
2173 case CXToken_Keyword: kind = "Keyword"; break;
2174 case CXToken_Identifier: kind = "Identifier"; break;
2175 case CXToken_Literal: kind = "Literal"; break;
2176 case CXToken_Comment: kind = "Comment"; break;
2177 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002178 clang_getSpellingLocation(clang_getRangeStart(extent),
2179 0, &start_line, &start_column, 0);
2180 clang_getSpellingLocation(clang_getRangeEnd(extent),
2181 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002182 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2183 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002184 if (!clang_isInvalid(cursors[i].kind)) {
2185 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002186 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002187 }
2188 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002189 }
2190 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002191 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002192
2193 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002194 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002195 clang_disposeTranslationUnit(TU);
2196 clang_disposeIndex(CIdx);
2197 free(filename);
2198 free_remapped_files(unsaved_files, num_unsaved_files);
2199 return errorCode;
2200}
2201
Ted Kremenek0d435192009-11-17 18:13:31 +00002202/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002203/* USR printing. */
2204/******************************************************************************/
2205
2206static int insufficient_usr(const char *kind, const char *usage) {
2207 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2208 return 1;
2209}
2210
2211static unsigned isUSR(const char *s) {
2212 return s[0] == 'c' && s[1] == ':';
2213}
2214
2215static int not_usr(const char *s, const char *arg) {
2216 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2217 return 1;
2218}
2219
2220static void print_usr(CXString usr) {
2221 const char *s = clang_getCString(usr);
2222 printf("%s\n", s);
2223 clang_disposeString(usr);
2224}
2225
2226static void display_usrs() {
2227 fprintf(stderr, "-print-usrs options:\n"
2228 " ObjCCategory <class name> <category name>\n"
2229 " ObjCClass <class name>\n"
2230 " ObjCIvar <ivar name> <class USR>\n"
2231 " ObjCMethod <selector> [0=class method|1=instance method] "
2232 "<class USR>\n"
2233 " ObjCProperty <property name> <class USR>\n"
2234 " ObjCProtocol <protocol name>\n");
2235}
2236
2237int print_usrs(const char **I, const char **E) {
2238 while (I != E) {
2239 const char *kind = *I;
2240 unsigned len = strlen(kind);
2241 switch (len) {
2242 case 8:
2243 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2244 if (I + 2 >= E)
2245 return insufficient_usr(kind, "<ivar name> <class USR>");
2246 if (!isUSR(I[2]))
2247 return not_usr("<class USR>", I[2]);
2248 else {
2249 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002250 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002251 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002252 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2253 }
2254
2255 I += 3;
2256 continue;
2257 }
2258 break;
2259 case 9:
2260 if (memcmp(kind, "ObjCClass", 9) == 0) {
2261 if (I + 1 >= E)
2262 return insufficient_usr(kind, "<class name>");
2263 print_usr(clang_constructUSR_ObjCClass(I[1]));
2264 I += 2;
2265 continue;
2266 }
2267 break;
2268 case 10:
2269 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2270 if (I + 3 >= E)
2271 return insufficient_usr(kind, "<method selector> "
2272 "[0=class method|1=instance method] <class USR>");
2273 if (!isUSR(I[3]))
2274 return not_usr("<class USR>", I[3]);
2275 else {
2276 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002277 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002278 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002279 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2280 }
2281 I += 4;
2282 continue;
2283 }
2284 break;
2285 case 12:
2286 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2287 if (I + 2 >= E)
2288 return insufficient_usr(kind, "<class name> <category name>");
2289 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2290 I += 3;
2291 continue;
2292 }
2293 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2294 if (I + 1 >= E)
2295 return insufficient_usr(kind, "<protocol name>");
2296 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2297 I += 2;
2298 continue;
2299 }
2300 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2301 if (I + 2 >= E)
2302 return insufficient_usr(kind, "<property name> <class USR>");
2303 if (!isUSR(I[2]))
2304 return not_usr("<class USR>", I[2]);
2305 else {
2306 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002307 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002308 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002309 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2310 }
2311 I += 3;
2312 continue;
2313 }
2314 break;
2315 default:
2316 break;
2317 }
2318 break;
2319 }
2320
2321 if (I != E) {
2322 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2323 display_usrs();
2324 return 1;
2325 }
2326 return 0;
2327}
2328
2329int print_usrs_file(const char *file_name) {
2330 char line[2048];
2331 const char *args[128];
2332 unsigned numChars = 0;
2333
2334 FILE *fp = fopen(file_name, "r");
2335 if (!fp) {
2336 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2337 return 1;
2338 }
2339
2340 /* This code is not really all that safe, but it works fine for testing. */
2341 while (!feof(fp)) {
2342 char c = fgetc(fp);
2343 if (c == '\n') {
2344 unsigned i = 0;
2345 const char *s = 0;
2346
2347 if (numChars == 0)
2348 continue;
2349
2350 line[numChars] = '\0';
2351 numChars = 0;
2352
2353 if (line[0] == '/' && line[1] == '/')
2354 continue;
2355
2356 s = strtok(line, " ");
2357 while (s) {
2358 args[i] = s;
2359 ++i;
2360 s = strtok(0, " ");
2361 }
2362 if (print_usrs(&args[0], &args[i]))
2363 return 1;
2364 }
2365 else
2366 line[numChars++] = c;
2367 }
2368
2369 fclose(fp);
2370 return 0;
2371}
2372
2373/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002374/* Command line processing. */
2375/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002376int write_pch_file(const char *filename, int argc, const char *argv[]) {
2377 CXIndex Idx;
2378 CXTranslationUnit TU;
2379 struct CXUnsavedFile *unsaved_files = 0;
2380 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002381 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002382
2383 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2384
2385 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2386 clang_disposeIndex(Idx);
2387 return -1;
2388 }
2389
2390 TU = clang_parseTranslationUnit(Idx, 0,
2391 argv + num_unsaved_files,
2392 argc - num_unsaved_files,
2393 unsaved_files,
2394 num_unsaved_files,
2395 CXTranslationUnit_Incomplete);
2396 if (!TU) {
2397 fprintf(stderr, "Unable to load translation unit!\n");
2398 free_remapped_files(unsaved_files, num_unsaved_files);
2399 clang_disposeIndex(Idx);
2400 return 1;
2401 }
2402
Douglas Gregor39c411f2011-07-06 16:43:36 +00002403 switch (clang_saveTranslationUnit(TU, filename,
2404 clang_defaultSaveOptions(TU))) {
2405 case CXSaveError_None:
2406 break;
2407
2408 case CXSaveError_TranslationErrors:
2409 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2410 filename);
2411 result = 2;
2412 break;
2413
2414 case CXSaveError_InvalidTU:
2415 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2416 filename);
2417 result = 3;
2418 break;
2419
2420 case CXSaveError_Unknown:
2421 default:
2422 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2423 result = 1;
2424 break;
2425 }
2426
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002427 clang_disposeTranslationUnit(TU);
2428 free_remapped_files(unsaved_files, num_unsaved_files);
2429 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002430 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002431}
2432
2433/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002434/* Serialized diagnostics. */
2435/******************************************************************************/
2436
2437static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2438 switch (error) {
2439 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2440 case CXLoadDiag_None: break;
2441 case CXLoadDiag_Unknown: return "Unknown";
2442 case CXLoadDiag_InvalidFile: return "Invalid File";
2443 }
2444 return "None";
2445}
2446
2447static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2448 switch (severity) {
2449 case CXDiagnostic_Note: return "note";
2450 case CXDiagnostic_Error: return "error";
2451 case CXDiagnostic_Fatal: return "fatal";
2452 case CXDiagnostic_Ignored: return "ignored";
2453 case CXDiagnostic_Warning: return "warning";
2454 }
2455 return "unknown";
2456}
2457
2458static void printIndent(unsigned indent) {
2459 while (indent > 0) {
2460 fprintf(stderr, " ");
2461 --indent;
2462 }
2463}
2464
2465static void printLocation(CXSourceLocation L) {
2466 CXFile File;
2467 CXString FileName;
2468 unsigned line, column, offset;
2469
2470 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2471 FileName = clang_getFileName(File);
2472
2473 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2474 clang_disposeString(FileName);
2475}
2476
2477static void printRanges(CXDiagnostic D, unsigned indent) {
2478 unsigned i, n = clang_getDiagnosticNumRanges(D);
2479
2480 for (i = 0; i < n; ++i) {
2481 CXSourceLocation Start, End;
2482 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2483 Start = clang_getRangeStart(SR);
2484 End = clang_getRangeEnd(SR);
2485
2486 printIndent(indent);
2487 fprintf(stderr, "Range: ");
2488 printLocation(Start);
2489 fprintf(stderr, " ");
2490 printLocation(End);
2491 fprintf(stderr, "\n");
2492 }
2493}
2494
2495static void printFixIts(CXDiagnostic D, unsigned indent) {
2496 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2497 for (i = 0 ; i < n; ++i) {
2498 CXSourceRange ReplacementRange;
2499 CXString text;
2500 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2501
2502 printIndent(indent);
2503 fprintf(stderr, "FIXIT: (");
2504 printLocation(clang_getRangeStart(ReplacementRange));
2505 fprintf(stderr, " - ");
2506 printLocation(clang_getRangeEnd(ReplacementRange));
2507 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2508 clang_disposeString(text);
2509 }
2510}
2511
2512static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002513 unsigned i, n;
2514
Ted Kremenek15322172011-11-10 08:43:12 +00002515 if (!Diags)
2516 return;
2517
2518 fprintf(stderr, "\n");
2519
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002520 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002521 for (i = 0; i < n; ++i) {
2522 CXSourceLocation DiagLoc;
2523 CXDiagnostic D;
2524 CXFile File;
2525 CXString FileName, DiagSpelling, DiagOption;
2526 unsigned line, column, offset;
2527 const char *DiagOptionStr = 0;
2528
2529 D = clang_getDiagnosticInSet(Diags, i);
2530 DiagLoc = clang_getDiagnosticLocation(D);
2531 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2532 FileName = clang_getFileName(File);
2533 DiagSpelling = clang_getDiagnosticSpelling(D);
2534
2535 printIndent(indent);
2536
2537 fprintf(stderr, "%s:%d:%d: %s: %s",
2538 clang_getCString(FileName),
2539 line,
2540 column,
2541 getSeverityString(clang_getDiagnosticSeverity(D)),
2542 clang_getCString(DiagSpelling));
2543
2544 DiagOption = clang_getDiagnosticOption(D, 0);
2545 DiagOptionStr = clang_getCString(DiagOption);
2546 if (DiagOptionStr) {
2547 fprintf(stderr, " [%s]", DiagOptionStr);
2548 }
2549
2550 fprintf(stderr, "\n");
2551
2552 printRanges(D, indent);
2553 printFixIts(D, indent);
2554
2555 // Print subdiagnostics.
2556 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2557
2558 clang_disposeString(FileName);
2559 clang_disposeString(DiagSpelling);
2560 clang_disposeString(DiagOption);
2561 }
2562}
2563
2564static int read_diagnostics(const char *filename) {
2565 enum CXLoadDiag_Error error;
2566 CXString errorString;
2567 CXDiagnosticSet Diags = 0;
2568
2569 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2570 if (!Diags) {
2571 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2572 getDiagnosticCodeStr(error),
2573 clang_getCString(errorString));
2574 clang_disposeString(errorString);
2575 return 1;
2576 }
2577
2578 printDiagnosticSet(Diags, 0);
2579 clang_disposeDiagnosticSet(Diags);
2580 return 0;
2581}
2582
2583/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002584/* Command line processing. */
2585/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002586
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002587static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002588 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002589 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002590 if (strcmp(s, "-usrs") == 0)
2591 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002592 if (strncmp(s, "-memory-usage", 13) == 0)
2593 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002594 return NULL;
2595}
2596
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002597static void print_usage(void) {
2598 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002599 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002600 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002601 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002602 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002603 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002604 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002605 "[FileCheck prefix]\n");
2606 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002607 " c-index-test -test-load-tu <AST file> <symbol filter> "
2608 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002609 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2610 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002611 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002612 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002613 " c-index-test -test-load-source-memory-usage "
2614 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002615 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2616 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002617 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002618 " c-index-test -test-load-source-usrs-memory-usage "
2619 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002620 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2621 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002622 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002623 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002624 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002625 " c-index-test -test-print-typekind {<args>}*\n"
2626 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002627 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002628 " c-index-test -write-pch <file> <compiler arguments>\n");
2629 fprintf(stderr,
2630 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002631 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002632 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002633 " all - load all symbols, including those from PCH\n"
2634 " local - load all symbols except those in PCH\n"
2635 " category - only load ObjC categories (non-PCH)\n"
2636 " interface - only load ObjC interfaces (non-PCH)\n"
2637 " protocol - only load ObjC protocols (non-PCH)\n"
2638 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002639 " typedef - only load typdefs (non-PCH)\n"
2640 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002641}
2642
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002643/***/
2644
2645int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002646 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002647 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2648 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002649 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002650 return perform_code_completion(argc, argv, 0);
2651 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2652 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002653 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2654 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002655 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2656 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002657 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2658 return index_file(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002659 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002660 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002661 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002662 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2663 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002664 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002665 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2666 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2667 if (I) {
2668 int trials = atoi(argv[2]);
2669 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2670 NULL);
2671 }
2672 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002673 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002674 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002675
2676 PostVisitTU postVisit = 0;
2677 if (strstr(argv[1], "-memory-usage"))
2678 postVisit = PrintMemoryUsage;
2679
Ted Kremenek7d405622010-01-12 23:34:26 +00002680 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002681 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2682 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002683 }
2684 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002685 return perform_file_scan(argv[2], argv[3],
2686 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002687 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2688 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002689 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2690 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2691 PrintInclusionStack);
2692 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2693 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2694 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002695 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2696 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2697 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002698 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2699 return perform_test_load_source(argc - 2, argv + 2, "all",
2700 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002701 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2702 if (argc > 2)
2703 return print_usrs(argv + 2, argv + argc);
2704 else {
2705 display_usrs();
2706 return 1;
2707 }
2708 }
2709 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2710 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002711 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2712 return write_pch_file(argv[2], argc - 3, argv + 3);
2713
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002714 print_usage();
2715 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002716}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002717
2718/***/
2719
2720/* We intentionally run in a separate thread to ensure we at least minimal
2721 * testing of a multithreaded environment (for example, having a reduced stack
2722 * size). */
2723
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002724typedef struct thread_info {
2725 int argc;
2726 const char **argv;
2727 int result;
2728} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002729void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002730 thread_info *client_data = client_data_v;
2731 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002732}
2733
2734int main(int argc, const char **argv) {
2735 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002736
Douglas Gregor61605982010-10-27 16:00:01 +00002737 if (getenv("CINDEXTEST_NOTHREADS"))
2738 return cindextest_main(argc, argv);
2739
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002740 client_data.argc = argc;
2741 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002742 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002743 return client_data.result;
2744}