blob: 3dfc732d2bdbdcd99dbfc28ae39ff12b9f3ea078 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00004#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00005#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00006#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00007#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00008#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009
Ted Kremenek0d435192009-11-17 18:13:31 +000010/******************************************************************************/
11/* Utility functions. */
12/******************************************************************************/
13
John Thompson2e06fc82009-10-27 13:42:56 +000014#ifdef _MSC_VER
15char *basename(const char* path)
16{
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
25
26 return((char*)path);
27}
28#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000029extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000030#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000031
Douglas Gregor45ba9a12010-07-25 17:39:21 +000032/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000033static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
35
36 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000037 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000038 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000040 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
41 options &= ~CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000042
43 return options;
44}
45
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000046static int checkForErrors(CXTranslationUnit TU);
47
Daniel Dunbar51b058c2010-02-14 08:32:24 +000048static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
49 unsigned end_line, unsigned end_column) {
50 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000051 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000052}
53
Ted Kremenek1c6da172009-11-17 19:37:36 +000054static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
55 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000056
Douglas Gregora88084b2010-02-18 18:08:43 +000057 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000058 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000059 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
60 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000061 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000062 return 1;
63}
64
Douglas Gregor4db64a42010-01-23 00:14:00 +000065void free_remapped_files(struct CXUnsavedFile *unsaved_files,
66 int num_unsaved_files) {
67 int i;
68 for (i = 0; i != num_unsaved_files; ++i) {
69 free((char *)unsaved_files[i].Filename);
70 free((char *)unsaved_files[i].Contents);
71 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000072 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000073}
74
75int parse_remapped_files(int argc, const char **argv, int start_arg,
76 struct CXUnsavedFile **unsaved_files,
77 int *num_unsaved_files) {
78 int i;
79 int arg;
80 int prefix_len = strlen("-remap-file=");
81 *unsaved_files = 0;
82 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000083
Douglas Gregor4db64a42010-01-23 00:14:00 +000084 /* Count the number of remapped files. */
85 for (arg = start_arg; arg < argc; ++arg) {
86 if (strncmp(argv[arg], "-remap-file=", prefix_len))
87 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000088
Douglas Gregor4db64a42010-01-23 00:14:00 +000089 ++*num_unsaved_files;
90 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000091
Douglas Gregor4db64a42010-01-23 00:14:00 +000092 if (*num_unsaved_files == 0)
93 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000094
Douglas Gregor4db64a42010-01-23 00:14:00 +000095 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000096 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
97 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000098 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
99 struct CXUnsavedFile *unsaved = *unsaved_files + i;
100 const char *arg_string = argv[arg] + prefix_len;
101 int filename_len;
102 char *filename;
103 char *contents;
104 FILE *to_file;
105 const char *semi = strchr(arg_string, ';');
106 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000107 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000108 "error: -remap-file=from;to argument is missing semicolon\n");
109 free_remapped_files(*unsaved_files, i);
110 *unsaved_files = 0;
111 *num_unsaved_files = 0;
112 return -1;
113 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000114
Douglas Gregor4db64a42010-01-23 00:14:00 +0000115 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000116 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 if (!to_file) {
118 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
119 semi + 1);
120 free_remapped_files(*unsaved_files, i);
121 *unsaved_files = 0;
122 *num_unsaved_files = 0;
123 return -1;
124 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000125
Douglas Gregor4db64a42010-01-23 00:14:00 +0000126 /* Determine the length of the file we're remapping to. */
127 fseek(to_file, 0, SEEK_END);
128 unsaved->Length = ftell(to_file);
129 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000130
Douglas Gregor4db64a42010-01-23 00:14:00 +0000131 /* Read the contents of the file we're remapping to. */
132 contents = (char *)malloc(unsaved->Length + 1);
133 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
134 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
135 (feof(to_file) ? "EOF" : "error"), semi + 1);
136 fclose(to_file);
137 free_remapped_files(*unsaved_files, i);
138 *unsaved_files = 0;
139 *num_unsaved_files = 0;
140 return -1;
141 }
142 contents[unsaved->Length] = 0;
143 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000144
Douglas Gregor4db64a42010-01-23 00:14:00 +0000145 /* Close the file. */
146 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000147
Douglas Gregor4db64a42010-01-23 00:14:00 +0000148 /* Copy the file name that we're remapping from. */
149 filename_len = semi - arg_string;
150 filename = (char *)malloc(filename_len + 1);
151 memcpy(filename, arg_string, filename_len);
152 filename[filename_len] = 0;
153 unsaved->Filename = filename;
154 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000155
Douglas Gregor4db64a42010-01-23 00:14:00 +0000156 return 0;
157}
158
Ted Kremenek0d435192009-11-17 18:13:31 +0000159/******************************************************************************/
160/* Pretty-printing. */
161/******************************************************************************/
162
Douglas Gregor430d7a12011-07-25 17:48:11 +0000163static void PrintRange(CXSourceRange R, const char *str) {
164 CXFile begin_file, end_file;
165 unsigned begin_line, begin_column, end_line, end_column;
166
167 clang_getSpellingLocation(clang_getRangeStart(R),
168 &begin_file, &begin_line, &begin_column, 0);
169 clang_getSpellingLocation(clang_getRangeEnd(R),
170 &end_file, &end_line, &end_column, 0);
171 if (!begin_file || !end_file)
172 return;
173
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000174 if (str)
175 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000176 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
177}
178
Douglas Gregor358559d2010-10-02 22:49:11 +0000179int want_display_name = 0;
180
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000181static void PrintCursor(CXCursor Cursor) {
182 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000183 if (clang_isInvalid(Cursor.kind)) {
184 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
185 printf("Invalid Cursor => %s", clang_getCString(ks));
186 clang_disposeString(ks);
187 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000188 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000189 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000190 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000191 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000192 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000193 CXCursor *overridden;
194 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000195 unsigned RefNameRangeNr;
196 CXSourceRange CursorExtent;
197 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000198
Ted Kremeneke68fff62010-02-17 00:41:32 +0000199 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000200 string = want_display_name? clang_getCursorDisplayName(Cursor)
201 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000202 printf("%s=%s", clang_getCString(ks),
203 clang_getCString(string));
204 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000205 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000206
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000207 Referenced = clang_getCursorReferenced(Cursor);
208 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000209 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
210 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
211 printf("[");
212 for (I = 0; I != N; ++I) {
213 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000214 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000215 if (I)
216 printf(", ");
217
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000218 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000219 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000220 printf("%d:%d", line, column);
221 }
222 printf("]");
223 } else {
224 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000225 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000226 printf(":%d:%d", line, column);
227 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000228 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000229
230 if (clang_isCursorDefinition(Cursor))
231 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000232
233 switch (clang_getCursorAvailability(Cursor)) {
234 case CXAvailability_Available:
235 break;
236
237 case CXAvailability_Deprecated:
238 printf(" (deprecated)");
239 break;
240
241 case CXAvailability_NotAvailable:
242 printf(" (unavailable)");
243 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000244
245 case CXAvailability_NotAccessible:
246 printf(" (inaccessible)");
247 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000248 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000249
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000250 if (clang_CXXMethod_isStatic(Cursor))
251 printf(" (static)");
252 if (clang_CXXMethod_isVirtual(Cursor))
253 printf(" (virtual)");
254
Ted Kremenek95f33552010-08-26 01:42:22 +0000255 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
256 CXType T =
257 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
258 CXString S = clang_getTypeKindSpelling(T.kind);
259 printf(" [IBOutletCollection=%s]", clang_getCString(S));
260 clang_disposeString(S);
261 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000262
263 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
264 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
265 unsigned isVirtual = clang_isVirtualBase(Cursor);
266 const char *accessStr = 0;
267
268 switch (access) {
269 case CX_CXXInvalidAccessSpecifier:
270 accessStr = "invalid"; break;
271 case CX_CXXPublic:
272 accessStr = "public"; break;
273 case CX_CXXProtected:
274 accessStr = "protected"; break;
275 case CX_CXXPrivate:
276 accessStr = "private"; break;
277 }
278
279 printf(" [access=%s isVirtual=%s]", accessStr,
280 isVirtual ? "true" : "false");
281 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000282
283 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
284 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
285 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
286 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000287 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000288 printf(" [Specialization of %s:%d:%d]",
289 clang_getCString(Name), line, column);
290 clang_disposeString(Name);
291 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000292
293 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
294 if (num_overridden) {
295 unsigned I;
296 printf(" [Overrides ");
297 for (I = 0; I != num_overridden; ++I) {
298 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000299 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000300 if (I)
301 printf(", ");
302 printf("@%d:%d", line, column);
303 }
304 printf("]");
305 clang_disposeOverriddenCursors(overridden);
306 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000307
308 if (Cursor.kind == CXCursor_InclusionDirective) {
309 CXFile File = clang_getIncludedFile(Cursor);
310 CXString Included = clang_getFileName(File);
311 printf(" (%s)", clang_getCString(Included));
312 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000313
314 if (clang_isFileMultipleIncludeGuarded(TU, File))
315 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000316 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000317
318 CursorExtent = clang_getCursorExtent(Cursor);
319 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
320 CXNameRange_WantQualifier
321 | CXNameRange_WantSinglePiece
322 | CXNameRange_WantTemplateArgs,
323 0);
324 if (!clang_equalRanges(CursorExtent, RefNameRange))
325 PrintRange(RefNameRange, "SingleRefName");
326
327 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
328 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
329 CXNameRange_WantQualifier
330 | CXNameRange_WantTemplateArgs,
331 RefNameRangeNr);
332 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
333 break;
334 if (!clang_equalRanges(CursorExtent, RefNameRange))
335 PrintRange(RefNameRange, "RefName");
336 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000337 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000338}
Steve Naroff89922f82009-08-31 00:59:03 +0000339
Ted Kremeneke68fff62010-02-17 00:41:32 +0000340static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000341 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000342 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000343 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000344 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000346 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000347 clang_disposeString(source);
348 return "<invalid loc>";
349 }
350 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000351 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000352 clang_disposeString(source);
353 return b;
354 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000355}
356
Ted Kremenek0d435192009-11-17 18:13:31 +0000357/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000358/* Callbacks. */
359/******************************************************************************/
360
361typedef void (*PostVisitTU)(CXTranslationUnit);
362
Douglas Gregora88084b2010-02-18 18:08:43 +0000363void PrintDiagnostic(CXDiagnostic Diagnostic) {
364 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000365 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000366 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000367 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000368 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
369 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000370 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000371
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000372 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000373 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000374
Douglas Gregor274f1902010-02-22 23:17:23 +0000375 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
376 fprintf(stderr, "%s\n", clang_getCString(Msg));
377 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000378
Douglas Gregora9b06d42010-11-09 06:24:54 +0000379 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
380 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000381 if (!file)
382 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000383
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000384 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000385 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000386 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000387 CXSourceRange range;
388 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
389 CXSourceLocation start = clang_getRangeStart(range);
390 CXSourceLocation end = clang_getRangeEnd(range);
391 unsigned start_line, start_column, end_line, end_column;
392 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000393 clang_getSpellingLocation(start, &start_file, &start_line,
394 &start_column, 0);
395 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000396 if (clang_equalLocations(start, end)) {
397 /* Insertion. */
398 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000399 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000400 clang_getCString(insertion_text), start_line, start_column);
401 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
402 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000403 if (start_file == file && end_file == file) {
404 fprintf(out, "FIX-IT: Remove ");
405 PrintExtent(out, start_line, start_column, end_line, end_column);
406 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000407 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000408 } else {
409 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000410 if (start_file == end_file) {
411 fprintf(out, "FIX-IT: Replace ");
412 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000413 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000414 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000415 break;
416 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000417 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000418 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000419}
420
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000421void PrintDiagnosticSet(CXDiagnosticSet Set) {
422 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
423 for ( ; i != n ; ++i) {
424 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
425 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000426 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000427 if (ChildDiags)
428 PrintDiagnosticSet(ChildDiags);
429 }
430}
431
432void PrintDiagnostics(CXTranslationUnit TU) {
433 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
434 PrintDiagnosticSet(TUSet);
435 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000436}
437
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000438void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000439 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000440 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000441 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000442 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000443 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000444 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000445 unsigned long amount = usage.entries[i].amount;
446 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000447 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000448 ((double) amount)/(1024*1024));
449 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000450 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000451 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000452 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000453}
454
Ted Kremenekce2ae882010-01-26 17:59:48 +0000455/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000456/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000457/******************************************************************************/
458
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000459static const char *FileCheckPrefix = "CHECK";
460
Douglas Gregora7bde202010-01-19 00:34:46 +0000461static void PrintCursorExtent(CXCursor C) {
462 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000463 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000464}
465
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000466/* Data used by all of the visitors. */
467typedef struct {
468 CXTranslationUnit TU;
469 enum CXCursorKind *Filter;
470} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000471
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000472
Ted Kremeneke68fff62010-02-17 00:41:32 +0000473enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000474 CXCursor Parent,
475 CXClientData ClientData) {
476 VisitorData *Data = (VisitorData *)ClientData;
477 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000478 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000479 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000480 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000481 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000482 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000483 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000484 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000485 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000486 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000487 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000488
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000489 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000490}
Steve Naroff50398192009-08-28 15:28:48 +0000491
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000493 CXCursor Parent,
494 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495 const char *startBuf, *endBuf;
496 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
497 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000498 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000499
Douglas Gregorb6998662010-01-19 19:34:47 +0000500 if (Cursor.kind != CXCursor_FunctionDecl ||
501 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000502 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000503
504 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
505 &startLine, &startColumn,
506 &endLine, &endColumn);
507 /* Probe the entire body, looking for both decls and refs. */
508 curLine = startLine;
509 curColumn = startColumn;
510
511 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000512 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000513 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000514 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000516 if (*startBuf == '\n') {
517 startBuf++;
518 curLine++;
519 curColumn = 1;
520 } else if (*startBuf != '\t')
521 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000522
Douglas Gregor98258af2010-01-18 22:46:11 +0000523 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000524 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000525
Douglas Gregor1db19de2010-01-19 21:36:55 +0000526 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000527 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000528 CXSourceLocation RefLoc
529 = clang_getLocation(Data->TU, file, curLine, curColumn);
530 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000531 if (Ref.kind == CXCursor_NoDeclFound) {
532 /* Nothing found here; that's fine. */
533 } else if (Ref.kind != CXCursor_FunctionDecl) {
534 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
535 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000536 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000537 printf("\n");
538 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000539 }
Ted Kremenek74844072010-02-17 00:41:20 +0000540 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000541 startBuf++;
542 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000543
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000544 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000545}
546
Ted Kremenek7d405622010-01-12 23:34:26 +0000547/******************************************************************************/
548/* USR testing. */
549/******************************************************************************/
550
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000551enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
552 CXClientData ClientData) {
553 VisitorData *Data = (VisitorData *)ClientData;
554 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000555 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000556 const char *cstr = clang_getCString(USR);
557 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000558 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000559 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000560 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000561 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
562
Douglas Gregora7bde202010-01-19 00:34:46 +0000563 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000564 printf("\n");
565 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000566
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000567 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000568 }
569
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000570 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000571}
572
573/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000574/* Inclusion stack testing. */
575/******************************************************************************/
576
577void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
578 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579
Ted Kremenek16b55a72010-01-26 19:31:51 +0000580 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000581 CXString fname;
582
583 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000584 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000585 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586
Ted Kremenek16b55a72010-01-26 19:31:51 +0000587 for (i = 0; i < includeStackLen; ++i) {
588 CXFile includingFile;
589 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000590 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
591 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000592 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000593 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000594 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000595 }
596 printf("\n");
597}
598
599void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000600 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000601}
602
603/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000604/* Linkage testing. */
605/******************************************************************************/
606
607static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
608 CXClientData d) {
609 const char *linkage = 0;
610
611 if (clang_isInvalid(clang_getCursorKind(cursor)))
612 return CXChildVisit_Recurse;
613
614 switch (clang_getCursorLinkage(cursor)) {
615 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000616 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
617 case CXLinkage_Internal: linkage = "Internal"; break;
618 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
619 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000620 }
621
622 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000623 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000624 printf("linkage=%s\n", linkage);
625 }
626
627 return CXChildVisit_Recurse;
628}
629
630/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000631/* Typekind testing. */
632/******************************************************************************/
633
634static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
635 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000636 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
637 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000638 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000639 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000640 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000641 if (clang_isConstQualifiedType(T))
642 printf(" const");
643 if (clang_isVolatileQualifiedType(T))
644 printf(" volatile");
645 if (clang_isRestrictQualifiedType(T))
646 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000647 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000648 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000649 {
650 CXType CT = clang_getCanonicalType(T);
651 if (!clang_equalTypes(T, CT)) {
652 CXString CS = clang_getTypeKindSpelling(CT.kind);
653 printf(" [canonical=%s]", clang_getCString(CS));
654 clang_disposeString(CS);
655 }
656 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000657 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000658 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000659 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660 if (RT.kind != CXType_Invalid) {
661 CXString RS = clang_getTypeKindSpelling(RT.kind);
662 printf(" [result=%s]", clang_getCString(RS));
663 clang_disposeString(RS);
664 }
665 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000666 /* Print the argument types if they exist. */
667 {
668 int numArgs = clang_Cursor_getNumArguments(cursor);
669 if (numArgs != -1 && numArgs != 0) {
670 printf(" [args=");
671 for (int i = 0; i < numArgs; ++i) {
672 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
673 if (T.kind != CXType_Invalid) {
674 CXString S = clang_getTypeKindSpelling(T.kind);
675 printf(" %s", clang_getCString(S));
676 clang_disposeString(S);
677 }
678 }
679 printf("]");
680 }
681 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000682 /* Print if this is a non-POD type. */
683 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000684
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000685 printf("\n");
686 }
687 return CXChildVisit_Recurse;
688}
689
690
691/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000692/* Loading ASTs/source. */
693/******************************************************************************/
694
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000695static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000696 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000697 CXCursorVisitor Visitor,
698 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000699
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000700 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000701 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000702
703 if (Visitor) {
704 enum CXCursorKind K = CXCursor_NotImplemented;
705 enum CXCursorKind *ck = &K;
706 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000708 /* Perform some simple filtering. */
709 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000710 else if (!strcmp(filter, "all-display") ||
711 !strcmp(filter, "local-display")) {
712 ck = NULL;
713 want_display_name = 1;
714 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000715 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000716 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
717 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
718 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
719 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
720 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
721 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
722 else {
723 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
724 return 1;
725 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000726
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000727 Data.TU = TU;
728 Data.Filter = ck;
729 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000730 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000731
Ted Kremenekce2ae882010-01-26 17:59:48 +0000732 if (PV)
733 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000734
Douglas Gregora88084b2010-02-18 18:08:43 +0000735 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000736 if (checkForErrors(TU) != 0) {
737 clang_disposeTranslationUnit(TU);
738 return -1;
739 }
740
Ted Kremenek0d435192009-11-17 18:13:31 +0000741 clang_disposeTranslationUnit(TU);
742 return 0;
743}
744
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000745int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000746 const char *prefix, CXCursorVisitor Visitor,
747 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000748 CXIndex Idx;
749 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000750 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000751 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000752 !strcmp(filter, "local") ? 1 : 0,
753 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000754
Ted Kremenek020a0952010-02-11 07:41:25 +0000755 if (!CreateTranslationUnit(Idx, file, &TU)) {
756 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000757 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000758 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000759
Ted Kremenek020a0952010-02-11 07:41:25 +0000760 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
761 clang_disposeIndex(Idx);
762 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000763}
764
Ted Kremenekce2ae882010-01-26 17:59:48 +0000765int perform_test_load_source(int argc, const char **argv,
766 const char *filter, CXCursorVisitor Visitor,
767 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000768 CXIndex Idx;
769 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000770 struct CXUnsavedFile *unsaved_files = 0;
771 int num_unsaved_files = 0;
772 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000773
Daniel Dunbarada487d2009-12-01 02:03:10 +0000774 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000775 (!strcmp(filter, "local") ||
776 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000777 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000778
Ted Kremenek020a0952010-02-11 07:41:25 +0000779 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
780 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000781 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000782 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000783
Douglas Gregordca8ee82011-05-06 16:33:08 +0000784 TU = clang_parseTranslationUnit(Idx, 0,
785 argv + num_unsaved_files,
786 argc - num_unsaved_files,
787 unsaved_files, num_unsaved_files,
788 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000789 if (!TU) {
790 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000791 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000792 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000793 return 1;
794 }
795
Ted Kremenekce2ae882010-01-26 17:59:48 +0000796 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000797 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000798 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000799 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000800}
801
Douglas Gregorabc563f2010-07-19 21:46:24 +0000802int perform_test_reparse_source(int argc, const char **argv, int trials,
803 const char *filter, CXCursorVisitor Visitor,
804 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000805 CXIndex Idx;
806 CXTranslationUnit TU;
807 struct CXUnsavedFile *unsaved_files = 0;
808 int num_unsaved_files = 0;
809 int result;
810 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000811 int remap_after_trial = 0;
812 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000813
814 Idx = clang_createIndex(/* excludeDeclsFromPCH */
815 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000816 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000817
Douglas Gregorabc563f2010-07-19 21:46:24 +0000818 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
819 clang_disposeIndex(Idx);
820 return -1;
821 }
822
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000823 /* Load the initial translation unit -- we do this without honoring remapped
824 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000825 TU = clang_parseTranslationUnit(Idx, 0,
826 argv + num_unsaved_files,
827 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000828 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000829 if (!TU) {
830 fprintf(stderr, "Unable to load translation unit!\n");
831 free_remapped_files(unsaved_files, num_unsaved_files);
832 clang_disposeIndex(Idx);
833 return 1;
834 }
835
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000836 if (checkForErrors(TU) != 0)
837 return -1;
838
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000839 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
840 remap_after_trial =
841 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
842 }
843
Douglas Gregorabc563f2010-07-19 21:46:24 +0000844 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000845 if (clang_reparseTranslationUnit(TU,
846 trial >= remap_after_trial ? num_unsaved_files : 0,
847 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000848 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000849 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000850 clang_disposeTranslationUnit(TU);
851 free_remapped_files(unsaved_files, num_unsaved_files);
852 clang_disposeIndex(Idx);
853 return -1;
854 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000855
856 if (checkForErrors(TU) != 0)
857 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000858 }
859
860 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000861
Douglas Gregorabc563f2010-07-19 21:46:24 +0000862 free_remapped_files(unsaved_files, num_unsaved_files);
863 clang_disposeIndex(Idx);
864 return result;
865}
866
Ted Kremenek0d435192009-11-17 18:13:31 +0000867/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000868/* Logic for testing clang_getCursor(). */
869/******************************************************************************/
870
Douglas Gregordd3e5542011-05-04 00:14:37 +0000871static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000872 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000873 unsigned end_line, unsigned end_col,
874 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000875 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000876 if (prefix)
877 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000878 PrintExtent(stdout, start_line, start_col, end_line, end_col);
879 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000880 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000881 printf("\n");
882}
883
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000884static int perform_file_scan(const char *ast_file, const char *source_file,
885 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000886 CXIndex Idx;
887 CXTranslationUnit TU;
888 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000889 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000890 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000891 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000892 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000893
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000894 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
895 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000896 fprintf(stderr, "Could not create Index\n");
897 return 1;
898 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000899
Ted Kremenek1c6da172009-11-17 19:37:36 +0000900 if (!CreateTranslationUnit(Idx, ast_file, &TU))
901 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000902
Ted Kremenek1c6da172009-11-17 19:37:36 +0000903 if ((fp = fopen(source_file, "r")) == NULL) {
904 fprintf(stderr, "Could not open '%s'\n", source_file);
905 return 1;
906 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000907
Douglas Gregorb9790342010-01-22 21:44:22 +0000908 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000909 for (;;) {
910 CXCursor cursor;
911 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000912
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000913 if (c == '\n') {
914 ++line;
915 col = 1;
916 } else
917 ++col;
918
919 /* Check the cursor at this position, and dump the previous one if we have
920 * found something new.
921 */
922 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
923 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
924 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000925 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000926 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000927 start_line = line;
928 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000929 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000930 if (c == EOF)
931 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000932
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000933 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000934 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000935
Ted Kremenek1c6da172009-11-17 19:37:36 +0000936 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000937 clang_disposeTranslationUnit(TU);
938 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000939 return 0;
940}
941
942/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000943/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000944/******************************************************************************/
945
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000946/* Parse file:line:column from the input string. Returns 0 on success, non-zero
947 on failure. If successful, the pointer *filename will contain newly-allocated
948 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000949int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000950 unsigned *column, unsigned *second_line,
951 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000952 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000953 const char *last_colon = strrchr(input, ':');
954 unsigned values[4], i;
955 unsigned num_values = (second_line && second_column)? 4 : 2;
956
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000957 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000958 if (!last_colon || last_colon == input) {
959 if (num_values == 4)
960 fprintf(stderr, "could not parse filename:line:column:line:column in "
961 "'%s'\n", input);
962 else
963 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000964 return 1;
965 }
966
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000967 for (i = 0; i != num_values; ++i) {
968 const char *prev_colon;
969
970 /* Parse the next line or column. */
971 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
972 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000973 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000974 (i % 2 ? "column" : "line"), input);
975 return 1;
976 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000977
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000978 if (i + 1 == num_values)
979 break;
980
981 /* Find the previous colon. */
982 prev_colon = last_colon - 1;
983 while (prev_colon != input && *prev_colon != ':')
984 --prev_colon;
985 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000986 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000987 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000988 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000989 }
990
991 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000992 }
993
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000994 *line = values[0];
995 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000996
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000997 if (second_line && second_column) {
998 *second_line = values[2];
999 *second_column = values[3];
1000 }
1001
Douglas Gregor88d23952009-11-09 18:19:57 +00001002 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001003 *filename = (char*)malloc(last_colon - input + 1);
1004 memcpy(*filename, input, last_colon - input);
1005 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001006 return 0;
1007}
1008
1009const char *
1010clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1011 switch (Kind) {
1012 case CXCompletionChunk_Optional: return "Optional";
1013 case CXCompletionChunk_TypedText: return "TypedText";
1014 case CXCompletionChunk_Text: return "Text";
1015 case CXCompletionChunk_Placeholder: return "Placeholder";
1016 case CXCompletionChunk_Informative: return "Informative";
1017 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1018 case CXCompletionChunk_LeftParen: return "LeftParen";
1019 case CXCompletionChunk_RightParen: return "RightParen";
1020 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1021 case CXCompletionChunk_RightBracket: return "RightBracket";
1022 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1023 case CXCompletionChunk_RightBrace: return "RightBrace";
1024 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1025 case CXCompletionChunk_RightAngle: return "RightAngle";
1026 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001027 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001028 case CXCompletionChunk_Colon: return "Colon";
1029 case CXCompletionChunk_SemiColon: return "SemiColon";
1030 case CXCompletionChunk_Equal: return "Equal";
1031 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1032 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001033 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001034
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001035 return "Unknown";
1036}
1037
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001038static int checkForErrors(CXTranslationUnit TU) {
1039 unsigned Num, i;
1040 CXDiagnostic Diag;
1041 CXString DiagStr;
1042
1043 if (!getenv("CINDEXTEST_FAILONERROR"))
1044 return 0;
1045
1046 Num = clang_getNumDiagnostics(TU);
1047 for (i = 0; i != Num; ++i) {
1048 Diag = clang_getDiagnostic(TU, i);
1049 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1050 DiagStr = clang_formatDiagnostic(Diag,
1051 clang_defaultDiagnosticDisplayOptions());
1052 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1053 clang_disposeString(DiagStr);
1054 clang_disposeDiagnostic(Diag);
1055 return -1;
1056 }
1057 clang_disposeDiagnostic(Diag);
1058 }
1059
1060 return 0;
1061}
1062
Douglas Gregor3ac73852009-11-09 16:04:45 +00001063void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001064 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001065
Douglas Gregor3ac73852009-11-09 16:04:45 +00001066 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001067 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001068 CXString text;
1069 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001070 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001071 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001072
Douglas Gregor3ac73852009-11-09 16:04:45 +00001073 if (Kind == CXCompletionChunk_Optional) {
1074 fprintf(file, "{Optional ");
1075 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001076 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001077 file);
1078 fprintf(file, "}");
1079 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001080 }
1081
1082 if (Kind == CXCompletionChunk_VerticalSpace) {
1083 fprintf(file, "{VerticalSpace }");
1084 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001085 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001086
Douglas Gregord5a20892009-11-09 17:05:28 +00001087 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001088 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001089 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001090 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001091 cstr ? cstr : "");
1092 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001093 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001094
Douglas Gregor3ac73852009-11-09 16:04:45 +00001095}
1096
1097void print_completion_result(CXCompletionResult *completion_result,
1098 CXClientData client_data) {
1099 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001100 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001101 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001102 enum CXCursorKind ParentKind;
1103 CXString ParentName;
1104
Ted Kremeneke68fff62010-02-17 00:41:32 +00001105 fprintf(file, "%s:", clang_getCString(ks));
1106 clang_disposeString(ks);
1107
Douglas Gregor3ac73852009-11-09 16:04:45 +00001108 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001109 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001110 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001111 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1112 case CXAvailability_Available:
1113 break;
1114
1115 case CXAvailability_Deprecated:
1116 fprintf(file, " (deprecated)");
1117 break;
1118
1119 case CXAvailability_NotAvailable:
1120 fprintf(file, " (unavailable)");
1121 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001122
1123 case CXAvailability_NotAccessible:
1124 fprintf(file, " (inaccessible)");
1125 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001126 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001127
1128 annotationCount = clang_getCompletionNumAnnotations(
1129 completion_result->CompletionString);
1130 if (annotationCount) {
1131 unsigned i;
1132 fprintf(file, " (");
1133 for (i = 0; i < annotationCount; ++i) {
1134 if (i != 0)
1135 fprintf(file, ", ");
1136 fprintf(file, "\"%s\"",
1137 clang_getCString(clang_getCompletionAnnotation(
1138 completion_result->CompletionString, i)));
1139 }
1140 fprintf(file, ")");
1141 }
1142
Douglas Gregorba103062012-03-27 23:34:16 +00001143 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1144 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1145 &ParentKind);
1146 if (ParentKind != CXCursor_NotImplemented) {
1147 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1148 fprintf(file, " (parent: %s '%s')",
1149 clang_getCString(KindSpelling),
1150 clang_getCString(ParentName));
1151 clang_disposeString(KindSpelling);
1152 }
1153 clang_disposeString(ParentName);
1154 }
1155
Douglas Gregor58ddb602010-08-23 23:00:57 +00001156 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001157}
1158
Douglas Gregor3da626b2011-07-07 16:03:39 +00001159void print_completion_contexts(unsigned long long contexts, FILE *file) {
1160 fprintf(file, "Completion contexts:\n");
1161 if (contexts == CXCompletionContext_Unknown) {
1162 fprintf(file, "Unknown\n");
1163 }
1164 if (contexts & CXCompletionContext_AnyType) {
1165 fprintf(file, "Any type\n");
1166 }
1167 if (contexts & CXCompletionContext_AnyValue) {
1168 fprintf(file, "Any value\n");
1169 }
1170 if (contexts & CXCompletionContext_ObjCObjectValue) {
1171 fprintf(file, "Objective-C object value\n");
1172 }
1173 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1174 fprintf(file, "Objective-C selector value\n");
1175 }
1176 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1177 fprintf(file, "C++ class type value\n");
1178 }
1179 if (contexts & CXCompletionContext_DotMemberAccess) {
1180 fprintf(file, "Dot member access\n");
1181 }
1182 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1183 fprintf(file, "Arrow member access\n");
1184 }
1185 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1186 fprintf(file, "Objective-C property access\n");
1187 }
1188 if (contexts & CXCompletionContext_EnumTag) {
1189 fprintf(file, "Enum tag\n");
1190 }
1191 if (contexts & CXCompletionContext_UnionTag) {
1192 fprintf(file, "Union tag\n");
1193 }
1194 if (contexts & CXCompletionContext_StructTag) {
1195 fprintf(file, "Struct tag\n");
1196 }
1197 if (contexts & CXCompletionContext_ClassTag) {
1198 fprintf(file, "Class name\n");
1199 }
1200 if (contexts & CXCompletionContext_Namespace) {
1201 fprintf(file, "Namespace or namespace alias\n");
1202 }
1203 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1204 fprintf(file, "Nested name specifier\n");
1205 }
1206 if (contexts & CXCompletionContext_ObjCInterface) {
1207 fprintf(file, "Objective-C interface\n");
1208 }
1209 if (contexts & CXCompletionContext_ObjCProtocol) {
1210 fprintf(file, "Objective-C protocol\n");
1211 }
1212 if (contexts & CXCompletionContext_ObjCCategory) {
1213 fprintf(file, "Objective-C category\n");
1214 }
1215 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1216 fprintf(file, "Objective-C instance method\n");
1217 }
1218 if (contexts & CXCompletionContext_ObjCClassMessage) {
1219 fprintf(file, "Objective-C class method\n");
1220 }
1221 if (contexts & CXCompletionContext_ObjCSelectorName) {
1222 fprintf(file, "Objective-C selector name\n");
1223 }
1224 if (contexts & CXCompletionContext_MacroName) {
1225 fprintf(file, "Macro name\n");
1226 }
1227 if (contexts & CXCompletionContext_NaturalLanguage) {
1228 fprintf(file, "Natural language\n");
1229 }
1230}
1231
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001232int my_stricmp(const char *s1, const char *s2) {
1233 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001234 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001235 if (c1 < c2)
1236 return -1;
1237 else if (c1 > c2)
1238 return 1;
1239
1240 ++s1;
1241 ++s2;
1242 }
1243
1244 if (*s1)
1245 return 1;
1246 else if (*s2)
1247 return -1;
1248 return 0;
1249}
1250
Douglas Gregor1982c182010-07-12 18:38:41 +00001251int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001252 const char *input = argv[1];
1253 char *filename = 0;
1254 unsigned line;
1255 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001256 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001257 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001258 struct CXUnsavedFile *unsaved_files = 0;
1259 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001260 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001261 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001262 unsigned I, Repeats = 1;
1263 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1264
1265 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1266 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001267
Douglas Gregor1982c182010-07-12 18:38:41 +00001268 if (timing_only)
1269 input += strlen("-code-completion-timing=");
1270 else
1271 input += strlen("-code-completion-at=");
1272
Ted Kremeneke68fff62010-02-17 00:41:32 +00001273 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001274 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001275 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001276
Douglas Gregor735df882009-12-02 09:21:34 +00001277 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1278 return -1;
1279
Douglas Gregor32be4a52010-10-11 21:37:58 +00001280 CIdx = clang_createIndex(0, 0);
1281
1282 if (getenv("CINDEXTEST_EDITING"))
1283 Repeats = 5;
1284
1285 TU = clang_parseTranslationUnit(CIdx, 0,
1286 argv + num_unsaved_files + 2,
1287 argc - num_unsaved_files - 2,
1288 0, 0, getDefaultParsingOptions());
1289 if (!TU) {
1290 fprintf(stderr, "Unable to load translation unit!\n");
1291 return 1;
1292 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001293
1294 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1295 fprintf(stderr, "Unable to reparse translation init!\n");
1296 return 1;
1297 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001298
1299 for (I = 0; I != Repeats; ++I) {
1300 results = clang_codeCompleteAt(TU, filename, line, column,
1301 unsaved_files, num_unsaved_files,
1302 completionOptions);
1303 if (!results) {
1304 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001305 return 1;
1306 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001307 if (I != Repeats-1)
1308 clang_disposeCodeCompleteResults(results);
1309 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001310
Douglas Gregorec6762c2009-12-18 16:20:58 +00001311 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001312 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001313 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001314 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001315 CXString objCSelector;
1316 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001317 if (!timing_only) {
1318 /* Sort the code-completion results based on the typed text. */
1319 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1320
Douglas Gregor1982c182010-07-12 18:38:41 +00001321 for (i = 0; i != n; ++i)
1322 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001323 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001324 n = clang_codeCompleteGetNumDiagnostics(results);
1325 for (i = 0; i != n; ++i) {
1326 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1327 PrintDiagnostic(diag);
1328 clang_disposeDiagnostic(diag);
1329 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001330
1331 contexts = clang_codeCompleteGetContexts(results);
1332 print_completion_contexts(contexts, stdout);
1333
Douglas Gregor0a47d692011-07-26 15:24:30 +00001334 containerKind = clang_codeCompleteGetContainerKind(results,
1335 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001336
1337 if (containerKind != CXCursor_InvalidCode) {
1338 /* We have found a container */
1339 CXString containerUSR, containerKindSpelling;
1340 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1341 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1342 clang_disposeString(containerKindSpelling);
1343
1344 if (containerIsIncomplete) {
1345 printf("Container is incomplete\n");
1346 }
1347 else {
1348 printf("Container is complete\n");
1349 }
1350
1351 containerUSR = clang_codeCompleteGetContainerUSR(results);
1352 printf("Container USR: %s\n", clang_getCString(containerUSR));
1353 clang_disposeString(containerUSR);
1354 }
1355
Douglas Gregor0a47d692011-07-26 15:24:30 +00001356 objCSelector = clang_codeCompleteGetObjCSelector(results);
1357 selectorString = clang_getCString(objCSelector);
1358 if (selectorString && strlen(selectorString) > 0) {
1359 printf("Objective-C selector: %s\n", selectorString);
1360 }
1361 clang_disposeString(objCSelector);
1362
Douglas Gregorec6762c2009-12-18 16:20:58 +00001363 clang_disposeCodeCompleteResults(results);
1364 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001365 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001366 clang_disposeIndex(CIdx);
1367 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368
Douglas Gregor735df882009-12-02 09:21:34 +00001369 free_remapped_files(unsaved_files, num_unsaved_files);
1370
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001371 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001372}
1373
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001374typedef struct {
1375 char *filename;
1376 unsigned line;
1377 unsigned column;
1378} CursorSourceLocation;
1379
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001380static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001381 CXIndex CIdx;
1382 int errorCode;
1383 struct CXUnsavedFile *unsaved_files = 0;
1384 int num_unsaved_files = 0;
1385 CXTranslationUnit TU;
1386 CXCursor Cursor;
1387 CursorSourceLocation *Locations = 0;
1388 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001389 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001390 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001391
Ted Kremeneke68fff62010-02-17 00:41:32 +00001392 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001393 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1394 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001395
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001396 /* Parse the locations. */
1397 assert(NumLocations > 0 && "Unable to count locations?");
1398 Locations = (CursorSourceLocation *)malloc(
1399 NumLocations * sizeof(CursorSourceLocation));
1400 for (Loc = 0; Loc < NumLocations; ++Loc) {
1401 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001402 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1403 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001404 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001405 return errorCode;
1406 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001407
1408 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001409 &num_unsaved_files))
1410 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001411
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001412 if (getenv("CINDEXTEST_EDITING"))
1413 Repeats = 5;
1414
1415 /* Parse the translation unit. When we're testing clang_getCursor() after
1416 reparsing, don't remap unsaved files until the second parse. */
1417 CIdx = clang_createIndex(1, 1);
1418 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1419 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001420 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001421 unsaved_files,
1422 Repeats > 1? 0 : num_unsaved_files,
1423 getDefaultParsingOptions());
1424
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001425 if (!TU) {
1426 fprintf(stderr, "unable to parse input\n");
1427 return -1;
1428 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001429
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001430 if (checkForErrors(TU) != 0)
1431 return -1;
1432
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001433 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001434 if (Repeats > 1 &&
1435 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1436 clang_defaultReparseOptions(TU))) {
1437 clang_disposeTranslationUnit(TU);
1438 return 1;
1439 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001440
1441 if (checkForErrors(TU) != 0)
1442 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001443
1444 for (Loc = 0; Loc < NumLocations; ++Loc) {
1445 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1446 if (!file)
1447 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001448
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001449 Cursor = clang_getCursor(TU,
1450 clang_getLocation(TU, file, Locations[Loc].line,
1451 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001452
1453 if (checkForErrors(TU) != 0)
1454 return -1;
1455
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001456 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001457 CXCompletionString completionString = clang_getCursorCompletionString(
1458 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001459 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1460 CXString Spelling;
1461 const char *cspell;
1462 unsigned line, column;
1463 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1464 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001465 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001466 PrintCursorExtent(Cursor);
1467 Spelling = clang_getCursorSpelling(Cursor);
1468 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001469 if (cspell && strlen(cspell) != 0) {
1470 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001471 printf(" Spelling=%s (", cspell);
1472 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001473 CXSourceRange range =
1474 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001475 if (clang_Range_isNull(range))
1476 break;
1477 PrintRange(range, 0);
1478 }
1479 printf(")");
1480 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001481 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001482 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1483 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001484 if (completionString != NULL) {
1485 printf("\nCompletion string: ");
1486 print_completion_string(completionString, stdout);
1487 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001488 printf("\n");
1489 free(Locations[Loc].filename);
1490 }
1491 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001492 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001493
Douglas Gregora88084b2010-02-18 18:08:43 +00001494 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001495 clang_disposeTranslationUnit(TU);
1496 clang_disposeIndex(CIdx);
1497 free(Locations);
1498 free_remapped_files(unsaved_files, num_unsaved_files);
1499 return 0;
1500}
1501
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001502static enum CXVisitorResult findFileRefsVisit(void *context,
1503 CXCursor cursor, CXSourceRange range) {
1504 if (clang_Range_isNull(range))
1505 return CXVisit_Continue;
1506
1507 PrintCursor(cursor);
1508 PrintRange(range, "");
1509 printf("\n");
1510 return CXVisit_Continue;
1511}
1512
1513static int find_file_refs_at(int argc, const char **argv) {
1514 CXIndex CIdx;
1515 int errorCode;
1516 struct CXUnsavedFile *unsaved_files = 0;
1517 int num_unsaved_files = 0;
1518 CXTranslationUnit TU;
1519 CXCursor Cursor;
1520 CursorSourceLocation *Locations = 0;
1521 unsigned NumLocations = 0, Loc;
1522 unsigned Repeats = 1;
1523 unsigned I;
1524
1525 /* Count the number of locations. */
1526 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1527 ++NumLocations;
1528
1529 /* Parse the locations. */
1530 assert(NumLocations > 0 && "Unable to count locations?");
1531 Locations = (CursorSourceLocation *)malloc(
1532 NumLocations * sizeof(CursorSourceLocation));
1533 for (Loc = 0; Loc < NumLocations; ++Loc) {
1534 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1535 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1536 &Locations[Loc].line,
1537 &Locations[Loc].column, 0, 0)))
1538 return errorCode;
1539 }
1540
1541 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1542 &num_unsaved_files))
1543 return -1;
1544
1545 if (getenv("CINDEXTEST_EDITING"))
1546 Repeats = 5;
1547
1548 /* Parse the translation unit. When we're testing clang_getCursor() after
1549 reparsing, don't remap unsaved files until the second parse. */
1550 CIdx = clang_createIndex(1, 1);
1551 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1552 argv + num_unsaved_files + 1 + NumLocations,
1553 argc - num_unsaved_files - 2 - NumLocations,
1554 unsaved_files,
1555 Repeats > 1? 0 : num_unsaved_files,
1556 getDefaultParsingOptions());
1557
1558 if (!TU) {
1559 fprintf(stderr, "unable to parse input\n");
1560 return -1;
1561 }
1562
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001563 if (checkForErrors(TU) != 0)
1564 return -1;
1565
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001566 for (I = 0; I != Repeats; ++I) {
1567 if (Repeats > 1 &&
1568 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1569 clang_defaultReparseOptions(TU))) {
1570 clang_disposeTranslationUnit(TU);
1571 return 1;
1572 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001573
1574 if (checkForErrors(TU) != 0)
1575 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001576
1577 for (Loc = 0; Loc < NumLocations; ++Loc) {
1578 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1579 if (!file)
1580 continue;
1581
1582 Cursor = clang_getCursor(TU,
1583 clang_getLocation(TU, file, Locations[Loc].line,
1584 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001585
1586 if (checkForErrors(TU) != 0)
1587 return -1;
1588
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001589 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001590 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001591 PrintCursor(Cursor);
1592 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001593 clang_findReferencesInFile(Cursor, file, visitor);
1594 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001595
1596 if (checkForErrors(TU) != 0)
1597 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001598 }
1599 }
1600 }
1601
1602 PrintDiagnostics(TU);
1603 clang_disposeTranslationUnit(TU);
1604 clang_disposeIndex(CIdx);
1605 free(Locations);
1606 free_remapped_files(unsaved_files, num_unsaved_files);
1607 return 0;
1608}
1609
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001610typedef struct {
1611 const char *check_prefix;
1612 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001613 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001614 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001615 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001616} IndexData;
1617
1618static void printCheck(IndexData *data) {
1619 if (data->check_prefix) {
1620 if (data->first_check_printed) {
1621 printf("// %s-NEXT: ", data->check_prefix);
1622 } else {
1623 printf("// %s : ", data->check_prefix);
1624 data->first_check_printed = 1;
1625 }
1626 }
1627}
1628
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001629static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001630 CXString filename = clang_getFileName((CXFile)file);
1631 printf("%s", clang_getCString(filename));
1632 clang_disposeString(filename);
1633}
1634
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001635static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1636 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001637 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001638 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001639 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001640 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001641 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001642
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001643 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001644 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1645 if (line == 0) {
1646 printf("<null loc>");
1647 return;
1648 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001649 if (!file) {
1650 printf("<no idxfile>");
1651 return;
1652 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001653 filename = clang_getFileName((CXFile)file);
1654 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001655 if (strcmp(cname, index_data->main_filename) == 0)
1656 isMainFile = 1;
1657 else
1658 isMainFile = 0;
1659 clang_disposeString(filename);
1660
1661 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001662 printCXIndexFile(file);
1663 printf(":");
1664 }
1665 printf("%d:%d", line, column);
1666}
1667
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001668static unsigned digitCount(unsigned val) {
1669 unsigned c = 1;
1670 while (1) {
1671 if (val < 10)
1672 return c;
1673 ++c;
1674 val /= 10;
1675 }
1676}
1677
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001678static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1679 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001680 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001681 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001682 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001683 unsigned line, column;
1684
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001685 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001686 if (!name)
1687 name = "<anon-tag>";
1688
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001689 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001690 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001691 newStr = (char *)malloc(strlen(name) +
1692 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001693 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001694 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001695}
1696
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001697static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1698 CXIdxClientContainer container;
1699 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001700 if (!container)
1701 printf("[<<NULL>>]");
1702 else
1703 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001704}
1705
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001706static const char *getEntityKindString(CXIdxEntityKind kind) {
1707 switch (kind) {
1708 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1709 case CXIdxEntity_Typedef: return "typedef";
1710 case CXIdxEntity_Function: return "function";
1711 case CXIdxEntity_Variable: return "variable";
1712 case CXIdxEntity_Field: return "field";
1713 case CXIdxEntity_EnumConstant: return "enumerator";
1714 case CXIdxEntity_ObjCClass: return "objc-class";
1715 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1716 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001717 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1718 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001719 case CXIdxEntity_ObjCProperty: return "objc-property";
1720 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1721 case CXIdxEntity_Enum: return "enum";
1722 case CXIdxEntity_Struct: return "struct";
1723 case CXIdxEntity_Union: return "union";
1724 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001725 case CXIdxEntity_CXXNamespace: return "namespace";
1726 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1727 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1728 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1729 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1730 case CXIdxEntity_CXXConstructor: return "constructor";
1731 case CXIdxEntity_CXXDestructor: return "destructor";
1732 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1733 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1734 }
1735 assert(0 && "Garbage entity kind");
1736 return 0;
1737}
1738
1739static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1740 switch (kind) {
1741 case CXIdxEntity_NonTemplate: return "";
1742 case CXIdxEntity_Template: return "-template";
1743 case CXIdxEntity_TemplatePartialSpecialization:
1744 return "-template-partial-spec";
1745 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001746 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001747 assert(0 && "Garbage entity kind");
1748 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001749}
1750
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001751static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1752 switch (kind) {
1753 case CXIdxEntityLang_None: return "<none>";
1754 case CXIdxEntityLang_C: return "C";
1755 case CXIdxEntityLang_ObjC: return "ObjC";
1756 case CXIdxEntityLang_CXX: return "C++";
1757 }
1758 assert(0 && "Garbage language kind");
1759 return 0;
1760}
1761
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001762static void printEntityInfo(const char *cb,
1763 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001764 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001765 const char *name;
1766 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001767 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001768 index_data = (IndexData *)client_data;
1769 printCheck(index_data);
1770
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001771 if (!info) {
1772 printf("%s: <<NULL>>", cb);
1773 return;
1774 }
1775
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001776 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001777 if (!name)
1778 name = "<anon-tag>";
1779
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001780 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1781 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001782 printf(" | name: %s", name);
1783 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001784 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001785
1786 for (i = 0; i != info->numAttributes; ++i) {
1787 const CXIdxAttrInfo *Attr = info->attributes[i];
1788 printf(" <attribute>: ");
1789 PrintCursor(Attr->cursor);
1790 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001791}
1792
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001793static void printBaseClassInfo(CXClientData client_data,
1794 const CXIdxBaseClassInfo *info) {
1795 printEntityInfo(" <base>", client_data, info->base);
1796 printf(" | cursor: ");
1797 PrintCursor(info->cursor);
1798 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001799 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001800}
1801
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001802static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1803 CXClientData client_data) {
1804 unsigned i;
1805 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1806 printEntityInfo(" <protocol>", client_data,
1807 ProtoInfo->protocols[i]->protocol);
1808 printf(" | cursor: ");
1809 PrintCursor(ProtoInfo->protocols[i]->cursor);
1810 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001811 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001812 printf("\n");
1813 }
1814}
1815
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001816static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001817 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001818 CXString str;
1819 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001820 unsigned numDiags, i;
1821 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001822 IndexData *index_data;
1823 index_data = (IndexData *)client_data;
1824 printCheck(index_data);
1825
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001826 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1827 for (i = 0; i != numDiags; ++i) {
1828 diag = clang_getDiagnosticInSet(diagSet, i);
1829 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1830 cstr = clang_getCString(str);
1831 printf("[diagnostic]: %s\n", cstr);
1832 clang_disposeString(str);
1833
1834 if (getenv("CINDEXTEST_FAILONERROR") &&
1835 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1836 index_data->fail_for_error = 1;
1837 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001838 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001839}
1840
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001841static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1842 CXFile file, void *reserved) {
1843 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001844 CXString filename;
1845
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001846 index_data = (IndexData *)client_data;
1847 printCheck(index_data);
1848
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001849 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001850 index_data->main_filename = clang_getCString(filename);
1851 clang_disposeString(filename);
1852
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001853 printf("[enteredMainFile]: ");
1854 printCXIndexFile((CXIdxClientFile)file);
1855 printf("\n");
1856
1857 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001858}
1859
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001860static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001861 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001862 IndexData *index_data;
1863 index_data = (IndexData *)client_data;
1864 printCheck(index_data);
1865
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001866 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001867 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001868 printf(" | name: \"%s\"", info->filename);
1869 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001870 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001871 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001872
1873 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001874}
1875
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001876static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001877 void *reserved) {
1878 IndexData *index_data;
1879 index_data = (IndexData *)client_data;
1880 printCheck(index_data);
1881
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001882 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001883 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001884}
1885
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001886static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001887 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001888 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001889 const CXIdxObjCCategoryDeclInfo *CatInfo;
1890 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001891 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001892 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001893 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001894 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001895 index_data = (IndexData *)client_data;
1896
1897 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1898 printf(" | cursor: ");
1899 PrintCursor(info->cursor);
1900 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001901 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001902 printf(" | semantic-container: ");
1903 printCXIndexContainer(info->semanticContainer);
1904 printf(" | lexical-container: ");
1905 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001906 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001907 printf(" | isDef: %d", info->isDefinition);
1908 printf(" | isContainer: %d", info->isContainer);
1909 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001910
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001911 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001912 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001913 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001914 PrintCursor(Attr->cursor);
1915 printf("\n");
1916 }
1917
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001918 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1919 const char *kindName = 0;
1920 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1921 switch (K) {
1922 case CXIdxObjCContainer_ForwardRef:
1923 kindName = "forward-ref"; break;
1924 case CXIdxObjCContainer_Interface:
1925 kindName = "interface"; break;
1926 case CXIdxObjCContainer_Implementation:
1927 kindName = "implementation"; break;
1928 }
1929 printCheck(index_data);
1930 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1931 }
1932
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001933 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001934 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1935 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001936 printf(" | cursor: ");
1937 PrintCursor(CatInfo->classCursor);
1938 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001939 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001940 printf("\n");
1941 }
1942
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001943 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1944 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001945 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001946 printf("\n");
1947 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001948 }
1949
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001950 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1951 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001952 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001953
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001954 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
1955 if (PropInfo->getter) {
1956 printEntityInfo(" <getter>", client_data, PropInfo->getter);
1957 printf("\n");
1958 }
1959 if (PropInfo->setter) {
1960 printEntityInfo(" <setter>", client_data, PropInfo->setter);
1961 printf("\n");
1962 }
1963 }
1964
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001965 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1966 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1967 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1968 printf("\n");
1969 }
1970 }
1971
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001972 if (info->declAsContainer)
1973 clang_index_setClientContainer(info->declAsContainer,
1974 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001975}
1976
1977static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001978 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001979 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001980 printf(" | cursor: ");
1981 PrintCursor(info->cursor);
1982 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001983 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001984 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001985 printf(" | container: ");
1986 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001987 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001988 switch (info->kind) {
1989 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001990 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001991 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001992 printf("\n");
1993}
1994
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001995static int index_abortQuery(CXClientData client_data, void *reserved) {
1996 IndexData *index_data;
1997 index_data = (IndexData *)client_data;
1998 return index_data->abort;
1999}
2000
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002001static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002002 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002003 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002004 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002005 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002006 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002007 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002008 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002009 index_indexEntityReference
2010};
2011
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002012static unsigned getIndexOptions(void) {
2013 unsigned index_opts;
2014 index_opts = 0;
2015 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2016 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2017 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2018 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2019
2020 return index_opts;
2021}
2022
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002023static int index_file(int argc, const char **argv) {
2024 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002025 CXIndex Idx;
2026 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002027 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002028 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002029 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002030
2031 check_prefix = 0;
2032 if (argc > 0) {
2033 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2034 check_prefix = argv[0] + strlen("-check-prefix=");
2035 ++argv;
2036 --argc;
2037 }
2038 }
2039
2040 if (argc == 0) {
2041 fprintf(stderr, "no compiler arguments\n");
2042 return -1;
2043 }
2044
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002045 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2046 /* displayDiagnosics=*/1))) {
2047 fprintf(stderr, "Could not create Index\n");
2048 return 1;
2049 }
2050 idxAction = 0;
2051 result = 1;
2052
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002053 index_data.check_prefix = check_prefix;
2054 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002055 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002056 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002057
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002058 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002059 idxAction = clang_IndexAction_create(Idx);
2060 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002061 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002062 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002063 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002064 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002065
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002066 clang_IndexAction_dispose(idxAction);
2067 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002068 return result;
2069}
2070
2071static int index_tu(int argc, const char **argv) {
2072 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002073 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002074 CXTranslationUnit TU;
2075 const char *check_prefix;
2076 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002077 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002078 int result;
2079
2080 check_prefix = 0;
2081 if (argc > 0) {
2082 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2083 check_prefix = argv[0] + strlen("-check-prefix=");
2084 ++argv;
2085 --argc;
2086 }
2087 }
2088
2089 if (argc == 0) {
2090 fprintf(stderr, "no ast file\n");
2091 return -1;
2092 }
2093
2094 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2095 /* displayDiagnosics=*/1))) {
2096 fprintf(stderr, "Could not create Index\n");
2097 return 1;
2098 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002099 idxAction = 0;
2100 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002101
2102 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002103 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002104
2105 index_data.check_prefix = check_prefix;
2106 index_data.first_check_printed = 0;
2107 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002108 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002109
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002110 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002111 idxAction = clang_IndexAction_create(Idx);
2112 result = clang_indexTranslationUnit(idxAction, &index_data,
2113 &IndexCB,sizeof(IndexCB),
2114 index_opts, TU);
2115 if (index_data.fail_for_error)
2116 goto finished;
2117
2118 finished:
2119 clang_IndexAction_dispose(idxAction);
2120 clang_disposeIndex(Idx);
2121
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002122 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002123}
2124
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002125int perform_token_annotation(int argc, const char **argv) {
2126 const char *input = argv[1];
2127 char *filename = 0;
2128 unsigned line, second_line;
2129 unsigned column, second_column;
2130 CXIndex CIdx;
2131 CXTranslationUnit TU = 0;
2132 int errorCode;
2133 struct CXUnsavedFile *unsaved_files = 0;
2134 int num_unsaved_files = 0;
2135 CXToken *tokens;
2136 unsigned num_tokens;
2137 CXSourceRange range;
2138 CXSourceLocation startLoc, endLoc;
2139 CXFile file = 0;
2140 CXCursor *cursors = 0;
2141 unsigned i;
2142
2143 input += strlen("-test-annotate-tokens=");
2144 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2145 &second_line, &second_column)))
2146 return errorCode;
2147
2148 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2149 return -1;
2150
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002151 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002152 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2153 argv + num_unsaved_files + 2,
2154 argc - num_unsaved_files - 3,
2155 unsaved_files,
2156 num_unsaved_files,
2157 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002158 if (!TU) {
2159 fprintf(stderr, "unable to parse input\n");
2160 clang_disposeIndex(CIdx);
2161 free(filename);
2162 free_remapped_files(unsaved_files, num_unsaved_files);
2163 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002164 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002165 errorCode = 0;
2166
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002167 if (checkForErrors(TU) != 0)
2168 return -1;
2169
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002170 if (getenv("CINDEXTEST_EDITING")) {
2171 for (i = 0; i < 5; ++i) {
2172 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2173 clang_defaultReparseOptions(TU))) {
2174 fprintf(stderr, "Unable to reparse translation unit!\n");
2175 errorCode = -1;
2176 goto teardown;
2177 }
2178 }
2179 }
2180
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002181 if (checkForErrors(TU) != 0) {
2182 errorCode = -1;
2183 goto teardown;
2184 }
2185
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002186 file = clang_getFile(TU, filename);
2187 if (!file) {
2188 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2189 errorCode = -1;
2190 goto teardown;
2191 }
2192
2193 startLoc = clang_getLocation(TU, file, line, column);
2194 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002195 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002196 column);
2197 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002198 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002199 }
2200
2201 endLoc = clang_getLocation(TU, file, second_line, second_column);
2202 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002203 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002204 second_line, second_column);
2205 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002206 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002207 }
2208
2209 range = clang_getRange(startLoc, endLoc);
2210 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002211
2212 if (checkForErrors(TU) != 0) {
2213 errorCode = -1;
2214 goto teardown;
2215 }
2216
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002217 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2218 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002219
2220 if (checkForErrors(TU) != 0) {
2221 errorCode = -1;
2222 goto teardown;
2223 }
2224
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002225 for (i = 0; i != num_tokens; ++i) {
2226 const char *kind = "<unknown>";
2227 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2228 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2229 unsigned start_line, start_column, end_line, end_column;
2230
2231 switch (clang_getTokenKind(tokens[i])) {
2232 case CXToken_Punctuation: kind = "Punctuation"; break;
2233 case CXToken_Keyword: kind = "Keyword"; break;
2234 case CXToken_Identifier: kind = "Identifier"; break;
2235 case CXToken_Literal: kind = "Literal"; break;
2236 case CXToken_Comment: kind = "Comment"; break;
2237 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002238 clang_getSpellingLocation(clang_getRangeStart(extent),
2239 0, &start_line, &start_column, 0);
2240 clang_getSpellingLocation(clang_getRangeEnd(extent),
2241 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002242 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2243 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002244 if (!clang_isInvalid(cursors[i].kind)) {
2245 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002246 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002247 }
2248 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002249 }
2250 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002251 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002252
2253 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002254 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002255 clang_disposeTranslationUnit(TU);
2256 clang_disposeIndex(CIdx);
2257 free(filename);
2258 free_remapped_files(unsaved_files, num_unsaved_files);
2259 return errorCode;
2260}
2261
Ted Kremenek0d435192009-11-17 18:13:31 +00002262/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002263/* USR printing. */
2264/******************************************************************************/
2265
2266static int insufficient_usr(const char *kind, const char *usage) {
2267 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2268 return 1;
2269}
2270
2271static unsigned isUSR(const char *s) {
2272 return s[0] == 'c' && s[1] == ':';
2273}
2274
2275static int not_usr(const char *s, const char *arg) {
2276 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2277 return 1;
2278}
2279
2280static void print_usr(CXString usr) {
2281 const char *s = clang_getCString(usr);
2282 printf("%s\n", s);
2283 clang_disposeString(usr);
2284}
2285
2286static void display_usrs() {
2287 fprintf(stderr, "-print-usrs options:\n"
2288 " ObjCCategory <class name> <category name>\n"
2289 " ObjCClass <class name>\n"
2290 " ObjCIvar <ivar name> <class USR>\n"
2291 " ObjCMethod <selector> [0=class method|1=instance method] "
2292 "<class USR>\n"
2293 " ObjCProperty <property name> <class USR>\n"
2294 " ObjCProtocol <protocol name>\n");
2295}
2296
2297int print_usrs(const char **I, const char **E) {
2298 while (I != E) {
2299 const char *kind = *I;
2300 unsigned len = strlen(kind);
2301 switch (len) {
2302 case 8:
2303 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2304 if (I + 2 >= E)
2305 return insufficient_usr(kind, "<ivar name> <class USR>");
2306 if (!isUSR(I[2]))
2307 return not_usr("<class USR>", I[2]);
2308 else {
2309 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002310 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002311 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002312 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2313 }
2314
2315 I += 3;
2316 continue;
2317 }
2318 break;
2319 case 9:
2320 if (memcmp(kind, "ObjCClass", 9) == 0) {
2321 if (I + 1 >= E)
2322 return insufficient_usr(kind, "<class name>");
2323 print_usr(clang_constructUSR_ObjCClass(I[1]));
2324 I += 2;
2325 continue;
2326 }
2327 break;
2328 case 10:
2329 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2330 if (I + 3 >= E)
2331 return insufficient_usr(kind, "<method selector> "
2332 "[0=class method|1=instance method] <class USR>");
2333 if (!isUSR(I[3]))
2334 return not_usr("<class USR>", I[3]);
2335 else {
2336 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002337 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002338 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002339 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2340 }
2341 I += 4;
2342 continue;
2343 }
2344 break;
2345 case 12:
2346 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2347 if (I + 2 >= E)
2348 return insufficient_usr(kind, "<class name> <category name>");
2349 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2350 I += 3;
2351 continue;
2352 }
2353 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2354 if (I + 1 >= E)
2355 return insufficient_usr(kind, "<protocol name>");
2356 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2357 I += 2;
2358 continue;
2359 }
2360 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2361 if (I + 2 >= E)
2362 return insufficient_usr(kind, "<property name> <class USR>");
2363 if (!isUSR(I[2]))
2364 return not_usr("<class USR>", I[2]);
2365 else {
2366 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002367 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002368 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002369 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2370 }
2371 I += 3;
2372 continue;
2373 }
2374 break;
2375 default:
2376 break;
2377 }
2378 break;
2379 }
2380
2381 if (I != E) {
2382 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2383 display_usrs();
2384 return 1;
2385 }
2386 return 0;
2387}
2388
2389int print_usrs_file(const char *file_name) {
2390 char line[2048];
2391 const char *args[128];
2392 unsigned numChars = 0;
2393
2394 FILE *fp = fopen(file_name, "r");
2395 if (!fp) {
2396 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2397 return 1;
2398 }
2399
2400 /* This code is not really all that safe, but it works fine for testing. */
2401 while (!feof(fp)) {
2402 char c = fgetc(fp);
2403 if (c == '\n') {
2404 unsigned i = 0;
2405 const char *s = 0;
2406
2407 if (numChars == 0)
2408 continue;
2409
2410 line[numChars] = '\0';
2411 numChars = 0;
2412
2413 if (line[0] == '/' && line[1] == '/')
2414 continue;
2415
2416 s = strtok(line, " ");
2417 while (s) {
2418 args[i] = s;
2419 ++i;
2420 s = strtok(0, " ");
2421 }
2422 if (print_usrs(&args[0], &args[i]))
2423 return 1;
2424 }
2425 else
2426 line[numChars++] = c;
2427 }
2428
2429 fclose(fp);
2430 return 0;
2431}
2432
2433/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002434/* Command line processing. */
2435/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002436int write_pch_file(const char *filename, int argc, const char *argv[]) {
2437 CXIndex Idx;
2438 CXTranslationUnit TU;
2439 struct CXUnsavedFile *unsaved_files = 0;
2440 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002441 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002442
2443 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2444
2445 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2446 clang_disposeIndex(Idx);
2447 return -1;
2448 }
2449
2450 TU = clang_parseTranslationUnit(Idx, 0,
2451 argv + num_unsaved_files,
2452 argc - num_unsaved_files,
2453 unsaved_files,
2454 num_unsaved_files,
2455 CXTranslationUnit_Incomplete);
2456 if (!TU) {
2457 fprintf(stderr, "Unable to load translation unit!\n");
2458 free_remapped_files(unsaved_files, num_unsaved_files);
2459 clang_disposeIndex(Idx);
2460 return 1;
2461 }
2462
Douglas Gregor39c411f2011-07-06 16:43:36 +00002463 switch (clang_saveTranslationUnit(TU, filename,
2464 clang_defaultSaveOptions(TU))) {
2465 case CXSaveError_None:
2466 break;
2467
2468 case CXSaveError_TranslationErrors:
2469 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2470 filename);
2471 result = 2;
2472 break;
2473
2474 case CXSaveError_InvalidTU:
2475 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2476 filename);
2477 result = 3;
2478 break;
2479
2480 case CXSaveError_Unknown:
2481 default:
2482 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2483 result = 1;
2484 break;
2485 }
2486
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002487 clang_disposeTranslationUnit(TU);
2488 free_remapped_files(unsaved_files, num_unsaved_files);
2489 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002490 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002491}
2492
2493/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002494/* Serialized diagnostics. */
2495/******************************************************************************/
2496
2497static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2498 switch (error) {
2499 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2500 case CXLoadDiag_None: break;
2501 case CXLoadDiag_Unknown: return "Unknown";
2502 case CXLoadDiag_InvalidFile: return "Invalid File";
2503 }
2504 return "None";
2505}
2506
2507static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2508 switch (severity) {
2509 case CXDiagnostic_Note: return "note";
2510 case CXDiagnostic_Error: return "error";
2511 case CXDiagnostic_Fatal: return "fatal";
2512 case CXDiagnostic_Ignored: return "ignored";
2513 case CXDiagnostic_Warning: return "warning";
2514 }
2515 return "unknown";
2516}
2517
2518static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002519 if (indent == 0)
2520 return;
2521 fprintf(stderr, "+");
2522 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002523 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002524 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002525 --indent;
2526 }
2527}
2528
2529static void printLocation(CXSourceLocation L) {
2530 CXFile File;
2531 CXString FileName;
2532 unsigned line, column, offset;
2533
2534 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2535 FileName = clang_getFileName(File);
2536
2537 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2538 clang_disposeString(FileName);
2539}
2540
2541static void printRanges(CXDiagnostic D, unsigned indent) {
2542 unsigned i, n = clang_getDiagnosticNumRanges(D);
2543
2544 for (i = 0; i < n; ++i) {
2545 CXSourceLocation Start, End;
2546 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2547 Start = clang_getRangeStart(SR);
2548 End = clang_getRangeEnd(SR);
2549
2550 printIndent(indent);
2551 fprintf(stderr, "Range: ");
2552 printLocation(Start);
2553 fprintf(stderr, " ");
2554 printLocation(End);
2555 fprintf(stderr, "\n");
2556 }
2557}
2558
2559static void printFixIts(CXDiagnostic D, unsigned indent) {
2560 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002561 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002562 for (i = 0 ; i < n; ++i) {
2563 CXSourceRange ReplacementRange;
2564 CXString text;
2565 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2566
2567 printIndent(indent);
2568 fprintf(stderr, "FIXIT: (");
2569 printLocation(clang_getRangeStart(ReplacementRange));
2570 fprintf(stderr, " - ");
2571 printLocation(clang_getRangeEnd(ReplacementRange));
2572 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2573 clang_disposeString(text);
2574 }
2575}
2576
2577static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002578 unsigned i, n;
2579
Ted Kremenek15322172011-11-10 08:43:12 +00002580 if (!Diags)
2581 return;
2582
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002583 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002584 for (i = 0; i < n; ++i) {
2585 CXSourceLocation DiagLoc;
2586 CXDiagnostic D;
2587 CXFile File;
2588 CXString FileName, DiagSpelling, DiagOption;
2589 unsigned line, column, offset;
2590 const char *DiagOptionStr = 0;
2591
2592 D = clang_getDiagnosticInSet(Diags, i);
2593 DiagLoc = clang_getDiagnosticLocation(D);
2594 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2595 FileName = clang_getFileName(File);
2596 DiagSpelling = clang_getDiagnosticSpelling(D);
2597
2598 printIndent(indent);
2599
2600 fprintf(stderr, "%s:%d:%d: %s: %s",
2601 clang_getCString(FileName),
2602 line,
2603 column,
2604 getSeverityString(clang_getDiagnosticSeverity(D)),
2605 clang_getCString(DiagSpelling));
2606
2607 DiagOption = clang_getDiagnosticOption(D, 0);
2608 DiagOptionStr = clang_getCString(DiagOption);
2609 if (DiagOptionStr) {
2610 fprintf(stderr, " [%s]", DiagOptionStr);
2611 }
2612
2613 fprintf(stderr, "\n");
2614
2615 printRanges(D, indent);
2616 printFixIts(D, indent);
2617
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002618 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002619 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2620
2621 clang_disposeString(FileName);
2622 clang_disposeString(DiagSpelling);
2623 clang_disposeString(DiagOption);
2624 }
2625}
2626
2627static int read_diagnostics(const char *filename) {
2628 enum CXLoadDiag_Error error;
2629 CXString errorString;
2630 CXDiagnosticSet Diags = 0;
2631
2632 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2633 if (!Diags) {
2634 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2635 getDiagnosticCodeStr(error),
2636 clang_getCString(errorString));
2637 clang_disposeString(errorString);
2638 return 1;
2639 }
2640
2641 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002642 fprintf(stderr, "Number of diagnostics: %d\n",
2643 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002644 clang_disposeDiagnosticSet(Diags);
2645 return 0;
2646}
2647
2648/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002649/* Command line processing. */
2650/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002651
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002652static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002653 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002654 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002655 if (strcmp(s, "-usrs") == 0)
2656 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002657 if (strncmp(s, "-memory-usage", 13) == 0)
2658 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002659 return NULL;
2660}
2661
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002662static void print_usage(void) {
2663 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002664 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002665 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002666 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002667 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002668 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002669 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002670 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002671 "[FileCheck prefix]\n");
2672 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002673 " c-index-test -test-load-tu <AST file> <symbol filter> "
2674 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002675 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2676 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002677 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002678 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002679 " c-index-test -test-load-source-memory-usage "
2680 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002681 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2682 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002683 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002684 " c-index-test -test-load-source-usrs-memory-usage "
2685 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002686 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2687 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002688 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002689 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002690 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002691 " c-index-test -test-print-typekind {<args>}*\n"
2692 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002693 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002694 " c-index-test -write-pch <file> <compiler arguments>\n");
2695 fprintf(stderr,
2696 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002697 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002698 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002699 " all - load all symbols, including those from PCH\n"
2700 " local - load all symbols except those in PCH\n"
2701 " category - only load ObjC categories (non-PCH)\n"
2702 " interface - only load ObjC interfaces (non-PCH)\n"
2703 " protocol - only load ObjC protocols (non-PCH)\n"
2704 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002705 " typedef - only load typdefs (non-PCH)\n"
2706 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002707}
2708
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002709/***/
2710
2711int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002712 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002713 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2714 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002715 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002716 return perform_code_completion(argc, argv, 0);
2717 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2718 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002719 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2720 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002721 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2722 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002723 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2724 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002725 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2726 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002727 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002728 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002729 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002730 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2731 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002732 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002733 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2734 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2735 if (I) {
2736 int trials = atoi(argv[2]);
2737 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2738 NULL);
2739 }
2740 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002741 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002742 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002743
2744 PostVisitTU postVisit = 0;
2745 if (strstr(argv[1], "-memory-usage"))
2746 postVisit = PrintMemoryUsage;
2747
Ted Kremenek7d405622010-01-12 23:34:26 +00002748 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002749 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2750 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002751 }
2752 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002753 return perform_file_scan(argv[2], argv[3],
2754 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002755 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2756 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002757 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2758 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2759 PrintInclusionStack);
2760 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2761 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2762 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002763 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2764 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2765 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002766 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2767 return perform_test_load_source(argc - 2, argv + 2, "all",
2768 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002769 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2770 if (argc > 2)
2771 return print_usrs(argv + 2, argv + argc);
2772 else {
2773 display_usrs();
2774 return 1;
2775 }
2776 }
2777 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2778 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002779 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2780 return write_pch_file(argv[2], argc - 3, argv + 3);
2781
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002782 print_usage();
2783 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002784}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002785
2786/***/
2787
2788/* We intentionally run in a separate thread to ensure we at least minimal
2789 * testing of a multithreaded environment (for example, having a reduced stack
2790 * size). */
2791
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002792typedef struct thread_info {
2793 int argc;
2794 const char **argv;
2795 int result;
2796} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002797void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002798 thread_info *client_data = client_data_v;
2799 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00002800#ifdef __CYGWIN__
2801 fflush(stdout); /* stdout is not flushed on Cygwin. */
2802#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002803}
2804
2805int main(int argc, const char **argv) {
2806 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002807
Douglas Gregor61605982010-10-27 16:00:01 +00002808 if (getenv("CINDEXTEST_NOTHREADS"))
2809 return cindextest_main(argc, argv);
2810
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002811 client_data.argc = argc;
2812 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002813 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002814 return client_data.result;
2815}