blob: a81de2717f7fee6e5a16da4c65b119ac1f06b59d [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) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000670 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000671 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000672 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000673 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
674 if (T.kind != CXType_Invalid) {
675 CXString S = clang_getTypeKindSpelling(T.kind);
676 printf(" %s", clang_getCString(S));
677 clang_disposeString(S);
678 }
679 }
680 printf("]");
681 }
682 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000683 /* Print if this is a non-POD type. */
684 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000685
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000686 printf("\n");
687 }
688 return CXChildVisit_Recurse;
689}
690
691
692/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000693/* Loading ASTs/source. */
694/******************************************************************************/
695
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000696static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000697 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000698 CXCursorVisitor Visitor,
699 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000700
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000701 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000702 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000703
704 if (Visitor) {
705 enum CXCursorKind K = CXCursor_NotImplemented;
706 enum CXCursorKind *ck = &K;
707 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000708
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000709 /* Perform some simple filtering. */
710 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000711 else if (!strcmp(filter, "all-display") ||
712 !strcmp(filter, "local-display")) {
713 ck = NULL;
714 want_display_name = 1;
715 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000716 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000717 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
718 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
719 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
720 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
721 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
722 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
723 else {
724 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
725 return 1;
726 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000727
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000728 Data.TU = TU;
729 Data.Filter = ck;
730 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000731 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000732
Ted Kremenekce2ae882010-01-26 17:59:48 +0000733 if (PV)
734 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000735
Douglas Gregora88084b2010-02-18 18:08:43 +0000736 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000737 if (checkForErrors(TU) != 0) {
738 clang_disposeTranslationUnit(TU);
739 return -1;
740 }
741
Ted Kremenek0d435192009-11-17 18:13:31 +0000742 clang_disposeTranslationUnit(TU);
743 return 0;
744}
745
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000746int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000747 const char *prefix, CXCursorVisitor Visitor,
748 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000749 CXIndex Idx;
750 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000751 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000752 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000753 !strcmp(filter, "local") ? 1 : 0,
754 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000755
Ted Kremenek020a0952010-02-11 07:41:25 +0000756 if (!CreateTranslationUnit(Idx, file, &TU)) {
757 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000758 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000759 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000760
Ted Kremenek020a0952010-02-11 07:41:25 +0000761 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
762 clang_disposeIndex(Idx);
763 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000764}
765
Ted Kremenekce2ae882010-01-26 17:59:48 +0000766int perform_test_load_source(int argc, const char **argv,
767 const char *filter, CXCursorVisitor Visitor,
768 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000769 CXIndex Idx;
770 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000771 struct CXUnsavedFile *unsaved_files = 0;
772 int num_unsaved_files = 0;
773 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000774
Daniel Dunbarada487d2009-12-01 02:03:10 +0000775 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000776 (!strcmp(filter, "local") ||
777 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000778 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000779
Ted Kremenek020a0952010-02-11 07:41:25 +0000780 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
781 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000782 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000783 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000784
Douglas Gregordca8ee82011-05-06 16:33:08 +0000785 TU = clang_parseTranslationUnit(Idx, 0,
786 argv + num_unsaved_files,
787 argc - num_unsaved_files,
788 unsaved_files, num_unsaved_files,
789 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000790 if (!TU) {
791 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000792 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000793 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000794 return 1;
795 }
796
Ted Kremenekce2ae882010-01-26 17:59:48 +0000797 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000798 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000799 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000800 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000801}
802
Douglas Gregorabc563f2010-07-19 21:46:24 +0000803int perform_test_reparse_source(int argc, const char **argv, int trials,
804 const char *filter, CXCursorVisitor Visitor,
805 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000806 CXIndex Idx;
807 CXTranslationUnit TU;
808 struct CXUnsavedFile *unsaved_files = 0;
809 int num_unsaved_files = 0;
810 int result;
811 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000812 int remap_after_trial = 0;
813 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000814
815 Idx = clang_createIndex(/* excludeDeclsFromPCH */
816 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000817 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000818
Douglas Gregorabc563f2010-07-19 21:46:24 +0000819 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
820 clang_disposeIndex(Idx);
821 return -1;
822 }
823
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000824 /* Load the initial translation unit -- we do this without honoring remapped
825 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000826 TU = clang_parseTranslationUnit(Idx, 0,
827 argv + num_unsaved_files,
828 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000829 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000830 if (!TU) {
831 fprintf(stderr, "Unable to load translation unit!\n");
832 free_remapped_files(unsaved_files, num_unsaved_files);
833 clang_disposeIndex(Idx);
834 return 1;
835 }
836
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000837 if (checkForErrors(TU) != 0)
838 return -1;
839
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000840 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
841 remap_after_trial =
842 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
843 }
844
Douglas Gregorabc563f2010-07-19 21:46:24 +0000845 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000846 if (clang_reparseTranslationUnit(TU,
847 trial >= remap_after_trial ? num_unsaved_files : 0,
848 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000849 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000850 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000851 clang_disposeTranslationUnit(TU);
852 free_remapped_files(unsaved_files, num_unsaved_files);
853 clang_disposeIndex(Idx);
854 return -1;
855 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000856
857 if (checkForErrors(TU) != 0)
858 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000859 }
860
861 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000862
Douglas Gregorabc563f2010-07-19 21:46:24 +0000863 free_remapped_files(unsaved_files, num_unsaved_files);
864 clang_disposeIndex(Idx);
865 return result;
866}
867
Ted Kremenek0d435192009-11-17 18:13:31 +0000868/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000869/* Logic for testing clang_getCursor(). */
870/******************************************************************************/
871
Douglas Gregordd3e5542011-05-04 00:14:37 +0000872static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000873 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000874 unsigned end_line, unsigned end_col,
875 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000876 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000877 if (prefix)
878 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000879 PrintExtent(stdout, start_line, start_col, end_line, end_col);
880 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000881 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000882 printf("\n");
883}
884
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000885static int perform_file_scan(const char *ast_file, const char *source_file,
886 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000887 CXIndex Idx;
888 CXTranslationUnit TU;
889 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000890 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000891 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000892 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000893 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000894
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000895 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
896 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000897 fprintf(stderr, "Could not create Index\n");
898 return 1;
899 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000900
Ted Kremenek1c6da172009-11-17 19:37:36 +0000901 if (!CreateTranslationUnit(Idx, ast_file, &TU))
902 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000903
Ted Kremenek1c6da172009-11-17 19:37:36 +0000904 if ((fp = fopen(source_file, "r")) == NULL) {
905 fprintf(stderr, "Could not open '%s'\n", source_file);
906 return 1;
907 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000908
Douglas Gregorb9790342010-01-22 21:44:22 +0000909 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000910 for (;;) {
911 CXCursor cursor;
912 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000913
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000914 if (c == '\n') {
915 ++line;
916 col = 1;
917 } else
918 ++col;
919
920 /* Check the cursor at this position, and dump the previous one if we have
921 * found something new.
922 */
923 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
924 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
925 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000926 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000927 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000928 start_line = line;
929 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000930 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000931 if (c == EOF)
932 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000933
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000934 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000935 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000936
Ted Kremenek1c6da172009-11-17 19:37:36 +0000937 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000938 clang_disposeTranslationUnit(TU);
939 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000940 return 0;
941}
942
943/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000944/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000945/******************************************************************************/
946
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000947/* Parse file:line:column from the input string. Returns 0 on success, non-zero
948 on failure. If successful, the pointer *filename will contain newly-allocated
949 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000950int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 unsigned *column, unsigned *second_line,
952 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000953 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000954 const char *last_colon = strrchr(input, ':');
955 unsigned values[4], i;
956 unsigned num_values = (second_line && second_column)? 4 : 2;
957
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000958 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000959 if (!last_colon || last_colon == input) {
960 if (num_values == 4)
961 fprintf(stderr, "could not parse filename:line:column:line:column in "
962 "'%s'\n", input);
963 else
964 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000965 return 1;
966 }
967
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000968 for (i = 0; i != num_values; ++i) {
969 const char *prev_colon;
970
971 /* Parse the next line or column. */
972 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
973 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000974 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000975 (i % 2 ? "column" : "line"), input);
976 return 1;
977 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000978
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000979 if (i + 1 == num_values)
980 break;
981
982 /* Find the previous colon. */
983 prev_colon = last_colon - 1;
984 while (prev_colon != input && *prev_colon != ':')
985 --prev_colon;
986 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000987 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000988 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000989 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000990 }
991
992 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000993 }
994
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000995 *line = values[0];
996 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000997
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000998 if (second_line && second_column) {
999 *second_line = values[2];
1000 *second_column = values[3];
1001 }
1002
Douglas Gregor88d23952009-11-09 18:19:57 +00001003 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001004 *filename = (char*)malloc(last_colon - input + 1);
1005 memcpy(*filename, input, last_colon - input);
1006 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001007 return 0;
1008}
1009
1010const char *
1011clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1012 switch (Kind) {
1013 case CXCompletionChunk_Optional: return "Optional";
1014 case CXCompletionChunk_TypedText: return "TypedText";
1015 case CXCompletionChunk_Text: return "Text";
1016 case CXCompletionChunk_Placeholder: return "Placeholder";
1017 case CXCompletionChunk_Informative: return "Informative";
1018 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1019 case CXCompletionChunk_LeftParen: return "LeftParen";
1020 case CXCompletionChunk_RightParen: return "RightParen";
1021 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1022 case CXCompletionChunk_RightBracket: return "RightBracket";
1023 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1024 case CXCompletionChunk_RightBrace: return "RightBrace";
1025 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1026 case CXCompletionChunk_RightAngle: return "RightAngle";
1027 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001028 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001029 case CXCompletionChunk_Colon: return "Colon";
1030 case CXCompletionChunk_SemiColon: return "SemiColon";
1031 case CXCompletionChunk_Equal: return "Equal";
1032 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1033 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001034 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001035
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001036 return "Unknown";
1037}
1038
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001039static int checkForErrors(CXTranslationUnit TU) {
1040 unsigned Num, i;
1041 CXDiagnostic Diag;
1042 CXString DiagStr;
1043
1044 if (!getenv("CINDEXTEST_FAILONERROR"))
1045 return 0;
1046
1047 Num = clang_getNumDiagnostics(TU);
1048 for (i = 0; i != Num; ++i) {
1049 Diag = clang_getDiagnostic(TU, i);
1050 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1051 DiagStr = clang_formatDiagnostic(Diag,
1052 clang_defaultDiagnosticDisplayOptions());
1053 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1054 clang_disposeString(DiagStr);
1055 clang_disposeDiagnostic(Diag);
1056 return -1;
1057 }
1058 clang_disposeDiagnostic(Diag);
1059 }
1060
1061 return 0;
1062}
1063
Douglas Gregor3ac73852009-11-09 16:04:45 +00001064void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001065 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001066
Douglas Gregor3ac73852009-11-09 16:04:45 +00001067 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001068 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001069 CXString text;
1070 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001071 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001072 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001073
Douglas Gregor3ac73852009-11-09 16:04:45 +00001074 if (Kind == CXCompletionChunk_Optional) {
1075 fprintf(file, "{Optional ");
1076 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001077 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001078 file);
1079 fprintf(file, "}");
1080 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001081 }
1082
1083 if (Kind == CXCompletionChunk_VerticalSpace) {
1084 fprintf(file, "{VerticalSpace }");
1085 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001086 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001087
Douglas Gregord5a20892009-11-09 17:05:28 +00001088 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001089 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001090 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001091 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001092 cstr ? cstr : "");
1093 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001094 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001095
Douglas Gregor3ac73852009-11-09 16:04:45 +00001096}
1097
1098void print_completion_result(CXCompletionResult *completion_result,
1099 CXClientData client_data) {
1100 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001101 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001102 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001103 enum CXCursorKind ParentKind;
1104 CXString ParentName;
1105
Ted Kremeneke68fff62010-02-17 00:41:32 +00001106 fprintf(file, "%s:", clang_getCString(ks));
1107 clang_disposeString(ks);
1108
Douglas Gregor3ac73852009-11-09 16:04:45 +00001109 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001110 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001111 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001112 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1113 case CXAvailability_Available:
1114 break;
1115
1116 case CXAvailability_Deprecated:
1117 fprintf(file, " (deprecated)");
1118 break;
1119
1120 case CXAvailability_NotAvailable:
1121 fprintf(file, " (unavailable)");
1122 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001123
1124 case CXAvailability_NotAccessible:
1125 fprintf(file, " (inaccessible)");
1126 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001127 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001128
1129 annotationCount = clang_getCompletionNumAnnotations(
1130 completion_result->CompletionString);
1131 if (annotationCount) {
1132 unsigned i;
1133 fprintf(file, " (");
1134 for (i = 0; i < annotationCount; ++i) {
1135 if (i != 0)
1136 fprintf(file, ", ");
1137 fprintf(file, "\"%s\"",
1138 clang_getCString(clang_getCompletionAnnotation(
1139 completion_result->CompletionString, i)));
1140 }
1141 fprintf(file, ")");
1142 }
1143
Douglas Gregorba103062012-03-27 23:34:16 +00001144 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1145 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1146 &ParentKind);
1147 if (ParentKind != CXCursor_NotImplemented) {
1148 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1149 fprintf(file, " (parent: %s '%s')",
1150 clang_getCString(KindSpelling),
1151 clang_getCString(ParentName));
1152 clang_disposeString(KindSpelling);
1153 }
1154 clang_disposeString(ParentName);
1155 }
1156
Douglas Gregor58ddb602010-08-23 23:00:57 +00001157 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001158}
1159
Douglas Gregor3da626b2011-07-07 16:03:39 +00001160void print_completion_contexts(unsigned long long contexts, FILE *file) {
1161 fprintf(file, "Completion contexts:\n");
1162 if (contexts == CXCompletionContext_Unknown) {
1163 fprintf(file, "Unknown\n");
1164 }
1165 if (contexts & CXCompletionContext_AnyType) {
1166 fprintf(file, "Any type\n");
1167 }
1168 if (contexts & CXCompletionContext_AnyValue) {
1169 fprintf(file, "Any value\n");
1170 }
1171 if (contexts & CXCompletionContext_ObjCObjectValue) {
1172 fprintf(file, "Objective-C object value\n");
1173 }
1174 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1175 fprintf(file, "Objective-C selector value\n");
1176 }
1177 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1178 fprintf(file, "C++ class type value\n");
1179 }
1180 if (contexts & CXCompletionContext_DotMemberAccess) {
1181 fprintf(file, "Dot member access\n");
1182 }
1183 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1184 fprintf(file, "Arrow member access\n");
1185 }
1186 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1187 fprintf(file, "Objective-C property access\n");
1188 }
1189 if (contexts & CXCompletionContext_EnumTag) {
1190 fprintf(file, "Enum tag\n");
1191 }
1192 if (contexts & CXCompletionContext_UnionTag) {
1193 fprintf(file, "Union tag\n");
1194 }
1195 if (contexts & CXCompletionContext_StructTag) {
1196 fprintf(file, "Struct tag\n");
1197 }
1198 if (contexts & CXCompletionContext_ClassTag) {
1199 fprintf(file, "Class name\n");
1200 }
1201 if (contexts & CXCompletionContext_Namespace) {
1202 fprintf(file, "Namespace or namespace alias\n");
1203 }
1204 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1205 fprintf(file, "Nested name specifier\n");
1206 }
1207 if (contexts & CXCompletionContext_ObjCInterface) {
1208 fprintf(file, "Objective-C interface\n");
1209 }
1210 if (contexts & CXCompletionContext_ObjCProtocol) {
1211 fprintf(file, "Objective-C protocol\n");
1212 }
1213 if (contexts & CXCompletionContext_ObjCCategory) {
1214 fprintf(file, "Objective-C category\n");
1215 }
1216 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1217 fprintf(file, "Objective-C instance method\n");
1218 }
1219 if (contexts & CXCompletionContext_ObjCClassMessage) {
1220 fprintf(file, "Objective-C class method\n");
1221 }
1222 if (contexts & CXCompletionContext_ObjCSelectorName) {
1223 fprintf(file, "Objective-C selector name\n");
1224 }
1225 if (contexts & CXCompletionContext_MacroName) {
1226 fprintf(file, "Macro name\n");
1227 }
1228 if (contexts & CXCompletionContext_NaturalLanguage) {
1229 fprintf(file, "Natural language\n");
1230 }
1231}
1232
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001233int my_stricmp(const char *s1, const char *s2) {
1234 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001235 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001236 if (c1 < c2)
1237 return -1;
1238 else if (c1 > c2)
1239 return 1;
1240
1241 ++s1;
1242 ++s2;
1243 }
1244
1245 if (*s1)
1246 return 1;
1247 else if (*s2)
1248 return -1;
1249 return 0;
1250}
1251
Douglas Gregor1982c182010-07-12 18:38:41 +00001252int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001253 const char *input = argv[1];
1254 char *filename = 0;
1255 unsigned line;
1256 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001257 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001258 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001259 struct CXUnsavedFile *unsaved_files = 0;
1260 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001261 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001262 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001263 unsigned I, Repeats = 1;
1264 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1265
1266 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1267 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001268
Douglas Gregor1982c182010-07-12 18:38:41 +00001269 if (timing_only)
1270 input += strlen("-code-completion-timing=");
1271 else
1272 input += strlen("-code-completion-at=");
1273
Ted Kremeneke68fff62010-02-17 00:41:32 +00001274 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001275 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001276 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001277
Douglas Gregor735df882009-12-02 09:21:34 +00001278 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1279 return -1;
1280
Douglas Gregor32be4a52010-10-11 21:37:58 +00001281 CIdx = clang_createIndex(0, 0);
1282
1283 if (getenv("CINDEXTEST_EDITING"))
1284 Repeats = 5;
1285
1286 TU = clang_parseTranslationUnit(CIdx, 0,
1287 argv + num_unsaved_files + 2,
1288 argc - num_unsaved_files - 2,
1289 0, 0, getDefaultParsingOptions());
1290 if (!TU) {
1291 fprintf(stderr, "Unable to load translation unit!\n");
1292 return 1;
1293 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001294
1295 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1296 fprintf(stderr, "Unable to reparse translation init!\n");
1297 return 1;
1298 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001299
1300 for (I = 0; I != Repeats; ++I) {
1301 results = clang_codeCompleteAt(TU, filename, line, column,
1302 unsaved_files, num_unsaved_files,
1303 completionOptions);
1304 if (!results) {
1305 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001306 return 1;
1307 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001308 if (I != Repeats-1)
1309 clang_disposeCodeCompleteResults(results);
1310 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001311
Douglas Gregorec6762c2009-12-18 16:20:58 +00001312 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001313 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001314 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001315 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001316 CXString objCSelector;
1317 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001318 if (!timing_only) {
1319 /* Sort the code-completion results based on the typed text. */
1320 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1321
Douglas Gregor1982c182010-07-12 18:38:41 +00001322 for (i = 0; i != n; ++i)
1323 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001324 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001325 n = clang_codeCompleteGetNumDiagnostics(results);
1326 for (i = 0; i != n; ++i) {
1327 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1328 PrintDiagnostic(diag);
1329 clang_disposeDiagnostic(diag);
1330 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001331
1332 contexts = clang_codeCompleteGetContexts(results);
1333 print_completion_contexts(contexts, stdout);
1334
Douglas Gregor0a47d692011-07-26 15:24:30 +00001335 containerKind = clang_codeCompleteGetContainerKind(results,
1336 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001337
1338 if (containerKind != CXCursor_InvalidCode) {
1339 /* We have found a container */
1340 CXString containerUSR, containerKindSpelling;
1341 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1342 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1343 clang_disposeString(containerKindSpelling);
1344
1345 if (containerIsIncomplete) {
1346 printf("Container is incomplete\n");
1347 }
1348 else {
1349 printf("Container is complete\n");
1350 }
1351
1352 containerUSR = clang_codeCompleteGetContainerUSR(results);
1353 printf("Container USR: %s\n", clang_getCString(containerUSR));
1354 clang_disposeString(containerUSR);
1355 }
1356
Douglas Gregor0a47d692011-07-26 15:24:30 +00001357 objCSelector = clang_codeCompleteGetObjCSelector(results);
1358 selectorString = clang_getCString(objCSelector);
1359 if (selectorString && strlen(selectorString) > 0) {
1360 printf("Objective-C selector: %s\n", selectorString);
1361 }
1362 clang_disposeString(objCSelector);
1363
Douglas Gregorec6762c2009-12-18 16:20:58 +00001364 clang_disposeCodeCompleteResults(results);
1365 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001366 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001367 clang_disposeIndex(CIdx);
1368 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001369
Douglas Gregor735df882009-12-02 09:21:34 +00001370 free_remapped_files(unsaved_files, num_unsaved_files);
1371
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001372 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001373}
1374
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001375typedef struct {
1376 char *filename;
1377 unsigned line;
1378 unsigned column;
1379} CursorSourceLocation;
1380
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001381static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001382 CXIndex CIdx;
1383 int errorCode;
1384 struct CXUnsavedFile *unsaved_files = 0;
1385 int num_unsaved_files = 0;
1386 CXTranslationUnit TU;
1387 CXCursor Cursor;
1388 CursorSourceLocation *Locations = 0;
1389 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001390 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001391 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001392
Ted Kremeneke68fff62010-02-17 00:41:32 +00001393 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001394 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1395 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001396
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001397 /* Parse the locations. */
1398 assert(NumLocations > 0 && "Unable to count locations?");
1399 Locations = (CursorSourceLocation *)malloc(
1400 NumLocations * sizeof(CursorSourceLocation));
1401 for (Loc = 0; Loc < NumLocations; ++Loc) {
1402 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001403 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1404 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001405 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001406 return errorCode;
1407 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001408
1409 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001410 &num_unsaved_files))
1411 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001412
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001413 if (getenv("CINDEXTEST_EDITING"))
1414 Repeats = 5;
1415
1416 /* Parse the translation unit. When we're testing clang_getCursor() after
1417 reparsing, don't remap unsaved files until the second parse. */
1418 CIdx = clang_createIndex(1, 1);
1419 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1420 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001421 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001422 unsaved_files,
1423 Repeats > 1? 0 : num_unsaved_files,
1424 getDefaultParsingOptions());
1425
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001426 if (!TU) {
1427 fprintf(stderr, "unable to parse input\n");
1428 return -1;
1429 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001430
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001431 if (checkForErrors(TU) != 0)
1432 return -1;
1433
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001434 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001435 if (Repeats > 1 &&
1436 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1437 clang_defaultReparseOptions(TU))) {
1438 clang_disposeTranslationUnit(TU);
1439 return 1;
1440 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001441
1442 if (checkForErrors(TU) != 0)
1443 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001444
1445 for (Loc = 0; Loc < NumLocations; ++Loc) {
1446 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1447 if (!file)
1448 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001449
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001450 Cursor = clang_getCursor(TU,
1451 clang_getLocation(TU, file, Locations[Loc].line,
1452 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001453
1454 if (checkForErrors(TU) != 0)
1455 return -1;
1456
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001457 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001458 CXCompletionString completionString = clang_getCursorCompletionString(
1459 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001460 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1461 CXString Spelling;
1462 const char *cspell;
1463 unsigned line, column;
1464 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1465 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001466 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001467 PrintCursorExtent(Cursor);
1468 Spelling = clang_getCursorSpelling(Cursor);
1469 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001470 if (cspell && strlen(cspell) != 0) {
1471 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001472 printf(" Spelling=%s (", cspell);
1473 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001474 CXSourceRange range =
1475 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001476 if (clang_Range_isNull(range))
1477 break;
1478 PrintRange(range, 0);
1479 }
1480 printf(")");
1481 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001482 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001483 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1484 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001485 if (completionString != NULL) {
1486 printf("\nCompletion string: ");
1487 print_completion_string(completionString, stdout);
1488 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001489 printf("\n");
1490 free(Locations[Loc].filename);
1491 }
1492 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001493 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001494
Douglas Gregora88084b2010-02-18 18:08:43 +00001495 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001496 clang_disposeTranslationUnit(TU);
1497 clang_disposeIndex(CIdx);
1498 free(Locations);
1499 free_remapped_files(unsaved_files, num_unsaved_files);
1500 return 0;
1501}
1502
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001503static enum CXVisitorResult findFileRefsVisit(void *context,
1504 CXCursor cursor, CXSourceRange range) {
1505 if (clang_Range_isNull(range))
1506 return CXVisit_Continue;
1507
1508 PrintCursor(cursor);
1509 PrintRange(range, "");
1510 printf("\n");
1511 return CXVisit_Continue;
1512}
1513
1514static int find_file_refs_at(int argc, const char **argv) {
1515 CXIndex CIdx;
1516 int errorCode;
1517 struct CXUnsavedFile *unsaved_files = 0;
1518 int num_unsaved_files = 0;
1519 CXTranslationUnit TU;
1520 CXCursor Cursor;
1521 CursorSourceLocation *Locations = 0;
1522 unsigned NumLocations = 0, Loc;
1523 unsigned Repeats = 1;
1524 unsigned I;
1525
1526 /* Count the number of locations. */
1527 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1528 ++NumLocations;
1529
1530 /* Parse the locations. */
1531 assert(NumLocations > 0 && "Unable to count locations?");
1532 Locations = (CursorSourceLocation *)malloc(
1533 NumLocations * sizeof(CursorSourceLocation));
1534 for (Loc = 0; Loc < NumLocations; ++Loc) {
1535 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1536 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1537 &Locations[Loc].line,
1538 &Locations[Loc].column, 0, 0)))
1539 return errorCode;
1540 }
1541
1542 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1543 &num_unsaved_files))
1544 return -1;
1545
1546 if (getenv("CINDEXTEST_EDITING"))
1547 Repeats = 5;
1548
1549 /* Parse the translation unit. When we're testing clang_getCursor() after
1550 reparsing, don't remap unsaved files until the second parse. */
1551 CIdx = clang_createIndex(1, 1);
1552 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1553 argv + num_unsaved_files + 1 + NumLocations,
1554 argc - num_unsaved_files - 2 - NumLocations,
1555 unsaved_files,
1556 Repeats > 1? 0 : num_unsaved_files,
1557 getDefaultParsingOptions());
1558
1559 if (!TU) {
1560 fprintf(stderr, "unable to parse input\n");
1561 return -1;
1562 }
1563
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001564 if (checkForErrors(TU) != 0)
1565 return -1;
1566
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001567 for (I = 0; I != Repeats; ++I) {
1568 if (Repeats > 1 &&
1569 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1570 clang_defaultReparseOptions(TU))) {
1571 clang_disposeTranslationUnit(TU);
1572 return 1;
1573 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001574
1575 if (checkForErrors(TU) != 0)
1576 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001577
1578 for (Loc = 0; Loc < NumLocations; ++Loc) {
1579 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1580 if (!file)
1581 continue;
1582
1583 Cursor = clang_getCursor(TU,
1584 clang_getLocation(TU, file, Locations[Loc].line,
1585 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001586
1587 if (checkForErrors(TU) != 0)
1588 return -1;
1589
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001590 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001591 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001592 PrintCursor(Cursor);
1593 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001594 clang_findReferencesInFile(Cursor, file, visitor);
1595 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001596
1597 if (checkForErrors(TU) != 0)
1598 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001599 }
1600 }
1601 }
1602
1603 PrintDiagnostics(TU);
1604 clang_disposeTranslationUnit(TU);
1605 clang_disposeIndex(CIdx);
1606 free(Locations);
1607 free_remapped_files(unsaved_files, num_unsaved_files);
1608 return 0;
1609}
1610
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001611typedef struct {
1612 const char *check_prefix;
1613 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001614 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001615 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001616 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001617} IndexData;
1618
1619static void printCheck(IndexData *data) {
1620 if (data->check_prefix) {
1621 if (data->first_check_printed) {
1622 printf("// %s-NEXT: ", data->check_prefix);
1623 } else {
1624 printf("// %s : ", data->check_prefix);
1625 data->first_check_printed = 1;
1626 }
1627 }
1628}
1629
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001630static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001631 CXString filename = clang_getFileName((CXFile)file);
1632 printf("%s", clang_getCString(filename));
1633 clang_disposeString(filename);
1634}
1635
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001636static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1637 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001638 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001639 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001640 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001641 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001642 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001643
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001644 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001645 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1646 if (line == 0) {
1647 printf("<null loc>");
1648 return;
1649 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001650 if (!file) {
1651 printf("<no idxfile>");
1652 return;
1653 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001654 filename = clang_getFileName((CXFile)file);
1655 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001656 if (strcmp(cname, index_data->main_filename) == 0)
1657 isMainFile = 1;
1658 else
1659 isMainFile = 0;
1660 clang_disposeString(filename);
1661
1662 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001663 printCXIndexFile(file);
1664 printf(":");
1665 }
1666 printf("%d:%d", line, column);
1667}
1668
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001669static unsigned digitCount(unsigned val) {
1670 unsigned c = 1;
1671 while (1) {
1672 if (val < 10)
1673 return c;
1674 ++c;
1675 val /= 10;
1676 }
1677}
1678
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001679static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1680 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001681 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001682 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001683 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001684 unsigned line, column;
1685
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001686 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001687 if (!name)
1688 name = "<anon-tag>";
1689
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001690 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001691 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001692 newStr = (char *)malloc(strlen(name) +
1693 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001694 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001695 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001696}
1697
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001698static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1699 CXIdxClientContainer container;
1700 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001701 if (!container)
1702 printf("[<<NULL>>]");
1703 else
1704 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001705}
1706
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001707static const char *getEntityKindString(CXIdxEntityKind kind) {
1708 switch (kind) {
1709 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1710 case CXIdxEntity_Typedef: return "typedef";
1711 case CXIdxEntity_Function: return "function";
1712 case CXIdxEntity_Variable: return "variable";
1713 case CXIdxEntity_Field: return "field";
1714 case CXIdxEntity_EnumConstant: return "enumerator";
1715 case CXIdxEntity_ObjCClass: return "objc-class";
1716 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1717 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001718 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1719 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001720 case CXIdxEntity_ObjCProperty: return "objc-property";
1721 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1722 case CXIdxEntity_Enum: return "enum";
1723 case CXIdxEntity_Struct: return "struct";
1724 case CXIdxEntity_Union: return "union";
1725 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001726 case CXIdxEntity_CXXNamespace: return "namespace";
1727 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1728 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1729 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1730 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1731 case CXIdxEntity_CXXConstructor: return "constructor";
1732 case CXIdxEntity_CXXDestructor: return "destructor";
1733 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1734 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1735 }
1736 assert(0 && "Garbage entity kind");
1737 return 0;
1738}
1739
1740static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1741 switch (kind) {
1742 case CXIdxEntity_NonTemplate: return "";
1743 case CXIdxEntity_Template: return "-template";
1744 case CXIdxEntity_TemplatePartialSpecialization:
1745 return "-template-partial-spec";
1746 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001747 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001748 assert(0 && "Garbage entity kind");
1749 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001750}
1751
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001752static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1753 switch (kind) {
1754 case CXIdxEntityLang_None: return "<none>";
1755 case CXIdxEntityLang_C: return "C";
1756 case CXIdxEntityLang_ObjC: return "ObjC";
1757 case CXIdxEntityLang_CXX: return "C++";
1758 }
1759 assert(0 && "Garbage language kind");
1760 return 0;
1761}
1762
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001763static void printEntityInfo(const char *cb,
1764 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001765 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001766 const char *name;
1767 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001768 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001769 index_data = (IndexData *)client_data;
1770 printCheck(index_data);
1771
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001772 if (!info) {
1773 printf("%s: <<NULL>>", cb);
1774 return;
1775 }
1776
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001777 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001778 if (!name)
1779 name = "<anon-tag>";
1780
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001781 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1782 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001783 printf(" | name: %s", name);
1784 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001785 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001786
1787 for (i = 0; i != info->numAttributes; ++i) {
1788 const CXIdxAttrInfo *Attr = info->attributes[i];
1789 printf(" <attribute>: ");
1790 PrintCursor(Attr->cursor);
1791 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001792}
1793
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001794static void printBaseClassInfo(CXClientData client_data,
1795 const CXIdxBaseClassInfo *info) {
1796 printEntityInfo(" <base>", client_data, info->base);
1797 printf(" | cursor: ");
1798 PrintCursor(info->cursor);
1799 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001800 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001801}
1802
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001803static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1804 CXClientData client_data) {
1805 unsigned i;
1806 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1807 printEntityInfo(" <protocol>", client_data,
1808 ProtoInfo->protocols[i]->protocol);
1809 printf(" | cursor: ");
1810 PrintCursor(ProtoInfo->protocols[i]->cursor);
1811 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001812 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001813 printf("\n");
1814 }
1815}
1816
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001817static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001818 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001819 CXString str;
1820 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001821 unsigned numDiags, i;
1822 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001823 IndexData *index_data;
1824 index_data = (IndexData *)client_data;
1825 printCheck(index_data);
1826
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001827 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1828 for (i = 0; i != numDiags; ++i) {
1829 diag = clang_getDiagnosticInSet(diagSet, i);
1830 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1831 cstr = clang_getCString(str);
1832 printf("[diagnostic]: %s\n", cstr);
1833 clang_disposeString(str);
1834
1835 if (getenv("CINDEXTEST_FAILONERROR") &&
1836 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1837 index_data->fail_for_error = 1;
1838 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001839 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001840}
1841
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001842static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1843 CXFile file, void *reserved) {
1844 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001845 CXString filename;
1846
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001847 index_data = (IndexData *)client_data;
1848 printCheck(index_data);
1849
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001850 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001851 index_data->main_filename = clang_getCString(filename);
1852 clang_disposeString(filename);
1853
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001854 printf("[enteredMainFile]: ");
1855 printCXIndexFile((CXIdxClientFile)file);
1856 printf("\n");
1857
1858 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001859}
1860
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001861static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001862 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001863 IndexData *index_data;
1864 index_data = (IndexData *)client_data;
1865 printCheck(index_data);
1866
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001867 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001868 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001869 printf(" | name: \"%s\"", info->filename);
1870 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001871 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001872 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001873
1874 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001875}
1876
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001877static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001878 void *reserved) {
1879 IndexData *index_data;
1880 index_data = (IndexData *)client_data;
1881 printCheck(index_data);
1882
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001883 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001884 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001885}
1886
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001887static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001888 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001889 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001890 const CXIdxObjCCategoryDeclInfo *CatInfo;
1891 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001892 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001893 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001894 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001895 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001896 index_data = (IndexData *)client_data;
1897
1898 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1899 printf(" | cursor: ");
1900 PrintCursor(info->cursor);
1901 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001902 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001903 printf(" | semantic-container: ");
1904 printCXIndexContainer(info->semanticContainer);
1905 printf(" | lexical-container: ");
1906 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001907 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001908 printf(" | isDef: %d", info->isDefinition);
1909 printf(" | isContainer: %d", info->isContainer);
1910 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001911
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001912 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001913 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001914 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001915 PrintCursor(Attr->cursor);
1916 printf("\n");
1917 }
1918
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001919 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1920 const char *kindName = 0;
1921 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1922 switch (K) {
1923 case CXIdxObjCContainer_ForwardRef:
1924 kindName = "forward-ref"; break;
1925 case CXIdxObjCContainer_Interface:
1926 kindName = "interface"; break;
1927 case CXIdxObjCContainer_Implementation:
1928 kindName = "implementation"; break;
1929 }
1930 printCheck(index_data);
1931 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1932 }
1933
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001934 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001935 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1936 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001937 printf(" | cursor: ");
1938 PrintCursor(CatInfo->classCursor);
1939 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001940 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001941 printf("\n");
1942 }
1943
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001944 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1945 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001946 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001947 printf("\n");
1948 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001949 }
1950
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001951 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1952 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001953 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001954
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001955 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
1956 if (PropInfo->getter) {
1957 printEntityInfo(" <getter>", client_data, PropInfo->getter);
1958 printf("\n");
1959 }
1960 if (PropInfo->setter) {
1961 printEntityInfo(" <setter>", client_data, PropInfo->setter);
1962 printf("\n");
1963 }
1964 }
1965
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001966 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1967 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1968 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1969 printf("\n");
1970 }
1971 }
1972
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001973 if (info->declAsContainer)
1974 clang_index_setClientContainer(info->declAsContainer,
1975 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001976}
1977
1978static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001979 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001980 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001981 printf(" | cursor: ");
1982 PrintCursor(info->cursor);
1983 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001984 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001985 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001986 printf(" | container: ");
1987 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001988 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001989 switch (info->kind) {
1990 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001991 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001992 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001993 printf("\n");
1994}
1995
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001996static int index_abortQuery(CXClientData client_data, void *reserved) {
1997 IndexData *index_data;
1998 index_data = (IndexData *)client_data;
1999 return index_data->abort;
2000}
2001
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002002static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002003 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002004 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002005 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002006 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002007 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002008 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002009 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002010 index_indexEntityReference
2011};
2012
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002013static unsigned getIndexOptions(void) {
2014 unsigned index_opts;
2015 index_opts = 0;
2016 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2017 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2018 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2019 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2020
2021 return index_opts;
2022}
2023
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002024static int index_file(int argc, const char **argv) {
2025 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002026 CXIndex Idx;
2027 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002028 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002029 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002030 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002031
2032 check_prefix = 0;
2033 if (argc > 0) {
2034 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2035 check_prefix = argv[0] + strlen("-check-prefix=");
2036 ++argv;
2037 --argc;
2038 }
2039 }
2040
2041 if (argc == 0) {
2042 fprintf(stderr, "no compiler arguments\n");
2043 return -1;
2044 }
2045
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002046 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2047 /* displayDiagnosics=*/1))) {
2048 fprintf(stderr, "Could not create Index\n");
2049 return 1;
2050 }
2051 idxAction = 0;
2052 result = 1;
2053
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002054 index_data.check_prefix = check_prefix;
2055 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002056 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002057 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002058
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002059 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002060 idxAction = clang_IndexAction_create(Idx);
2061 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002062 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002063 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002064 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002065 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002066
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002067 clang_IndexAction_dispose(idxAction);
2068 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002069 return result;
2070}
2071
2072static int index_tu(int argc, const char **argv) {
2073 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002074 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002075 CXTranslationUnit TU;
2076 const char *check_prefix;
2077 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002078 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002079 int result;
2080
2081 check_prefix = 0;
2082 if (argc > 0) {
2083 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2084 check_prefix = argv[0] + strlen("-check-prefix=");
2085 ++argv;
2086 --argc;
2087 }
2088 }
2089
2090 if (argc == 0) {
2091 fprintf(stderr, "no ast file\n");
2092 return -1;
2093 }
2094
2095 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2096 /* displayDiagnosics=*/1))) {
2097 fprintf(stderr, "Could not create Index\n");
2098 return 1;
2099 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002100 idxAction = 0;
2101 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002102
2103 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002104 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002105
2106 index_data.check_prefix = check_prefix;
2107 index_data.first_check_printed = 0;
2108 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002109 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002110
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002111 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002112 idxAction = clang_IndexAction_create(Idx);
2113 result = clang_indexTranslationUnit(idxAction, &index_data,
2114 &IndexCB,sizeof(IndexCB),
2115 index_opts, TU);
2116 if (index_data.fail_for_error)
2117 goto finished;
2118
2119 finished:
2120 clang_IndexAction_dispose(idxAction);
2121 clang_disposeIndex(Idx);
2122
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002123 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002124}
2125
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002126int perform_token_annotation(int argc, const char **argv) {
2127 const char *input = argv[1];
2128 char *filename = 0;
2129 unsigned line, second_line;
2130 unsigned column, second_column;
2131 CXIndex CIdx;
2132 CXTranslationUnit TU = 0;
2133 int errorCode;
2134 struct CXUnsavedFile *unsaved_files = 0;
2135 int num_unsaved_files = 0;
2136 CXToken *tokens;
2137 unsigned num_tokens;
2138 CXSourceRange range;
2139 CXSourceLocation startLoc, endLoc;
2140 CXFile file = 0;
2141 CXCursor *cursors = 0;
2142 unsigned i;
2143
2144 input += strlen("-test-annotate-tokens=");
2145 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2146 &second_line, &second_column)))
2147 return errorCode;
2148
2149 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2150 return -1;
2151
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002152 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002153 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2154 argv + num_unsaved_files + 2,
2155 argc - num_unsaved_files - 3,
2156 unsaved_files,
2157 num_unsaved_files,
2158 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002159 if (!TU) {
2160 fprintf(stderr, "unable to parse input\n");
2161 clang_disposeIndex(CIdx);
2162 free(filename);
2163 free_remapped_files(unsaved_files, num_unsaved_files);
2164 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002165 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002166 errorCode = 0;
2167
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002168 if (checkForErrors(TU) != 0)
2169 return -1;
2170
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002171 if (getenv("CINDEXTEST_EDITING")) {
2172 for (i = 0; i < 5; ++i) {
2173 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2174 clang_defaultReparseOptions(TU))) {
2175 fprintf(stderr, "Unable to reparse translation unit!\n");
2176 errorCode = -1;
2177 goto teardown;
2178 }
2179 }
2180 }
2181
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002182 if (checkForErrors(TU) != 0) {
2183 errorCode = -1;
2184 goto teardown;
2185 }
2186
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002187 file = clang_getFile(TU, filename);
2188 if (!file) {
2189 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2190 errorCode = -1;
2191 goto teardown;
2192 }
2193
2194 startLoc = clang_getLocation(TU, file, line, column);
2195 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002196 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002197 column);
2198 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002199 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002200 }
2201
2202 endLoc = clang_getLocation(TU, file, second_line, second_column);
2203 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002204 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002205 second_line, second_column);
2206 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002207 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002208 }
2209
2210 range = clang_getRange(startLoc, endLoc);
2211 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002212
2213 if (checkForErrors(TU) != 0) {
2214 errorCode = -1;
2215 goto teardown;
2216 }
2217
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002218 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2219 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002220
2221 if (checkForErrors(TU) != 0) {
2222 errorCode = -1;
2223 goto teardown;
2224 }
2225
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002226 for (i = 0; i != num_tokens; ++i) {
2227 const char *kind = "<unknown>";
2228 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2229 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2230 unsigned start_line, start_column, end_line, end_column;
2231
2232 switch (clang_getTokenKind(tokens[i])) {
2233 case CXToken_Punctuation: kind = "Punctuation"; break;
2234 case CXToken_Keyword: kind = "Keyword"; break;
2235 case CXToken_Identifier: kind = "Identifier"; break;
2236 case CXToken_Literal: kind = "Literal"; break;
2237 case CXToken_Comment: kind = "Comment"; break;
2238 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002239 clang_getSpellingLocation(clang_getRangeStart(extent),
2240 0, &start_line, &start_column, 0);
2241 clang_getSpellingLocation(clang_getRangeEnd(extent),
2242 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002243 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2244 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002245 if (!clang_isInvalid(cursors[i].kind)) {
2246 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002247 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002248 }
2249 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002250 }
2251 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002252 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002253
2254 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002255 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002256 clang_disposeTranslationUnit(TU);
2257 clang_disposeIndex(CIdx);
2258 free(filename);
2259 free_remapped_files(unsaved_files, num_unsaved_files);
2260 return errorCode;
2261}
2262
Ted Kremenek0d435192009-11-17 18:13:31 +00002263/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002264/* USR printing. */
2265/******************************************************************************/
2266
2267static int insufficient_usr(const char *kind, const char *usage) {
2268 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2269 return 1;
2270}
2271
2272static unsigned isUSR(const char *s) {
2273 return s[0] == 'c' && s[1] == ':';
2274}
2275
2276static int not_usr(const char *s, const char *arg) {
2277 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2278 return 1;
2279}
2280
2281static void print_usr(CXString usr) {
2282 const char *s = clang_getCString(usr);
2283 printf("%s\n", s);
2284 clang_disposeString(usr);
2285}
2286
2287static void display_usrs() {
2288 fprintf(stderr, "-print-usrs options:\n"
2289 " ObjCCategory <class name> <category name>\n"
2290 " ObjCClass <class name>\n"
2291 " ObjCIvar <ivar name> <class USR>\n"
2292 " ObjCMethod <selector> [0=class method|1=instance method] "
2293 "<class USR>\n"
2294 " ObjCProperty <property name> <class USR>\n"
2295 " ObjCProtocol <protocol name>\n");
2296}
2297
2298int print_usrs(const char **I, const char **E) {
2299 while (I != E) {
2300 const char *kind = *I;
2301 unsigned len = strlen(kind);
2302 switch (len) {
2303 case 8:
2304 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2305 if (I + 2 >= E)
2306 return insufficient_usr(kind, "<ivar name> <class USR>");
2307 if (!isUSR(I[2]))
2308 return not_usr("<class USR>", I[2]);
2309 else {
2310 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002311 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002312 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002313 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2314 }
2315
2316 I += 3;
2317 continue;
2318 }
2319 break;
2320 case 9:
2321 if (memcmp(kind, "ObjCClass", 9) == 0) {
2322 if (I + 1 >= E)
2323 return insufficient_usr(kind, "<class name>");
2324 print_usr(clang_constructUSR_ObjCClass(I[1]));
2325 I += 2;
2326 continue;
2327 }
2328 break;
2329 case 10:
2330 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2331 if (I + 3 >= E)
2332 return insufficient_usr(kind, "<method selector> "
2333 "[0=class method|1=instance method] <class USR>");
2334 if (!isUSR(I[3]))
2335 return not_usr("<class USR>", I[3]);
2336 else {
2337 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002338 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002339 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002340 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2341 }
2342 I += 4;
2343 continue;
2344 }
2345 break;
2346 case 12:
2347 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2348 if (I + 2 >= E)
2349 return insufficient_usr(kind, "<class name> <category name>");
2350 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2351 I += 3;
2352 continue;
2353 }
2354 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2355 if (I + 1 >= E)
2356 return insufficient_usr(kind, "<protocol name>");
2357 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2358 I += 2;
2359 continue;
2360 }
2361 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2362 if (I + 2 >= E)
2363 return insufficient_usr(kind, "<property name> <class USR>");
2364 if (!isUSR(I[2]))
2365 return not_usr("<class USR>", I[2]);
2366 else {
2367 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002368 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002369 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002370 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2371 }
2372 I += 3;
2373 continue;
2374 }
2375 break;
2376 default:
2377 break;
2378 }
2379 break;
2380 }
2381
2382 if (I != E) {
2383 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2384 display_usrs();
2385 return 1;
2386 }
2387 return 0;
2388}
2389
2390int print_usrs_file(const char *file_name) {
2391 char line[2048];
2392 const char *args[128];
2393 unsigned numChars = 0;
2394
2395 FILE *fp = fopen(file_name, "r");
2396 if (!fp) {
2397 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2398 return 1;
2399 }
2400
2401 /* This code is not really all that safe, but it works fine for testing. */
2402 while (!feof(fp)) {
2403 char c = fgetc(fp);
2404 if (c == '\n') {
2405 unsigned i = 0;
2406 const char *s = 0;
2407
2408 if (numChars == 0)
2409 continue;
2410
2411 line[numChars] = '\0';
2412 numChars = 0;
2413
2414 if (line[0] == '/' && line[1] == '/')
2415 continue;
2416
2417 s = strtok(line, " ");
2418 while (s) {
2419 args[i] = s;
2420 ++i;
2421 s = strtok(0, " ");
2422 }
2423 if (print_usrs(&args[0], &args[i]))
2424 return 1;
2425 }
2426 else
2427 line[numChars++] = c;
2428 }
2429
2430 fclose(fp);
2431 return 0;
2432}
2433
2434/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002435/* Command line processing. */
2436/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002437int write_pch_file(const char *filename, int argc, const char *argv[]) {
2438 CXIndex Idx;
2439 CXTranslationUnit TU;
2440 struct CXUnsavedFile *unsaved_files = 0;
2441 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002442 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002443
2444 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2445
2446 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2447 clang_disposeIndex(Idx);
2448 return -1;
2449 }
2450
2451 TU = clang_parseTranslationUnit(Idx, 0,
2452 argv + num_unsaved_files,
2453 argc - num_unsaved_files,
2454 unsaved_files,
2455 num_unsaved_files,
2456 CXTranslationUnit_Incomplete);
2457 if (!TU) {
2458 fprintf(stderr, "Unable to load translation unit!\n");
2459 free_remapped_files(unsaved_files, num_unsaved_files);
2460 clang_disposeIndex(Idx);
2461 return 1;
2462 }
2463
Douglas Gregor39c411f2011-07-06 16:43:36 +00002464 switch (clang_saveTranslationUnit(TU, filename,
2465 clang_defaultSaveOptions(TU))) {
2466 case CXSaveError_None:
2467 break;
2468
2469 case CXSaveError_TranslationErrors:
2470 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2471 filename);
2472 result = 2;
2473 break;
2474
2475 case CXSaveError_InvalidTU:
2476 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2477 filename);
2478 result = 3;
2479 break;
2480
2481 case CXSaveError_Unknown:
2482 default:
2483 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2484 result = 1;
2485 break;
2486 }
2487
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002488 clang_disposeTranslationUnit(TU);
2489 free_remapped_files(unsaved_files, num_unsaved_files);
2490 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002491 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002492}
2493
2494/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002495/* Serialized diagnostics. */
2496/******************************************************************************/
2497
2498static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2499 switch (error) {
2500 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2501 case CXLoadDiag_None: break;
2502 case CXLoadDiag_Unknown: return "Unknown";
2503 case CXLoadDiag_InvalidFile: return "Invalid File";
2504 }
2505 return "None";
2506}
2507
2508static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2509 switch (severity) {
2510 case CXDiagnostic_Note: return "note";
2511 case CXDiagnostic_Error: return "error";
2512 case CXDiagnostic_Fatal: return "fatal";
2513 case CXDiagnostic_Ignored: return "ignored";
2514 case CXDiagnostic_Warning: return "warning";
2515 }
2516 return "unknown";
2517}
2518
2519static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002520 if (indent == 0)
2521 return;
2522 fprintf(stderr, "+");
2523 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002524 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002525 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002526 --indent;
2527 }
2528}
2529
2530static void printLocation(CXSourceLocation L) {
2531 CXFile File;
2532 CXString FileName;
2533 unsigned line, column, offset;
2534
2535 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2536 FileName = clang_getFileName(File);
2537
2538 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2539 clang_disposeString(FileName);
2540}
2541
2542static void printRanges(CXDiagnostic D, unsigned indent) {
2543 unsigned i, n = clang_getDiagnosticNumRanges(D);
2544
2545 for (i = 0; i < n; ++i) {
2546 CXSourceLocation Start, End;
2547 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2548 Start = clang_getRangeStart(SR);
2549 End = clang_getRangeEnd(SR);
2550
2551 printIndent(indent);
2552 fprintf(stderr, "Range: ");
2553 printLocation(Start);
2554 fprintf(stderr, " ");
2555 printLocation(End);
2556 fprintf(stderr, "\n");
2557 }
2558}
2559
2560static void printFixIts(CXDiagnostic D, unsigned indent) {
2561 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002562 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002563 for (i = 0 ; i < n; ++i) {
2564 CXSourceRange ReplacementRange;
2565 CXString text;
2566 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2567
2568 printIndent(indent);
2569 fprintf(stderr, "FIXIT: (");
2570 printLocation(clang_getRangeStart(ReplacementRange));
2571 fprintf(stderr, " - ");
2572 printLocation(clang_getRangeEnd(ReplacementRange));
2573 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2574 clang_disposeString(text);
2575 }
2576}
2577
2578static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002579 unsigned i, n;
2580
Ted Kremenek15322172011-11-10 08:43:12 +00002581 if (!Diags)
2582 return;
2583
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002584 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002585 for (i = 0; i < n; ++i) {
2586 CXSourceLocation DiagLoc;
2587 CXDiagnostic D;
2588 CXFile File;
2589 CXString FileName, DiagSpelling, DiagOption;
2590 unsigned line, column, offset;
2591 const char *DiagOptionStr = 0;
2592
2593 D = clang_getDiagnosticInSet(Diags, i);
2594 DiagLoc = clang_getDiagnosticLocation(D);
2595 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2596 FileName = clang_getFileName(File);
2597 DiagSpelling = clang_getDiagnosticSpelling(D);
2598
2599 printIndent(indent);
2600
2601 fprintf(stderr, "%s:%d:%d: %s: %s",
2602 clang_getCString(FileName),
2603 line,
2604 column,
2605 getSeverityString(clang_getDiagnosticSeverity(D)),
2606 clang_getCString(DiagSpelling));
2607
2608 DiagOption = clang_getDiagnosticOption(D, 0);
2609 DiagOptionStr = clang_getCString(DiagOption);
2610 if (DiagOptionStr) {
2611 fprintf(stderr, " [%s]", DiagOptionStr);
2612 }
2613
2614 fprintf(stderr, "\n");
2615
2616 printRanges(D, indent);
2617 printFixIts(D, indent);
2618
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002619 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002620 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2621
2622 clang_disposeString(FileName);
2623 clang_disposeString(DiagSpelling);
2624 clang_disposeString(DiagOption);
2625 }
2626}
2627
2628static int read_diagnostics(const char *filename) {
2629 enum CXLoadDiag_Error error;
2630 CXString errorString;
2631 CXDiagnosticSet Diags = 0;
2632
2633 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2634 if (!Diags) {
2635 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2636 getDiagnosticCodeStr(error),
2637 clang_getCString(errorString));
2638 clang_disposeString(errorString);
2639 return 1;
2640 }
2641
2642 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002643 fprintf(stderr, "Number of diagnostics: %d\n",
2644 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002645 clang_disposeDiagnosticSet(Diags);
2646 return 0;
2647}
2648
2649/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002650/* Command line processing. */
2651/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002652
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002653static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002654 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002655 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002656 if (strcmp(s, "-usrs") == 0)
2657 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002658 if (strncmp(s, "-memory-usage", 13) == 0)
2659 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002660 return NULL;
2661}
2662
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002663static void print_usage(void) {
2664 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002665 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002666 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002667 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002668 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002669 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002670 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002671 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002672 "[FileCheck prefix]\n");
2673 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002674 " c-index-test -test-load-tu <AST file> <symbol filter> "
2675 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002676 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2677 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002678 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002679 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002680 " c-index-test -test-load-source-memory-usage "
2681 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002682 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2683 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002684 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002685 " c-index-test -test-load-source-usrs-memory-usage "
2686 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002687 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2688 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002689 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002690 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002691 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002692 " c-index-test -test-print-typekind {<args>}*\n"
2693 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002694 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002695 " c-index-test -write-pch <file> <compiler arguments>\n");
2696 fprintf(stderr,
2697 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002698 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002699 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002700 " all - load all symbols, including those from PCH\n"
2701 " local - load all symbols except those in PCH\n"
2702 " category - only load ObjC categories (non-PCH)\n"
2703 " interface - only load ObjC interfaces (non-PCH)\n"
2704 " protocol - only load ObjC protocols (non-PCH)\n"
2705 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002706 " typedef - only load typdefs (non-PCH)\n"
2707 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002708}
2709
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002710/***/
2711
2712int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002713 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002714 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2715 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002716 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002717 return perform_code_completion(argc, argv, 0);
2718 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2719 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002720 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2721 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002722 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2723 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002724 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2725 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002726 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2727 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002728 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002729 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002730 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002731 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2732 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002733 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002734 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2735 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2736 if (I) {
2737 int trials = atoi(argv[2]);
2738 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2739 NULL);
2740 }
2741 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002742 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002743 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002744
2745 PostVisitTU postVisit = 0;
2746 if (strstr(argv[1], "-memory-usage"))
2747 postVisit = PrintMemoryUsage;
2748
Ted Kremenek7d405622010-01-12 23:34:26 +00002749 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002750 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2751 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002752 }
2753 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002754 return perform_file_scan(argv[2], argv[3],
2755 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002756 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2757 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002758 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2759 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2760 PrintInclusionStack);
2761 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2762 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2763 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002764 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2765 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2766 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002767 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2768 return perform_test_load_source(argc - 2, argv + 2, "all",
2769 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002770 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2771 if (argc > 2)
2772 return print_usrs(argv + 2, argv + argc);
2773 else {
2774 display_usrs();
2775 return 1;
2776 }
2777 }
2778 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2779 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002780 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2781 return write_pch_file(argv[2], argc - 3, argv + 3);
2782
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002783 print_usage();
2784 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002785}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002786
2787/***/
2788
2789/* We intentionally run in a separate thread to ensure we at least minimal
2790 * testing of a multithreaded environment (for example, having a reduced stack
2791 * size). */
2792
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002793typedef struct thread_info {
2794 int argc;
2795 const char **argv;
2796 int result;
2797} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002798void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002799 thread_info *client_data = client_data_v;
2800 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00002801#ifdef __CYGWIN__
2802 fflush(stdout); /* stdout is not flushed on Cygwin. */
2803#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002804}
2805
2806int main(int argc, const char **argv) {
2807 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002808
Douglas Gregor61605982010-10-27 16:00:01 +00002809 if (getenv("CINDEXTEST_NOTHREADS"))
2810 return cindextest_main(argc, argv);
2811
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002812 client_data.argc = argc;
2813 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002814 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002815 return client_data.result;
2816}