blob: 5dd925463133655a0e77d11d04b20e6113491050 [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
174 printf(" %s=", str);
175 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
176}
177
Douglas Gregor358559d2010-10-02 22:49:11 +0000178int want_display_name = 0;
179
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000180static void PrintCursor(CXCursor Cursor) {
181 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000182 if (clang_isInvalid(Cursor.kind)) {
183 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
184 printf("Invalid Cursor => %s", clang_getCString(ks));
185 clang_disposeString(ks);
186 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000187 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000188 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000189 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000190 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000191 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000192 CXCursor *overridden;
193 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000194 unsigned RefNameRangeNr;
195 CXSourceRange CursorExtent;
196 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000197
Ted Kremeneke68fff62010-02-17 00:41:32 +0000198 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000199 string = want_display_name? clang_getCursorDisplayName(Cursor)
200 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000201 printf("%s=%s", clang_getCString(ks),
202 clang_getCString(string));
203 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000204 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000205
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000206 Referenced = clang_getCursorReferenced(Cursor);
207 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000208 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
209 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
210 printf("[");
211 for (I = 0; I != N; ++I) {
212 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000213 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000214 if (I)
215 printf(", ");
216
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000217 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000218 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000219 printf("%d:%d", line, column);
220 }
221 printf("]");
222 } else {
223 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000224 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000225 printf(":%d:%d", line, column);
226 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000227 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000228
229 if (clang_isCursorDefinition(Cursor))
230 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000231
232 switch (clang_getCursorAvailability(Cursor)) {
233 case CXAvailability_Available:
234 break;
235
236 case CXAvailability_Deprecated:
237 printf(" (deprecated)");
238 break;
239
240 case CXAvailability_NotAvailable:
241 printf(" (unavailable)");
242 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000243
244 case CXAvailability_NotAccessible:
245 printf(" (inaccessible)");
246 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000247 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000248
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000249 if (clang_CXXMethod_isStatic(Cursor))
250 printf(" (static)");
251 if (clang_CXXMethod_isVirtual(Cursor))
252 printf(" (virtual)");
253
Ted Kremenek95f33552010-08-26 01:42:22 +0000254 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
255 CXType T =
256 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
257 CXString S = clang_getTypeKindSpelling(T.kind);
258 printf(" [IBOutletCollection=%s]", clang_getCString(S));
259 clang_disposeString(S);
260 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000261
262 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
263 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
264 unsigned isVirtual = clang_isVirtualBase(Cursor);
265 const char *accessStr = 0;
266
267 switch (access) {
268 case CX_CXXInvalidAccessSpecifier:
269 accessStr = "invalid"; break;
270 case CX_CXXPublic:
271 accessStr = "public"; break;
272 case CX_CXXProtected:
273 accessStr = "protected"; break;
274 case CX_CXXPrivate:
275 accessStr = "private"; break;
276 }
277
278 printf(" [access=%s isVirtual=%s]", accessStr,
279 isVirtual ? "true" : "false");
280 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000281
282 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
283 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
284 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
285 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000286 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000287 printf(" [Specialization of %s:%d:%d]",
288 clang_getCString(Name), line, column);
289 clang_disposeString(Name);
290 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000291
292 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
293 if (num_overridden) {
294 unsigned I;
295 printf(" [Overrides ");
296 for (I = 0; I != num_overridden; ++I) {
297 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000298 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000299 if (I)
300 printf(", ");
301 printf("@%d:%d", line, column);
302 }
303 printf("]");
304 clang_disposeOverriddenCursors(overridden);
305 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000306
307 if (Cursor.kind == CXCursor_InclusionDirective) {
308 CXFile File = clang_getIncludedFile(Cursor);
309 CXString Included = clang_getFileName(File);
310 printf(" (%s)", clang_getCString(Included));
311 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000312
313 if (clang_isFileMultipleIncludeGuarded(TU, File))
314 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000315 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000316
317 CursorExtent = clang_getCursorExtent(Cursor);
318 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
319 CXNameRange_WantQualifier
320 | CXNameRange_WantSinglePiece
321 | CXNameRange_WantTemplateArgs,
322 0);
323 if (!clang_equalRanges(CursorExtent, RefNameRange))
324 PrintRange(RefNameRange, "SingleRefName");
325
326 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
327 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
328 CXNameRange_WantQualifier
329 | CXNameRange_WantTemplateArgs,
330 RefNameRangeNr);
331 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
332 break;
333 if (!clang_equalRanges(CursorExtent, RefNameRange))
334 PrintRange(RefNameRange, "RefName");
335 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000336 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000337}
Steve Naroff89922f82009-08-31 00:59:03 +0000338
Ted Kremeneke68fff62010-02-17 00:41:32 +0000339static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000340 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000341 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000343 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000345 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000346 clang_disposeString(source);
347 return "<invalid loc>";
348 }
349 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000350 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000351 clang_disposeString(source);
352 return b;
353 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000354}
355
Ted Kremenek0d435192009-11-17 18:13:31 +0000356/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000357/* Callbacks. */
358/******************************************************************************/
359
360typedef void (*PostVisitTU)(CXTranslationUnit);
361
Douglas Gregora88084b2010-02-18 18:08:43 +0000362void PrintDiagnostic(CXDiagnostic Diagnostic) {
363 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000364 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000365 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000366 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000367 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
368 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000369 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000370
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000371 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000372 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000373
Douglas Gregor274f1902010-02-22 23:17:23 +0000374 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
375 fprintf(stderr, "%s\n", clang_getCString(Msg));
376 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000377
Douglas Gregora9b06d42010-11-09 06:24:54 +0000378 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
379 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000380 if (!file)
381 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000382
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000383 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000384 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000385 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000386 CXSourceRange range;
387 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
388 CXSourceLocation start = clang_getRangeStart(range);
389 CXSourceLocation end = clang_getRangeEnd(range);
390 unsigned start_line, start_column, end_line, end_column;
391 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000392 clang_getSpellingLocation(start, &start_file, &start_line,
393 &start_column, 0);
394 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000395 if (clang_equalLocations(start, end)) {
396 /* Insertion. */
397 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000398 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000399 clang_getCString(insertion_text), start_line, start_column);
400 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
401 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000402 if (start_file == file && end_file == file) {
403 fprintf(out, "FIX-IT: Remove ");
404 PrintExtent(out, start_line, start_column, end_line, end_column);
405 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000406 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000407 } else {
408 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000409 if (start_file == end_file) {
410 fprintf(out, "FIX-IT: Replace ");
411 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000412 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000413 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000414 break;
415 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000416 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000417 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000418}
419
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000420void PrintDiagnosticSet(CXDiagnosticSet Set) {
421 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
422 for ( ; i != n ; ++i) {
423 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
424 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000425 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000426 if (ChildDiags)
427 PrintDiagnosticSet(ChildDiags);
428 }
429}
430
431void PrintDiagnostics(CXTranslationUnit TU) {
432 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
433 PrintDiagnosticSet(TUSet);
434 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000435}
436
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000437void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000438 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000439 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000440 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000441 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000442 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000443 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000444 unsigned long amount = usage.entries[i].amount;
445 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000446 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000447 ((double) amount)/(1024*1024));
448 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000449 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000450 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000451 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000452}
453
Ted Kremenekce2ae882010-01-26 17:59:48 +0000454/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000455/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000456/******************************************************************************/
457
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000458static const char *FileCheckPrefix = "CHECK";
459
Douglas Gregora7bde202010-01-19 00:34:46 +0000460static void PrintCursorExtent(CXCursor C) {
461 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000462 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000463}
464
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000465/* Data used by all of the visitors. */
466typedef struct {
467 CXTranslationUnit TU;
468 enum CXCursorKind *Filter;
469} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000470
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000471
Ted Kremeneke68fff62010-02-17 00:41:32 +0000472enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000473 CXCursor Parent,
474 CXClientData ClientData) {
475 VisitorData *Data = (VisitorData *)ClientData;
476 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000477 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000478 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000479 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000480 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000481 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000482 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000483 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000484 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000485 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000486 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000487
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000488 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000489}
Steve Naroff50398192009-08-28 15:28:48 +0000490
Ted Kremeneke68fff62010-02-17 00:41:32 +0000491static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000492 CXCursor Parent,
493 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000494 const char *startBuf, *endBuf;
495 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
496 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000497 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000498
Douglas Gregorb6998662010-01-19 19:34:47 +0000499 if (Cursor.kind != CXCursor_FunctionDecl ||
500 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000501 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000502
503 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
504 &startLine, &startColumn,
505 &endLine, &endColumn);
506 /* Probe the entire body, looking for both decls and refs. */
507 curLine = startLine;
508 curColumn = startColumn;
509
510 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000511 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000512 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000513 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000514
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000515 if (*startBuf == '\n') {
516 startBuf++;
517 curLine++;
518 curColumn = 1;
519 } else if (*startBuf != '\t')
520 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000521
Douglas Gregor98258af2010-01-18 22:46:11 +0000522 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000523 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000524
Douglas Gregor1db19de2010-01-19 21:36:55 +0000525 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000526 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000527 CXSourceLocation RefLoc
528 = clang_getLocation(Data->TU, file, curLine, curColumn);
529 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000530 if (Ref.kind == CXCursor_NoDeclFound) {
531 /* Nothing found here; that's fine. */
532 } else if (Ref.kind != CXCursor_FunctionDecl) {
533 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
534 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000535 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000536 printf("\n");
537 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000538 }
Ted Kremenek74844072010-02-17 00:41:20 +0000539 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000540 startBuf++;
541 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000542
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000543 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000544}
545
Ted Kremenek7d405622010-01-12 23:34:26 +0000546/******************************************************************************/
547/* USR testing. */
548/******************************************************************************/
549
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000550enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
551 CXClientData ClientData) {
552 VisitorData *Data = (VisitorData *)ClientData;
553 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000554 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000555 const char *cstr = clang_getCString(USR);
556 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000557 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000558 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000559 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000560 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
561
Douglas Gregora7bde202010-01-19 00:34:46 +0000562 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000563 printf("\n");
564 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000565
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000566 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000567 }
568
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000569 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000570}
571
572/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000573/* Inclusion stack testing. */
574/******************************************************************************/
575
576void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
577 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578
Ted Kremenek16b55a72010-01-26 19:31:51 +0000579 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000580 CXString fname;
581
582 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000583 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000584 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000585
Ted Kremenek16b55a72010-01-26 19:31:51 +0000586 for (i = 0; i < includeStackLen; ++i) {
587 CXFile includingFile;
588 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000589 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
590 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000591 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000592 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000593 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000594 }
595 printf("\n");
596}
597
598void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000599 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000600}
601
602/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000603/* Linkage testing. */
604/******************************************************************************/
605
606static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
607 CXClientData d) {
608 const char *linkage = 0;
609
610 if (clang_isInvalid(clang_getCursorKind(cursor)))
611 return CXChildVisit_Recurse;
612
613 switch (clang_getCursorLinkage(cursor)) {
614 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000615 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
616 case CXLinkage_Internal: linkage = "Internal"; break;
617 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
618 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000619 }
620
621 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000622 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000623 printf("linkage=%s\n", linkage);
624 }
625
626 return CXChildVisit_Recurse;
627}
628
629/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000630/* Typekind testing. */
631/******************************************************************************/
632
633static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
634 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000635 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
636 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000637 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000638 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000639 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000640 if (clang_isConstQualifiedType(T))
641 printf(" const");
642 if (clang_isVolatileQualifiedType(T))
643 printf(" volatile");
644 if (clang_isRestrictQualifiedType(T))
645 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000646 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000647 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000648 {
649 CXType CT = clang_getCanonicalType(T);
650 if (!clang_equalTypes(T, CT)) {
651 CXString CS = clang_getTypeKindSpelling(CT.kind);
652 printf(" [canonical=%s]", clang_getCString(CS));
653 clang_disposeString(CS);
654 }
655 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000656 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000657 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000658 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000659 if (RT.kind != CXType_Invalid) {
660 CXString RS = clang_getTypeKindSpelling(RT.kind);
661 printf(" [result=%s]", clang_getCString(RS));
662 clang_disposeString(RS);
663 }
664 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000665 /* Print if this is a non-POD type. */
666 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000667
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000668 printf("\n");
669 }
670 return CXChildVisit_Recurse;
671}
672
673
674/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000675/* Loading ASTs/source. */
676/******************************************************************************/
677
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000678static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000679 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000680 CXCursorVisitor Visitor,
681 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000682
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000683 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000684 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000685
686 if (Visitor) {
687 enum CXCursorKind K = CXCursor_NotImplemented;
688 enum CXCursorKind *ck = &K;
689 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000690
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000691 /* Perform some simple filtering. */
692 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000693 else if (!strcmp(filter, "all-display") ||
694 !strcmp(filter, "local-display")) {
695 ck = NULL;
696 want_display_name = 1;
697 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000698 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000699 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
700 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
701 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
702 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
703 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
704 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
705 else {
706 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
707 return 1;
708 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000709
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000710 Data.TU = TU;
711 Data.Filter = ck;
712 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000713 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000714
Ted Kremenekce2ae882010-01-26 17:59:48 +0000715 if (PV)
716 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000717
Douglas Gregora88084b2010-02-18 18:08:43 +0000718 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000719 if (checkForErrors(TU) != 0) {
720 clang_disposeTranslationUnit(TU);
721 return -1;
722 }
723
Ted Kremenek0d435192009-11-17 18:13:31 +0000724 clang_disposeTranslationUnit(TU);
725 return 0;
726}
727
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000728int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000729 const char *prefix, CXCursorVisitor Visitor,
730 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000731 CXIndex Idx;
732 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000733 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000734 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000735 !strcmp(filter, "local") ? 1 : 0,
736 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000737
Ted Kremenek020a0952010-02-11 07:41:25 +0000738 if (!CreateTranslationUnit(Idx, file, &TU)) {
739 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000740 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000741 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000742
Ted Kremenek020a0952010-02-11 07:41:25 +0000743 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
744 clang_disposeIndex(Idx);
745 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000746}
747
Ted Kremenekce2ae882010-01-26 17:59:48 +0000748int perform_test_load_source(int argc, const char **argv,
749 const char *filter, CXCursorVisitor Visitor,
750 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000751 CXIndex Idx;
752 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000753 struct CXUnsavedFile *unsaved_files = 0;
754 int num_unsaved_files = 0;
755 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000756
Daniel Dunbarada487d2009-12-01 02:03:10 +0000757 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000758 (!strcmp(filter, "local") ||
759 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000760 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000761
Ted Kremenek020a0952010-02-11 07:41:25 +0000762 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
763 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000764 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000765 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000766
Douglas Gregordca8ee82011-05-06 16:33:08 +0000767 TU = clang_parseTranslationUnit(Idx, 0,
768 argv + num_unsaved_files,
769 argc - num_unsaved_files,
770 unsaved_files, num_unsaved_files,
771 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000772 if (!TU) {
773 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000774 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000775 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000776 return 1;
777 }
778
Ted Kremenekce2ae882010-01-26 17:59:48 +0000779 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000780 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000781 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000782 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000783}
784
Douglas Gregorabc563f2010-07-19 21:46:24 +0000785int perform_test_reparse_source(int argc, const char **argv, int trials,
786 const char *filter, CXCursorVisitor Visitor,
787 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000788 CXIndex Idx;
789 CXTranslationUnit TU;
790 struct CXUnsavedFile *unsaved_files = 0;
791 int num_unsaved_files = 0;
792 int result;
793 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000794 int remap_after_trial = 0;
795 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000796
797 Idx = clang_createIndex(/* excludeDeclsFromPCH */
798 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000799 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000800
Douglas Gregorabc563f2010-07-19 21:46:24 +0000801 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
802 clang_disposeIndex(Idx);
803 return -1;
804 }
805
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000806 /* Load the initial translation unit -- we do this without honoring remapped
807 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000808 TU = clang_parseTranslationUnit(Idx, 0,
809 argv + num_unsaved_files,
810 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000811 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000812 if (!TU) {
813 fprintf(stderr, "Unable to load translation unit!\n");
814 free_remapped_files(unsaved_files, num_unsaved_files);
815 clang_disposeIndex(Idx);
816 return 1;
817 }
818
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000819 if (checkForErrors(TU) != 0)
820 return -1;
821
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000822 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
823 remap_after_trial =
824 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
825 }
826
Douglas Gregorabc563f2010-07-19 21:46:24 +0000827 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000828 if (clang_reparseTranslationUnit(TU,
829 trial >= remap_after_trial ? num_unsaved_files : 0,
830 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000831 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000832 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000833 clang_disposeTranslationUnit(TU);
834 free_remapped_files(unsaved_files, num_unsaved_files);
835 clang_disposeIndex(Idx);
836 return -1;
837 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000838
839 if (checkForErrors(TU) != 0)
840 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000841 }
842
843 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000844
Douglas Gregorabc563f2010-07-19 21:46:24 +0000845 free_remapped_files(unsaved_files, num_unsaved_files);
846 clang_disposeIndex(Idx);
847 return result;
848}
849
Ted Kremenek0d435192009-11-17 18:13:31 +0000850/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000851/* Logic for testing clang_getCursor(). */
852/******************************************************************************/
853
Douglas Gregordd3e5542011-05-04 00:14:37 +0000854static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000855 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000856 unsigned end_line, unsigned end_col,
857 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000858 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000859 if (prefix)
860 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000861 PrintExtent(stdout, start_line, start_col, end_line, end_col);
862 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000863 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000864 printf("\n");
865}
866
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000867static int perform_file_scan(const char *ast_file, const char *source_file,
868 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000869 CXIndex Idx;
870 CXTranslationUnit TU;
871 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000872 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000873 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000874 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000875 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000876
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000877 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
878 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000879 fprintf(stderr, "Could not create Index\n");
880 return 1;
881 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000882
Ted Kremenek1c6da172009-11-17 19:37:36 +0000883 if (!CreateTranslationUnit(Idx, ast_file, &TU))
884 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000885
Ted Kremenek1c6da172009-11-17 19:37:36 +0000886 if ((fp = fopen(source_file, "r")) == NULL) {
887 fprintf(stderr, "Could not open '%s'\n", source_file);
888 return 1;
889 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000890
Douglas Gregorb9790342010-01-22 21:44:22 +0000891 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000892 for (;;) {
893 CXCursor cursor;
894 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000895
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000896 if (c == '\n') {
897 ++line;
898 col = 1;
899 } else
900 ++col;
901
902 /* Check the cursor at this position, and dump the previous one if we have
903 * found something new.
904 */
905 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
906 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
907 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000908 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000909 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000910 start_line = line;
911 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000912 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000913 if (c == EOF)
914 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000915
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000916 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000917 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000918
Ted Kremenek1c6da172009-11-17 19:37:36 +0000919 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000920 clang_disposeTranslationUnit(TU);
921 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000922 return 0;
923}
924
925/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000926/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000927/******************************************************************************/
928
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000929/* Parse file:line:column from the input string. Returns 0 on success, non-zero
930 on failure. If successful, the pointer *filename will contain newly-allocated
931 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000932int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000933 unsigned *column, unsigned *second_line,
934 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000935 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000936 const char *last_colon = strrchr(input, ':');
937 unsigned values[4], i;
938 unsigned num_values = (second_line && second_column)? 4 : 2;
939
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000940 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000941 if (!last_colon || last_colon == input) {
942 if (num_values == 4)
943 fprintf(stderr, "could not parse filename:line:column:line:column in "
944 "'%s'\n", input);
945 else
946 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000947 return 1;
948 }
949
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000950 for (i = 0; i != num_values; ++i) {
951 const char *prev_colon;
952
953 /* Parse the next line or column. */
954 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
955 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000956 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000957 (i % 2 ? "column" : "line"), input);
958 return 1;
959 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000960
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000961 if (i + 1 == num_values)
962 break;
963
964 /* Find the previous colon. */
965 prev_colon = last_colon - 1;
966 while (prev_colon != input && *prev_colon != ':')
967 --prev_colon;
968 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000969 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000970 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000971 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000972 }
973
974 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000975 }
976
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000977 *line = values[0];
978 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000979
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000980 if (second_line && second_column) {
981 *second_line = values[2];
982 *second_column = values[3];
983 }
984
Douglas Gregor88d23952009-11-09 18:19:57 +0000985 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000986 *filename = (char*)malloc(last_colon - input + 1);
987 memcpy(*filename, input, last_colon - input);
988 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000989 return 0;
990}
991
992const char *
993clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
994 switch (Kind) {
995 case CXCompletionChunk_Optional: return "Optional";
996 case CXCompletionChunk_TypedText: return "TypedText";
997 case CXCompletionChunk_Text: return "Text";
998 case CXCompletionChunk_Placeholder: return "Placeholder";
999 case CXCompletionChunk_Informative: return "Informative";
1000 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1001 case CXCompletionChunk_LeftParen: return "LeftParen";
1002 case CXCompletionChunk_RightParen: return "RightParen";
1003 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1004 case CXCompletionChunk_RightBracket: return "RightBracket";
1005 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1006 case CXCompletionChunk_RightBrace: return "RightBrace";
1007 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1008 case CXCompletionChunk_RightAngle: return "RightAngle";
1009 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001010 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001011 case CXCompletionChunk_Colon: return "Colon";
1012 case CXCompletionChunk_SemiColon: return "SemiColon";
1013 case CXCompletionChunk_Equal: return "Equal";
1014 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1015 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001016 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001017
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001018 return "Unknown";
1019}
1020
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001021static int checkForErrors(CXTranslationUnit TU) {
1022 unsigned Num, i;
1023 CXDiagnostic Diag;
1024 CXString DiagStr;
1025
1026 if (!getenv("CINDEXTEST_FAILONERROR"))
1027 return 0;
1028
1029 Num = clang_getNumDiagnostics(TU);
1030 for (i = 0; i != Num; ++i) {
1031 Diag = clang_getDiagnostic(TU, i);
1032 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1033 DiagStr = clang_formatDiagnostic(Diag,
1034 clang_defaultDiagnosticDisplayOptions());
1035 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1036 clang_disposeString(DiagStr);
1037 clang_disposeDiagnostic(Diag);
1038 return -1;
1039 }
1040 clang_disposeDiagnostic(Diag);
1041 }
1042
1043 return 0;
1044}
1045
Douglas Gregor3ac73852009-11-09 16:04:45 +00001046void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001047 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001048
Douglas Gregor3ac73852009-11-09 16:04:45 +00001049 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001050 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001051 CXString text;
1052 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001053 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001054 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001055
Douglas Gregor3ac73852009-11-09 16:04:45 +00001056 if (Kind == CXCompletionChunk_Optional) {
1057 fprintf(file, "{Optional ");
1058 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001059 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001060 file);
1061 fprintf(file, "}");
1062 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001063 }
1064
1065 if (Kind == CXCompletionChunk_VerticalSpace) {
1066 fprintf(file, "{VerticalSpace }");
1067 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001068 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001069
Douglas Gregord5a20892009-11-09 17:05:28 +00001070 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001071 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001072 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001073 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001074 cstr ? cstr : "");
1075 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001076 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001077
Douglas Gregor3ac73852009-11-09 16:04:45 +00001078}
1079
1080void print_completion_result(CXCompletionResult *completion_result,
1081 CXClientData client_data) {
1082 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001083 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001084 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001085 enum CXCursorKind ParentKind;
1086 CXString ParentName;
1087
Ted Kremeneke68fff62010-02-17 00:41:32 +00001088 fprintf(file, "%s:", clang_getCString(ks));
1089 clang_disposeString(ks);
1090
Douglas Gregor3ac73852009-11-09 16:04:45 +00001091 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001092 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001093 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001094 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1095 case CXAvailability_Available:
1096 break;
1097
1098 case CXAvailability_Deprecated:
1099 fprintf(file, " (deprecated)");
1100 break;
1101
1102 case CXAvailability_NotAvailable:
1103 fprintf(file, " (unavailable)");
1104 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001105
1106 case CXAvailability_NotAccessible:
1107 fprintf(file, " (inaccessible)");
1108 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001109 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001110
1111 annotationCount = clang_getCompletionNumAnnotations(
1112 completion_result->CompletionString);
1113 if (annotationCount) {
1114 unsigned i;
1115 fprintf(file, " (");
1116 for (i = 0; i < annotationCount; ++i) {
1117 if (i != 0)
1118 fprintf(file, ", ");
1119 fprintf(file, "\"%s\"",
1120 clang_getCString(clang_getCompletionAnnotation(
1121 completion_result->CompletionString, i)));
1122 }
1123 fprintf(file, ")");
1124 }
1125
Douglas Gregorba103062012-03-27 23:34:16 +00001126 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1127 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1128 &ParentKind);
1129 if (ParentKind != CXCursor_NotImplemented) {
1130 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1131 fprintf(file, " (parent: %s '%s')",
1132 clang_getCString(KindSpelling),
1133 clang_getCString(ParentName));
1134 clang_disposeString(KindSpelling);
1135 }
1136 clang_disposeString(ParentName);
1137 }
1138
Douglas Gregor58ddb602010-08-23 23:00:57 +00001139 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001140}
1141
Douglas Gregor3da626b2011-07-07 16:03:39 +00001142void print_completion_contexts(unsigned long long contexts, FILE *file) {
1143 fprintf(file, "Completion contexts:\n");
1144 if (contexts == CXCompletionContext_Unknown) {
1145 fprintf(file, "Unknown\n");
1146 }
1147 if (contexts & CXCompletionContext_AnyType) {
1148 fprintf(file, "Any type\n");
1149 }
1150 if (contexts & CXCompletionContext_AnyValue) {
1151 fprintf(file, "Any value\n");
1152 }
1153 if (contexts & CXCompletionContext_ObjCObjectValue) {
1154 fprintf(file, "Objective-C object value\n");
1155 }
1156 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1157 fprintf(file, "Objective-C selector value\n");
1158 }
1159 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1160 fprintf(file, "C++ class type value\n");
1161 }
1162 if (contexts & CXCompletionContext_DotMemberAccess) {
1163 fprintf(file, "Dot member access\n");
1164 }
1165 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1166 fprintf(file, "Arrow member access\n");
1167 }
1168 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1169 fprintf(file, "Objective-C property access\n");
1170 }
1171 if (contexts & CXCompletionContext_EnumTag) {
1172 fprintf(file, "Enum tag\n");
1173 }
1174 if (contexts & CXCompletionContext_UnionTag) {
1175 fprintf(file, "Union tag\n");
1176 }
1177 if (contexts & CXCompletionContext_StructTag) {
1178 fprintf(file, "Struct tag\n");
1179 }
1180 if (contexts & CXCompletionContext_ClassTag) {
1181 fprintf(file, "Class name\n");
1182 }
1183 if (contexts & CXCompletionContext_Namespace) {
1184 fprintf(file, "Namespace or namespace alias\n");
1185 }
1186 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1187 fprintf(file, "Nested name specifier\n");
1188 }
1189 if (contexts & CXCompletionContext_ObjCInterface) {
1190 fprintf(file, "Objective-C interface\n");
1191 }
1192 if (contexts & CXCompletionContext_ObjCProtocol) {
1193 fprintf(file, "Objective-C protocol\n");
1194 }
1195 if (contexts & CXCompletionContext_ObjCCategory) {
1196 fprintf(file, "Objective-C category\n");
1197 }
1198 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1199 fprintf(file, "Objective-C instance method\n");
1200 }
1201 if (contexts & CXCompletionContext_ObjCClassMessage) {
1202 fprintf(file, "Objective-C class method\n");
1203 }
1204 if (contexts & CXCompletionContext_ObjCSelectorName) {
1205 fprintf(file, "Objective-C selector name\n");
1206 }
1207 if (contexts & CXCompletionContext_MacroName) {
1208 fprintf(file, "Macro name\n");
1209 }
1210 if (contexts & CXCompletionContext_NaturalLanguage) {
1211 fprintf(file, "Natural language\n");
1212 }
1213}
1214
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001215int my_stricmp(const char *s1, const char *s2) {
1216 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001217 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001218 if (c1 < c2)
1219 return -1;
1220 else if (c1 > c2)
1221 return 1;
1222
1223 ++s1;
1224 ++s2;
1225 }
1226
1227 if (*s1)
1228 return 1;
1229 else if (*s2)
1230 return -1;
1231 return 0;
1232}
1233
Douglas Gregor1982c182010-07-12 18:38:41 +00001234int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001235 const char *input = argv[1];
1236 char *filename = 0;
1237 unsigned line;
1238 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001239 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001240 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001241 struct CXUnsavedFile *unsaved_files = 0;
1242 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001243 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001244 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001245 unsigned I, Repeats = 1;
1246 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1247
1248 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1249 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001250
Douglas Gregor1982c182010-07-12 18:38:41 +00001251 if (timing_only)
1252 input += strlen("-code-completion-timing=");
1253 else
1254 input += strlen("-code-completion-at=");
1255
Ted Kremeneke68fff62010-02-17 00:41:32 +00001256 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001257 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001258 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001259
Douglas Gregor735df882009-12-02 09:21:34 +00001260 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1261 return -1;
1262
Douglas Gregor32be4a52010-10-11 21:37:58 +00001263 CIdx = clang_createIndex(0, 0);
1264
1265 if (getenv("CINDEXTEST_EDITING"))
1266 Repeats = 5;
1267
1268 TU = clang_parseTranslationUnit(CIdx, 0,
1269 argv + num_unsaved_files + 2,
1270 argc - num_unsaved_files - 2,
1271 0, 0, getDefaultParsingOptions());
1272 if (!TU) {
1273 fprintf(stderr, "Unable to load translation unit!\n");
1274 return 1;
1275 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001276
1277 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1278 fprintf(stderr, "Unable to reparse translation init!\n");
1279 return 1;
1280 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001281
1282 for (I = 0; I != Repeats; ++I) {
1283 results = clang_codeCompleteAt(TU, filename, line, column,
1284 unsaved_files, num_unsaved_files,
1285 completionOptions);
1286 if (!results) {
1287 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001288 return 1;
1289 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001290 if (I != Repeats-1)
1291 clang_disposeCodeCompleteResults(results);
1292 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001293
Douglas Gregorec6762c2009-12-18 16:20:58 +00001294 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001295 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001296 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001297 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001298 CXString objCSelector;
1299 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001300 if (!timing_only) {
1301 /* Sort the code-completion results based on the typed text. */
1302 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1303
Douglas Gregor1982c182010-07-12 18:38:41 +00001304 for (i = 0; i != n; ++i)
1305 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001306 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001307 n = clang_codeCompleteGetNumDiagnostics(results);
1308 for (i = 0; i != n; ++i) {
1309 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1310 PrintDiagnostic(diag);
1311 clang_disposeDiagnostic(diag);
1312 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001313
1314 contexts = clang_codeCompleteGetContexts(results);
1315 print_completion_contexts(contexts, stdout);
1316
Douglas Gregor0a47d692011-07-26 15:24:30 +00001317 containerKind = clang_codeCompleteGetContainerKind(results,
1318 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001319
1320 if (containerKind != CXCursor_InvalidCode) {
1321 /* We have found a container */
1322 CXString containerUSR, containerKindSpelling;
1323 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1324 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1325 clang_disposeString(containerKindSpelling);
1326
1327 if (containerIsIncomplete) {
1328 printf("Container is incomplete\n");
1329 }
1330 else {
1331 printf("Container is complete\n");
1332 }
1333
1334 containerUSR = clang_codeCompleteGetContainerUSR(results);
1335 printf("Container USR: %s\n", clang_getCString(containerUSR));
1336 clang_disposeString(containerUSR);
1337 }
1338
Douglas Gregor0a47d692011-07-26 15:24:30 +00001339 objCSelector = clang_codeCompleteGetObjCSelector(results);
1340 selectorString = clang_getCString(objCSelector);
1341 if (selectorString && strlen(selectorString) > 0) {
1342 printf("Objective-C selector: %s\n", selectorString);
1343 }
1344 clang_disposeString(objCSelector);
1345
Douglas Gregorec6762c2009-12-18 16:20:58 +00001346 clang_disposeCodeCompleteResults(results);
1347 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001348 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001349 clang_disposeIndex(CIdx);
1350 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001351
Douglas Gregor735df882009-12-02 09:21:34 +00001352 free_remapped_files(unsaved_files, num_unsaved_files);
1353
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001354 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001355}
1356
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001357typedef struct {
1358 char *filename;
1359 unsigned line;
1360 unsigned column;
1361} CursorSourceLocation;
1362
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001363static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001364 CXIndex CIdx;
1365 int errorCode;
1366 struct CXUnsavedFile *unsaved_files = 0;
1367 int num_unsaved_files = 0;
1368 CXTranslationUnit TU;
1369 CXCursor Cursor;
1370 CursorSourceLocation *Locations = 0;
1371 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001372 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001373 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001374
Ted Kremeneke68fff62010-02-17 00:41:32 +00001375 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001376 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1377 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001378
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001379 /* Parse the locations. */
1380 assert(NumLocations > 0 && "Unable to count locations?");
1381 Locations = (CursorSourceLocation *)malloc(
1382 NumLocations * sizeof(CursorSourceLocation));
1383 for (Loc = 0; Loc < NumLocations; ++Loc) {
1384 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001385 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1386 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001387 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001388 return errorCode;
1389 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001390
1391 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001392 &num_unsaved_files))
1393 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001394
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001395 if (getenv("CINDEXTEST_EDITING"))
1396 Repeats = 5;
1397
1398 /* Parse the translation unit. When we're testing clang_getCursor() after
1399 reparsing, don't remap unsaved files until the second parse. */
1400 CIdx = clang_createIndex(1, 1);
1401 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1402 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001403 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001404 unsaved_files,
1405 Repeats > 1? 0 : num_unsaved_files,
1406 getDefaultParsingOptions());
1407
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001408 if (!TU) {
1409 fprintf(stderr, "unable to parse input\n");
1410 return -1;
1411 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001412
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001413 if (checkForErrors(TU) != 0)
1414 return -1;
1415
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001416 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001417 if (Repeats > 1 &&
1418 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1419 clang_defaultReparseOptions(TU))) {
1420 clang_disposeTranslationUnit(TU);
1421 return 1;
1422 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001423
1424 if (checkForErrors(TU) != 0)
1425 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001426
1427 for (Loc = 0; Loc < NumLocations; ++Loc) {
1428 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1429 if (!file)
1430 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001431
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001432 Cursor = clang_getCursor(TU,
1433 clang_getLocation(TU, file, Locations[Loc].line,
1434 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001435
1436 if (checkForErrors(TU) != 0)
1437 return -1;
1438
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001439 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001440 CXCompletionString completionString = clang_getCursorCompletionString(
1441 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001442 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1443 CXString Spelling;
1444 const char *cspell;
1445 unsigned line, column;
1446 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1447 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001448 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001449 PrintCursorExtent(Cursor);
1450 Spelling = clang_getCursorSpelling(Cursor);
1451 cspell = clang_getCString(Spelling);
1452 if (cspell && strlen(cspell) != 0)
1453 printf(" Spelling=%s", cspell);
1454 clang_disposeString(Spelling);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001455 if (completionString != NULL) {
1456 printf("\nCompletion string: ");
1457 print_completion_string(completionString, stdout);
1458 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001459 printf("\n");
1460 free(Locations[Loc].filename);
1461 }
1462 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001463 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001464
Douglas Gregora88084b2010-02-18 18:08:43 +00001465 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001466 clang_disposeTranslationUnit(TU);
1467 clang_disposeIndex(CIdx);
1468 free(Locations);
1469 free_remapped_files(unsaved_files, num_unsaved_files);
1470 return 0;
1471}
1472
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001473static enum CXVisitorResult findFileRefsVisit(void *context,
1474 CXCursor cursor, CXSourceRange range) {
1475 if (clang_Range_isNull(range))
1476 return CXVisit_Continue;
1477
1478 PrintCursor(cursor);
1479 PrintRange(range, "");
1480 printf("\n");
1481 return CXVisit_Continue;
1482}
1483
1484static int find_file_refs_at(int argc, const char **argv) {
1485 CXIndex CIdx;
1486 int errorCode;
1487 struct CXUnsavedFile *unsaved_files = 0;
1488 int num_unsaved_files = 0;
1489 CXTranslationUnit TU;
1490 CXCursor Cursor;
1491 CursorSourceLocation *Locations = 0;
1492 unsigned NumLocations = 0, Loc;
1493 unsigned Repeats = 1;
1494 unsigned I;
1495
1496 /* Count the number of locations. */
1497 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1498 ++NumLocations;
1499
1500 /* Parse the locations. */
1501 assert(NumLocations > 0 && "Unable to count locations?");
1502 Locations = (CursorSourceLocation *)malloc(
1503 NumLocations * sizeof(CursorSourceLocation));
1504 for (Loc = 0; Loc < NumLocations; ++Loc) {
1505 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1506 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1507 &Locations[Loc].line,
1508 &Locations[Loc].column, 0, 0)))
1509 return errorCode;
1510 }
1511
1512 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1513 &num_unsaved_files))
1514 return -1;
1515
1516 if (getenv("CINDEXTEST_EDITING"))
1517 Repeats = 5;
1518
1519 /* Parse the translation unit. When we're testing clang_getCursor() after
1520 reparsing, don't remap unsaved files until the second parse. */
1521 CIdx = clang_createIndex(1, 1);
1522 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1523 argv + num_unsaved_files + 1 + NumLocations,
1524 argc - num_unsaved_files - 2 - NumLocations,
1525 unsaved_files,
1526 Repeats > 1? 0 : num_unsaved_files,
1527 getDefaultParsingOptions());
1528
1529 if (!TU) {
1530 fprintf(stderr, "unable to parse input\n");
1531 return -1;
1532 }
1533
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001534 if (checkForErrors(TU) != 0)
1535 return -1;
1536
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001537 for (I = 0; I != Repeats; ++I) {
1538 if (Repeats > 1 &&
1539 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1540 clang_defaultReparseOptions(TU))) {
1541 clang_disposeTranslationUnit(TU);
1542 return 1;
1543 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001544
1545 if (checkForErrors(TU) != 0)
1546 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001547
1548 for (Loc = 0; Loc < NumLocations; ++Loc) {
1549 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1550 if (!file)
1551 continue;
1552
1553 Cursor = clang_getCursor(TU,
1554 clang_getLocation(TU, file, Locations[Loc].line,
1555 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001556
1557 if (checkForErrors(TU) != 0)
1558 return -1;
1559
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001560 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001561 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001562 PrintCursor(Cursor);
1563 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001564 clang_findReferencesInFile(Cursor, file, visitor);
1565 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001566
1567 if (checkForErrors(TU) != 0)
1568 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001569 }
1570 }
1571 }
1572
1573 PrintDiagnostics(TU);
1574 clang_disposeTranslationUnit(TU);
1575 clang_disposeIndex(CIdx);
1576 free(Locations);
1577 free_remapped_files(unsaved_files, num_unsaved_files);
1578 return 0;
1579}
1580
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001581typedef struct {
1582 const char *check_prefix;
1583 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001584 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001585 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001586 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001587} IndexData;
1588
1589static void printCheck(IndexData *data) {
1590 if (data->check_prefix) {
1591 if (data->first_check_printed) {
1592 printf("// %s-NEXT: ", data->check_prefix);
1593 } else {
1594 printf("// %s : ", data->check_prefix);
1595 data->first_check_printed = 1;
1596 }
1597 }
1598}
1599
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001600static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001601 CXString filename = clang_getFileName((CXFile)file);
1602 printf("%s", clang_getCString(filename));
1603 clang_disposeString(filename);
1604}
1605
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001606static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1607 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001608 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001609 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001610 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001611 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001612 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001613
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001614 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001615 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1616 if (line == 0) {
1617 printf("<null loc>");
1618 return;
1619 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001620 if (!file) {
1621 printf("<no idxfile>");
1622 return;
1623 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001624 filename = clang_getFileName((CXFile)file);
1625 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001626 if (strcmp(cname, index_data->main_filename) == 0)
1627 isMainFile = 1;
1628 else
1629 isMainFile = 0;
1630 clang_disposeString(filename);
1631
1632 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001633 printCXIndexFile(file);
1634 printf(":");
1635 }
1636 printf("%d:%d", line, column);
1637}
1638
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001639static unsigned digitCount(unsigned val) {
1640 unsigned c = 1;
1641 while (1) {
1642 if (val < 10)
1643 return c;
1644 ++c;
1645 val /= 10;
1646 }
1647}
1648
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001649static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1650 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001651 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001652 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001653 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001654 unsigned line, column;
1655
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001656 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001657 if (!name)
1658 name = "<anon-tag>";
1659
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001660 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001661 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001662 newStr = (char *)malloc(strlen(name) +
1663 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001664 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001665 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001666}
1667
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001668static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1669 CXIdxClientContainer container;
1670 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001671 if (!container)
1672 printf("[<<NULL>>]");
1673 else
1674 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001675}
1676
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001677static const char *getEntityKindString(CXIdxEntityKind kind) {
1678 switch (kind) {
1679 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1680 case CXIdxEntity_Typedef: return "typedef";
1681 case CXIdxEntity_Function: return "function";
1682 case CXIdxEntity_Variable: return "variable";
1683 case CXIdxEntity_Field: return "field";
1684 case CXIdxEntity_EnumConstant: return "enumerator";
1685 case CXIdxEntity_ObjCClass: return "objc-class";
1686 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1687 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001688 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1689 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001690 case CXIdxEntity_ObjCProperty: return "objc-property";
1691 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1692 case CXIdxEntity_Enum: return "enum";
1693 case CXIdxEntity_Struct: return "struct";
1694 case CXIdxEntity_Union: return "union";
1695 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001696 case CXIdxEntity_CXXNamespace: return "namespace";
1697 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1698 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1699 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1700 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1701 case CXIdxEntity_CXXConstructor: return "constructor";
1702 case CXIdxEntity_CXXDestructor: return "destructor";
1703 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1704 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1705 }
1706 assert(0 && "Garbage entity kind");
1707 return 0;
1708}
1709
1710static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1711 switch (kind) {
1712 case CXIdxEntity_NonTemplate: return "";
1713 case CXIdxEntity_Template: return "-template";
1714 case CXIdxEntity_TemplatePartialSpecialization:
1715 return "-template-partial-spec";
1716 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001717 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001718 assert(0 && "Garbage entity kind");
1719 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001720}
1721
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001722static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1723 switch (kind) {
1724 case CXIdxEntityLang_None: return "<none>";
1725 case CXIdxEntityLang_C: return "C";
1726 case CXIdxEntityLang_ObjC: return "ObjC";
1727 case CXIdxEntityLang_CXX: return "C++";
1728 }
1729 assert(0 && "Garbage language kind");
1730 return 0;
1731}
1732
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001733static void printEntityInfo(const char *cb,
1734 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001735 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001736 const char *name;
1737 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001738 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001739 index_data = (IndexData *)client_data;
1740 printCheck(index_data);
1741
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001742 if (!info) {
1743 printf("%s: <<NULL>>", cb);
1744 return;
1745 }
1746
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001747 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001748 if (!name)
1749 name = "<anon-tag>";
1750
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001751 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1752 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001753 printf(" | name: %s", name);
1754 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001755 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001756
1757 for (i = 0; i != info->numAttributes; ++i) {
1758 const CXIdxAttrInfo *Attr = info->attributes[i];
1759 printf(" <attribute>: ");
1760 PrintCursor(Attr->cursor);
1761 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001762}
1763
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001764static void printBaseClassInfo(CXClientData client_data,
1765 const CXIdxBaseClassInfo *info) {
1766 printEntityInfo(" <base>", client_data, info->base);
1767 printf(" | cursor: ");
1768 PrintCursor(info->cursor);
1769 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001770 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001771}
1772
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001773static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1774 CXClientData client_data) {
1775 unsigned i;
1776 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1777 printEntityInfo(" <protocol>", client_data,
1778 ProtoInfo->protocols[i]->protocol);
1779 printf(" | cursor: ");
1780 PrintCursor(ProtoInfo->protocols[i]->cursor);
1781 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001782 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001783 printf("\n");
1784 }
1785}
1786
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001787static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001788 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001789 CXString str;
1790 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001791 unsigned numDiags, i;
1792 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001793 IndexData *index_data;
1794 index_data = (IndexData *)client_data;
1795 printCheck(index_data);
1796
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001797 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1798 for (i = 0; i != numDiags; ++i) {
1799 diag = clang_getDiagnosticInSet(diagSet, i);
1800 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1801 cstr = clang_getCString(str);
1802 printf("[diagnostic]: %s\n", cstr);
1803 clang_disposeString(str);
1804
1805 if (getenv("CINDEXTEST_FAILONERROR") &&
1806 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1807 index_data->fail_for_error = 1;
1808 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001809 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001810}
1811
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001812static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1813 CXFile file, void *reserved) {
1814 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001815 CXString filename;
1816
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001817 index_data = (IndexData *)client_data;
1818 printCheck(index_data);
1819
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00001820 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001821 index_data->main_filename = clang_getCString(filename);
1822 clang_disposeString(filename);
1823
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001824 printf("[enteredMainFile]: ");
1825 printCXIndexFile((CXIdxClientFile)file);
1826 printf("\n");
1827
1828 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001829}
1830
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001831static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001832 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001833 IndexData *index_data;
1834 index_data = (IndexData *)client_data;
1835 printCheck(index_data);
1836
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001837 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001838 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001839 printf(" | name: \"%s\"", info->filename);
1840 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001841 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001842 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001843
1844 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001845}
1846
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001847static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001848 void *reserved) {
1849 IndexData *index_data;
1850 index_data = (IndexData *)client_data;
1851 printCheck(index_data);
1852
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001853 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001854 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001855}
1856
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001857static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001858 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001859 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001860 const CXIdxObjCCategoryDeclInfo *CatInfo;
1861 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001862 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001863 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001864 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001865 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001866 index_data = (IndexData *)client_data;
1867
1868 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1869 printf(" | cursor: ");
1870 PrintCursor(info->cursor);
1871 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001872 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001873 printf(" | semantic-container: ");
1874 printCXIndexContainer(info->semanticContainer);
1875 printf(" | lexical-container: ");
1876 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001877 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001878 printf(" | isDef: %d", info->isDefinition);
1879 printf(" | isContainer: %d", info->isContainer);
1880 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001881
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001882 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001883 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001884 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001885 PrintCursor(Attr->cursor);
1886 printf("\n");
1887 }
1888
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001889 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1890 const char *kindName = 0;
1891 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1892 switch (K) {
1893 case CXIdxObjCContainer_ForwardRef:
1894 kindName = "forward-ref"; break;
1895 case CXIdxObjCContainer_Interface:
1896 kindName = "interface"; break;
1897 case CXIdxObjCContainer_Implementation:
1898 kindName = "implementation"; break;
1899 }
1900 printCheck(index_data);
1901 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1902 }
1903
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001904 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001905 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1906 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001907 printf(" | cursor: ");
1908 PrintCursor(CatInfo->classCursor);
1909 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001910 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001911 printf("\n");
1912 }
1913
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001914 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1915 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001916 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001917 printf("\n");
1918 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001919 }
1920
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001921 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1922 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001923 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001924
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00001925 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
1926 if (PropInfo->getter) {
1927 printEntityInfo(" <getter>", client_data, PropInfo->getter);
1928 printf("\n");
1929 }
1930 if (PropInfo->setter) {
1931 printEntityInfo(" <setter>", client_data, PropInfo->setter);
1932 printf("\n");
1933 }
1934 }
1935
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001936 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1937 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1938 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1939 printf("\n");
1940 }
1941 }
1942
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001943 if (info->declAsContainer)
1944 clang_index_setClientContainer(info->declAsContainer,
1945 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001946}
1947
1948static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001949 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001950 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001951 printf(" | cursor: ");
1952 PrintCursor(info->cursor);
1953 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001954 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001955 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001956 printf(" | container: ");
1957 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001958 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001959 switch (info->kind) {
1960 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001961 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001962 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001963 printf("\n");
1964}
1965
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001966static int index_abortQuery(CXClientData client_data, void *reserved) {
1967 IndexData *index_data;
1968 index_data = (IndexData *)client_data;
1969 return index_data->abort;
1970}
1971
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001972static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001973 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001974 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001975 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001976 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001977 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001978 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001979 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001980 index_indexEntityReference
1981};
1982
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00001983static unsigned getIndexOptions(void) {
1984 unsigned index_opts;
1985 index_opts = 0;
1986 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1987 index_opts |= CXIndexOpt_SuppressRedundantRefs;
1988 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
1989 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
1990
1991 return index_opts;
1992}
1993
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001994static int index_file(int argc, const char **argv) {
1995 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001996 CXIndex Idx;
1997 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001998 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001999 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002000 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002001
2002 check_prefix = 0;
2003 if (argc > 0) {
2004 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2005 check_prefix = argv[0] + strlen("-check-prefix=");
2006 ++argv;
2007 --argc;
2008 }
2009 }
2010
2011 if (argc == 0) {
2012 fprintf(stderr, "no compiler arguments\n");
2013 return -1;
2014 }
2015
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002016 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2017 /* displayDiagnosics=*/1))) {
2018 fprintf(stderr, "Could not create Index\n");
2019 return 1;
2020 }
2021 idxAction = 0;
2022 result = 1;
2023
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002024 index_data.check_prefix = check_prefix;
2025 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002026 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002027 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002028
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002029 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002030 idxAction = clang_IndexAction_create(Idx);
2031 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002032 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002033 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002034 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002035 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002036
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002037 clang_IndexAction_dispose(idxAction);
2038 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002039 return result;
2040}
2041
2042static int index_tu(int argc, const char **argv) {
2043 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002044 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002045 CXTranslationUnit TU;
2046 const char *check_prefix;
2047 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002048 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002049 int result;
2050
2051 check_prefix = 0;
2052 if (argc > 0) {
2053 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2054 check_prefix = argv[0] + strlen("-check-prefix=");
2055 ++argv;
2056 --argc;
2057 }
2058 }
2059
2060 if (argc == 0) {
2061 fprintf(stderr, "no ast file\n");
2062 return -1;
2063 }
2064
2065 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2066 /* displayDiagnosics=*/1))) {
2067 fprintf(stderr, "Could not create Index\n");
2068 return 1;
2069 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002070 idxAction = 0;
2071 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002072
2073 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002074 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002075
2076 index_data.check_prefix = check_prefix;
2077 index_data.first_check_printed = 0;
2078 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002079 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002080
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002081 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002082 idxAction = clang_IndexAction_create(Idx);
2083 result = clang_indexTranslationUnit(idxAction, &index_data,
2084 &IndexCB,sizeof(IndexCB),
2085 index_opts, TU);
2086 if (index_data.fail_for_error)
2087 goto finished;
2088
2089 finished:
2090 clang_IndexAction_dispose(idxAction);
2091 clang_disposeIndex(Idx);
2092
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002093 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002094}
2095
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002096int perform_token_annotation(int argc, const char **argv) {
2097 const char *input = argv[1];
2098 char *filename = 0;
2099 unsigned line, second_line;
2100 unsigned column, second_column;
2101 CXIndex CIdx;
2102 CXTranslationUnit TU = 0;
2103 int errorCode;
2104 struct CXUnsavedFile *unsaved_files = 0;
2105 int num_unsaved_files = 0;
2106 CXToken *tokens;
2107 unsigned num_tokens;
2108 CXSourceRange range;
2109 CXSourceLocation startLoc, endLoc;
2110 CXFile file = 0;
2111 CXCursor *cursors = 0;
2112 unsigned i;
2113
2114 input += strlen("-test-annotate-tokens=");
2115 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2116 &second_line, &second_column)))
2117 return errorCode;
2118
2119 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2120 return -1;
2121
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002122 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002123 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2124 argv + num_unsaved_files + 2,
2125 argc - num_unsaved_files - 3,
2126 unsaved_files,
2127 num_unsaved_files,
2128 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002129 if (!TU) {
2130 fprintf(stderr, "unable to parse input\n");
2131 clang_disposeIndex(CIdx);
2132 free(filename);
2133 free_remapped_files(unsaved_files, num_unsaved_files);
2134 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002135 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002136 errorCode = 0;
2137
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002138 if (checkForErrors(TU) != 0)
2139 return -1;
2140
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002141 if (getenv("CINDEXTEST_EDITING")) {
2142 for (i = 0; i < 5; ++i) {
2143 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2144 clang_defaultReparseOptions(TU))) {
2145 fprintf(stderr, "Unable to reparse translation unit!\n");
2146 errorCode = -1;
2147 goto teardown;
2148 }
2149 }
2150 }
2151
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002152 if (checkForErrors(TU) != 0) {
2153 errorCode = -1;
2154 goto teardown;
2155 }
2156
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002157 file = clang_getFile(TU, filename);
2158 if (!file) {
2159 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2160 errorCode = -1;
2161 goto teardown;
2162 }
2163
2164 startLoc = clang_getLocation(TU, file, line, column);
2165 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002166 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002167 column);
2168 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002169 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002170 }
2171
2172 endLoc = clang_getLocation(TU, file, second_line, second_column);
2173 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002174 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002175 second_line, second_column);
2176 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002177 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002178 }
2179
2180 range = clang_getRange(startLoc, endLoc);
2181 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002182
2183 if (checkForErrors(TU) != 0) {
2184 errorCode = -1;
2185 goto teardown;
2186 }
2187
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002188 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2189 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002190
2191 if (checkForErrors(TU) != 0) {
2192 errorCode = -1;
2193 goto teardown;
2194 }
2195
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002196 for (i = 0; i != num_tokens; ++i) {
2197 const char *kind = "<unknown>";
2198 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2199 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2200 unsigned start_line, start_column, end_line, end_column;
2201
2202 switch (clang_getTokenKind(tokens[i])) {
2203 case CXToken_Punctuation: kind = "Punctuation"; break;
2204 case CXToken_Keyword: kind = "Keyword"; break;
2205 case CXToken_Identifier: kind = "Identifier"; break;
2206 case CXToken_Literal: kind = "Literal"; break;
2207 case CXToken_Comment: kind = "Comment"; break;
2208 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002209 clang_getSpellingLocation(clang_getRangeStart(extent),
2210 0, &start_line, &start_column, 0);
2211 clang_getSpellingLocation(clang_getRangeEnd(extent),
2212 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002213 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2214 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002215 if (!clang_isInvalid(cursors[i].kind)) {
2216 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002217 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002218 }
2219 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002220 }
2221 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002222 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002223
2224 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002225 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002226 clang_disposeTranslationUnit(TU);
2227 clang_disposeIndex(CIdx);
2228 free(filename);
2229 free_remapped_files(unsaved_files, num_unsaved_files);
2230 return errorCode;
2231}
2232
Ted Kremenek0d435192009-11-17 18:13:31 +00002233/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002234/* USR printing. */
2235/******************************************************************************/
2236
2237static int insufficient_usr(const char *kind, const char *usage) {
2238 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2239 return 1;
2240}
2241
2242static unsigned isUSR(const char *s) {
2243 return s[0] == 'c' && s[1] == ':';
2244}
2245
2246static int not_usr(const char *s, const char *arg) {
2247 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2248 return 1;
2249}
2250
2251static void print_usr(CXString usr) {
2252 const char *s = clang_getCString(usr);
2253 printf("%s\n", s);
2254 clang_disposeString(usr);
2255}
2256
2257static void display_usrs() {
2258 fprintf(stderr, "-print-usrs options:\n"
2259 " ObjCCategory <class name> <category name>\n"
2260 " ObjCClass <class name>\n"
2261 " ObjCIvar <ivar name> <class USR>\n"
2262 " ObjCMethod <selector> [0=class method|1=instance method] "
2263 "<class USR>\n"
2264 " ObjCProperty <property name> <class USR>\n"
2265 " ObjCProtocol <protocol name>\n");
2266}
2267
2268int print_usrs(const char **I, const char **E) {
2269 while (I != E) {
2270 const char *kind = *I;
2271 unsigned len = strlen(kind);
2272 switch (len) {
2273 case 8:
2274 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2275 if (I + 2 >= E)
2276 return insufficient_usr(kind, "<ivar name> <class USR>");
2277 if (!isUSR(I[2]))
2278 return not_usr("<class USR>", I[2]);
2279 else {
2280 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002281 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002282 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002283 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2284 }
2285
2286 I += 3;
2287 continue;
2288 }
2289 break;
2290 case 9:
2291 if (memcmp(kind, "ObjCClass", 9) == 0) {
2292 if (I + 1 >= E)
2293 return insufficient_usr(kind, "<class name>");
2294 print_usr(clang_constructUSR_ObjCClass(I[1]));
2295 I += 2;
2296 continue;
2297 }
2298 break;
2299 case 10:
2300 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2301 if (I + 3 >= E)
2302 return insufficient_usr(kind, "<method selector> "
2303 "[0=class method|1=instance method] <class USR>");
2304 if (!isUSR(I[3]))
2305 return not_usr("<class USR>", I[3]);
2306 else {
2307 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002308 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002309 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002310 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2311 }
2312 I += 4;
2313 continue;
2314 }
2315 break;
2316 case 12:
2317 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2318 if (I + 2 >= E)
2319 return insufficient_usr(kind, "<class name> <category name>");
2320 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2321 I += 3;
2322 continue;
2323 }
2324 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2325 if (I + 1 >= E)
2326 return insufficient_usr(kind, "<protocol name>");
2327 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2328 I += 2;
2329 continue;
2330 }
2331 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2332 if (I + 2 >= E)
2333 return insufficient_usr(kind, "<property name> <class USR>");
2334 if (!isUSR(I[2]))
2335 return not_usr("<class USR>", I[2]);
2336 else {
2337 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002338 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002339 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002340 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2341 }
2342 I += 3;
2343 continue;
2344 }
2345 break;
2346 default:
2347 break;
2348 }
2349 break;
2350 }
2351
2352 if (I != E) {
2353 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2354 display_usrs();
2355 return 1;
2356 }
2357 return 0;
2358}
2359
2360int print_usrs_file(const char *file_name) {
2361 char line[2048];
2362 const char *args[128];
2363 unsigned numChars = 0;
2364
2365 FILE *fp = fopen(file_name, "r");
2366 if (!fp) {
2367 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2368 return 1;
2369 }
2370
2371 /* This code is not really all that safe, but it works fine for testing. */
2372 while (!feof(fp)) {
2373 char c = fgetc(fp);
2374 if (c == '\n') {
2375 unsigned i = 0;
2376 const char *s = 0;
2377
2378 if (numChars == 0)
2379 continue;
2380
2381 line[numChars] = '\0';
2382 numChars = 0;
2383
2384 if (line[0] == '/' && line[1] == '/')
2385 continue;
2386
2387 s = strtok(line, " ");
2388 while (s) {
2389 args[i] = s;
2390 ++i;
2391 s = strtok(0, " ");
2392 }
2393 if (print_usrs(&args[0], &args[i]))
2394 return 1;
2395 }
2396 else
2397 line[numChars++] = c;
2398 }
2399
2400 fclose(fp);
2401 return 0;
2402}
2403
2404/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002405/* Command line processing. */
2406/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002407int write_pch_file(const char *filename, int argc, const char *argv[]) {
2408 CXIndex Idx;
2409 CXTranslationUnit TU;
2410 struct CXUnsavedFile *unsaved_files = 0;
2411 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002412 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002413
2414 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2415
2416 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2417 clang_disposeIndex(Idx);
2418 return -1;
2419 }
2420
2421 TU = clang_parseTranslationUnit(Idx, 0,
2422 argv + num_unsaved_files,
2423 argc - num_unsaved_files,
2424 unsaved_files,
2425 num_unsaved_files,
2426 CXTranslationUnit_Incomplete);
2427 if (!TU) {
2428 fprintf(stderr, "Unable to load translation unit!\n");
2429 free_remapped_files(unsaved_files, num_unsaved_files);
2430 clang_disposeIndex(Idx);
2431 return 1;
2432 }
2433
Douglas Gregor39c411f2011-07-06 16:43:36 +00002434 switch (clang_saveTranslationUnit(TU, filename,
2435 clang_defaultSaveOptions(TU))) {
2436 case CXSaveError_None:
2437 break;
2438
2439 case CXSaveError_TranslationErrors:
2440 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2441 filename);
2442 result = 2;
2443 break;
2444
2445 case CXSaveError_InvalidTU:
2446 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2447 filename);
2448 result = 3;
2449 break;
2450
2451 case CXSaveError_Unknown:
2452 default:
2453 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2454 result = 1;
2455 break;
2456 }
2457
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002458 clang_disposeTranslationUnit(TU);
2459 free_remapped_files(unsaved_files, num_unsaved_files);
2460 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002461 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002462}
2463
2464/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002465/* Serialized diagnostics. */
2466/******************************************************************************/
2467
2468static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2469 switch (error) {
2470 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2471 case CXLoadDiag_None: break;
2472 case CXLoadDiag_Unknown: return "Unknown";
2473 case CXLoadDiag_InvalidFile: return "Invalid File";
2474 }
2475 return "None";
2476}
2477
2478static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2479 switch (severity) {
2480 case CXDiagnostic_Note: return "note";
2481 case CXDiagnostic_Error: return "error";
2482 case CXDiagnostic_Fatal: return "fatal";
2483 case CXDiagnostic_Ignored: return "ignored";
2484 case CXDiagnostic_Warning: return "warning";
2485 }
2486 return "unknown";
2487}
2488
2489static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002490 if (indent == 0)
2491 return;
2492 fprintf(stderr, "+");
2493 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002494 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002495 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002496 --indent;
2497 }
2498}
2499
2500static void printLocation(CXSourceLocation L) {
2501 CXFile File;
2502 CXString FileName;
2503 unsigned line, column, offset;
2504
2505 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2506 FileName = clang_getFileName(File);
2507
2508 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2509 clang_disposeString(FileName);
2510}
2511
2512static void printRanges(CXDiagnostic D, unsigned indent) {
2513 unsigned i, n = clang_getDiagnosticNumRanges(D);
2514
2515 for (i = 0; i < n; ++i) {
2516 CXSourceLocation Start, End;
2517 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2518 Start = clang_getRangeStart(SR);
2519 End = clang_getRangeEnd(SR);
2520
2521 printIndent(indent);
2522 fprintf(stderr, "Range: ");
2523 printLocation(Start);
2524 fprintf(stderr, " ");
2525 printLocation(End);
2526 fprintf(stderr, "\n");
2527 }
2528}
2529
2530static void printFixIts(CXDiagnostic D, unsigned indent) {
2531 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002532 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002533 for (i = 0 ; i < n; ++i) {
2534 CXSourceRange ReplacementRange;
2535 CXString text;
2536 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2537
2538 printIndent(indent);
2539 fprintf(stderr, "FIXIT: (");
2540 printLocation(clang_getRangeStart(ReplacementRange));
2541 fprintf(stderr, " - ");
2542 printLocation(clang_getRangeEnd(ReplacementRange));
2543 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2544 clang_disposeString(text);
2545 }
2546}
2547
2548static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002549 unsigned i, n;
2550
Ted Kremenek15322172011-11-10 08:43:12 +00002551 if (!Diags)
2552 return;
2553
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002554 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002555 for (i = 0; i < n; ++i) {
2556 CXSourceLocation DiagLoc;
2557 CXDiagnostic D;
2558 CXFile File;
2559 CXString FileName, DiagSpelling, DiagOption;
2560 unsigned line, column, offset;
2561 const char *DiagOptionStr = 0;
2562
2563 D = clang_getDiagnosticInSet(Diags, i);
2564 DiagLoc = clang_getDiagnosticLocation(D);
2565 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2566 FileName = clang_getFileName(File);
2567 DiagSpelling = clang_getDiagnosticSpelling(D);
2568
2569 printIndent(indent);
2570
2571 fprintf(stderr, "%s:%d:%d: %s: %s",
2572 clang_getCString(FileName),
2573 line,
2574 column,
2575 getSeverityString(clang_getDiagnosticSeverity(D)),
2576 clang_getCString(DiagSpelling));
2577
2578 DiagOption = clang_getDiagnosticOption(D, 0);
2579 DiagOptionStr = clang_getCString(DiagOption);
2580 if (DiagOptionStr) {
2581 fprintf(stderr, " [%s]", DiagOptionStr);
2582 }
2583
2584 fprintf(stderr, "\n");
2585
2586 printRanges(D, indent);
2587 printFixIts(D, indent);
2588
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002589 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002590 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2591
2592 clang_disposeString(FileName);
2593 clang_disposeString(DiagSpelling);
2594 clang_disposeString(DiagOption);
2595 }
2596}
2597
2598static int read_diagnostics(const char *filename) {
2599 enum CXLoadDiag_Error error;
2600 CXString errorString;
2601 CXDiagnosticSet Diags = 0;
2602
2603 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2604 if (!Diags) {
2605 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2606 getDiagnosticCodeStr(error),
2607 clang_getCString(errorString));
2608 clang_disposeString(errorString);
2609 return 1;
2610 }
2611
2612 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002613 fprintf(stderr, "Number of diagnostics: %d\n",
2614 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002615 clang_disposeDiagnosticSet(Diags);
2616 return 0;
2617}
2618
2619/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002620/* Command line processing. */
2621/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002622
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002623static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002624 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002625 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002626 if (strcmp(s, "-usrs") == 0)
2627 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002628 if (strncmp(s, "-memory-usage", 13) == 0)
2629 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002630 return NULL;
2631}
2632
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002633static void print_usage(void) {
2634 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002635 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002636 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002637 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002638 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002639 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002640 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002641 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002642 "[FileCheck prefix]\n");
2643 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002644 " c-index-test -test-load-tu <AST file> <symbol filter> "
2645 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002646 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2647 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002648 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002649 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002650 " c-index-test -test-load-source-memory-usage "
2651 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002652 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2653 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002654 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002655 " c-index-test -test-load-source-usrs-memory-usage "
2656 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002657 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2658 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002659 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002660 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002661 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002662 " c-index-test -test-print-typekind {<args>}*\n"
2663 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002664 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002665 " c-index-test -write-pch <file> <compiler arguments>\n");
2666 fprintf(stderr,
2667 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002668 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002669 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002670 " all - load all symbols, including those from PCH\n"
2671 " local - load all symbols except those in PCH\n"
2672 " category - only load ObjC categories (non-PCH)\n"
2673 " interface - only load ObjC interfaces (non-PCH)\n"
2674 " protocol - only load ObjC protocols (non-PCH)\n"
2675 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002676 " typedef - only load typdefs (non-PCH)\n"
2677 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002678}
2679
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002680/***/
2681
2682int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002683 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002684 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2685 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002686 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002687 return perform_code_completion(argc, argv, 0);
2688 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2689 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002690 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2691 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002692 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2693 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002694 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2695 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002696 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2697 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002698 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002699 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002700 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002701 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2702 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002703 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002704 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2705 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2706 if (I) {
2707 int trials = atoi(argv[2]);
2708 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2709 NULL);
2710 }
2711 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002712 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002713 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002714
2715 PostVisitTU postVisit = 0;
2716 if (strstr(argv[1], "-memory-usage"))
2717 postVisit = PrintMemoryUsage;
2718
Ted Kremenek7d405622010-01-12 23:34:26 +00002719 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002720 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2721 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002722 }
2723 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002724 return perform_file_scan(argv[2], argv[3],
2725 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002726 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2727 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002728 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2729 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2730 PrintInclusionStack);
2731 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2732 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2733 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002734 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2735 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2736 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002737 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2738 return perform_test_load_source(argc - 2, argv + 2, "all",
2739 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002740 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2741 if (argc > 2)
2742 return print_usrs(argv + 2, argv + argc);
2743 else {
2744 display_usrs();
2745 return 1;
2746 }
2747 }
2748 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2749 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002750 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2751 return write_pch_file(argv[2], argc - 3, argv + 3);
2752
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002753 print_usage();
2754 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002755}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002756
2757/***/
2758
2759/* We intentionally run in a separate thread to ensure we at least minimal
2760 * testing of a multithreaded environment (for example, having a reduced stack
2761 * size). */
2762
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002763typedef struct thread_info {
2764 int argc;
2765 const char **argv;
2766 int result;
2767} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002768void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002769 thread_info *client_data = client_data_v;
2770 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002771}
2772
2773int main(int argc, const char **argv) {
2774 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002775
Douglas Gregor61605982010-10-27 16:00:01 +00002776 if (getenv("CINDEXTEST_NOTHREADS"))
2777 return cindextest_main(argc, argv);
2778
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002779 client_data.argc = argc;
2780 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002781 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002782 return client_data.result;
2783}