blob: 873233575cb24d7cb6661c27e68bdc1646007908 [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;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000040 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
41 options &= ~CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000042
43 return options;
44}
45
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000046static int checkForErrors(CXTranslationUnit TU);
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
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000174 if (str)
175 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000176 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
177}
178
Douglas Gregor358559d2010-10-02 22:49:11 +0000179int want_display_name = 0;
180
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000181static void PrintCursor(CXCursor Cursor) {
182 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000183 if (clang_isInvalid(Cursor.kind)) {
184 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
185 printf("Invalid Cursor => %s", clang_getCString(ks));
186 clang_disposeString(ks);
187 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000188 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000189 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000190 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000191 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000192 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000193 CXCursor *overridden;
194 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000195 unsigned RefNameRangeNr;
196 CXSourceRange CursorExtent;
197 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000198
Ted Kremeneke68fff62010-02-17 00:41:32 +0000199 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000200 string = want_display_name? clang_getCursorDisplayName(Cursor)
201 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000202 printf("%s=%s", clang_getCString(ks),
203 clang_getCString(string));
204 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000205 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000206
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000207 Referenced = clang_getCursorReferenced(Cursor);
208 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000209 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
210 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
211 printf("[");
212 for (I = 0; I != N; ++I) {
213 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000214 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000215 if (I)
216 printf(", ");
217
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000218 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000219 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000220 printf("%d:%d", line, column);
221 }
222 printf("]");
223 } else {
224 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000225 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000226 printf(":%d:%d", line, column);
227 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000228 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000229
230 if (clang_isCursorDefinition(Cursor))
231 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000232
233 switch (clang_getCursorAvailability(Cursor)) {
234 case CXAvailability_Available:
235 break;
236
237 case CXAvailability_Deprecated:
238 printf(" (deprecated)");
239 break;
240
241 case CXAvailability_NotAvailable:
242 printf(" (unavailable)");
243 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000244
245 case CXAvailability_NotAccessible:
246 printf(" (inaccessible)");
247 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000248 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000249
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000250 if (clang_CXXMethod_isStatic(Cursor))
251 printf(" (static)");
252 if (clang_CXXMethod_isVirtual(Cursor))
253 printf(" (virtual)");
254
Ted Kremenek95f33552010-08-26 01:42:22 +0000255 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
256 CXType T =
257 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
258 CXString S = clang_getTypeKindSpelling(T.kind);
259 printf(" [IBOutletCollection=%s]", clang_getCString(S));
260 clang_disposeString(S);
261 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000262
263 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
264 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
265 unsigned isVirtual = clang_isVirtualBase(Cursor);
266 const char *accessStr = 0;
267
268 switch (access) {
269 case CX_CXXInvalidAccessSpecifier:
270 accessStr = "invalid"; break;
271 case CX_CXXPublic:
272 accessStr = "public"; break;
273 case CX_CXXProtected:
274 accessStr = "protected"; break;
275 case CX_CXXPrivate:
276 accessStr = "private"; break;
277 }
278
279 printf(" [access=%s isVirtual=%s]", accessStr,
280 isVirtual ? "true" : "false");
281 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000282
283 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
284 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
285 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
286 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000287 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000288 printf(" [Specialization of %s:%d:%d]",
289 clang_getCString(Name), line, column);
290 clang_disposeString(Name);
291 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000292
293 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
294 if (num_overridden) {
295 unsigned I;
296 printf(" [Overrides ");
297 for (I = 0; I != num_overridden; ++I) {
298 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000299 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000300 if (I)
301 printf(", ");
302 printf("@%d:%d", line, column);
303 }
304 printf("]");
305 clang_disposeOverriddenCursors(overridden);
306 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000307
308 if (Cursor.kind == CXCursor_InclusionDirective) {
309 CXFile File = clang_getIncludedFile(Cursor);
310 CXString Included = clang_getFileName(File);
311 printf(" (%s)", clang_getCString(Included));
312 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000313
314 if (clang_isFileMultipleIncludeGuarded(TU, File))
315 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000316 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000317
318 CursorExtent = clang_getCursorExtent(Cursor);
319 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
320 CXNameRange_WantQualifier
321 | CXNameRange_WantSinglePiece
322 | CXNameRange_WantTemplateArgs,
323 0);
324 if (!clang_equalRanges(CursorExtent, RefNameRange))
325 PrintRange(RefNameRange, "SingleRefName");
326
327 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
328 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
329 CXNameRange_WantQualifier
330 | CXNameRange_WantTemplateArgs,
331 RefNameRangeNr);
332 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
333 break;
334 if (!clang_equalRanges(CursorExtent, RefNameRange))
335 PrintRange(RefNameRange, "RefName");
336 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000337 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000338}
Steve Naroff89922f82009-08-31 00:59:03 +0000339
Ted Kremeneke68fff62010-02-17 00:41:32 +0000340static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000341 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000342 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000343 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000344 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000346 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000347 clang_disposeString(source);
348 return "<invalid loc>";
349 }
350 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000351 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000352 clang_disposeString(source);
353 return b;
354 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000355}
356
Ted Kremenek0d435192009-11-17 18:13:31 +0000357/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000358/* Callbacks. */
359/******************************************************************************/
360
361typedef void (*PostVisitTU)(CXTranslationUnit);
362
Douglas Gregora88084b2010-02-18 18:08:43 +0000363void PrintDiagnostic(CXDiagnostic Diagnostic) {
364 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000365 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000366 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000367 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000368 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
369 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000370 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000371
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000372 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000373 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000374
Douglas Gregor274f1902010-02-22 23:17:23 +0000375 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
376 fprintf(stderr, "%s\n", clang_getCString(Msg));
377 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000378
Douglas Gregora9b06d42010-11-09 06:24:54 +0000379 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
380 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000381 if (!file)
382 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000383
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000384 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000385 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000386 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000387 CXSourceRange range;
388 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
389 CXSourceLocation start = clang_getRangeStart(range);
390 CXSourceLocation end = clang_getRangeEnd(range);
391 unsigned start_line, start_column, end_line, end_column;
392 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000393 clang_getSpellingLocation(start, &start_file, &start_line,
394 &start_column, 0);
395 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000396 if (clang_equalLocations(start, end)) {
397 /* Insertion. */
398 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000399 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000400 clang_getCString(insertion_text), start_line, start_column);
401 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
402 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000403 if (start_file == file && end_file == file) {
404 fprintf(out, "FIX-IT: Remove ");
405 PrintExtent(out, start_line, start_column, end_line, end_column);
406 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000407 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000408 } else {
409 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000410 if (start_file == end_file) {
411 fprintf(out, "FIX-IT: Replace ");
412 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000413 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000414 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000415 break;
416 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000417 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000418 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000419}
420
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000421void PrintDiagnosticSet(CXDiagnosticSet Set) {
422 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
423 for ( ; i != n ; ++i) {
424 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
425 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000426 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000427 if (ChildDiags)
428 PrintDiagnosticSet(ChildDiags);
429 }
430}
431
432void PrintDiagnostics(CXTranslationUnit TU) {
433 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
434 PrintDiagnosticSet(TUSet);
435 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000436}
437
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000438void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000439 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000440 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000441 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000442 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000443 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000444 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000445 unsigned long amount = usage.entries[i].amount;
446 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000447 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000448 ((double) amount)/(1024*1024));
449 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000450 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000451 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000452 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000453}
454
Ted Kremenekce2ae882010-01-26 17:59:48 +0000455/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000456/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000457/******************************************************************************/
458
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000459static const char *FileCheckPrefix = "CHECK";
460
Douglas Gregora7bde202010-01-19 00:34:46 +0000461static void PrintCursorExtent(CXCursor C) {
462 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000463 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000464}
465
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000466/* Data used by all of the visitors. */
467typedef struct {
468 CXTranslationUnit TU;
469 enum CXCursorKind *Filter;
470} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000471
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000472
Ted Kremeneke68fff62010-02-17 00:41:32 +0000473enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000474 CXCursor Parent,
475 CXClientData ClientData) {
476 VisitorData *Data = (VisitorData *)ClientData;
477 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000478 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000479 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000480 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000481 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000482 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000483 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000484 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000485 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000486 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000487 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000488
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000489 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000490}
Steve Naroff50398192009-08-28 15:28:48 +0000491
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000493 CXCursor Parent,
494 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495 const char *startBuf, *endBuf;
496 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
497 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000498 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000499
Douglas Gregorb6998662010-01-19 19:34:47 +0000500 if (Cursor.kind != CXCursor_FunctionDecl ||
501 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000502 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000503
504 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
505 &startLine, &startColumn,
506 &endLine, &endColumn);
507 /* Probe the entire body, looking for both decls and refs. */
508 curLine = startLine;
509 curColumn = startColumn;
510
511 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000512 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000513 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000514 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000516 if (*startBuf == '\n') {
517 startBuf++;
518 curLine++;
519 curColumn = 1;
520 } else if (*startBuf != '\t')
521 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000522
Douglas Gregor98258af2010-01-18 22:46:11 +0000523 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000524 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000525
Douglas Gregor1db19de2010-01-19 21:36:55 +0000526 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000527 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000528 CXSourceLocation RefLoc
529 = clang_getLocation(Data->TU, file, curLine, curColumn);
530 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000531 if (Ref.kind == CXCursor_NoDeclFound) {
532 /* Nothing found here; that's fine. */
533 } else if (Ref.kind != CXCursor_FunctionDecl) {
534 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
535 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000536 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000537 printf("\n");
538 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000539 }
Ted Kremenek74844072010-02-17 00:41:20 +0000540 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000541 startBuf++;
542 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000543
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000544 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000545}
546
Ted Kremenek7d405622010-01-12 23:34:26 +0000547/******************************************************************************/
548/* USR testing. */
549/******************************************************************************/
550
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000551enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
552 CXClientData ClientData) {
553 VisitorData *Data = (VisitorData *)ClientData;
554 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000555 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000556 const char *cstr = clang_getCString(USR);
557 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000558 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000559 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000560 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000561 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
562
Douglas Gregora7bde202010-01-19 00:34:46 +0000563 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000564 printf("\n");
565 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000566
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000567 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000568 }
569
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000570 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000571}
572
573/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000574/* Inclusion stack testing. */
575/******************************************************************************/
576
577void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
578 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579
Ted Kremenek16b55a72010-01-26 19:31:51 +0000580 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000581 CXString fname;
582
583 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000584 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000585 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586
Ted Kremenek16b55a72010-01-26 19:31:51 +0000587 for (i = 0; i < includeStackLen; ++i) {
588 CXFile includingFile;
589 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000590 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
591 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000592 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000593 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000594 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000595 }
596 printf("\n");
597}
598
599void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000600 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000601}
602
603/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000604/* Linkage testing. */
605/******************************************************************************/
606
607static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
608 CXClientData d) {
609 const char *linkage = 0;
610
611 if (clang_isInvalid(clang_getCursorKind(cursor)))
612 return CXChildVisit_Recurse;
613
614 switch (clang_getCursorLinkage(cursor)) {
615 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000616 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
617 case CXLinkage_Internal: linkage = "Internal"; break;
618 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
619 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000620 }
621
622 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000623 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000624 printf("linkage=%s\n", linkage);
625 }
626
627 return CXChildVisit_Recurse;
628}
629
630/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000631/* Typekind testing. */
632/******************************************************************************/
633
634static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
635 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000636 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
637 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000638 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000639 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000640 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000641 if (clang_isConstQualifiedType(T))
642 printf(" const");
643 if (clang_isVolatileQualifiedType(T))
644 printf(" volatile");
645 if (clang_isRestrictQualifiedType(T))
646 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000647 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000648 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000649 {
650 CXType CT = clang_getCanonicalType(T);
651 if (!clang_equalTypes(T, CT)) {
652 CXString CS = clang_getTypeKindSpelling(CT.kind);
653 printf(" [canonical=%s]", clang_getCString(CS));
654 clang_disposeString(CS);
655 }
656 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000657 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000658 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000659 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660 if (RT.kind != CXType_Invalid) {
661 CXString RS = clang_getTypeKindSpelling(RT.kind);
662 printf(" [result=%s]", clang_getCString(RS));
663 clang_disposeString(RS);
664 }
665 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000666 /* Print if this is a non-POD type. */
667 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000668
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000669 printf("\n");
670 }
671 return CXChildVisit_Recurse;
672}
673
674
675/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000676/* Loading ASTs/source. */
677/******************************************************************************/
678
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000679static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000680 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000681 CXCursorVisitor Visitor,
682 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000684 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000685 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000686
687 if (Visitor) {
688 enum CXCursorKind K = CXCursor_NotImplemented;
689 enum CXCursorKind *ck = &K;
690 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000691
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000692 /* Perform some simple filtering. */
693 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000694 else if (!strcmp(filter, "all-display") ||
695 !strcmp(filter, "local-display")) {
696 ck = NULL;
697 want_display_name = 1;
698 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000699 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000700 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
701 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
702 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
703 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
704 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
705 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
706 else {
707 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
708 return 1;
709 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000710
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000711 Data.TU = TU;
712 Data.Filter = ck;
713 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000714 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000715
Ted Kremenekce2ae882010-01-26 17:59:48 +0000716 if (PV)
717 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000718
Douglas Gregora88084b2010-02-18 18:08:43 +0000719 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000720 if (checkForErrors(TU) != 0) {
721 clang_disposeTranslationUnit(TU);
722 return -1;
723 }
724
Ted Kremenek0d435192009-11-17 18:13:31 +0000725 clang_disposeTranslationUnit(TU);
726 return 0;
727}
728
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000729int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000730 const char *prefix, CXCursorVisitor Visitor,
731 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000732 CXIndex Idx;
733 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000734 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000735 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000736 !strcmp(filter, "local") ? 1 : 0,
737 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000738
Ted Kremenek020a0952010-02-11 07:41:25 +0000739 if (!CreateTranslationUnit(Idx, file, &TU)) {
740 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000741 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000742 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000743
Ted Kremenek020a0952010-02-11 07:41:25 +0000744 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
745 clang_disposeIndex(Idx);
746 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000747}
748
Ted Kremenekce2ae882010-01-26 17:59:48 +0000749int perform_test_load_source(int argc, const char **argv,
750 const char *filter, CXCursorVisitor Visitor,
751 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000752 CXIndex Idx;
753 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000754 struct CXUnsavedFile *unsaved_files = 0;
755 int num_unsaved_files = 0;
756 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000757
Daniel Dunbarada487d2009-12-01 02:03:10 +0000758 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000759 (!strcmp(filter, "local") ||
760 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000761 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000762
Ted Kremenek020a0952010-02-11 07:41:25 +0000763 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
764 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000765 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000766 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000767
Douglas Gregordca8ee82011-05-06 16:33:08 +0000768 TU = clang_parseTranslationUnit(Idx, 0,
769 argv + num_unsaved_files,
770 argc - num_unsaved_files,
771 unsaved_files, num_unsaved_files,
772 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000773 if (!TU) {
774 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000775 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000776 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000777 return 1;
778 }
779
Ted Kremenekce2ae882010-01-26 17:59:48 +0000780 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000781 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000782 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000783 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000784}
785
Douglas Gregorabc563f2010-07-19 21:46:24 +0000786int perform_test_reparse_source(int argc, const char **argv, int trials,
787 const char *filter, CXCursorVisitor Visitor,
788 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000789 CXIndex Idx;
790 CXTranslationUnit TU;
791 struct CXUnsavedFile *unsaved_files = 0;
792 int num_unsaved_files = 0;
793 int result;
794 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000795 int remap_after_trial = 0;
796 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000797
798 Idx = clang_createIndex(/* excludeDeclsFromPCH */
799 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000800 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000801
Douglas Gregorabc563f2010-07-19 21:46:24 +0000802 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
803 clang_disposeIndex(Idx);
804 return -1;
805 }
806
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000807 /* Load the initial translation unit -- we do this without honoring remapped
808 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000809 TU = clang_parseTranslationUnit(Idx, 0,
810 argv + num_unsaved_files,
811 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000812 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000813 if (!TU) {
814 fprintf(stderr, "Unable to load translation unit!\n");
815 free_remapped_files(unsaved_files, num_unsaved_files);
816 clang_disposeIndex(Idx);
817 return 1;
818 }
819
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000820 if (checkForErrors(TU) != 0)
821 return -1;
822
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000823 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
824 remap_after_trial =
825 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
826 }
827
Douglas Gregorabc563f2010-07-19 21:46:24 +0000828 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000829 if (clang_reparseTranslationUnit(TU,
830 trial >= remap_after_trial ? num_unsaved_files : 0,
831 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000832 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000833 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000834 clang_disposeTranslationUnit(TU);
835 free_remapped_files(unsaved_files, num_unsaved_files);
836 clang_disposeIndex(Idx);
837 return -1;
838 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000839
840 if (checkForErrors(TU) != 0)
841 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000842 }
843
844 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000845
Douglas Gregorabc563f2010-07-19 21:46:24 +0000846 free_remapped_files(unsaved_files, num_unsaved_files);
847 clang_disposeIndex(Idx);
848 return result;
849}
850
Ted Kremenek0d435192009-11-17 18:13:31 +0000851/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000852/* Logic for testing clang_getCursor(). */
853/******************************************************************************/
854
Douglas Gregordd3e5542011-05-04 00:14:37 +0000855static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000856 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000857 unsigned end_line, unsigned end_col,
858 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000859 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000860 if (prefix)
861 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000862 PrintExtent(stdout, start_line, start_col, end_line, end_col);
863 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000864 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000865 printf("\n");
866}
867
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000868static int perform_file_scan(const char *ast_file, const char *source_file,
869 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000870 CXIndex Idx;
871 CXTranslationUnit TU;
872 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000873 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000874 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000875 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000876 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000877
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000878 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
879 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000880 fprintf(stderr, "Could not create Index\n");
881 return 1;
882 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883
Ted Kremenek1c6da172009-11-17 19:37:36 +0000884 if (!CreateTranslationUnit(Idx, ast_file, &TU))
885 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000886
Ted Kremenek1c6da172009-11-17 19:37:36 +0000887 if ((fp = fopen(source_file, "r")) == NULL) {
888 fprintf(stderr, "Could not open '%s'\n", source_file);
889 return 1;
890 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000891
Douglas Gregorb9790342010-01-22 21:44:22 +0000892 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000893 for (;;) {
894 CXCursor cursor;
895 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000896
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000897 if (c == '\n') {
898 ++line;
899 col = 1;
900 } else
901 ++col;
902
903 /* Check the cursor at this position, and dump the previous one if we have
904 * found something new.
905 */
906 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
907 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
908 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000909 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000910 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000911 start_line = line;
912 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000913 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000914 if (c == EOF)
915 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000916
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000917 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000918 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000919
Ted Kremenek1c6da172009-11-17 19:37:36 +0000920 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000921 clang_disposeTranslationUnit(TU);
922 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000923 return 0;
924}
925
926/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000927/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000928/******************************************************************************/
929
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000930/* Parse file:line:column from the input string. Returns 0 on success, non-zero
931 on failure. If successful, the pointer *filename will contain newly-allocated
932 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000933int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000934 unsigned *column, unsigned *second_line,
935 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000936 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000937 const char *last_colon = strrchr(input, ':');
938 unsigned values[4], i;
939 unsigned num_values = (second_line && second_column)? 4 : 2;
940
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000941 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000942 if (!last_colon || last_colon == input) {
943 if (num_values == 4)
944 fprintf(stderr, "could not parse filename:line:column:line:column in "
945 "'%s'\n", input);
946 else
947 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000948 return 1;
949 }
950
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 for (i = 0; i != num_values; ++i) {
952 const char *prev_colon;
953
954 /* Parse the next line or column. */
955 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
956 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000957 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000958 (i % 2 ? "column" : "line"), input);
959 return 1;
960 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000961
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000962 if (i + 1 == num_values)
963 break;
964
965 /* Find the previous colon. */
966 prev_colon = last_colon - 1;
967 while (prev_colon != input && *prev_colon != ':')
968 --prev_colon;
969 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000970 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000971 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000973 }
974
975 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000976 }
977
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000978 *line = values[0];
979 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000980
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000981 if (second_line && second_column) {
982 *second_line = values[2];
983 *second_column = values[3];
984 }
985
Douglas Gregor88d23952009-11-09 18:19:57 +0000986 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000987 *filename = (char*)malloc(last_colon - input + 1);
988 memcpy(*filename, input, last_colon - input);
989 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000990 return 0;
991}
992
993const char *
994clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
995 switch (Kind) {
996 case CXCompletionChunk_Optional: return "Optional";
997 case CXCompletionChunk_TypedText: return "TypedText";
998 case CXCompletionChunk_Text: return "Text";
999 case CXCompletionChunk_Placeholder: return "Placeholder";
1000 case CXCompletionChunk_Informative: return "Informative";
1001 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1002 case CXCompletionChunk_LeftParen: return "LeftParen";
1003 case CXCompletionChunk_RightParen: return "RightParen";
1004 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1005 case CXCompletionChunk_RightBracket: return "RightBracket";
1006 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1007 case CXCompletionChunk_RightBrace: return "RightBrace";
1008 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1009 case CXCompletionChunk_RightAngle: return "RightAngle";
1010 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001011 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001012 case CXCompletionChunk_Colon: return "Colon";
1013 case CXCompletionChunk_SemiColon: return "SemiColon";
1014 case CXCompletionChunk_Equal: return "Equal";
1015 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1016 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001017 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001018
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001019 return "Unknown";
1020}
1021
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001022static int checkForErrors(CXTranslationUnit TU) {
1023 unsigned Num, i;
1024 CXDiagnostic Diag;
1025 CXString DiagStr;
1026
1027 if (!getenv("CINDEXTEST_FAILONERROR"))
1028 return 0;
1029
1030 Num = clang_getNumDiagnostics(TU);
1031 for (i = 0; i != Num; ++i) {
1032 Diag = clang_getDiagnostic(TU, i);
1033 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1034 DiagStr = clang_formatDiagnostic(Diag,
1035 clang_defaultDiagnosticDisplayOptions());
1036 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1037 clang_disposeString(DiagStr);
1038 clang_disposeDiagnostic(Diag);
1039 return -1;
1040 }
1041 clang_disposeDiagnostic(Diag);
1042 }
1043
1044 return 0;
1045}
1046
Douglas Gregor3ac73852009-11-09 16:04:45 +00001047void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001048 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001049
Douglas Gregor3ac73852009-11-09 16:04:45 +00001050 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001051 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001052 CXString text;
1053 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001054 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001055 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001056
Douglas Gregor3ac73852009-11-09 16:04:45 +00001057 if (Kind == CXCompletionChunk_Optional) {
1058 fprintf(file, "{Optional ");
1059 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001060 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001061 file);
1062 fprintf(file, "}");
1063 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001064 }
1065
1066 if (Kind == CXCompletionChunk_VerticalSpace) {
1067 fprintf(file, "{VerticalSpace }");
1068 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001069 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001070
Douglas Gregord5a20892009-11-09 17:05:28 +00001071 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001072 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001073 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001074 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001075 cstr ? cstr : "");
1076 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001077 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001078
Douglas Gregor3ac73852009-11-09 16:04:45 +00001079}
1080
1081void print_completion_result(CXCompletionResult *completion_result,
1082 CXClientData client_data) {
1083 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001084 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001085 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001086 enum CXCursorKind ParentKind;
1087 CXString ParentName;
1088
Ted Kremeneke68fff62010-02-17 00:41:32 +00001089 fprintf(file, "%s:", clang_getCString(ks));
1090 clang_disposeString(ks);
1091
Douglas Gregor3ac73852009-11-09 16:04:45 +00001092 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001093 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001094 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001095 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1096 case CXAvailability_Available:
1097 break;
1098
1099 case CXAvailability_Deprecated:
1100 fprintf(file, " (deprecated)");
1101 break;
1102
1103 case CXAvailability_NotAvailable:
1104 fprintf(file, " (unavailable)");
1105 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001106
1107 case CXAvailability_NotAccessible:
1108 fprintf(file, " (inaccessible)");
1109 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001110 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001111
1112 annotationCount = clang_getCompletionNumAnnotations(
1113 completion_result->CompletionString);
1114 if (annotationCount) {
1115 unsigned i;
1116 fprintf(file, " (");
1117 for (i = 0; i < annotationCount; ++i) {
1118 if (i != 0)
1119 fprintf(file, ", ");
1120 fprintf(file, "\"%s\"",
1121 clang_getCString(clang_getCompletionAnnotation(
1122 completion_result->CompletionString, i)));
1123 }
1124 fprintf(file, ")");
1125 }
1126
Douglas Gregorba103062012-03-27 23:34:16 +00001127 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1128 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1129 &ParentKind);
1130 if (ParentKind != CXCursor_NotImplemented) {
1131 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1132 fprintf(file, " (parent: %s '%s')",
1133 clang_getCString(KindSpelling),
1134 clang_getCString(ParentName));
1135 clang_disposeString(KindSpelling);
1136 }
1137 clang_disposeString(ParentName);
1138 }
1139
Douglas Gregor58ddb602010-08-23 23:00:57 +00001140 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001141}
1142
Douglas Gregor3da626b2011-07-07 16:03:39 +00001143void print_completion_contexts(unsigned long long contexts, FILE *file) {
1144 fprintf(file, "Completion contexts:\n");
1145 if (contexts == CXCompletionContext_Unknown) {
1146 fprintf(file, "Unknown\n");
1147 }
1148 if (contexts & CXCompletionContext_AnyType) {
1149 fprintf(file, "Any type\n");
1150 }
1151 if (contexts & CXCompletionContext_AnyValue) {
1152 fprintf(file, "Any value\n");
1153 }
1154 if (contexts & CXCompletionContext_ObjCObjectValue) {
1155 fprintf(file, "Objective-C object value\n");
1156 }
1157 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1158 fprintf(file, "Objective-C selector value\n");
1159 }
1160 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1161 fprintf(file, "C++ class type value\n");
1162 }
1163 if (contexts & CXCompletionContext_DotMemberAccess) {
1164 fprintf(file, "Dot member access\n");
1165 }
1166 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1167 fprintf(file, "Arrow member access\n");
1168 }
1169 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1170 fprintf(file, "Objective-C property access\n");
1171 }
1172 if (contexts & CXCompletionContext_EnumTag) {
1173 fprintf(file, "Enum tag\n");
1174 }
1175 if (contexts & CXCompletionContext_UnionTag) {
1176 fprintf(file, "Union tag\n");
1177 }
1178 if (contexts & CXCompletionContext_StructTag) {
1179 fprintf(file, "Struct tag\n");
1180 }
1181 if (contexts & CXCompletionContext_ClassTag) {
1182 fprintf(file, "Class name\n");
1183 }
1184 if (contexts & CXCompletionContext_Namespace) {
1185 fprintf(file, "Namespace or namespace alias\n");
1186 }
1187 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1188 fprintf(file, "Nested name specifier\n");
1189 }
1190 if (contexts & CXCompletionContext_ObjCInterface) {
1191 fprintf(file, "Objective-C interface\n");
1192 }
1193 if (contexts & CXCompletionContext_ObjCProtocol) {
1194 fprintf(file, "Objective-C protocol\n");
1195 }
1196 if (contexts & CXCompletionContext_ObjCCategory) {
1197 fprintf(file, "Objective-C category\n");
1198 }
1199 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1200 fprintf(file, "Objective-C instance method\n");
1201 }
1202 if (contexts & CXCompletionContext_ObjCClassMessage) {
1203 fprintf(file, "Objective-C class method\n");
1204 }
1205 if (contexts & CXCompletionContext_ObjCSelectorName) {
1206 fprintf(file, "Objective-C selector name\n");
1207 }
1208 if (contexts & CXCompletionContext_MacroName) {
1209 fprintf(file, "Macro name\n");
1210 }
1211 if (contexts & CXCompletionContext_NaturalLanguage) {
1212 fprintf(file, "Natural language\n");
1213 }
1214}
1215
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001216int my_stricmp(const char *s1, const char *s2) {
1217 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001218 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001219 if (c1 < c2)
1220 return -1;
1221 else if (c1 > c2)
1222 return 1;
1223
1224 ++s1;
1225 ++s2;
1226 }
1227
1228 if (*s1)
1229 return 1;
1230 else if (*s2)
1231 return -1;
1232 return 0;
1233}
1234
Douglas Gregor1982c182010-07-12 18:38:41 +00001235int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001236 const char *input = argv[1];
1237 char *filename = 0;
1238 unsigned line;
1239 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001240 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001241 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001242 struct CXUnsavedFile *unsaved_files = 0;
1243 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001244 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001245 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001246 unsigned I, Repeats = 1;
1247 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1248
1249 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1250 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001251
Douglas Gregor1982c182010-07-12 18:38:41 +00001252 if (timing_only)
1253 input += strlen("-code-completion-timing=");
1254 else
1255 input += strlen("-code-completion-at=");
1256
Ted Kremeneke68fff62010-02-17 00:41:32 +00001257 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001258 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001259 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001260
Douglas Gregor735df882009-12-02 09:21:34 +00001261 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1262 return -1;
1263
Douglas Gregor32be4a52010-10-11 21:37:58 +00001264 CIdx = clang_createIndex(0, 0);
1265
1266 if (getenv("CINDEXTEST_EDITING"))
1267 Repeats = 5;
1268
1269 TU = clang_parseTranslationUnit(CIdx, 0,
1270 argv + num_unsaved_files + 2,
1271 argc - num_unsaved_files - 2,
1272 0, 0, getDefaultParsingOptions());
1273 if (!TU) {
1274 fprintf(stderr, "Unable to load translation unit!\n");
1275 return 1;
1276 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001277
1278 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1279 fprintf(stderr, "Unable to reparse translation init!\n");
1280 return 1;
1281 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001282
1283 for (I = 0; I != Repeats; ++I) {
1284 results = clang_codeCompleteAt(TU, filename, line, column,
1285 unsaved_files, num_unsaved_files,
1286 completionOptions);
1287 if (!results) {
1288 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001289 return 1;
1290 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001291 if (I != Repeats-1)
1292 clang_disposeCodeCompleteResults(results);
1293 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001294
Douglas Gregorec6762c2009-12-18 16:20:58 +00001295 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001296 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001297 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001298 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001299 CXString objCSelector;
1300 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001301 if (!timing_only) {
1302 /* Sort the code-completion results based on the typed text. */
1303 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1304
Douglas Gregor1982c182010-07-12 18:38:41 +00001305 for (i = 0; i != n; ++i)
1306 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001307 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001308 n = clang_codeCompleteGetNumDiagnostics(results);
1309 for (i = 0; i != n; ++i) {
1310 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1311 PrintDiagnostic(diag);
1312 clang_disposeDiagnostic(diag);
1313 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001314
1315 contexts = clang_codeCompleteGetContexts(results);
1316 print_completion_contexts(contexts, stdout);
1317
Douglas Gregor0a47d692011-07-26 15:24:30 +00001318 containerKind = clang_codeCompleteGetContainerKind(results,
1319 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001320
1321 if (containerKind != CXCursor_InvalidCode) {
1322 /* We have found a container */
1323 CXString containerUSR, containerKindSpelling;
1324 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1325 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1326 clang_disposeString(containerKindSpelling);
1327
1328 if (containerIsIncomplete) {
1329 printf("Container is incomplete\n");
1330 }
1331 else {
1332 printf("Container is complete\n");
1333 }
1334
1335 containerUSR = clang_codeCompleteGetContainerUSR(results);
1336 printf("Container USR: %s\n", clang_getCString(containerUSR));
1337 clang_disposeString(containerUSR);
1338 }
1339
Douglas Gregor0a47d692011-07-26 15:24:30 +00001340 objCSelector = clang_codeCompleteGetObjCSelector(results);
1341 selectorString = clang_getCString(objCSelector);
1342 if (selectorString && strlen(selectorString) > 0) {
1343 printf("Objective-C selector: %s\n", selectorString);
1344 }
1345 clang_disposeString(objCSelector);
1346
Douglas Gregorec6762c2009-12-18 16:20:58 +00001347 clang_disposeCodeCompleteResults(results);
1348 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001349 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001350 clang_disposeIndex(CIdx);
1351 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001352
Douglas Gregor735df882009-12-02 09:21:34 +00001353 free_remapped_files(unsaved_files, num_unsaved_files);
1354
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001355 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001356}
1357
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001358typedef struct {
1359 char *filename;
1360 unsigned line;
1361 unsigned column;
1362} CursorSourceLocation;
1363
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001364static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001365 CXIndex CIdx;
1366 int errorCode;
1367 struct CXUnsavedFile *unsaved_files = 0;
1368 int num_unsaved_files = 0;
1369 CXTranslationUnit TU;
1370 CXCursor Cursor;
1371 CursorSourceLocation *Locations = 0;
1372 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001373 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001374 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001375
Ted Kremeneke68fff62010-02-17 00:41:32 +00001376 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001377 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1378 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001379
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001380 /* Parse the locations. */
1381 assert(NumLocations > 0 && "Unable to count locations?");
1382 Locations = (CursorSourceLocation *)malloc(
1383 NumLocations * sizeof(CursorSourceLocation));
1384 for (Loc = 0; Loc < NumLocations; ++Loc) {
1385 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001386 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1387 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001388 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001389 return errorCode;
1390 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001391
1392 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001393 &num_unsaved_files))
1394 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001395
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001396 if (getenv("CINDEXTEST_EDITING"))
1397 Repeats = 5;
1398
1399 /* Parse the translation unit. When we're testing clang_getCursor() after
1400 reparsing, don't remap unsaved files until the second parse. */
1401 CIdx = clang_createIndex(1, 1);
1402 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1403 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001404 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001405 unsaved_files,
1406 Repeats > 1? 0 : num_unsaved_files,
1407 getDefaultParsingOptions());
1408
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001409 if (!TU) {
1410 fprintf(stderr, "unable to parse input\n");
1411 return -1;
1412 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001413
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001414 if (checkForErrors(TU) != 0)
1415 return -1;
1416
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001417 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001418 if (Repeats > 1 &&
1419 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1420 clang_defaultReparseOptions(TU))) {
1421 clang_disposeTranslationUnit(TU);
1422 return 1;
1423 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001424
1425 if (checkForErrors(TU) != 0)
1426 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001427
1428 for (Loc = 0; Loc < NumLocations; ++Loc) {
1429 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1430 if (!file)
1431 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001432
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001433 Cursor = clang_getCursor(TU,
1434 clang_getLocation(TU, file, Locations[Loc].line,
1435 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001436
1437 if (checkForErrors(TU) != 0)
1438 return -1;
1439
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001440 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001441 CXCompletionString completionString = clang_getCursorCompletionString(
1442 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001443 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1444 CXString Spelling;
1445 const char *cspell;
1446 unsigned line, column;
1447 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1448 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001449 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001450 PrintCursorExtent(Cursor);
1451 Spelling = clang_getCursorSpelling(Cursor);
1452 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001453 if (cspell && strlen(cspell) != 0) {
1454 unsigned pieceIndex;
1455 CXSourceRange range, extent;
1456 extent = clang_getCursorExtent(Cursor);
1457 printf(" Spelling=%s (", cspell);
1458 for (pieceIndex = 0; ; ++pieceIndex) {
1459 range = clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
1460 if (clang_Range_isNull(range))
1461 break;
1462 PrintRange(range, 0);
1463 }
1464 printf(")");
1465 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001466 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001467 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1468 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001469 if (completionString != NULL) {
1470 printf("\nCompletion string: ");
1471 print_completion_string(completionString, stdout);
1472 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001473 printf("\n");
1474 free(Locations[Loc].filename);
1475 }
1476 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001477 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001478
Douglas Gregora88084b2010-02-18 18:08:43 +00001479 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001480 clang_disposeTranslationUnit(TU);
1481 clang_disposeIndex(CIdx);
1482 free(Locations);
1483 free_remapped_files(unsaved_files, num_unsaved_files);
1484 return 0;
1485}
1486
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001487static enum CXVisitorResult findFileRefsVisit(void *context,
1488 CXCursor cursor, CXSourceRange range) {
1489 if (clang_Range_isNull(range))
1490 return CXVisit_Continue;
1491
1492 PrintCursor(cursor);
1493 PrintRange(range, "");
1494 printf("\n");
1495 return CXVisit_Continue;
1496}
1497
1498static int find_file_refs_at(int argc, const char **argv) {
1499 CXIndex CIdx;
1500 int errorCode;
1501 struct CXUnsavedFile *unsaved_files = 0;
1502 int num_unsaved_files = 0;
1503 CXTranslationUnit TU;
1504 CXCursor Cursor;
1505 CursorSourceLocation *Locations = 0;
1506 unsigned NumLocations = 0, Loc;
1507 unsigned Repeats = 1;
1508 unsigned I;
1509
1510 /* Count the number of locations. */
1511 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1512 ++NumLocations;
1513
1514 /* Parse the locations. */
1515 assert(NumLocations > 0 && "Unable to count locations?");
1516 Locations = (CursorSourceLocation *)malloc(
1517 NumLocations * sizeof(CursorSourceLocation));
1518 for (Loc = 0; Loc < NumLocations; ++Loc) {
1519 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1520 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1521 &Locations[Loc].line,
1522 &Locations[Loc].column, 0, 0)))
1523 return errorCode;
1524 }
1525
1526 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1527 &num_unsaved_files))
1528 return -1;
1529
1530 if (getenv("CINDEXTEST_EDITING"))
1531 Repeats = 5;
1532
1533 /* Parse the translation unit. When we're testing clang_getCursor() after
1534 reparsing, don't remap unsaved files until the second parse. */
1535 CIdx = clang_createIndex(1, 1);
1536 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1537 argv + num_unsaved_files + 1 + NumLocations,
1538 argc - num_unsaved_files - 2 - NumLocations,
1539 unsaved_files,
1540 Repeats > 1? 0 : num_unsaved_files,
1541 getDefaultParsingOptions());
1542
1543 if (!TU) {
1544 fprintf(stderr, "unable to parse input\n");
1545 return -1;
1546 }
1547
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001548 if (checkForErrors(TU) != 0)
1549 return -1;
1550
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001551 for (I = 0; I != Repeats; ++I) {
1552 if (Repeats > 1 &&
1553 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1554 clang_defaultReparseOptions(TU))) {
1555 clang_disposeTranslationUnit(TU);
1556 return 1;
1557 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001558
1559 if (checkForErrors(TU) != 0)
1560 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001561
1562 for (Loc = 0; Loc < NumLocations; ++Loc) {
1563 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1564 if (!file)
1565 continue;
1566
1567 Cursor = clang_getCursor(TU,
1568 clang_getLocation(TU, file, Locations[Loc].line,
1569 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001570
1571 if (checkForErrors(TU) != 0)
1572 return -1;
1573
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001574 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001575 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001576 PrintCursor(Cursor);
1577 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001578 clang_findReferencesInFile(Cursor, file, visitor);
1579 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001580
1581 if (checkForErrors(TU) != 0)
1582 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001583 }
1584 }
1585 }
1586
1587 PrintDiagnostics(TU);
1588 clang_disposeTranslationUnit(TU);
1589 clang_disposeIndex(CIdx);
1590 free(Locations);
1591 free_remapped_files(unsaved_files, num_unsaved_files);
1592 return 0;
1593}
1594
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001595typedef struct {
1596 const char *check_prefix;
1597 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001598 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001599 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001600 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001601} IndexData;
1602
1603static void printCheck(IndexData *data) {
1604 if (data->check_prefix) {
1605 if (data->first_check_printed) {
1606 printf("// %s-NEXT: ", data->check_prefix);
1607 } else {
1608 printf("// %s : ", data->check_prefix);
1609 data->first_check_printed = 1;
1610 }
1611 }
1612}
1613
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001614static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001615 CXString filename = clang_getFileName((CXFile)file);
1616 printf("%s", clang_getCString(filename));
1617 clang_disposeString(filename);
1618}
1619
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001620static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1621 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001622 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001623 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001624 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001625 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001626 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001627
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001628 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001629 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1630 if (line == 0) {
1631 printf("<null loc>");
1632 return;
1633 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001634 if (!file) {
1635 printf("<no idxfile>");
1636 return;
1637 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001638 filename = clang_getFileName((CXFile)file);
1639 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001640 if (strcmp(cname, index_data->main_filename) == 0)
1641 isMainFile = 1;
1642 else
1643 isMainFile = 0;
1644 clang_disposeString(filename);
1645
1646 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001647 printCXIndexFile(file);
1648 printf(":");
1649 }
1650 printf("%d:%d", line, column);
1651}
1652
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001653static unsigned digitCount(unsigned val) {
1654 unsigned c = 1;
1655 while (1) {
1656 if (val < 10)
1657 return c;
1658 ++c;
1659 val /= 10;
1660 }
1661}
1662
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001663static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1664 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001665 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001666 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001667 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001668 unsigned line, column;
1669
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001670 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001671 if (!name)
1672 name = "<anon-tag>";
1673
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001674 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001675 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001676 newStr = (char *)malloc(strlen(name) +
1677 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001678 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001679 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001680}
1681
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001682static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1683 CXIdxClientContainer container;
1684 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001685 if (!container)
1686 printf("[<<NULL>>]");
1687 else
1688 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001689}
1690
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001691static const char *getEntityKindString(CXIdxEntityKind kind) {
1692 switch (kind) {
1693 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1694 case CXIdxEntity_Typedef: return "typedef";
1695 case CXIdxEntity_Function: return "function";
1696 case CXIdxEntity_Variable: return "variable";
1697 case CXIdxEntity_Field: return "field";
1698 case CXIdxEntity_EnumConstant: return "enumerator";
1699 case CXIdxEntity_ObjCClass: return "objc-class";
1700 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1701 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001702 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1703 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001704 case CXIdxEntity_ObjCProperty: return "objc-property";
1705 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1706 case CXIdxEntity_Enum: return "enum";
1707 case CXIdxEntity_Struct: return "struct";
1708 case CXIdxEntity_Union: return "union";
1709 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001710 case CXIdxEntity_CXXNamespace: return "namespace";
1711 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1712 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1713 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1714 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1715 case CXIdxEntity_CXXConstructor: return "constructor";
1716 case CXIdxEntity_CXXDestructor: return "destructor";
1717 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1718 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1719 }
1720 assert(0 && "Garbage entity kind");
1721 return 0;
1722}
1723
1724static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1725 switch (kind) {
1726 case CXIdxEntity_NonTemplate: return "";
1727 case CXIdxEntity_Template: return "-template";
1728 case CXIdxEntity_TemplatePartialSpecialization:
1729 return "-template-partial-spec";
1730 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001731 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001732 assert(0 && "Garbage entity kind");
1733 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001734}
1735
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001736static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1737 switch (kind) {
1738 case CXIdxEntityLang_None: return "<none>";
1739 case CXIdxEntityLang_C: return "C";
1740 case CXIdxEntityLang_ObjC: return "ObjC";
1741 case CXIdxEntityLang_CXX: return "C++";
1742 }
1743 assert(0 && "Garbage language kind");
1744 return 0;
1745}
1746
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001747static void printEntityInfo(const char *cb,
1748 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001749 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001750 const char *name;
1751 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001752 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001753 index_data = (IndexData *)client_data;
1754 printCheck(index_data);
1755
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001756 if (!info) {
1757 printf("%s: <<NULL>>", cb);
1758 return;
1759 }
1760
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001761 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001762 if (!name)
1763 name = "<anon-tag>";
1764
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001765 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1766 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001767 printf(" | name: %s", name);
1768 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001769 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001770
1771 for (i = 0; i != info->numAttributes; ++i) {
1772 const CXIdxAttrInfo *Attr = info->attributes[i];
1773 printf(" <attribute>: ");
1774 PrintCursor(Attr->cursor);
1775 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001776}
1777
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001778static void printBaseClassInfo(CXClientData client_data,
1779 const CXIdxBaseClassInfo *info) {
1780 printEntityInfo(" <base>", client_data, info->base);
1781 printf(" | cursor: ");
1782 PrintCursor(info->cursor);
1783 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001784 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001785}
1786
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001787static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1788 CXClientData client_data) {
1789 unsigned i;
1790 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1791 printEntityInfo(" <protocol>", client_data,
1792 ProtoInfo->protocols[i]->protocol);
1793 printf(" | cursor: ");
1794 PrintCursor(ProtoInfo->protocols[i]->cursor);
1795 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001796 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001797 printf("\n");
1798 }
1799}
1800
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001801static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001802 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001803 CXString str;
1804 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001805 unsigned numDiags, i;
1806 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001807 IndexData *index_data;
1808 index_data = (IndexData *)client_data;
1809 printCheck(index_data);
1810
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001811 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1812 for (i = 0; i != numDiags; ++i) {
1813 diag = clang_getDiagnosticInSet(diagSet, i);
1814 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1815 cstr = clang_getCString(str);
1816 printf("[diagnostic]: %s\n", cstr);
1817 clang_disposeString(str);
1818
1819 if (getenv("CINDEXTEST_FAILONERROR") &&
1820 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1821 index_data->fail_for_error = 1;
1822 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001823 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001824}
1825
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001826static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1827 CXFile file, void *reserved) {
1828 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001829 CXString filename;
1830
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001831 index_data = (IndexData *)client_data;
1832 printCheck(index_data);
1833
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001834 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001835 index_data->main_filename = clang_getCString(filename);
1836 clang_disposeString(filename);
1837
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001838 printf("[enteredMainFile]: ");
1839 printCXIndexFile((CXIdxClientFile)file);
1840 printf("\n");
1841
1842 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001843}
1844
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001845static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001846 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001847 IndexData *index_data;
1848 index_data = (IndexData *)client_data;
1849 printCheck(index_data);
1850
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001851 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001852 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001853 printf(" | name: \"%s\"", info->filename);
1854 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001855 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001856 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001857
1858 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001859}
1860
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001861static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001862 void *reserved) {
1863 IndexData *index_data;
1864 index_data = (IndexData *)client_data;
1865 printCheck(index_data);
1866
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001867 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001868 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001869}
1870
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001871static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001872 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001873 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001874 const CXIdxObjCCategoryDeclInfo *CatInfo;
1875 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001876 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001877 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001878 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001879 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001880 index_data = (IndexData *)client_data;
1881
1882 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1883 printf(" | cursor: ");
1884 PrintCursor(info->cursor);
1885 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001886 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001887 printf(" | semantic-container: ");
1888 printCXIndexContainer(info->semanticContainer);
1889 printf(" | lexical-container: ");
1890 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001891 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001892 printf(" | isDef: %d", info->isDefinition);
1893 printf(" | isContainer: %d", info->isContainer);
1894 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001895
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001896 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001897 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001898 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001899 PrintCursor(Attr->cursor);
1900 printf("\n");
1901 }
1902
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001903 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1904 const char *kindName = 0;
1905 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1906 switch (K) {
1907 case CXIdxObjCContainer_ForwardRef:
1908 kindName = "forward-ref"; break;
1909 case CXIdxObjCContainer_Interface:
1910 kindName = "interface"; break;
1911 case CXIdxObjCContainer_Implementation:
1912 kindName = "implementation"; break;
1913 }
1914 printCheck(index_data);
1915 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1916 }
1917
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001918 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001919 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1920 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001921 printf(" | cursor: ");
1922 PrintCursor(CatInfo->classCursor);
1923 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001924 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001925 printf("\n");
1926 }
1927
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001928 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1929 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001930 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001931 printf("\n");
1932 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001933 }
1934
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001935 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1936 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001937 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001938
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001939 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
1940 if (PropInfo->getter) {
1941 printEntityInfo(" <getter>", client_data, PropInfo->getter);
1942 printf("\n");
1943 }
1944 if (PropInfo->setter) {
1945 printEntityInfo(" <setter>", client_data, PropInfo->setter);
1946 printf("\n");
1947 }
1948 }
1949
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001950 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1951 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1952 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1953 printf("\n");
1954 }
1955 }
1956
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001957 if (info->declAsContainer)
1958 clang_index_setClientContainer(info->declAsContainer,
1959 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001960}
1961
1962static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001963 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001964 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001965 printf(" | cursor: ");
1966 PrintCursor(info->cursor);
1967 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001968 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001969 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001970 printf(" | container: ");
1971 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001972 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001973 switch (info->kind) {
1974 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001975 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001976 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001977 printf("\n");
1978}
1979
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001980static int index_abortQuery(CXClientData client_data, void *reserved) {
1981 IndexData *index_data;
1982 index_data = (IndexData *)client_data;
1983 return index_data->abort;
1984}
1985
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001986static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001987 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001988 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001989 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001990 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001991 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001992 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001993 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001994 index_indexEntityReference
1995};
1996
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00001997static unsigned getIndexOptions(void) {
1998 unsigned index_opts;
1999 index_opts = 0;
2000 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2001 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2002 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2003 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2004
2005 return index_opts;
2006}
2007
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002008static int index_file(int argc, const char **argv) {
2009 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002010 CXIndex Idx;
2011 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002012 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002013 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002014 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002015
2016 check_prefix = 0;
2017 if (argc > 0) {
2018 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2019 check_prefix = argv[0] + strlen("-check-prefix=");
2020 ++argv;
2021 --argc;
2022 }
2023 }
2024
2025 if (argc == 0) {
2026 fprintf(stderr, "no compiler arguments\n");
2027 return -1;
2028 }
2029
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002030 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2031 /* displayDiagnosics=*/1))) {
2032 fprintf(stderr, "Could not create Index\n");
2033 return 1;
2034 }
2035 idxAction = 0;
2036 result = 1;
2037
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002038 index_data.check_prefix = check_prefix;
2039 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002040 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002041 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002042
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002043 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002044 idxAction = clang_IndexAction_create(Idx);
2045 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002046 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002047 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002048 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002049 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002050
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002051 clang_IndexAction_dispose(idxAction);
2052 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002053 return result;
2054}
2055
2056static int index_tu(int argc, const char **argv) {
2057 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002058 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002059 CXTranslationUnit TU;
2060 const char *check_prefix;
2061 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002062 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002063 int result;
2064
2065 check_prefix = 0;
2066 if (argc > 0) {
2067 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2068 check_prefix = argv[0] + strlen("-check-prefix=");
2069 ++argv;
2070 --argc;
2071 }
2072 }
2073
2074 if (argc == 0) {
2075 fprintf(stderr, "no ast file\n");
2076 return -1;
2077 }
2078
2079 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2080 /* displayDiagnosics=*/1))) {
2081 fprintf(stderr, "Could not create Index\n");
2082 return 1;
2083 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002084 idxAction = 0;
2085 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002086
2087 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002088 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002089
2090 index_data.check_prefix = check_prefix;
2091 index_data.first_check_printed = 0;
2092 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002093 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002094
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002095 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002096 idxAction = clang_IndexAction_create(Idx);
2097 result = clang_indexTranslationUnit(idxAction, &index_data,
2098 &IndexCB,sizeof(IndexCB),
2099 index_opts, TU);
2100 if (index_data.fail_for_error)
2101 goto finished;
2102
2103 finished:
2104 clang_IndexAction_dispose(idxAction);
2105 clang_disposeIndex(Idx);
2106
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002107 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002108}
2109
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002110int perform_token_annotation(int argc, const char **argv) {
2111 const char *input = argv[1];
2112 char *filename = 0;
2113 unsigned line, second_line;
2114 unsigned column, second_column;
2115 CXIndex CIdx;
2116 CXTranslationUnit TU = 0;
2117 int errorCode;
2118 struct CXUnsavedFile *unsaved_files = 0;
2119 int num_unsaved_files = 0;
2120 CXToken *tokens;
2121 unsigned num_tokens;
2122 CXSourceRange range;
2123 CXSourceLocation startLoc, endLoc;
2124 CXFile file = 0;
2125 CXCursor *cursors = 0;
2126 unsigned i;
2127
2128 input += strlen("-test-annotate-tokens=");
2129 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2130 &second_line, &second_column)))
2131 return errorCode;
2132
2133 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2134 return -1;
2135
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002136 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002137 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2138 argv + num_unsaved_files + 2,
2139 argc - num_unsaved_files - 3,
2140 unsaved_files,
2141 num_unsaved_files,
2142 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002143 if (!TU) {
2144 fprintf(stderr, "unable to parse input\n");
2145 clang_disposeIndex(CIdx);
2146 free(filename);
2147 free_remapped_files(unsaved_files, num_unsaved_files);
2148 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002149 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002150 errorCode = 0;
2151
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002152 if (checkForErrors(TU) != 0)
2153 return -1;
2154
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002155 if (getenv("CINDEXTEST_EDITING")) {
2156 for (i = 0; i < 5; ++i) {
2157 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2158 clang_defaultReparseOptions(TU))) {
2159 fprintf(stderr, "Unable to reparse translation unit!\n");
2160 errorCode = -1;
2161 goto teardown;
2162 }
2163 }
2164 }
2165
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002166 if (checkForErrors(TU) != 0) {
2167 errorCode = -1;
2168 goto teardown;
2169 }
2170
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002171 file = clang_getFile(TU, filename);
2172 if (!file) {
2173 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2174 errorCode = -1;
2175 goto teardown;
2176 }
2177
2178 startLoc = clang_getLocation(TU, file, line, column);
2179 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002180 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002181 column);
2182 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002183 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002184 }
2185
2186 endLoc = clang_getLocation(TU, file, second_line, second_column);
2187 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002188 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002189 second_line, second_column);
2190 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002191 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002192 }
2193
2194 range = clang_getRange(startLoc, endLoc);
2195 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002196
2197 if (checkForErrors(TU) != 0) {
2198 errorCode = -1;
2199 goto teardown;
2200 }
2201
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002202 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2203 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002204
2205 if (checkForErrors(TU) != 0) {
2206 errorCode = -1;
2207 goto teardown;
2208 }
2209
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002210 for (i = 0; i != num_tokens; ++i) {
2211 const char *kind = "<unknown>";
2212 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2213 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2214 unsigned start_line, start_column, end_line, end_column;
2215
2216 switch (clang_getTokenKind(tokens[i])) {
2217 case CXToken_Punctuation: kind = "Punctuation"; break;
2218 case CXToken_Keyword: kind = "Keyword"; break;
2219 case CXToken_Identifier: kind = "Identifier"; break;
2220 case CXToken_Literal: kind = "Literal"; break;
2221 case CXToken_Comment: kind = "Comment"; break;
2222 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002223 clang_getSpellingLocation(clang_getRangeStart(extent),
2224 0, &start_line, &start_column, 0);
2225 clang_getSpellingLocation(clang_getRangeEnd(extent),
2226 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002227 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2228 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002229 if (!clang_isInvalid(cursors[i].kind)) {
2230 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002231 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002232 }
2233 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002234 }
2235 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002236 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002237
2238 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002239 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002240 clang_disposeTranslationUnit(TU);
2241 clang_disposeIndex(CIdx);
2242 free(filename);
2243 free_remapped_files(unsaved_files, num_unsaved_files);
2244 return errorCode;
2245}
2246
Ted Kremenek0d435192009-11-17 18:13:31 +00002247/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002248/* USR printing. */
2249/******************************************************************************/
2250
2251static int insufficient_usr(const char *kind, const char *usage) {
2252 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2253 return 1;
2254}
2255
2256static unsigned isUSR(const char *s) {
2257 return s[0] == 'c' && s[1] == ':';
2258}
2259
2260static int not_usr(const char *s, const char *arg) {
2261 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2262 return 1;
2263}
2264
2265static void print_usr(CXString usr) {
2266 const char *s = clang_getCString(usr);
2267 printf("%s\n", s);
2268 clang_disposeString(usr);
2269}
2270
2271static void display_usrs() {
2272 fprintf(stderr, "-print-usrs options:\n"
2273 " ObjCCategory <class name> <category name>\n"
2274 " ObjCClass <class name>\n"
2275 " ObjCIvar <ivar name> <class USR>\n"
2276 " ObjCMethod <selector> [0=class method|1=instance method] "
2277 "<class USR>\n"
2278 " ObjCProperty <property name> <class USR>\n"
2279 " ObjCProtocol <protocol name>\n");
2280}
2281
2282int print_usrs(const char **I, const char **E) {
2283 while (I != E) {
2284 const char *kind = *I;
2285 unsigned len = strlen(kind);
2286 switch (len) {
2287 case 8:
2288 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2289 if (I + 2 >= E)
2290 return insufficient_usr(kind, "<ivar name> <class USR>");
2291 if (!isUSR(I[2]))
2292 return not_usr("<class USR>", I[2]);
2293 else {
2294 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002295 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002296 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002297 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2298 }
2299
2300 I += 3;
2301 continue;
2302 }
2303 break;
2304 case 9:
2305 if (memcmp(kind, "ObjCClass", 9) == 0) {
2306 if (I + 1 >= E)
2307 return insufficient_usr(kind, "<class name>");
2308 print_usr(clang_constructUSR_ObjCClass(I[1]));
2309 I += 2;
2310 continue;
2311 }
2312 break;
2313 case 10:
2314 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2315 if (I + 3 >= E)
2316 return insufficient_usr(kind, "<method selector> "
2317 "[0=class method|1=instance method] <class USR>");
2318 if (!isUSR(I[3]))
2319 return not_usr("<class USR>", I[3]);
2320 else {
2321 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002322 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002323 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002324 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2325 }
2326 I += 4;
2327 continue;
2328 }
2329 break;
2330 case 12:
2331 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2332 if (I + 2 >= E)
2333 return insufficient_usr(kind, "<class name> <category name>");
2334 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2335 I += 3;
2336 continue;
2337 }
2338 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2339 if (I + 1 >= E)
2340 return insufficient_usr(kind, "<protocol name>");
2341 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2342 I += 2;
2343 continue;
2344 }
2345 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2346 if (I + 2 >= E)
2347 return insufficient_usr(kind, "<property name> <class USR>");
2348 if (!isUSR(I[2]))
2349 return not_usr("<class USR>", I[2]);
2350 else {
2351 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002352 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002353 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002354 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2355 }
2356 I += 3;
2357 continue;
2358 }
2359 break;
2360 default:
2361 break;
2362 }
2363 break;
2364 }
2365
2366 if (I != E) {
2367 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2368 display_usrs();
2369 return 1;
2370 }
2371 return 0;
2372}
2373
2374int print_usrs_file(const char *file_name) {
2375 char line[2048];
2376 const char *args[128];
2377 unsigned numChars = 0;
2378
2379 FILE *fp = fopen(file_name, "r");
2380 if (!fp) {
2381 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2382 return 1;
2383 }
2384
2385 /* This code is not really all that safe, but it works fine for testing. */
2386 while (!feof(fp)) {
2387 char c = fgetc(fp);
2388 if (c == '\n') {
2389 unsigned i = 0;
2390 const char *s = 0;
2391
2392 if (numChars == 0)
2393 continue;
2394
2395 line[numChars] = '\0';
2396 numChars = 0;
2397
2398 if (line[0] == '/' && line[1] == '/')
2399 continue;
2400
2401 s = strtok(line, " ");
2402 while (s) {
2403 args[i] = s;
2404 ++i;
2405 s = strtok(0, " ");
2406 }
2407 if (print_usrs(&args[0], &args[i]))
2408 return 1;
2409 }
2410 else
2411 line[numChars++] = c;
2412 }
2413
2414 fclose(fp);
2415 return 0;
2416}
2417
2418/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002419/* Command line processing. */
2420/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002421int write_pch_file(const char *filename, int argc, const char *argv[]) {
2422 CXIndex Idx;
2423 CXTranslationUnit TU;
2424 struct CXUnsavedFile *unsaved_files = 0;
2425 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002426 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002427
2428 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2429
2430 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2431 clang_disposeIndex(Idx);
2432 return -1;
2433 }
2434
2435 TU = clang_parseTranslationUnit(Idx, 0,
2436 argv + num_unsaved_files,
2437 argc - num_unsaved_files,
2438 unsaved_files,
2439 num_unsaved_files,
2440 CXTranslationUnit_Incomplete);
2441 if (!TU) {
2442 fprintf(stderr, "Unable to load translation unit!\n");
2443 free_remapped_files(unsaved_files, num_unsaved_files);
2444 clang_disposeIndex(Idx);
2445 return 1;
2446 }
2447
Douglas Gregor39c411f2011-07-06 16:43:36 +00002448 switch (clang_saveTranslationUnit(TU, filename,
2449 clang_defaultSaveOptions(TU))) {
2450 case CXSaveError_None:
2451 break;
2452
2453 case CXSaveError_TranslationErrors:
2454 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2455 filename);
2456 result = 2;
2457 break;
2458
2459 case CXSaveError_InvalidTU:
2460 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2461 filename);
2462 result = 3;
2463 break;
2464
2465 case CXSaveError_Unknown:
2466 default:
2467 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2468 result = 1;
2469 break;
2470 }
2471
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002472 clang_disposeTranslationUnit(TU);
2473 free_remapped_files(unsaved_files, num_unsaved_files);
2474 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002475 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002476}
2477
2478/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002479/* Serialized diagnostics. */
2480/******************************************************************************/
2481
2482static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2483 switch (error) {
2484 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2485 case CXLoadDiag_None: break;
2486 case CXLoadDiag_Unknown: return "Unknown";
2487 case CXLoadDiag_InvalidFile: return "Invalid File";
2488 }
2489 return "None";
2490}
2491
2492static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2493 switch (severity) {
2494 case CXDiagnostic_Note: return "note";
2495 case CXDiagnostic_Error: return "error";
2496 case CXDiagnostic_Fatal: return "fatal";
2497 case CXDiagnostic_Ignored: return "ignored";
2498 case CXDiagnostic_Warning: return "warning";
2499 }
2500 return "unknown";
2501}
2502
2503static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002504 if (indent == 0)
2505 return;
2506 fprintf(stderr, "+");
2507 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002508 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002509 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002510 --indent;
2511 }
2512}
2513
2514static void printLocation(CXSourceLocation L) {
2515 CXFile File;
2516 CXString FileName;
2517 unsigned line, column, offset;
2518
2519 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2520 FileName = clang_getFileName(File);
2521
2522 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2523 clang_disposeString(FileName);
2524}
2525
2526static void printRanges(CXDiagnostic D, unsigned indent) {
2527 unsigned i, n = clang_getDiagnosticNumRanges(D);
2528
2529 for (i = 0; i < n; ++i) {
2530 CXSourceLocation Start, End;
2531 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2532 Start = clang_getRangeStart(SR);
2533 End = clang_getRangeEnd(SR);
2534
2535 printIndent(indent);
2536 fprintf(stderr, "Range: ");
2537 printLocation(Start);
2538 fprintf(stderr, " ");
2539 printLocation(End);
2540 fprintf(stderr, "\n");
2541 }
2542}
2543
2544static void printFixIts(CXDiagnostic D, unsigned indent) {
2545 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002546 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002547 for (i = 0 ; i < n; ++i) {
2548 CXSourceRange ReplacementRange;
2549 CXString text;
2550 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2551
2552 printIndent(indent);
2553 fprintf(stderr, "FIXIT: (");
2554 printLocation(clang_getRangeStart(ReplacementRange));
2555 fprintf(stderr, " - ");
2556 printLocation(clang_getRangeEnd(ReplacementRange));
2557 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2558 clang_disposeString(text);
2559 }
2560}
2561
2562static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002563 unsigned i, n;
2564
Ted Kremenek15322172011-11-10 08:43:12 +00002565 if (!Diags)
2566 return;
2567
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002568 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002569 for (i = 0; i < n; ++i) {
2570 CXSourceLocation DiagLoc;
2571 CXDiagnostic D;
2572 CXFile File;
2573 CXString FileName, DiagSpelling, DiagOption;
2574 unsigned line, column, offset;
2575 const char *DiagOptionStr = 0;
2576
2577 D = clang_getDiagnosticInSet(Diags, i);
2578 DiagLoc = clang_getDiagnosticLocation(D);
2579 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2580 FileName = clang_getFileName(File);
2581 DiagSpelling = clang_getDiagnosticSpelling(D);
2582
2583 printIndent(indent);
2584
2585 fprintf(stderr, "%s:%d:%d: %s: %s",
2586 clang_getCString(FileName),
2587 line,
2588 column,
2589 getSeverityString(clang_getDiagnosticSeverity(D)),
2590 clang_getCString(DiagSpelling));
2591
2592 DiagOption = clang_getDiagnosticOption(D, 0);
2593 DiagOptionStr = clang_getCString(DiagOption);
2594 if (DiagOptionStr) {
2595 fprintf(stderr, " [%s]", DiagOptionStr);
2596 }
2597
2598 fprintf(stderr, "\n");
2599
2600 printRanges(D, indent);
2601 printFixIts(D, indent);
2602
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002603 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002604 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2605
2606 clang_disposeString(FileName);
2607 clang_disposeString(DiagSpelling);
2608 clang_disposeString(DiagOption);
2609 }
2610}
2611
2612static int read_diagnostics(const char *filename) {
2613 enum CXLoadDiag_Error error;
2614 CXString errorString;
2615 CXDiagnosticSet Diags = 0;
2616
2617 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2618 if (!Diags) {
2619 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2620 getDiagnosticCodeStr(error),
2621 clang_getCString(errorString));
2622 clang_disposeString(errorString);
2623 return 1;
2624 }
2625
2626 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002627 fprintf(stderr, "Number of diagnostics: %d\n",
2628 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002629 clang_disposeDiagnosticSet(Diags);
2630 return 0;
2631}
2632
2633/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002634/* Command line processing. */
2635/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002636
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002637static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002638 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002639 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002640 if (strcmp(s, "-usrs") == 0)
2641 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002642 if (strncmp(s, "-memory-usage", 13) == 0)
2643 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002644 return NULL;
2645}
2646
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002647static void print_usage(void) {
2648 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002649 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002650 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002651 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002652 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002653 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002654 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002655 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002656 "[FileCheck prefix]\n");
2657 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002658 " c-index-test -test-load-tu <AST file> <symbol filter> "
2659 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002660 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2661 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002662 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002663 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002664 " c-index-test -test-load-source-memory-usage "
2665 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002666 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2667 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002668 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002669 " c-index-test -test-load-source-usrs-memory-usage "
2670 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002671 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2672 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002673 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002674 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002675 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002676 " c-index-test -test-print-typekind {<args>}*\n"
2677 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002678 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002679 " c-index-test -write-pch <file> <compiler arguments>\n");
2680 fprintf(stderr,
2681 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002682 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002683 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002684 " all - load all symbols, including those from PCH\n"
2685 " local - load all symbols except those in PCH\n"
2686 " category - only load ObjC categories (non-PCH)\n"
2687 " interface - only load ObjC interfaces (non-PCH)\n"
2688 " protocol - only load ObjC protocols (non-PCH)\n"
2689 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002690 " typedef - only load typdefs (non-PCH)\n"
2691 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002692}
2693
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002694/***/
2695
2696int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002697 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002698 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2699 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002700 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002701 return perform_code_completion(argc, argv, 0);
2702 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2703 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002704 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2705 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002706 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2707 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002708 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2709 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002710 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2711 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002712 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002713 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002714 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002715 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2716 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002717 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002718 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2719 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2720 if (I) {
2721 int trials = atoi(argv[2]);
2722 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2723 NULL);
2724 }
2725 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002726 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002727 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002728
2729 PostVisitTU postVisit = 0;
2730 if (strstr(argv[1], "-memory-usage"))
2731 postVisit = PrintMemoryUsage;
2732
Ted Kremenek7d405622010-01-12 23:34:26 +00002733 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002734 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2735 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002736 }
2737 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002738 return perform_file_scan(argv[2], argv[3],
2739 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002740 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2741 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002742 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2743 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2744 PrintInclusionStack);
2745 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2746 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2747 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002748 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2749 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2750 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002751 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2752 return perform_test_load_source(argc - 2, argv + 2, "all",
2753 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002754 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2755 if (argc > 2)
2756 return print_usrs(argv + 2, argv + argc);
2757 else {
2758 display_usrs();
2759 return 1;
2760 }
2761 }
2762 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2763 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002764 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2765 return write_pch_file(argv[2], argc - 3, argv + 3);
2766
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002767 print_usage();
2768 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002769}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002770
2771/***/
2772
2773/* We intentionally run in a separate thread to ensure we at least minimal
2774 * testing of a multithreaded environment (for example, having a reduced stack
2775 * size). */
2776
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002777typedef struct thread_info {
2778 int argc;
2779 const char **argv;
2780 int result;
2781} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002782void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002783 thread_info *client_data = client_data_v;
2784 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002785}
2786
2787int main(int argc, const char **argv) {
2788 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002789
Douglas Gregor61605982010-10-27 16:00:01 +00002790 if (getenv("CINDEXTEST_NOTHREADS"))
2791 return cindextest_main(argc, argv);
2792
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002793 client_data.argc = argc;
2794 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002795 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002796 return client_data.result;
2797}