blob: 573e6dc3141f8e1726df2b6a42b73e457608ab5f [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;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000042 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
43 options |= CXTranslationUnit_SkipFunctionBodies;
Douglas Gregor44c181a2010-07-23 00:33:23 +000044
45 return options;
46}
47
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000048static int checkForErrors(CXTranslationUnit TU);
49
Daniel Dunbar51b058c2010-02-14 08:32:24 +000050static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
51 unsigned end_line, unsigned end_column) {
52 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000053 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000054}
55
Ted Kremenek1c6da172009-11-17 19:37:36 +000056static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
57 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000058
Douglas Gregora88084b2010-02-18 18:08:43 +000059 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000060 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000061 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
62 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000063 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000064 return 1;
65}
66
Douglas Gregor4db64a42010-01-23 00:14:00 +000067void free_remapped_files(struct CXUnsavedFile *unsaved_files,
68 int num_unsaved_files) {
69 int i;
70 for (i = 0; i != num_unsaved_files; ++i) {
71 free((char *)unsaved_files[i].Filename);
72 free((char *)unsaved_files[i].Contents);
73 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000074 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000075}
76
77int parse_remapped_files(int argc, const char **argv, int start_arg,
78 struct CXUnsavedFile **unsaved_files,
79 int *num_unsaved_files) {
80 int i;
81 int arg;
82 int prefix_len = strlen("-remap-file=");
83 *unsaved_files = 0;
84 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000085
Douglas Gregor4db64a42010-01-23 00:14:00 +000086 /* Count the number of remapped files. */
87 for (arg = start_arg; arg < argc; ++arg) {
88 if (strncmp(argv[arg], "-remap-file=", prefix_len))
89 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 ++*num_unsaved_files;
92 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000093
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 if (*num_unsaved_files == 0)
95 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000096
Douglas Gregor4db64a42010-01-23 00:14:00 +000097 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000098 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
99 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
101 struct CXUnsavedFile *unsaved = *unsaved_files + i;
102 const char *arg_string = argv[arg] + prefix_len;
103 int filename_len;
104 char *filename;
105 char *contents;
106 FILE *to_file;
107 const char *semi = strchr(arg_string, ';');
108 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000109 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000110 "error: -remap-file=from;to argument is missing semicolon\n");
111 free_remapped_files(*unsaved_files, i);
112 *unsaved_files = 0;
113 *num_unsaved_files = 0;
114 return -1;
115 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000118 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 if (!to_file) {
120 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
121 semi + 1);
122 free_remapped_files(*unsaved_files, i);
123 *unsaved_files = 0;
124 *num_unsaved_files = 0;
125 return -1;
126 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000127
Douglas Gregor4db64a42010-01-23 00:14:00 +0000128 /* Determine the length of the file we're remapping to. */
129 fseek(to_file, 0, SEEK_END);
130 unsaved->Length = ftell(to_file);
131 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000132
Douglas Gregor4db64a42010-01-23 00:14:00 +0000133 /* Read the contents of the file we're remapping to. */
134 contents = (char *)malloc(unsaved->Length + 1);
135 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
136 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
137 (feof(to_file) ? "EOF" : "error"), semi + 1);
138 fclose(to_file);
139 free_remapped_files(*unsaved_files, i);
140 *unsaved_files = 0;
141 *num_unsaved_files = 0;
142 return -1;
143 }
144 contents[unsaved->Length] = 0;
145 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000146
Douglas Gregor4db64a42010-01-23 00:14:00 +0000147 /* Close the file. */
148 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 /* Copy the file name that we're remapping from. */
151 filename_len = semi - arg_string;
152 filename = (char *)malloc(filename_len + 1);
153 memcpy(filename, arg_string, filename_len);
154 filename[filename_len] = 0;
155 unsaved->Filename = filename;
156 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000157
Douglas Gregor4db64a42010-01-23 00:14:00 +0000158 return 0;
159}
160
Ted Kremenek0d435192009-11-17 18:13:31 +0000161/******************************************************************************/
162/* Pretty-printing. */
163/******************************************************************************/
164
Douglas Gregor430d7a12011-07-25 17:48:11 +0000165static void PrintRange(CXSourceRange R, const char *str) {
166 CXFile begin_file, end_file;
167 unsigned begin_line, begin_column, end_line, end_column;
168
169 clang_getSpellingLocation(clang_getRangeStart(R),
170 &begin_file, &begin_line, &begin_column, 0);
171 clang_getSpellingLocation(clang_getRangeEnd(R),
172 &end_file, &end_line, &end_column, 0);
173 if (!begin_file || !end_file)
174 return;
175
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000176 if (str)
177 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000178 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
179}
180
Douglas Gregor358559d2010-10-02 22:49:11 +0000181int want_display_name = 0;
182
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000183static void PrintCursor(CXCursor Cursor) {
184 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000185 if (clang_isInvalid(Cursor.kind)) {
186 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
187 printf("Invalid Cursor => %s", clang_getCString(ks));
188 clang_disposeString(ks);
189 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000190 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000191 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000192 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000193 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000194 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000195 CXCursor *overridden;
196 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000197 unsigned RefNameRangeNr;
198 CXSourceRange CursorExtent;
199 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000200
Ted Kremeneke68fff62010-02-17 00:41:32 +0000201 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000202 string = want_display_name? clang_getCursorDisplayName(Cursor)
203 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000204 printf("%s=%s", clang_getCString(ks),
205 clang_getCString(string));
206 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000207 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000208
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000209 Referenced = clang_getCursorReferenced(Cursor);
210 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000211 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
212 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
213 printf("[");
214 for (I = 0; I != N; ++I) {
215 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000216 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000217 if (I)
218 printf(", ");
219
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000220 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000221 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000222 printf("%d:%d", line, column);
223 }
224 printf("]");
225 } else {
226 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000227 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000228 printf(":%d:%d", line, column);
229 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000230 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000231
232 if (clang_isCursorDefinition(Cursor))
233 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000234
235 switch (clang_getCursorAvailability(Cursor)) {
236 case CXAvailability_Available:
237 break;
238
239 case CXAvailability_Deprecated:
240 printf(" (deprecated)");
241 break;
242
243 case CXAvailability_NotAvailable:
244 printf(" (unavailable)");
245 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000246
247 case CXAvailability_NotAccessible:
248 printf(" (inaccessible)");
249 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000250 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000251
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000252 if (clang_CXXMethod_isStatic(Cursor))
253 printf(" (static)");
254 if (clang_CXXMethod_isVirtual(Cursor))
255 printf(" (virtual)");
256
Ted Kremenek95f33552010-08-26 01:42:22 +0000257 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
258 CXType T =
259 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
260 CXString S = clang_getTypeKindSpelling(T.kind);
261 printf(" [IBOutletCollection=%s]", clang_getCString(S));
262 clang_disposeString(S);
263 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000264
265 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
266 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
267 unsigned isVirtual = clang_isVirtualBase(Cursor);
268 const char *accessStr = 0;
269
270 switch (access) {
271 case CX_CXXInvalidAccessSpecifier:
272 accessStr = "invalid"; break;
273 case CX_CXXPublic:
274 accessStr = "public"; break;
275 case CX_CXXProtected:
276 accessStr = "protected"; break;
277 case CX_CXXPrivate:
278 accessStr = "private"; break;
279 }
280
281 printf(" [access=%s isVirtual=%s]", accessStr,
282 isVirtual ? "true" : "false");
283 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000284
285 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
286 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
287 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
288 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000289 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000290 printf(" [Specialization of %s:%d:%d]",
291 clang_getCString(Name), line, column);
292 clang_disposeString(Name);
293 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000294
295 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
296 if (num_overridden) {
297 unsigned I;
298 printf(" [Overrides ");
299 for (I = 0; I != num_overridden; ++I) {
300 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000301 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000302 if (I)
303 printf(", ");
304 printf("@%d:%d", line, column);
305 }
306 printf("]");
307 clang_disposeOverriddenCursors(overridden);
308 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000309
310 if (Cursor.kind == CXCursor_InclusionDirective) {
311 CXFile File = clang_getIncludedFile(Cursor);
312 CXString Included = clang_getFileName(File);
313 printf(" (%s)", clang_getCString(Included));
314 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000315
316 if (clang_isFileMultipleIncludeGuarded(TU, File))
317 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000318 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000319
320 CursorExtent = clang_getCursorExtent(Cursor);
321 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
322 CXNameRange_WantQualifier
323 | CXNameRange_WantSinglePiece
324 | CXNameRange_WantTemplateArgs,
325 0);
326 if (!clang_equalRanges(CursorExtent, RefNameRange))
327 PrintRange(RefNameRange, "SingleRefName");
328
329 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
330 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
331 CXNameRange_WantQualifier
332 | CXNameRange_WantTemplateArgs,
333 RefNameRangeNr);
334 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
335 break;
336 if (!clang_equalRanges(CursorExtent, RefNameRange))
337 PrintRange(RefNameRange, "RefName");
338 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000339 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000340}
Steve Naroff89922f82009-08-31 00:59:03 +0000341
Ted Kremeneke68fff62010-02-17 00:41:32 +0000342static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000343 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000344 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000346 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000347 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000348 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000349 clang_disposeString(source);
350 return "<invalid loc>";
351 }
352 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000353 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000354 clang_disposeString(source);
355 return b;
356 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000357}
358
Ted Kremenek0d435192009-11-17 18:13:31 +0000359/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000360/* Callbacks. */
361/******************************************************************************/
362
363typedef void (*PostVisitTU)(CXTranslationUnit);
364
Douglas Gregora88084b2010-02-18 18:08:43 +0000365void PrintDiagnostic(CXDiagnostic Diagnostic) {
366 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000367 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000368 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000369 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000370 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
371 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000372 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000373
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000374 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000375 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000376
Douglas Gregor274f1902010-02-22 23:17:23 +0000377 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
378 fprintf(stderr, "%s\n", clang_getCString(Msg));
379 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000380
Douglas Gregora9b06d42010-11-09 06:24:54 +0000381 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
382 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000383 if (!file)
384 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000385
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000386 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000387 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000388 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000389 CXSourceRange range;
390 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
391 CXSourceLocation start = clang_getRangeStart(range);
392 CXSourceLocation end = clang_getRangeEnd(range);
393 unsigned start_line, start_column, end_line, end_column;
394 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000395 clang_getSpellingLocation(start, &start_file, &start_line,
396 &start_column, 0);
397 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000398 if (clang_equalLocations(start, end)) {
399 /* Insertion. */
400 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000401 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000402 clang_getCString(insertion_text), start_line, start_column);
403 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
404 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000405 if (start_file == file && end_file == file) {
406 fprintf(out, "FIX-IT: Remove ");
407 PrintExtent(out, start_line, start_column, end_line, end_column);
408 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000409 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000410 } else {
411 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000412 if (start_file == end_file) {
413 fprintf(out, "FIX-IT: Replace ");
414 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000415 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000416 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000417 break;
418 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000419 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000420 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000421}
422
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000423void PrintDiagnosticSet(CXDiagnosticSet Set) {
424 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
425 for ( ; i != n ; ++i) {
426 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
427 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000428 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000429 if (ChildDiags)
430 PrintDiagnosticSet(ChildDiags);
431 }
432}
433
434void PrintDiagnostics(CXTranslationUnit TU) {
435 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
436 PrintDiagnosticSet(TUSet);
437 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000438}
439
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000440void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000441 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000442 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000443 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000444 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000445 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000446 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000447 unsigned long amount = usage.entries[i].amount;
448 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000449 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000450 ((double) amount)/(1024*1024));
451 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000452 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000453 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000454 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000455}
456
Ted Kremenekce2ae882010-01-26 17:59:48 +0000457/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000458/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000459/******************************************************************************/
460
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000461static const char *FileCheckPrefix = "CHECK";
462
Douglas Gregora7bde202010-01-19 00:34:46 +0000463static void PrintCursorExtent(CXCursor C) {
464 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000465 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000466}
467
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000468/* Data used by all of the visitors. */
469typedef struct {
470 CXTranslationUnit TU;
471 enum CXCursorKind *Filter;
472} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000473
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000474
Ted Kremeneke68fff62010-02-17 00:41:32 +0000475enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000476 CXCursor Parent,
477 CXClientData ClientData) {
478 VisitorData *Data = (VisitorData *)ClientData;
479 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000480 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000481 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000482 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000483 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000484 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000485 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000486 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000487 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000488 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000489 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000490
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000491 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000492}
Steve Naroff50398192009-08-28 15:28:48 +0000493
Ted Kremeneke68fff62010-02-17 00:41:32 +0000494static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000495 CXCursor Parent,
496 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000497 const char *startBuf, *endBuf;
498 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
499 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000500 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000501
Douglas Gregorb6998662010-01-19 19:34:47 +0000502 if (Cursor.kind != CXCursor_FunctionDecl ||
503 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000504 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000505
506 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
507 &startLine, &startColumn,
508 &endLine, &endColumn);
509 /* Probe the entire body, looking for both decls and refs. */
510 curLine = startLine;
511 curColumn = startColumn;
512
513 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000514 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000515 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000516 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000518 if (*startBuf == '\n') {
519 startBuf++;
520 curLine++;
521 curColumn = 1;
522 } else if (*startBuf != '\t')
523 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000524
Douglas Gregor98258af2010-01-18 22:46:11 +0000525 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000526 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000527
Douglas Gregor1db19de2010-01-19 21:36:55 +0000528 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000529 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000530 CXSourceLocation RefLoc
531 = clang_getLocation(Data->TU, file, curLine, curColumn);
532 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000533 if (Ref.kind == CXCursor_NoDeclFound) {
534 /* Nothing found here; that's fine. */
535 } else if (Ref.kind != CXCursor_FunctionDecl) {
536 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
537 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000538 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000539 printf("\n");
540 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000541 }
Ted Kremenek74844072010-02-17 00:41:20 +0000542 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000543 startBuf++;
544 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000545
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000546 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000547}
548
Ted Kremenek7d405622010-01-12 23:34:26 +0000549/******************************************************************************/
550/* USR testing. */
551/******************************************************************************/
552
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000553enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
554 CXClientData ClientData) {
555 VisitorData *Data = (VisitorData *)ClientData;
556 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000557 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000558 const char *cstr = clang_getCString(USR);
559 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000560 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000561 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000562 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000563 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
564
Douglas Gregora7bde202010-01-19 00:34:46 +0000565 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000566 printf("\n");
567 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000568
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000569 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000570 }
571
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000572 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000573}
574
575/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000576/* Inclusion stack testing. */
577/******************************************************************************/
578
579void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
580 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000581
Ted Kremenek16b55a72010-01-26 19:31:51 +0000582 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000583 CXString fname;
584
585 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000587 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000588
Ted Kremenek16b55a72010-01-26 19:31:51 +0000589 for (i = 0; i < includeStackLen; ++i) {
590 CXFile includingFile;
591 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000592 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
593 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000594 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000595 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000596 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000597 }
598 printf("\n");
599}
600
601void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000602 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000603}
604
605/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000606/* Linkage testing. */
607/******************************************************************************/
608
609static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
610 CXClientData d) {
611 const char *linkage = 0;
612
613 if (clang_isInvalid(clang_getCursorKind(cursor)))
614 return CXChildVisit_Recurse;
615
616 switch (clang_getCursorLinkage(cursor)) {
617 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000618 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
619 case CXLinkage_Internal: linkage = "Internal"; break;
620 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
621 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000622 }
623
624 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000625 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000626 printf("linkage=%s\n", linkage);
627 }
628
629 return CXChildVisit_Recurse;
630}
631
632/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000633/* Typekind testing. */
634/******************************************************************************/
635
636static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
637 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000638 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
639 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000640 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000641 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000642 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000643 if (clang_isConstQualifiedType(T))
644 printf(" const");
645 if (clang_isVolatileQualifiedType(T))
646 printf(" volatile");
647 if (clang_isRestrictQualifiedType(T))
648 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000649 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000650 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000651 {
652 CXType CT = clang_getCanonicalType(T);
653 if (!clang_equalTypes(T, CT)) {
654 CXString CS = clang_getTypeKindSpelling(CT.kind);
655 printf(" [canonical=%s]", clang_getCString(CS));
656 clang_disposeString(CS);
657 }
658 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000659 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000661 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000662 if (RT.kind != CXType_Invalid) {
663 CXString RS = clang_getTypeKindSpelling(RT.kind);
664 printf(" [result=%s]", clang_getCString(RS));
665 clang_disposeString(RS);
666 }
667 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000668 /* Print the argument types if they exist. */
669 {
670 int numArgs = clang_Cursor_getNumArguments(cursor);
671 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000672 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000673 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000674 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000675 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
676 if (T.kind != CXType_Invalid) {
677 CXString S = clang_getTypeKindSpelling(T.kind);
678 printf(" %s", clang_getCString(S));
679 clang_disposeString(S);
680 }
681 }
682 printf("]");
683 }
684 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000685 /* Print if this is a non-POD type. */
686 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000687
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000688 printf("\n");
689 }
690 return CXChildVisit_Recurse;
691}
692
693
694/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000695/* Loading ASTs/source. */
696/******************************************************************************/
697
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000698static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000699 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000700 CXCursorVisitor Visitor,
701 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000702
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000703 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000704 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000705
706 if (Visitor) {
707 enum CXCursorKind K = CXCursor_NotImplemented;
708 enum CXCursorKind *ck = &K;
709 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000710
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000711 /* Perform some simple filtering. */
712 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000713 else if (!strcmp(filter, "all-display") ||
714 !strcmp(filter, "local-display")) {
715 ck = NULL;
716 want_display_name = 1;
717 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000718 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000719 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
720 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
721 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
722 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
723 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
724 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
725 else {
726 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
727 return 1;
728 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000729
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000730 Data.TU = TU;
731 Data.Filter = ck;
732 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000733 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000734
Ted Kremenekce2ae882010-01-26 17:59:48 +0000735 if (PV)
736 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000737
Douglas Gregora88084b2010-02-18 18:08:43 +0000738 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000739 if (checkForErrors(TU) != 0) {
740 clang_disposeTranslationUnit(TU);
741 return -1;
742 }
743
Ted Kremenek0d435192009-11-17 18:13:31 +0000744 clang_disposeTranslationUnit(TU);
745 return 0;
746}
747
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000748int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000749 const char *prefix, CXCursorVisitor Visitor,
750 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000751 CXIndex Idx;
752 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000753 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000754 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000755 !strcmp(filter, "local") ? 1 : 0,
756 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000757
Ted Kremenek020a0952010-02-11 07:41:25 +0000758 if (!CreateTranslationUnit(Idx, file, &TU)) {
759 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000760 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000761 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000762
Ted Kremenek020a0952010-02-11 07:41:25 +0000763 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
764 clang_disposeIndex(Idx);
765 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000766}
767
Ted Kremenekce2ae882010-01-26 17:59:48 +0000768int perform_test_load_source(int argc, const char **argv,
769 const char *filter, CXCursorVisitor Visitor,
770 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000771 CXIndex Idx;
772 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000773 struct CXUnsavedFile *unsaved_files = 0;
774 int num_unsaved_files = 0;
775 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000776
Daniel Dunbarada487d2009-12-01 02:03:10 +0000777 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000778 (!strcmp(filter, "local") ||
779 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000780 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000781
Ted Kremenek020a0952010-02-11 07:41:25 +0000782 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
783 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000784 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000785 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000786
Douglas Gregordca8ee82011-05-06 16:33:08 +0000787 TU = clang_parseTranslationUnit(Idx, 0,
788 argv + num_unsaved_files,
789 argc - num_unsaved_files,
790 unsaved_files, num_unsaved_files,
791 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000792 if (!TU) {
793 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000794 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000795 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000796 return 1;
797 }
798
Ted Kremenekce2ae882010-01-26 17:59:48 +0000799 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000800 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000801 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000802 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000803}
804
Douglas Gregorabc563f2010-07-19 21:46:24 +0000805int perform_test_reparse_source(int argc, const char **argv, int trials,
806 const char *filter, CXCursorVisitor Visitor,
807 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000808 CXIndex Idx;
809 CXTranslationUnit TU;
810 struct CXUnsavedFile *unsaved_files = 0;
811 int num_unsaved_files = 0;
812 int result;
813 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000814 int remap_after_trial = 0;
815 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000816
817 Idx = clang_createIndex(/* excludeDeclsFromPCH */
818 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000819 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000820
Douglas Gregorabc563f2010-07-19 21:46:24 +0000821 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
822 clang_disposeIndex(Idx);
823 return -1;
824 }
825
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000826 /* Load the initial translation unit -- we do this without honoring remapped
827 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000828 TU = clang_parseTranslationUnit(Idx, 0,
829 argv + num_unsaved_files,
830 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000831 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000832 if (!TU) {
833 fprintf(stderr, "Unable to load translation unit!\n");
834 free_remapped_files(unsaved_files, num_unsaved_files);
835 clang_disposeIndex(Idx);
836 return 1;
837 }
838
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000839 if (checkForErrors(TU) != 0)
840 return -1;
841
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000842 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
843 remap_after_trial =
844 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
845 }
846
Douglas Gregorabc563f2010-07-19 21:46:24 +0000847 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000848 if (clang_reparseTranslationUnit(TU,
849 trial >= remap_after_trial ? num_unsaved_files : 0,
850 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000851 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000852 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000853 clang_disposeTranslationUnit(TU);
854 free_remapped_files(unsaved_files, num_unsaved_files);
855 clang_disposeIndex(Idx);
856 return -1;
857 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000858
859 if (checkForErrors(TU) != 0)
860 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000861 }
862
863 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000864
Douglas Gregorabc563f2010-07-19 21:46:24 +0000865 free_remapped_files(unsaved_files, num_unsaved_files);
866 clang_disposeIndex(Idx);
867 return result;
868}
869
Ted Kremenek0d435192009-11-17 18:13:31 +0000870/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000871/* Logic for testing clang_getCursor(). */
872/******************************************************************************/
873
Douglas Gregordd3e5542011-05-04 00:14:37 +0000874static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000875 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000876 unsigned end_line, unsigned end_col,
877 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000878 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000879 if (prefix)
880 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000881 PrintExtent(stdout, start_line, start_col, end_line, end_col);
882 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000883 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000884 printf("\n");
885}
886
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000887static int perform_file_scan(const char *ast_file, const char *source_file,
888 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000889 CXIndex Idx;
890 CXTranslationUnit TU;
891 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000892 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000893 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000894 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000895 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000896
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000897 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
898 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000899 fprintf(stderr, "Could not create Index\n");
900 return 1;
901 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000902
Ted Kremenek1c6da172009-11-17 19:37:36 +0000903 if (!CreateTranslationUnit(Idx, ast_file, &TU))
904 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000905
Ted Kremenek1c6da172009-11-17 19:37:36 +0000906 if ((fp = fopen(source_file, "r")) == NULL) {
907 fprintf(stderr, "Could not open '%s'\n", source_file);
908 return 1;
909 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000910
Douglas Gregorb9790342010-01-22 21:44:22 +0000911 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000912 for (;;) {
913 CXCursor cursor;
914 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000915
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000916 if (c == '\n') {
917 ++line;
918 col = 1;
919 } else
920 ++col;
921
922 /* Check the cursor at this position, and dump the previous one if we have
923 * found something new.
924 */
925 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
926 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
927 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000928 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000929 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000930 start_line = line;
931 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000932 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000933 if (c == EOF)
934 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000935
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000936 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000937 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000938
Ted Kremenek1c6da172009-11-17 19:37:36 +0000939 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000940 clang_disposeTranslationUnit(TU);
941 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000942 return 0;
943}
944
945/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000946/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000947/******************************************************************************/
948
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000949/* Parse file:line:column from the input string. Returns 0 on success, non-zero
950 on failure. If successful, the pointer *filename will contain newly-allocated
951 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000952int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000953 unsigned *column, unsigned *second_line,
954 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000955 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000956 const char *last_colon = strrchr(input, ':');
957 unsigned values[4], i;
958 unsigned num_values = (second_line && second_column)? 4 : 2;
959
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000960 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000961 if (!last_colon || last_colon == input) {
962 if (num_values == 4)
963 fprintf(stderr, "could not parse filename:line:column:line:column in "
964 "'%s'\n", input);
965 else
966 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000967 return 1;
968 }
969
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000970 for (i = 0; i != num_values; ++i) {
971 const char *prev_colon;
972
973 /* Parse the next line or column. */
974 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
975 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000976 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000977 (i % 2 ? "column" : "line"), input);
978 return 1;
979 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000980
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000981 if (i + 1 == num_values)
982 break;
983
984 /* Find the previous colon. */
985 prev_colon = last_colon - 1;
986 while (prev_colon != input && *prev_colon != ':')
987 --prev_colon;
988 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000989 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000990 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000991 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000992 }
993
994 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000995 }
996
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000997 *line = values[0];
998 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000999
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001000 if (second_line && second_column) {
1001 *second_line = values[2];
1002 *second_column = values[3];
1003 }
1004
Douglas Gregor88d23952009-11-09 18:19:57 +00001005 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001006 *filename = (char*)malloc(last_colon - input + 1);
1007 memcpy(*filename, input, last_colon - input);
1008 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001009 return 0;
1010}
1011
1012const char *
1013clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1014 switch (Kind) {
1015 case CXCompletionChunk_Optional: return "Optional";
1016 case CXCompletionChunk_TypedText: return "TypedText";
1017 case CXCompletionChunk_Text: return "Text";
1018 case CXCompletionChunk_Placeholder: return "Placeholder";
1019 case CXCompletionChunk_Informative: return "Informative";
1020 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1021 case CXCompletionChunk_LeftParen: return "LeftParen";
1022 case CXCompletionChunk_RightParen: return "RightParen";
1023 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1024 case CXCompletionChunk_RightBracket: return "RightBracket";
1025 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1026 case CXCompletionChunk_RightBrace: return "RightBrace";
1027 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1028 case CXCompletionChunk_RightAngle: return "RightAngle";
1029 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001030 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001031 case CXCompletionChunk_Colon: return "Colon";
1032 case CXCompletionChunk_SemiColon: return "SemiColon";
1033 case CXCompletionChunk_Equal: return "Equal";
1034 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1035 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001036 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001037
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001038 return "Unknown";
1039}
1040
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001041static int checkForErrors(CXTranslationUnit TU) {
1042 unsigned Num, i;
1043 CXDiagnostic Diag;
1044 CXString DiagStr;
1045
1046 if (!getenv("CINDEXTEST_FAILONERROR"))
1047 return 0;
1048
1049 Num = clang_getNumDiagnostics(TU);
1050 for (i = 0; i != Num; ++i) {
1051 Diag = clang_getDiagnostic(TU, i);
1052 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1053 DiagStr = clang_formatDiagnostic(Diag,
1054 clang_defaultDiagnosticDisplayOptions());
1055 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1056 clang_disposeString(DiagStr);
1057 clang_disposeDiagnostic(Diag);
1058 return -1;
1059 }
1060 clang_disposeDiagnostic(Diag);
1061 }
1062
1063 return 0;
1064}
1065
Douglas Gregor3ac73852009-11-09 16:04:45 +00001066void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001067 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001068
Douglas Gregor3ac73852009-11-09 16:04:45 +00001069 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001070 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001071 CXString text;
1072 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001073 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001074 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001075
Douglas Gregor3ac73852009-11-09 16:04:45 +00001076 if (Kind == CXCompletionChunk_Optional) {
1077 fprintf(file, "{Optional ");
1078 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001079 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001080 file);
1081 fprintf(file, "}");
1082 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001083 }
1084
1085 if (Kind == CXCompletionChunk_VerticalSpace) {
1086 fprintf(file, "{VerticalSpace }");
1087 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001088 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001089
Douglas Gregord5a20892009-11-09 17:05:28 +00001090 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001091 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001092 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001093 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001094 cstr ? cstr : "");
1095 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001096 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001097
Douglas Gregor3ac73852009-11-09 16:04:45 +00001098}
1099
1100void print_completion_result(CXCompletionResult *completion_result,
1101 CXClientData client_data) {
1102 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001103 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001104 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001105 enum CXCursorKind ParentKind;
1106 CXString ParentName;
1107
Ted Kremeneke68fff62010-02-17 00:41:32 +00001108 fprintf(file, "%s:", clang_getCString(ks));
1109 clang_disposeString(ks);
1110
Douglas Gregor3ac73852009-11-09 16:04:45 +00001111 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001112 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001113 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001114 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1115 case CXAvailability_Available:
1116 break;
1117
1118 case CXAvailability_Deprecated:
1119 fprintf(file, " (deprecated)");
1120 break;
1121
1122 case CXAvailability_NotAvailable:
1123 fprintf(file, " (unavailable)");
1124 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001125
1126 case CXAvailability_NotAccessible:
1127 fprintf(file, " (inaccessible)");
1128 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001129 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001130
1131 annotationCount = clang_getCompletionNumAnnotations(
1132 completion_result->CompletionString);
1133 if (annotationCount) {
1134 unsigned i;
1135 fprintf(file, " (");
1136 for (i = 0; i < annotationCount; ++i) {
1137 if (i != 0)
1138 fprintf(file, ", ");
1139 fprintf(file, "\"%s\"",
1140 clang_getCString(clang_getCompletionAnnotation(
1141 completion_result->CompletionString, i)));
1142 }
1143 fprintf(file, ")");
1144 }
1145
Douglas Gregorba103062012-03-27 23:34:16 +00001146 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1147 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1148 &ParentKind);
1149 if (ParentKind != CXCursor_NotImplemented) {
1150 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1151 fprintf(file, " (parent: %s '%s')",
1152 clang_getCString(KindSpelling),
1153 clang_getCString(ParentName));
1154 clang_disposeString(KindSpelling);
1155 }
1156 clang_disposeString(ParentName);
1157 }
1158
Douglas Gregor58ddb602010-08-23 23:00:57 +00001159 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001160}
1161
Douglas Gregor3da626b2011-07-07 16:03:39 +00001162void print_completion_contexts(unsigned long long contexts, FILE *file) {
1163 fprintf(file, "Completion contexts:\n");
1164 if (contexts == CXCompletionContext_Unknown) {
1165 fprintf(file, "Unknown\n");
1166 }
1167 if (contexts & CXCompletionContext_AnyType) {
1168 fprintf(file, "Any type\n");
1169 }
1170 if (contexts & CXCompletionContext_AnyValue) {
1171 fprintf(file, "Any value\n");
1172 }
1173 if (contexts & CXCompletionContext_ObjCObjectValue) {
1174 fprintf(file, "Objective-C object value\n");
1175 }
1176 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1177 fprintf(file, "Objective-C selector value\n");
1178 }
1179 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1180 fprintf(file, "C++ class type value\n");
1181 }
1182 if (contexts & CXCompletionContext_DotMemberAccess) {
1183 fprintf(file, "Dot member access\n");
1184 }
1185 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1186 fprintf(file, "Arrow member access\n");
1187 }
1188 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1189 fprintf(file, "Objective-C property access\n");
1190 }
1191 if (contexts & CXCompletionContext_EnumTag) {
1192 fprintf(file, "Enum tag\n");
1193 }
1194 if (contexts & CXCompletionContext_UnionTag) {
1195 fprintf(file, "Union tag\n");
1196 }
1197 if (contexts & CXCompletionContext_StructTag) {
1198 fprintf(file, "Struct tag\n");
1199 }
1200 if (contexts & CXCompletionContext_ClassTag) {
1201 fprintf(file, "Class name\n");
1202 }
1203 if (contexts & CXCompletionContext_Namespace) {
1204 fprintf(file, "Namespace or namespace alias\n");
1205 }
1206 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1207 fprintf(file, "Nested name specifier\n");
1208 }
1209 if (contexts & CXCompletionContext_ObjCInterface) {
1210 fprintf(file, "Objective-C interface\n");
1211 }
1212 if (contexts & CXCompletionContext_ObjCProtocol) {
1213 fprintf(file, "Objective-C protocol\n");
1214 }
1215 if (contexts & CXCompletionContext_ObjCCategory) {
1216 fprintf(file, "Objective-C category\n");
1217 }
1218 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1219 fprintf(file, "Objective-C instance method\n");
1220 }
1221 if (contexts & CXCompletionContext_ObjCClassMessage) {
1222 fprintf(file, "Objective-C class method\n");
1223 }
1224 if (contexts & CXCompletionContext_ObjCSelectorName) {
1225 fprintf(file, "Objective-C selector name\n");
1226 }
1227 if (contexts & CXCompletionContext_MacroName) {
1228 fprintf(file, "Macro name\n");
1229 }
1230 if (contexts & CXCompletionContext_NaturalLanguage) {
1231 fprintf(file, "Natural language\n");
1232 }
1233}
1234
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001235int my_stricmp(const char *s1, const char *s2) {
1236 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001237 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001238 if (c1 < c2)
1239 return -1;
1240 else if (c1 > c2)
1241 return 1;
1242
1243 ++s1;
1244 ++s2;
1245 }
1246
1247 if (*s1)
1248 return 1;
1249 else if (*s2)
1250 return -1;
1251 return 0;
1252}
1253
Douglas Gregor1982c182010-07-12 18:38:41 +00001254int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001255 const char *input = argv[1];
1256 char *filename = 0;
1257 unsigned line;
1258 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001259 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001260 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001261 struct CXUnsavedFile *unsaved_files = 0;
1262 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001263 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001264 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001265 unsigned I, Repeats = 1;
1266 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1267
1268 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1269 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001270
Douglas Gregor1982c182010-07-12 18:38:41 +00001271 if (timing_only)
1272 input += strlen("-code-completion-timing=");
1273 else
1274 input += strlen("-code-completion-at=");
1275
Ted Kremeneke68fff62010-02-17 00:41:32 +00001276 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001277 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001278 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001279
Douglas Gregor735df882009-12-02 09:21:34 +00001280 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1281 return -1;
1282
Douglas Gregor32be4a52010-10-11 21:37:58 +00001283 CIdx = clang_createIndex(0, 0);
1284
1285 if (getenv("CINDEXTEST_EDITING"))
1286 Repeats = 5;
1287
1288 TU = clang_parseTranslationUnit(CIdx, 0,
1289 argv + num_unsaved_files + 2,
1290 argc - num_unsaved_files - 2,
1291 0, 0, getDefaultParsingOptions());
1292 if (!TU) {
1293 fprintf(stderr, "Unable to load translation unit!\n");
1294 return 1;
1295 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001296
1297 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1298 fprintf(stderr, "Unable to reparse translation init!\n");
1299 return 1;
1300 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001301
1302 for (I = 0; I != Repeats; ++I) {
1303 results = clang_codeCompleteAt(TU, filename, line, column,
1304 unsaved_files, num_unsaved_files,
1305 completionOptions);
1306 if (!results) {
1307 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001308 return 1;
1309 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001310 if (I != Repeats-1)
1311 clang_disposeCodeCompleteResults(results);
1312 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001313
Douglas Gregorec6762c2009-12-18 16:20:58 +00001314 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001315 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001316 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001317 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001318 CXString objCSelector;
1319 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001320 if (!timing_only) {
1321 /* Sort the code-completion results based on the typed text. */
1322 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1323
Douglas Gregor1982c182010-07-12 18:38:41 +00001324 for (i = 0; i != n; ++i)
1325 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001326 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001327 n = clang_codeCompleteGetNumDiagnostics(results);
1328 for (i = 0; i != n; ++i) {
1329 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1330 PrintDiagnostic(diag);
1331 clang_disposeDiagnostic(diag);
1332 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001333
1334 contexts = clang_codeCompleteGetContexts(results);
1335 print_completion_contexts(contexts, stdout);
1336
Douglas Gregor0a47d692011-07-26 15:24:30 +00001337 containerKind = clang_codeCompleteGetContainerKind(results,
1338 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001339
1340 if (containerKind != CXCursor_InvalidCode) {
1341 /* We have found a container */
1342 CXString containerUSR, containerKindSpelling;
1343 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1344 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1345 clang_disposeString(containerKindSpelling);
1346
1347 if (containerIsIncomplete) {
1348 printf("Container is incomplete\n");
1349 }
1350 else {
1351 printf("Container is complete\n");
1352 }
1353
1354 containerUSR = clang_codeCompleteGetContainerUSR(results);
1355 printf("Container USR: %s\n", clang_getCString(containerUSR));
1356 clang_disposeString(containerUSR);
1357 }
1358
Douglas Gregor0a47d692011-07-26 15:24:30 +00001359 objCSelector = clang_codeCompleteGetObjCSelector(results);
1360 selectorString = clang_getCString(objCSelector);
1361 if (selectorString && strlen(selectorString) > 0) {
1362 printf("Objective-C selector: %s\n", selectorString);
1363 }
1364 clang_disposeString(objCSelector);
1365
Douglas Gregorec6762c2009-12-18 16:20:58 +00001366 clang_disposeCodeCompleteResults(results);
1367 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001368 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001369 clang_disposeIndex(CIdx);
1370 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001371
Douglas Gregor735df882009-12-02 09:21:34 +00001372 free_remapped_files(unsaved_files, num_unsaved_files);
1373
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001374 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001375}
1376
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001377typedef struct {
1378 char *filename;
1379 unsigned line;
1380 unsigned column;
1381} CursorSourceLocation;
1382
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001383static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001384 CXIndex CIdx;
1385 int errorCode;
1386 struct CXUnsavedFile *unsaved_files = 0;
1387 int num_unsaved_files = 0;
1388 CXTranslationUnit TU;
1389 CXCursor Cursor;
1390 CursorSourceLocation *Locations = 0;
1391 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001392 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001393 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001394
Ted Kremeneke68fff62010-02-17 00:41:32 +00001395 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001396 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1397 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001398
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001399 /* Parse the locations. */
1400 assert(NumLocations > 0 && "Unable to count locations?");
1401 Locations = (CursorSourceLocation *)malloc(
1402 NumLocations * sizeof(CursorSourceLocation));
1403 for (Loc = 0; Loc < NumLocations; ++Loc) {
1404 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001405 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1406 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001407 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001408 return errorCode;
1409 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001410
1411 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001412 &num_unsaved_files))
1413 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001414
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001415 if (getenv("CINDEXTEST_EDITING"))
1416 Repeats = 5;
1417
1418 /* Parse the translation unit. When we're testing clang_getCursor() after
1419 reparsing, don't remap unsaved files until the second parse. */
1420 CIdx = clang_createIndex(1, 1);
1421 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1422 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001423 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001424 unsaved_files,
1425 Repeats > 1? 0 : num_unsaved_files,
1426 getDefaultParsingOptions());
1427
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001428 if (!TU) {
1429 fprintf(stderr, "unable to parse input\n");
1430 return -1;
1431 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001432
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001433 if (checkForErrors(TU) != 0)
1434 return -1;
1435
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001436 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001437 if (Repeats > 1 &&
1438 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1439 clang_defaultReparseOptions(TU))) {
1440 clang_disposeTranslationUnit(TU);
1441 return 1;
1442 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001443
1444 if (checkForErrors(TU) != 0)
1445 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001446
1447 for (Loc = 0; Loc < NumLocations; ++Loc) {
1448 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1449 if (!file)
1450 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001451
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001452 Cursor = clang_getCursor(TU,
1453 clang_getLocation(TU, file, Locations[Loc].line,
1454 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001455
1456 if (checkForErrors(TU) != 0)
1457 return -1;
1458
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001459 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001460 CXCompletionString completionString = clang_getCursorCompletionString(
1461 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001462 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1463 CXString Spelling;
1464 const char *cspell;
1465 unsigned line, column;
1466 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1467 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001468 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001469 PrintCursorExtent(Cursor);
1470 Spelling = clang_getCursorSpelling(Cursor);
1471 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001472 if (cspell && strlen(cspell) != 0) {
1473 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001474 printf(" Spelling=%s (", cspell);
1475 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001476 CXSourceRange range =
1477 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001478 if (clang_Range_isNull(range))
1479 break;
1480 PrintRange(range, 0);
1481 }
1482 printf(")");
1483 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001484 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001485 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1486 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001487 if (completionString != NULL) {
1488 printf("\nCompletion string: ");
1489 print_completion_string(completionString, stdout);
1490 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001491 printf("\n");
1492 free(Locations[Loc].filename);
1493 }
1494 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001495 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001496
Douglas Gregora88084b2010-02-18 18:08:43 +00001497 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001498 clang_disposeTranslationUnit(TU);
1499 clang_disposeIndex(CIdx);
1500 free(Locations);
1501 free_remapped_files(unsaved_files, num_unsaved_files);
1502 return 0;
1503}
1504
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001505static enum CXVisitorResult findFileRefsVisit(void *context,
1506 CXCursor cursor, CXSourceRange range) {
1507 if (clang_Range_isNull(range))
1508 return CXVisit_Continue;
1509
1510 PrintCursor(cursor);
1511 PrintRange(range, "");
1512 printf("\n");
1513 return CXVisit_Continue;
1514}
1515
1516static int find_file_refs_at(int argc, const char **argv) {
1517 CXIndex CIdx;
1518 int errorCode;
1519 struct CXUnsavedFile *unsaved_files = 0;
1520 int num_unsaved_files = 0;
1521 CXTranslationUnit TU;
1522 CXCursor Cursor;
1523 CursorSourceLocation *Locations = 0;
1524 unsigned NumLocations = 0, Loc;
1525 unsigned Repeats = 1;
1526 unsigned I;
1527
1528 /* Count the number of locations. */
1529 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1530 ++NumLocations;
1531
1532 /* Parse the locations. */
1533 assert(NumLocations > 0 && "Unable to count locations?");
1534 Locations = (CursorSourceLocation *)malloc(
1535 NumLocations * sizeof(CursorSourceLocation));
1536 for (Loc = 0; Loc < NumLocations; ++Loc) {
1537 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1538 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1539 &Locations[Loc].line,
1540 &Locations[Loc].column, 0, 0)))
1541 return errorCode;
1542 }
1543
1544 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1545 &num_unsaved_files))
1546 return -1;
1547
1548 if (getenv("CINDEXTEST_EDITING"))
1549 Repeats = 5;
1550
1551 /* Parse the translation unit. When we're testing clang_getCursor() after
1552 reparsing, don't remap unsaved files until the second parse. */
1553 CIdx = clang_createIndex(1, 1);
1554 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1555 argv + num_unsaved_files + 1 + NumLocations,
1556 argc - num_unsaved_files - 2 - NumLocations,
1557 unsaved_files,
1558 Repeats > 1? 0 : num_unsaved_files,
1559 getDefaultParsingOptions());
1560
1561 if (!TU) {
1562 fprintf(stderr, "unable to parse input\n");
1563 return -1;
1564 }
1565
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001566 if (checkForErrors(TU) != 0)
1567 return -1;
1568
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001569 for (I = 0; I != Repeats; ++I) {
1570 if (Repeats > 1 &&
1571 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1572 clang_defaultReparseOptions(TU))) {
1573 clang_disposeTranslationUnit(TU);
1574 return 1;
1575 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001576
1577 if (checkForErrors(TU) != 0)
1578 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001579
1580 for (Loc = 0; Loc < NumLocations; ++Loc) {
1581 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1582 if (!file)
1583 continue;
1584
1585 Cursor = clang_getCursor(TU,
1586 clang_getLocation(TU, file, Locations[Loc].line,
1587 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001588
1589 if (checkForErrors(TU) != 0)
1590 return -1;
1591
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001592 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001593 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001594 PrintCursor(Cursor);
1595 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001596 clang_findReferencesInFile(Cursor, file, visitor);
1597 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001598
1599 if (checkForErrors(TU) != 0)
1600 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001601 }
1602 }
1603 }
1604
1605 PrintDiagnostics(TU);
1606 clang_disposeTranslationUnit(TU);
1607 clang_disposeIndex(CIdx);
1608 free(Locations);
1609 free_remapped_files(unsaved_files, num_unsaved_files);
1610 return 0;
1611}
1612
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001613typedef struct {
1614 const char *check_prefix;
1615 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001616 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001617 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001618 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001619} IndexData;
1620
1621static void printCheck(IndexData *data) {
1622 if (data->check_prefix) {
1623 if (data->first_check_printed) {
1624 printf("// %s-NEXT: ", data->check_prefix);
1625 } else {
1626 printf("// %s : ", data->check_prefix);
1627 data->first_check_printed = 1;
1628 }
1629 }
1630}
1631
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001632static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001633 CXString filename = clang_getFileName((CXFile)file);
1634 printf("%s", clang_getCString(filename));
1635 clang_disposeString(filename);
1636}
1637
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001638static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1639 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001640 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001641 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001642 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001643 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001644 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001645
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001646 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001647 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1648 if (line == 0) {
1649 printf("<null loc>");
1650 return;
1651 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001652 if (!file) {
1653 printf("<no idxfile>");
1654 return;
1655 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001656 filename = clang_getFileName((CXFile)file);
1657 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001658 if (strcmp(cname, index_data->main_filename) == 0)
1659 isMainFile = 1;
1660 else
1661 isMainFile = 0;
1662 clang_disposeString(filename);
1663
1664 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001665 printCXIndexFile(file);
1666 printf(":");
1667 }
1668 printf("%d:%d", line, column);
1669}
1670
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001671static unsigned digitCount(unsigned val) {
1672 unsigned c = 1;
1673 while (1) {
1674 if (val < 10)
1675 return c;
1676 ++c;
1677 val /= 10;
1678 }
1679}
1680
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001681static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1682 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001683 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001684 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001685 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001686 unsigned line, column;
1687
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001688 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001689 if (!name)
1690 name = "<anon-tag>";
1691
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001692 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001693 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001694 newStr = (char *)malloc(strlen(name) +
1695 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001696 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001697 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001698}
1699
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001700static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1701 CXIdxClientContainer container;
1702 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001703 if (!container)
1704 printf("[<<NULL>>]");
1705 else
1706 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001707}
1708
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001709static const char *getEntityKindString(CXIdxEntityKind kind) {
1710 switch (kind) {
1711 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1712 case CXIdxEntity_Typedef: return "typedef";
1713 case CXIdxEntity_Function: return "function";
1714 case CXIdxEntity_Variable: return "variable";
1715 case CXIdxEntity_Field: return "field";
1716 case CXIdxEntity_EnumConstant: return "enumerator";
1717 case CXIdxEntity_ObjCClass: return "objc-class";
1718 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1719 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001720 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1721 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001722 case CXIdxEntity_ObjCProperty: return "objc-property";
1723 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1724 case CXIdxEntity_Enum: return "enum";
1725 case CXIdxEntity_Struct: return "struct";
1726 case CXIdxEntity_Union: return "union";
1727 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001728 case CXIdxEntity_CXXNamespace: return "namespace";
1729 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1730 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1731 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1732 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1733 case CXIdxEntity_CXXConstructor: return "constructor";
1734 case CXIdxEntity_CXXDestructor: return "destructor";
1735 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1736 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1737 }
1738 assert(0 && "Garbage entity kind");
1739 return 0;
1740}
1741
1742static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1743 switch (kind) {
1744 case CXIdxEntity_NonTemplate: return "";
1745 case CXIdxEntity_Template: return "-template";
1746 case CXIdxEntity_TemplatePartialSpecialization:
1747 return "-template-partial-spec";
1748 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001749 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001750 assert(0 && "Garbage entity kind");
1751 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001752}
1753
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001754static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1755 switch (kind) {
1756 case CXIdxEntityLang_None: return "<none>";
1757 case CXIdxEntityLang_C: return "C";
1758 case CXIdxEntityLang_ObjC: return "ObjC";
1759 case CXIdxEntityLang_CXX: return "C++";
1760 }
1761 assert(0 && "Garbage language kind");
1762 return 0;
1763}
1764
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001765static void printEntityInfo(const char *cb,
1766 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001767 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001768 const char *name;
1769 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001770 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001771 index_data = (IndexData *)client_data;
1772 printCheck(index_data);
1773
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001774 if (!info) {
1775 printf("%s: <<NULL>>", cb);
1776 return;
1777 }
1778
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001779 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001780 if (!name)
1781 name = "<anon-tag>";
1782
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001783 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1784 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001785 printf(" | name: %s", name);
1786 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001787 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001788
1789 for (i = 0; i != info->numAttributes; ++i) {
1790 const CXIdxAttrInfo *Attr = info->attributes[i];
1791 printf(" <attribute>: ");
1792 PrintCursor(Attr->cursor);
1793 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001794}
1795
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001796static void printBaseClassInfo(CXClientData client_data,
1797 const CXIdxBaseClassInfo *info) {
1798 printEntityInfo(" <base>", client_data, info->base);
1799 printf(" | cursor: ");
1800 PrintCursor(info->cursor);
1801 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001802 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001803}
1804
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001805static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1806 CXClientData client_data) {
1807 unsigned i;
1808 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1809 printEntityInfo(" <protocol>", client_data,
1810 ProtoInfo->protocols[i]->protocol);
1811 printf(" | cursor: ");
1812 PrintCursor(ProtoInfo->protocols[i]->cursor);
1813 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001814 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001815 printf("\n");
1816 }
1817}
1818
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001819static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001820 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001821 CXString str;
1822 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001823 unsigned numDiags, i;
1824 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001825 IndexData *index_data;
1826 index_data = (IndexData *)client_data;
1827 printCheck(index_data);
1828
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001829 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1830 for (i = 0; i != numDiags; ++i) {
1831 diag = clang_getDiagnosticInSet(diagSet, i);
1832 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1833 cstr = clang_getCString(str);
1834 printf("[diagnostic]: %s\n", cstr);
1835 clang_disposeString(str);
1836
1837 if (getenv("CINDEXTEST_FAILONERROR") &&
1838 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1839 index_data->fail_for_error = 1;
1840 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001841 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001842}
1843
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001844static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1845 CXFile file, void *reserved) {
1846 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001847 CXString filename;
1848
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001849 index_data = (IndexData *)client_data;
1850 printCheck(index_data);
1851
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001852 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001853 index_data->main_filename = clang_getCString(filename);
1854 clang_disposeString(filename);
1855
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001856 printf("[enteredMainFile]: ");
1857 printCXIndexFile((CXIdxClientFile)file);
1858 printf("\n");
1859
1860 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001861}
1862
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001863static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001864 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001865 IndexData *index_data;
1866 index_data = (IndexData *)client_data;
1867 printCheck(index_data);
1868
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001869 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001870 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001871 printf(" | name: \"%s\"", info->filename);
1872 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001873 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001874 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001875
1876 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001877}
1878
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001879static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001880 void *reserved) {
1881 IndexData *index_data;
1882 index_data = (IndexData *)client_data;
1883 printCheck(index_data);
1884
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001885 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001886 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001887}
1888
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001889static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001890 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001891 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001892 const CXIdxObjCCategoryDeclInfo *CatInfo;
1893 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001894 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001895 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001896 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001897 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001898 index_data = (IndexData *)client_data;
1899
1900 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1901 printf(" | cursor: ");
1902 PrintCursor(info->cursor);
1903 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001904 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001905 printf(" | semantic-container: ");
1906 printCXIndexContainer(info->semanticContainer);
1907 printf(" | lexical-container: ");
1908 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001909 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001910 printf(" | isDef: %d", info->isDefinition);
1911 printf(" | isContainer: %d", info->isContainer);
1912 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001913
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001914 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001915 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001916 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001917 PrintCursor(Attr->cursor);
1918 printf("\n");
1919 }
1920
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001921 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1922 const char *kindName = 0;
1923 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1924 switch (K) {
1925 case CXIdxObjCContainer_ForwardRef:
1926 kindName = "forward-ref"; break;
1927 case CXIdxObjCContainer_Interface:
1928 kindName = "interface"; break;
1929 case CXIdxObjCContainer_Implementation:
1930 kindName = "implementation"; break;
1931 }
1932 printCheck(index_data);
1933 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1934 }
1935
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001936 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001937 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1938 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001939 printf(" | cursor: ");
1940 PrintCursor(CatInfo->classCursor);
1941 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001942 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001943 printf("\n");
1944 }
1945
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001946 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1947 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001948 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001949 printf("\n");
1950 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001951 }
1952
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001953 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1954 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001955 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001956
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001957 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
1958 if (PropInfo->getter) {
1959 printEntityInfo(" <getter>", client_data, PropInfo->getter);
1960 printf("\n");
1961 }
1962 if (PropInfo->setter) {
1963 printEntityInfo(" <setter>", client_data, PropInfo->setter);
1964 printf("\n");
1965 }
1966 }
1967
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001968 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1969 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1970 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1971 printf("\n");
1972 }
1973 }
1974
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001975 if (info->declAsContainer)
1976 clang_index_setClientContainer(info->declAsContainer,
1977 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001978}
1979
1980static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001981 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001982 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001983 printf(" | cursor: ");
1984 PrintCursor(info->cursor);
1985 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001986 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001987 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001988 printf(" | container: ");
1989 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001990 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001991 switch (info->kind) {
1992 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001993 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001994 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001995 printf("\n");
1996}
1997
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001998static int index_abortQuery(CXClientData client_data, void *reserved) {
1999 IndexData *index_data;
2000 index_data = (IndexData *)client_data;
2001 return index_data->abort;
2002}
2003
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002004static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002005 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002006 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002007 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002008 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002009 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002010 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002011 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002012 index_indexEntityReference
2013};
2014
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002015static unsigned getIndexOptions(void) {
2016 unsigned index_opts;
2017 index_opts = 0;
2018 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2019 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2020 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2021 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2022
2023 return index_opts;
2024}
2025
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002026static int index_file(int argc, const char **argv) {
2027 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002028 CXIndex Idx;
2029 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002030 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002031 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002032 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002033
2034 check_prefix = 0;
2035 if (argc > 0) {
2036 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2037 check_prefix = argv[0] + strlen("-check-prefix=");
2038 ++argv;
2039 --argc;
2040 }
2041 }
2042
2043 if (argc == 0) {
2044 fprintf(stderr, "no compiler arguments\n");
2045 return -1;
2046 }
2047
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002048 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2049 /* displayDiagnosics=*/1))) {
2050 fprintf(stderr, "Could not create Index\n");
2051 return 1;
2052 }
2053 idxAction = 0;
2054 result = 1;
2055
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002056 index_data.check_prefix = check_prefix;
2057 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002058 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002059 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002060
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002061 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002062 idxAction = clang_IndexAction_create(Idx);
2063 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002064 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002065 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002066 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002067 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002068
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002069 clang_IndexAction_dispose(idxAction);
2070 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002071 return result;
2072}
2073
2074static int index_tu(int argc, const char **argv) {
2075 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002076 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002077 CXTranslationUnit TU;
2078 const char *check_prefix;
2079 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002080 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002081 int result;
2082
2083 check_prefix = 0;
2084 if (argc > 0) {
2085 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2086 check_prefix = argv[0] + strlen("-check-prefix=");
2087 ++argv;
2088 --argc;
2089 }
2090 }
2091
2092 if (argc == 0) {
2093 fprintf(stderr, "no ast file\n");
2094 return -1;
2095 }
2096
2097 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2098 /* displayDiagnosics=*/1))) {
2099 fprintf(stderr, "Could not create Index\n");
2100 return 1;
2101 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002102 idxAction = 0;
2103 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002104
2105 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002106 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002107
2108 index_data.check_prefix = check_prefix;
2109 index_data.first_check_printed = 0;
2110 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002111 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002112
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002113 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002114 idxAction = clang_IndexAction_create(Idx);
2115 result = clang_indexTranslationUnit(idxAction, &index_data,
2116 &IndexCB,sizeof(IndexCB),
2117 index_opts, TU);
2118 if (index_data.fail_for_error)
2119 goto finished;
2120
2121 finished:
2122 clang_IndexAction_dispose(idxAction);
2123 clang_disposeIndex(Idx);
2124
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002125 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002126}
2127
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002128int perform_token_annotation(int argc, const char **argv) {
2129 const char *input = argv[1];
2130 char *filename = 0;
2131 unsigned line, second_line;
2132 unsigned column, second_column;
2133 CXIndex CIdx;
2134 CXTranslationUnit TU = 0;
2135 int errorCode;
2136 struct CXUnsavedFile *unsaved_files = 0;
2137 int num_unsaved_files = 0;
2138 CXToken *tokens;
2139 unsigned num_tokens;
2140 CXSourceRange range;
2141 CXSourceLocation startLoc, endLoc;
2142 CXFile file = 0;
2143 CXCursor *cursors = 0;
2144 unsigned i;
2145
2146 input += strlen("-test-annotate-tokens=");
2147 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2148 &second_line, &second_column)))
2149 return errorCode;
2150
2151 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2152 return -1;
2153
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002154 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002155 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2156 argv + num_unsaved_files + 2,
2157 argc - num_unsaved_files - 3,
2158 unsaved_files,
2159 num_unsaved_files,
2160 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002161 if (!TU) {
2162 fprintf(stderr, "unable to parse input\n");
2163 clang_disposeIndex(CIdx);
2164 free(filename);
2165 free_remapped_files(unsaved_files, num_unsaved_files);
2166 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002167 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002168 errorCode = 0;
2169
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002170 if (checkForErrors(TU) != 0)
2171 return -1;
2172
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002173 if (getenv("CINDEXTEST_EDITING")) {
2174 for (i = 0; i < 5; ++i) {
2175 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2176 clang_defaultReparseOptions(TU))) {
2177 fprintf(stderr, "Unable to reparse translation unit!\n");
2178 errorCode = -1;
2179 goto teardown;
2180 }
2181 }
2182 }
2183
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002184 if (checkForErrors(TU) != 0) {
2185 errorCode = -1;
2186 goto teardown;
2187 }
2188
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002189 file = clang_getFile(TU, filename);
2190 if (!file) {
2191 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2192 errorCode = -1;
2193 goto teardown;
2194 }
2195
2196 startLoc = clang_getLocation(TU, file, line, column);
2197 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002198 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002199 column);
2200 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002201 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002202 }
2203
2204 endLoc = clang_getLocation(TU, file, second_line, second_column);
2205 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002206 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002207 second_line, second_column);
2208 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002209 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002210 }
2211
2212 range = clang_getRange(startLoc, endLoc);
2213 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002214
2215 if (checkForErrors(TU) != 0) {
2216 errorCode = -1;
2217 goto teardown;
2218 }
2219
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002220 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2221 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002222
2223 if (checkForErrors(TU) != 0) {
2224 errorCode = -1;
2225 goto teardown;
2226 }
2227
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002228 for (i = 0; i != num_tokens; ++i) {
2229 const char *kind = "<unknown>";
2230 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2231 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2232 unsigned start_line, start_column, end_line, end_column;
2233
2234 switch (clang_getTokenKind(tokens[i])) {
2235 case CXToken_Punctuation: kind = "Punctuation"; break;
2236 case CXToken_Keyword: kind = "Keyword"; break;
2237 case CXToken_Identifier: kind = "Identifier"; break;
2238 case CXToken_Literal: kind = "Literal"; break;
2239 case CXToken_Comment: kind = "Comment"; break;
2240 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002241 clang_getSpellingLocation(clang_getRangeStart(extent),
2242 0, &start_line, &start_column, 0);
2243 clang_getSpellingLocation(clang_getRangeEnd(extent),
2244 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002245 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2246 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002247 if (!clang_isInvalid(cursors[i].kind)) {
2248 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002249 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002250 }
2251 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002252 }
2253 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002254 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002255
2256 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002257 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002258 clang_disposeTranslationUnit(TU);
2259 clang_disposeIndex(CIdx);
2260 free(filename);
2261 free_remapped_files(unsaved_files, num_unsaved_files);
2262 return errorCode;
2263}
2264
Ted Kremenek0d435192009-11-17 18:13:31 +00002265/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002266/* USR printing. */
2267/******************************************************************************/
2268
2269static int insufficient_usr(const char *kind, const char *usage) {
2270 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2271 return 1;
2272}
2273
2274static unsigned isUSR(const char *s) {
2275 return s[0] == 'c' && s[1] == ':';
2276}
2277
2278static int not_usr(const char *s, const char *arg) {
2279 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2280 return 1;
2281}
2282
2283static void print_usr(CXString usr) {
2284 const char *s = clang_getCString(usr);
2285 printf("%s\n", s);
2286 clang_disposeString(usr);
2287}
2288
2289static void display_usrs() {
2290 fprintf(stderr, "-print-usrs options:\n"
2291 " ObjCCategory <class name> <category name>\n"
2292 " ObjCClass <class name>\n"
2293 " ObjCIvar <ivar name> <class USR>\n"
2294 " ObjCMethod <selector> [0=class method|1=instance method] "
2295 "<class USR>\n"
2296 " ObjCProperty <property name> <class USR>\n"
2297 " ObjCProtocol <protocol name>\n");
2298}
2299
2300int print_usrs(const char **I, const char **E) {
2301 while (I != E) {
2302 const char *kind = *I;
2303 unsigned len = strlen(kind);
2304 switch (len) {
2305 case 8:
2306 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2307 if (I + 2 >= E)
2308 return insufficient_usr(kind, "<ivar name> <class USR>");
2309 if (!isUSR(I[2]))
2310 return not_usr("<class USR>", I[2]);
2311 else {
2312 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002313 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002314 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002315 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2316 }
2317
2318 I += 3;
2319 continue;
2320 }
2321 break;
2322 case 9:
2323 if (memcmp(kind, "ObjCClass", 9) == 0) {
2324 if (I + 1 >= E)
2325 return insufficient_usr(kind, "<class name>");
2326 print_usr(clang_constructUSR_ObjCClass(I[1]));
2327 I += 2;
2328 continue;
2329 }
2330 break;
2331 case 10:
2332 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2333 if (I + 3 >= E)
2334 return insufficient_usr(kind, "<method selector> "
2335 "[0=class method|1=instance method] <class USR>");
2336 if (!isUSR(I[3]))
2337 return not_usr("<class USR>", I[3]);
2338 else {
2339 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002340 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002341 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002342 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2343 }
2344 I += 4;
2345 continue;
2346 }
2347 break;
2348 case 12:
2349 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2350 if (I + 2 >= E)
2351 return insufficient_usr(kind, "<class name> <category name>");
2352 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2353 I += 3;
2354 continue;
2355 }
2356 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2357 if (I + 1 >= E)
2358 return insufficient_usr(kind, "<protocol name>");
2359 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2360 I += 2;
2361 continue;
2362 }
2363 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2364 if (I + 2 >= E)
2365 return insufficient_usr(kind, "<property name> <class USR>");
2366 if (!isUSR(I[2]))
2367 return not_usr("<class USR>", I[2]);
2368 else {
2369 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002370 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002371 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002372 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2373 }
2374 I += 3;
2375 continue;
2376 }
2377 break;
2378 default:
2379 break;
2380 }
2381 break;
2382 }
2383
2384 if (I != E) {
2385 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2386 display_usrs();
2387 return 1;
2388 }
2389 return 0;
2390}
2391
2392int print_usrs_file(const char *file_name) {
2393 char line[2048];
2394 const char *args[128];
2395 unsigned numChars = 0;
2396
2397 FILE *fp = fopen(file_name, "r");
2398 if (!fp) {
2399 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2400 return 1;
2401 }
2402
2403 /* This code is not really all that safe, but it works fine for testing. */
2404 while (!feof(fp)) {
2405 char c = fgetc(fp);
2406 if (c == '\n') {
2407 unsigned i = 0;
2408 const char *s = 0;
2409
2410 if (numChars == 0)
2411 continue;
2412
2413 line[numChars] = '\0';
2414 numChars = 0;
2415
2416 if (line[0] == '/' && line[1] == '/')
2417 continue;
2418
2419 s = strtok(line, " ");
2420 while (s) {
2421 args[i] = s;
2422 ++i;
2423 s = strtok(0, " ");
2424 }
2425 if (print_usrs(&args[0], &args[i]))
2426 return 1;
2427 }
2428 else
2429 line[numChars++] = c;
2430 }
2431
2432 fclose(fp);
2433 return 0;
2434}
2435
2436/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002437/* Command line processing. */
2438/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002439int write_pch_file(const char *filename, int argc, const char *argv[]) {
2440 CXIndex Idx;
2441 CXTranslationUnit TU;
2442 struct CXUnsavedFile *unsaved_files = 0;
2443 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002444 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002445
2446 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2447
2448 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2449 clang_disposeIndex(Idx);
2450 return -1;
2451 }
2452
2453 TU = clang_parseTranslationUnit(Idx, 0,
2454 argv + num_unsaved_files,
2455 argc - num_unsaved_files,
2456 unsaved_files,
2457 num_unsaved_files,
2458 CXTranslationUnit_Incomplete);
2459 if (!TU) {
2460 fprintf(stderr, "Unable to load translation unit!\n");
2461 free_remapped_files(unsaved_files, num_unsaved_files);
2462 clang_disposeIndex(Idx);
2463 return 1;
2464 }
2465
Douglas Gregor39c411f2011-07-06 16:43:36 +00002466 switch (clang_saveTranslationUnit(TU, filename,
2467 clang_defaultSaveOptions(TU))) {
2468 case CXSaveError_None:
2469 break;
2470
2471 case CXSaveError_TranslationErrors:
2472 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2473 filename);
2474 result = 2;
2475 break;
2476
2477 case CXSaveError_InvalidTU:
2478 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2479 filename);
2480 result = 3;
2481 break;
2482
2483 case CXSaveError_Unknown:
2484 default:
2485 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2486 result = 1;
2487 break;
2488 }
2489
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002490 clang_disposeTranslationUnit(TU);
2491 free_remapped_files(unsaved_files, num_unsaved_files);
2492 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002493 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002494}
2495
2496/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002497/* Serialized diagnostics. */
2498/******************************************************************************/
2499
2500static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2501 switch (error) {
2502 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2503 case CXLoadDiag_None: break;
2504 case CXLoadDiag_Unknown: return "Unknown";
2505 case CXLoadDiag_InvalidFile: return "Invalid File";
2506 }
2507 return "None";
2508}
2509
2510static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2511 switch (severity) {
2512 case CXDiagnostic_Note: return "note";
2513 case CXDiagnostic_Error: return "error";
2514 case CXDiagnostic_Fatal: return "fatal";
2515 case CXDiagnostic_Ignored: return "ignored";
2516 case CXDiagnostic_Warning: return "warning";
2517 }
2518 return "unknown";
2519}
2520
2521static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002522 if (indent == 0)
2523 return;
2524 fprintf(stderr, "+");
2525 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002526 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002527 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002528 --indent;
2529 }
2530}
2531
2532static void printLocation(CXSourceLocation L) {
2533 CXFile File;
2534 CXString FileName;
2535 unsigned line, column, offset;
2536
2537 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2538 FileName = clang_getFileName(File);
2539
2540 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2541 clang_disposeString(FileName);
2542}
2543
2544static void printRanges(CXDiagnostic D, unsigned indent) {
2545 unsigned i, n = clang_getDiagnosticNumRanges(D);
2546
2547 for (i = 0; i < n; ++i) {
2548 CXSourceLocation Start, End;
2549 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2550 Start = clang_getRangeStart(SR);
2551 End = clang_getRangeEnd(SR);
2552
2553 printIndent(indent);
2554 fprintf(stderr, "Range: ");
2555 printLocation(Start);
2556 fprintf(stderr, " ");
2557 printLocation(End);
2558 fprintf(stderr, "\n");
2559 }
2560}
2561
2562static void printFixIts(CXDiagnostic D, unsigned indent) {
2563 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002564 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002565 for (i = 0 ; i < n; ++i) {
2566 CXSourceRange ReplacementRange;
2567 CXString text;
2568 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2569
2570 printIndent(indent);
2571 fprintf(stderr, "FIXIT: (");
2572 printLocation(clang_getRangeStart(ReplacementRange));
2573 fprintf(stderr, " - ");
2574 printLocation(clang_getRangeEnd(ReplacementRange));
2575 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2576 clang_disposeString(text);
2577 }
2578}
2579
2580static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002581 unsigned i, n;
2582
Ted Kremenek15322172011-11-10 08:43:12 +00002583 if (!Diags)
2584 return;
2585
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002586 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002587 for (i = 0; i < n; ++i) {
2588 CXSourceLocation DiagLoc;
2589 CXDiagnostic D;
2590 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002591 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002592 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002593 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002594
2595 D = clang_getDiagnosticInSet(Diags, i);
2596 DiagLoc = clang_getDiagnosticLocation(D);
2597 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2598 FileName = clang_getFileName(File);
2599 DiagSpelling = clang_getDiagnosticSpelling(D);
2600
2601 printIndent(indent);
2602
2603 fprintf(stderr, "%s:%d:%d: %s: %s",
2604 clang_getCString(FileName),
2605 line,
2606 column,
2607 getSeverityString(clang_getDiagnosticSeverity(D)),
2608 clang_getCString(DiagSpelling));
2609
2610 DiagOption = clang_getDiagnosticOption(D, 0);
2611 DiagOptionStr = clang_getCString(DiagOption);
2612 if (DiagOptionStr) {
2613 fprintf(stderr, " [%s]", DiagOptionStr);
2614 }
2615
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002616 DiagCat = clang_getDiagnosticCategoryText(D);
2617 DiagCatStr = clang_getCString(DiagCat);
2618 if (DiagCatStr) {
2619 fprintf(stderr, " [%s]", DiagCatStr);
2620 }
2621
Ted Kremenek15322172011-11-10 08:43:12 +00002622 fprintf(stderr, "\n");
2623
2624 printRanges(D, indent);
2625 printFixIts(D, indent);
2626
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002627 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002628 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2629
2630 clang_disposeString(FileName);
2631 clang_disposeString(DiagSpelling);
2632 clang_disposeString(DiagOption);
2633 }
2634}
2635
2636static int read_diagnostics(const char *filename) {
2637 enum CXLoadDiag_Error error;
2638 CXString errorString;
2639 CXDiagnosticSet Diags = 0;
2640
2641 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2642 if (!Diags) {
2643 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2644 getDiagnosticCodeStr(error),
2645 clang_getCString(errorString));
2646 clang_disposeString(errorString);
2647 return 1;
2648 }
2649
2650 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002651 fprintf(stderr, "Number of diagnostics: %d\n",
2652 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002653 clang_disposeDiagnosticSet(Diags);
2654 return 0;
2655}
2656
2657/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002658/* Command line processing. */
2659/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002660
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002661static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002662 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002663 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002664 if (strcmp(s, "-usrs") == 0)
2665 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002666 if (strncmp(s, "-memory-usage", 13) == 0)
2667 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002668 return NULL;
2669}
2670
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002671static void print_usage(void) {
2672 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002673 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002674 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002675 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002676 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002677 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002678 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002679 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002680 "[FileCheck prefix]\n");
2681 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002682 " c-index-test -test-load-tu <AST file> <symbol filter> "
2683 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002684 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2685 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002686 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002687 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002688 " c-index-test -test-load-source-memory-usage "
2689 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002690 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2691 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002692 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002693 " c-index-test -test-load-source-usrs-memory-usage "
2694 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002695 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2696 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002697 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002698 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002699 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002700 " c-index-test -test-print-typekind {<args>}*\n"
2701 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002702 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002703 " c-index-test -write-pch <file> <compiler arguments>\n");
2704 fprintf(stderr,
2705 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002706 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002707 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002708 " all - load all symbols, including those from PCH\n"
2709 " local - load all symbols except those in PCH\n"
2710 " category - only load ObjC categories (non-PCH)\n"
2711 " interface - only load ObjC interfaces (non-PCH)\n"
2712 " protocol - only load ObjC protocols (non-PCH)\n"
2713 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002714 " typedef - only load typdefs (non-PCH)\n"
2715 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002716}
2717
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002718/***/
2719
2720int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002721 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002722 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2723 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002724 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002725 return perform_code_completion(argc, argv, 0);
2726 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2727 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002728 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2729 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002730 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2731 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002732 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2733 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002734 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2735 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002736 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002737 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002738 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002739 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2740 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002741 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002742 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2743 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2744 if (I) {
2745 int trials = atoi(argv[2]);
2746 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2747 NULL);
2748 }
2749 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002750 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002751 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002752
2753 PostVisitTU postVisit = 0;
2754 if (strstr(argv[1], "-memory-usage"))
2755 postVisit = PrintMemoryUsage;
2756
Ted Kremenek7d405622010-01-12 23:34:26 +00002757 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002758 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2759 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002760 }
2761 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002762 return perform_file_scan(argv[2], argv[3],
2763 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002764 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2765 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002766 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2767 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2768 PrintInclusionStack);
2769 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2770 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2771 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002772 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2773 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2774 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002775 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2776 return perform_test_load_source(argc - 2, argv + 2, "all",
2777 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002778 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2779 if (argc > 2)
2780 return print_usrs(argv + 2, argv + argc);
2781 else {
2782 display_usrs();
2783 return 1;
2784 }
2785 }
2786 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2787 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002788 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2789 return write_pch_file(argv[2], argc - 3, argv + 3);
2790
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002791 print_usage();
2792 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002793}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002794
2795/***/
2796
2797/* We intentionally run in a separate thread to ensure we at least minimal
2798 * testing of a multithreaded environment (for example, having a reduced stack
2799 * size). */
2800
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002801typedef struct thread_info {
2802 int argc;
2803 const char **argv;
2804 int result;
2805} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002806void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002807 thread_info *client_data = client_data_v;
2808 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00002809#ifdef __CYGWIN__
2810 fflush(stdout); /* stdout is not flushed on Cygwin. */
2811#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002812}
2813
2814int main(int argc, const char **argv) {
2815 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002816
Douglas Gregor61605982010-10-27 16:00:01 +00002817 if (getenv("CINDEXTEST_NOTHREADS"))
2818 return cindextest_main(argc, argv);
2819
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002820 client_data.argc = argc;
2821 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002822 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002823 return client_data.result;
2824}