blob: eee7f46ee41a1dddc1bdf1f36e1783ed1c305cbf [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);
384 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000385 CXSourceRange range;
386 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
387 CXSourceLocation start = clang_getRangeStart(range);
388 CXSourceLocation end = clang_getRangeEnd(range);
389 unsigned start_line, start_column, end_line, end_column;
390 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000391 clang_getSpellingLocation(start, &start_file, &start_line,
392 &start_column, 0);
393 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000394 if (clang_equalLocations(start, end)) {
395 /* Insertion. */
396 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000397 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000398 clang_getCString(insertion_text), start_line, start_column);
399 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
400 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000401 if (start_file == file && end_file == file) {
402 fprintf(out, "FIX-IT: Remove ");
403 PrintExtent(out, start_line, start_column, end_line, end_column);
404 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000405 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000406 } else {
407 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000408 if (start_file == end_file) {
409 fprintf(out, "FIX-IT: Replace ");
410 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000411 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000412 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000413 break;
414 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000415 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000416 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000417}
418
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000419void PrintDiagnosticSet(CXDiagnosticSet Set) {
420 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
421 for ( ; i != n ; ++i) {
422 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
423 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000424 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000425 if (ChildDiags)
426 PrintDiagnosticSet(ChildDiags);
427 }
428}
429
430void PrintDiagnostics(CXTranslationUnit TU) {
431 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
432 PrintDiagnosticSet(TUSet);
433 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000434}
435
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000436void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000437 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000438 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000439 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000440 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000441 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000442 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000443 unsigned long amount = usage.entries[i].amount;
444 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000445 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000446 ((double) amount)/(1024*1024));
447 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000448 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000449 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000450 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000451}
452
Ted Kremenekce2ae882010-01-26 17:59:48 +0000453/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000454/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000455/******************************************************************************/
456
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000457static const char *FileCheckPrefix = "CHECK";
458
Douglas Gregora7bde202010-01-19 00:34:46 +0000459static void PrintCursorExtent(CXCursor C) {
460 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000461 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000462}
463
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000464/* Data used by all of the visitors. */
465typedef struct {
466 CXTranslationUnit TU;
467 enum CXCursorKind *Filter;
468} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000469
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000470
Ted Kremeneke68fff62010-02-17 00:41:32 +0000471enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000472 CXCursor Parent,
473 CXClientData ClientData) {
474 VisitorData *Data = (VisitorData *)ClientData;
475 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000476 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000477 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000478 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000479 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000480 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000481 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000482 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000483 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000484 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000485 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000486
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000487 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000488}
Steve Naroff50398192009-08-28 15:28:48 +0000489
Ted Kremeneke68fff62010-02-17 00:41:32 +0000490static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000491 CXCursor Parent,
492 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000493 const char *startBuf, *endBuf;
494 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
495 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000496 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000497
Douglas Gregorb6998662010-01-19 19:34:47 +0000498 if (Cursor.kind != CXCursor_FunctionDecl ||
499 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000500 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000501
502 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
503 &startLine, &startColumn,
504 &endLine, &endColumn);
505 /* Probe the entire body, looking for both decls and refs. */
506 curLine = startLine;
507 curColumn = startColumn;
508
509 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000510 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000511 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000512 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000513
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000514 if (*startBuf == '\n') {
515 startBuf++;
516 curLine++;
517 curColumn = 1;
518 } else if (*startBuf != '\t')
519 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000520
Douglas Gregor98258af2010-01-18 22:46:11 +0000521 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000522 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000523
Douglas Gregor1db19de2010-01-19 21:36:55 +0000524 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000525 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000526 CXSourceLocation RefLoc
527 = clang_getLocation(Data->TU, file, curLine, curColumn);
528 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000529 if (Ref.kind == CXCursor_NoDeclFound) {
530 /* Nothing found here; that's fine. */
531 } else if (Ref.kind != CXCursor_FunctionDecl) {
532 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
533 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000534 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000535 printf("\n");
536 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000537 }
Ted Kremenek74844072010-02-17 00:41:20 +0000538 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000539 startBuf++;
540 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000541
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000542 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000543}
544
Ted Kremenek7d405622010-01-12 23:34:26 +0000545/******************************************************************************/
546/* USR testing. */
547/******************************************************************************/
548
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000549enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
550 CXClientData ClientData) {
551 VisitorData *Data = (VisitorData *)ClientData;
552 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000553 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000554 const char *cstr = clang_getCString(USR);
555 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000556 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000557 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000558 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000559 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
560
Douglas Gregora7bde202010-01-19 00:34:46 +0000561 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000562 printf("\n");
563 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000564
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000565 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000566 }
567
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000568 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000569}
570
571/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000572/* Inclusion stack testing. */
573/******************************************************************************/
574
575void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
576 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000577
Ted Kremenek16b55a72010-01-26 19:31:51 +0000578 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000579 CXString fname;
580
581 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000582 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000583 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000584
Ted Kremenek16b55a72010-01-26 19:31:51 +0000585 for (i = 0; i < includeStackLen; ++i) {
586 CXFile includingFile;
587 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000588 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
589 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000590 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000591 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000592 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000593 }
594 printf("\n");
595}
596
597void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000598 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000599}
600
601/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000602/* Linkage testing. */
603/******************************************************************************/
604
605static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
606 CXClientData d) {
607 const char *linkage = 0;
608
609 if (clang_isInvalid(clang_getCursorKind(cursor)))
610 return CXChildVisit_Recurse;
611
612 switch (clang_getCursorLinkage(cursor)) {
613 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000614 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
615 case CXLinkage_Internal: linkage = "Internal"; break;
616 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
617 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000618 }
619
620 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000621 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000622 printf("linkage=%s\n", linkage);
623 }
624
625 return CXChildVisit_Recurse;
626}
627
628/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000629/* Typekind testing. */
630/******************************************************************************/
631
632static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
633 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000634 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
635 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000636 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000637 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000638 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000639 if (clang_isConstQualifiedType(T))
640 printf(" const");
641 if (clang_isVolatileQualifiedType(T))
642 printf(" volatile");
643 if (clang_isRestrictQualifiedType(T))
644 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000645 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000646 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000647 {
648 CXType CT = clang_getCanonicalType(T);
649 if (!clang_equalTypes(T, CT)) {
650 CXString CS = clang_getTypeKindSpelling(CT.kind);
651 printf(" [canonical=%s]", clang_getCString(CS));
652 clang_disposeString(CS);
653 }
654 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000655 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000656 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000657 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000658 if (RT.kind != CXType_Invalid) {
659 CXString RS = clang_getTypeKindSpelling(RT.kind);
660 printf(" [result=%s]", clang_getCString(RS));
661 clang_disposeString(RS);
662 }
663 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000664 /* Print if this is a non-POD type. */
665 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000666
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000667 printf("\n");
668 }
669 return CXChildVisit_Recurse;
670}
671
672
673/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000674/* Loading ASTs/source. */
675/******************************************************************************/
676
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000677static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000678 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000679 CXCursorVisitor Visitor,
680 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000681
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000682 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000684
685 if (Visitor) {
686 enum CXCursorKind K = CXCursor_NotImplemented;
687 enum CXCursorKind *ck = &K;
688 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000689
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000690 /* Perform some simple filtering. */
691 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000692 else if (!strcmp(filter, "all-display") ||
693 !strcmp(filter, "local-display")) {
694 ck = NULL;
695 want_display_name = 1;
696 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000697 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000698 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
699 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
700 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
701 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
702 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
703 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
704 else {
705 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
706 return 1;
707 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000708
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000709 Data.TU = TU;
710 Data.Filter = ck;
711 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000712 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000713
Ted Kremenekce2ae882010-01-26 17:59:48 +0000714 if (PV)
715 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000716
Douglas Gregora88084b2010-02-18 18:08:43 +0000717 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000718 if (checkForErrors(TU) != 0) {
719 clang_disposeTranslationUnit(TU);
720 return -1;
721 }
722
Ted Kremenek0d435192009-11-17 18:13:31 +0000723 clang_disposeTranslationUnit(TU);
724 return 0;
725}
726
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000727int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000728 const char *prefix, CXCursorVisitor Visitor,
729 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000730 CXIndex Idx;
731 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000732 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000733 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000734 !strcmp(filter, "local") ? 1 : 0,
735 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000736
Ted Kremenek020a0952010-02-11 07:41:25 +0000737 if (!CreateTranslationUnit(Idx, file, &TU)) {
738 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000739 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000740 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000741
Ted Kremenek020a0952010-02-11 07:41:25 +0000742 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
743 clang_disposeIndex(Idx);
744 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000745}
746
Ted Kremenekce2ae882010-01-26 17:59:48 +0000747int perform_test_load_source(int argc, const char **argv,
748 const char *filter, CXCursorVisitor Visitor,
749 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000750 CXIndex Idx;
751 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000752 struct CXUnsavedFile *unsaved_files = 0;
753 int num_unsaved_files = 0;
754 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000755
Daniel Dunbarada487d2009-12-01 02:03:10 +0000756 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000757 (!strcmp(filter, "local") ||
758 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000759 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000760
Ted Kremenek020a0952010-02-11 07:41:25 +0000761 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
762 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000763 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000764 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000765
Douglas Gregordca8ee82011-05-06 16:33:08 +0000766 TU = clang_parseTranslationUnit(Idx, 0,
767 argv + num_unsaved_files,
768 argc - num_unsaved_files,
769 unsaved_files, num_unsaved_files,
770 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000771 if (!TU) {
772 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000773 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000774 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000775 return 1;
776 }
777
Ted Kremenekce2ae882010-01-26 17:59:48 +0000778 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000779 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000780 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000781 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000782}
783
Douglas Gregorabc563f2010-07-19 21:46:24 +0000784int perform_test_reparse_source(int argc, const char **argv, int trials,
785 const char *filter, CXCursorVisitor Visitor,
786 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000787 CXIndex Idx;
788 CXTranslationUnit TU;
789 struct CXUnsavedFile *unsaved_files = 0;
790 int num_unsaved_files = 0;
791 int result;
792 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000793 int remap_after_trial = 0;
794 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000795
796 Idx = clang_createIndex(/* excludeDeclsFromPCH */
797 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000798 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000799
Douglas Gregorabc563f2010-07-19 21:46:24 +0000800 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
801 clang_disposeIndex(Idx);
802 return -1;
803 }
804
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000805 /* Load the initial translation unit -- we do this without honoring remapped
806 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000807 TU = clang_parseTranslationUnit(Idx, 0,
808 argv + num_unsaved_files,
809 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000810 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000811 if (!TU) {
812 fprintf(stderr, "Unable to load translation unit!\n");
813 free_remapped_files(unsaved_files, num_unsaved_files);
814 clang_disposeIndex(Idx);
815 return 1;
816 }
817
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000818 if (checkForErrors(TU) != 0)
819 return -1;
820
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000821 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
822 remap_after_trial =
823 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
824 }
825
Douglas Gregorabc563f2010-07-19 21:46:24 +0000826 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000827 if (clang_reparseTranslationUnit(TU,
828 trial >= remap_after_trial ? num_unsaved_files : 0,
829 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000830 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000831 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000832 clang_disposeTranslationUnit(TU);
833 free_remapped_files(unsaved_files, num_unsaved_files);
834 clang_disposeIndex(Idx);
835 return -1;
836 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000837
838 if (checkForErrors(TU) != 0)
839 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000840 }
841
842 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000843
Douglas Gregorabc563f2010-07-19 21:46:24 +0000844 free_remapped_files(unsaved_files, num_unsaved_files);
845 clang_disposeIndex(Idx);
846 return result;
847}
848
Ted Kremenek0d435192009-11-17 18:13:31 +0000849/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000850/* Logic for testing clang_getCursor(). */
851/******************************************************************************/
852
Douglas Gregordd3e5542011-05-04 00:14:37 +0000853static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000854 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000855 unsigned end_line, unsigned end_col,
856 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000857 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000858 if (prefix)
859 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000860 PrintExtent(stdout, start_line, start_col, end_line, end_col);
861 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000862 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000863 printf("\n");
864}
865
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000866static int perform_file_scan(const char *ast_file, const char *source_file,
867 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000868 CXIndex Idx;
869 CXTranslationUnit TU;
870 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000871 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000872 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000873 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000874 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000876 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
877 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000878 fprintf(stderr, "Could not create Index\n");
879 return 1;
880 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000881
Ted Kremenek1c6da172009-11-17 19:37:36 +0000882 if (!CreateTranslationUnit(Idx, ast_file, &TU))
883 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000884
Ted Kremenek1c6da172009-11-17 19:37:36 +0000885 if ((fp = fopen(source_file, "r")) == NULL) {
886 fprintf(stderr, "Could not open '%s'\n", source_file);
887 return 1;
888 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000889
Douglas Gregorb9790342010-01-22 21:44:22 +0000890 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000891 for (;;) {
892 CXCursor cursor;
893 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000894
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000895 if (c == '\n') {
896 ++line;
897 col = 1;
898 } else
899 ++col;
900
901 /* Check the cursor at this position, and dump the previous one if we have
902 * found something new.
903 */
904 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
905 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
906 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000907 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000908 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000909 start_line = line;
910 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000911 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000912 if (c == EOF)
913 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000914
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000915 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000916 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000917
Ted Kremenek1c6da172009-11-17 19:37:36 +0000918 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000919 clang_disposeTranslationUnit(TU);
920 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000921 return 0;
922}
923
924/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000925/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000926/******************************************************************************/
927
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000928/* Parse file:line:column from the input string. Returns 0 on success, non-zero
929 on failure. If successful, the pointer *filename will contain newly-allocated
930 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000931int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000932 unsigned *column, unsigned *second_line,
933 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000934 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000935 const char *last_colon = strrchr(input, ':');
936 unsigned values[4], i;
937 unsigned num_values = (second_line && second_column)? 4 : 2;
938
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000939 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000940 if (!last_colon || last_colon == input) {
941 if (num_values == 4)
942 fprintf(stderr, "could not parse filename:line:column:line:column in "
943 "'%s'\n", input);
944 else
945 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000946 return 1;
947 }
948
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000949 for (i = 0; i != num_values; ++i) {
950 const char *prev_colon;
951
952 /* Parse the next line or column. */
953 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
954 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000955 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000956 (i % 2 ? "column" : "line"), input);
957 return 1;
958 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000959
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000960 if (i + 1 == num_values)
961 break;
962
963 /* Find the previous colon. */
964 prev_colon = last_colon - 1;
965 while (prev_colon != input && *prev_colon != ':')
966 --prev_colon;
967 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000968 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000969 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000970 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000971 }
972
973 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000974 }
975
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000976 *line = values[0];
977 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000978
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000979 if (second_line && second_column) {
980 *second_line = values[2];
981 *second_column = values[3];
982 }
983
Douglas Gregor88d23952009-11-09 18:19:57 +0000984 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000985 *filename = (char*)malloc(last_colon - input + 1);
986 memcpy(*filename, input, last_colon - input);
987 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000988 return 0;
989}
990
991const char *
992clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
993 switch (Kind) {
994 case CXCompletionChunk_Optional: return "Optional";
995 case CXCompletionChunk_TypedText: return "TypedText";
996 case CXCompletionChunk_Text: return "Text";
997 case CXCompletionChunk_Placeholder: return "Placeholder";
998 case CXCompletionChunk_Informative: return "Informative";
999 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1000 case CXCompletionChunk_LeftParen: return "LeftParen";
1001 case CXCompletionChunk_RightParen: return "RightParen";
1002 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1003 case CXCompletionChunk_RightBracket: return "RightBracket";
1004 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1005 case CXCompletionChunk_RightBrace: return "RightBrace";
1006 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1007 case CXCompletionChunk_RightAngle: return "RightAngle";
1008 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001009 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001010 case CXCompletionChunk_Colon: return "Colon";
1011 case CXCompletionChunk_SemiColon: return "SemiColon";
1012 case CXCompletionChunk_Equal: return "Equal";
1013 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1014 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001015 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001016
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001017 return "Unknown";
1018}
1019
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001020static int checkForErrors(CXTranslationUnit TU) {
1021 unsigned Num, i;
1022 CXDiagnostic Diag;
1023 CXString DiagStr;
1024
1025 if (!getenv("CINDEXTEST_FAILONERROR"))
1026 return 0;
1027
1028 Num = clang_getNumDiagnostics(TU);
1029 for (i = 0; i != Num; ++i) {
1030 Diag = clang_getDiagnostic(TU, i);
1031 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1032 DiagStr = clang_formatDiagnostic(Diag,
1033 clang_defaultDiagnosticDisplayOptions());
1034 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1035 clang_disposeString(DiagStr);
1036 clang_disposeDiagnostic(Diag);
1037 return -1;
1038 }
1039 clang_disposeDiagnostic(Diag);
1040 }
1041
1042 return 0;
1043}
1044
Douglas Gregor3ac73852009-11-09 16:04:45 +00001045void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001046 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001047
Douglas Gregor3ac73852009-11-09 16:04:45 +00001048 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001049 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001050 CXString text;
1051 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001052 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001053 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001054
Douglas Gregor3ac73852009-11-09 16:04:45 +00001055 if (Kind == CXCompletionChunk_Optional) {
1056 fprintf(file, "{Optional ");
1057 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001058 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001059 file);
1060 fprintf(file, "}");
1061 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001062 }
1063
1064 if (Kind == CXCompletionChunk_VerticalSpace) {
1065 fprintf(file, "{VerticalSpace }");
1066 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001067 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001068
Douglas Gregord5a20892009-11-09 17:05:28 +00001069 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001070 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001071 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001072 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001073 cstr ? cstr : "");
1074 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001075 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001076
Douglas Gregor3ac73852009-11-09 16:04:45 +00001077}
1078
1079void print_completion_result(CXCompletionResult *completion_result,
1080 CXClientData client_data) {
1081 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001082 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001083 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001084
1085 fprintf(file, "%s:", clang_getCString(ks));
1086 clang_disposeString(ks);
1087
Douglas Gregor3ac73852009-11-09 16:04:45 +00001088 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001089 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001090 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001091 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1092 case CXAvailability_Available:
1093 break;
1094
1095 case CXAvailability_Deprecated:
1096 fprintf(file, " (deprecated)");
1097 break;
1098
1099 case CXAvailability_NotAvailable:
1100 fprintf(file, " (unavailable)");
1101 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001102
1103 case CXAvailability_NotAccessible:
1104 fprintf(file, " (inaccessible)");
1105 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001106 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001107
1108 annotationCount = clang_getCompletionNumAnnotations(
1109 completion_result->CompletionString);
1110 if (annotationCount) {
1111 unsigned i;
1112 fprintf(file, " (");
1113 for (i = 0; i < annotationCount; ++i) {
1114 if (i != 0)
1115 fprintf(file, ", ");
1116 fprintf(file, "\"%s\"",
1117 clang_getCString(clang_getCompletionAnnotation(
1118 completion_result->CompletionString, i)));
1119 }
1120 fprintf(file, ")");
1121 }
1122
Douglas Gregor58ddb602010-08-23 23:00:57 +00001123 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001124}
1125
Douglas Gregor3da626b2011-07-07 16:03:39 +00001126void print_completion_contexts(unsigned long long contexts, FILE *file) {
1127 fprintf(file, "Completion contexts:\n");
1128 if (contexts == CXCompletionContext_Unknown) {
1129 fprintf(file, "Unknown\n");
1130 }
1131 if (contexts & CXCompletionContext_AnyType) {
1132 fprintf(file, "Any type\n");
1133 }
1134 if (contexts & CXCompletionContext_AnyValue) {
1135 fprintf(file, "Any value\n");
1136 }
1137 if (contexts & CXCompletionContext_ObjCObjectValue) {
1138 fprintf(file, "Objective-C object value\n");
1139 }
1140 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1141 fprintf(file, "Objective-C selector value\n");
1142 }
1143 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1144 fprintf(file, "C++ class type value\n");
1145 }
1146 if (contexts & CXCompletionContext_DotMemberAccess) {
1147 fprintf(file, "Dot member access\n");
1148 }
1149 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1150 fprintf(file, "Arrow member access\n");
1151 }
1152 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1153 fprintf(file, "Objective-C property access\n");
1154 }
1155 if (contexts & CXCompletionContext_EnumTag) {
1156 fprintf(file, "Enum tag\n");
1157 }
1158 if (contexts & CXCompletionContext_UnionTag) {
1159 fprintf(file, "Union tag\n");
1160 }
1161 if (contexts & CXCompletionContext_StructTag) {
1162 fprintf(file, "Struct tag\n");
1163 }
1164 if (contexts & CXCompletionContext_ClassTag) {
1165 fprintf(file, "Class name\n");
1166 }
1167 if (contexts & CXCompletionContext_Namespace) {
1168 fprintf(file, "Namespace or namespace alias\n");
1169 }
1170 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1171 fprintf(file, "Nested name specifier\n");
1172 }
1173 if (contexts & CXCompletionContext_ObjCInterface) {
1174 fprintf(file, "Objective-C interface\n");
1175 }
1176 if (contexts & CXCompletionContext_ObjCProtocol) {
1177 fprintf(file, "Objective-C protocol\n");
1178 }
1179 if (contexts & CXCompletionContext_ObjCCategory) {
1180 fprintf(file, "Objective-C category\n");
1181 }
1182 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1183 fprintf(file, "Objective-C instance method\n");
1184 }
1185 if (contexts & CXCompletionContext_ObjCClassMessage) {
1186 fprintf(file, "Objective-C class method\n");
1187 }
1188 if (contexts & CXCompletionContext_ObjCSelectorName) {
1189 fprintf(file, "Objective-C selector name\n");
1190 }
1191 if (contexts & CXCompletionContext_MacroName) {
1192 fprintf(file, "Macro name\n");
1193 }
1194 if (contexts & CXCompletionContext_NaturalLanguage) {
1195 fprintf(file, "Natural language\n");
1196 }
1197}
1198
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001199int my_stricmp(const char *s1, const char *s2) {
1200 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001201 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001202 if (c1 < c2)
1203 return -1;
1204 else if (c1 > c2)
1205 return 1;
1206
1207 ++s1;
1208 ++s2;
1209 }
1210
1211 if (*s1)
1212 return 1;
1213 else if (*s2)
1214 return -1;
1215 return 0;
1216}
1217
Douglas Gregor1982c182010-07-12 18:38:41 +00001218int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001219 const char *input = argv[1];
1220 char *filename = 0;
1221 unsigned line;
1222 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001223 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001224 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001225 struct CXUnsavedFile *unsaved_files = 0;
1226 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001227 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001228 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001229 unsigned I, Repeats = 1;
1230 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1231
1232 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1233 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001234
Douglas Gregor1982c182010-07-12 18:38:41 +00001235 if (timing_only)
1236 input += strlen("-code-completion-timing=");
1237 else
1238 input += strlen("-code-completion-at=");
1239
Ted Kremeneke68fff62010-02-17 00:41:32 +00001240 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001241 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001242 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001243
Douglas Gregor735df882009-12-02 09:21:34 +00001244 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1245 return -1;
1246
Douglas Gregor32be4a52010-10-11 21:37:58 +00001247 CIdx = clang_createIndex(0, 0);
1248
1249 if (getenv("CINDEXTEST_EDITING"))
1250 Repeats = 5;
1251
1252 TU = clang_parseTranslationUnit(CIdx, 0,
1253 argv + num_unsaved_files + 2,
1254 argc - num_unsaved_files - 2,
1255 0, 0, getDefaultParsingOptions());
1256 if (!TU) {
1257 fprintf(stderr, "Unable to load translation unit!\n");
1258 return 1;
1259 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001260
1261 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1262 fprintf(stderr, "Unable to reparse translation init!\n");
1263 return 1;
1264 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001265
1266 for (I = 0; I != Repeats; ++I) {
1267 results = clang_codeCompleteAt(TU, filename, line, column,
1268 unsaved_files, num_unsaved_files,
1269 completionOptions);
1270 if (!results) {
1271 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001272 return 1;
1273 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001274 if (I != Repeats-1)
1275 clang_disposeCodeCompleteResults(results);
1276 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001277
Douglas Gregorec6762c2009-12-18 16:20:58 +00001278 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001279 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001280 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001281 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001282 CXString objCSelector;
1283 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001284 if (!timing_only) {
1285 /* Sort the code-completion results based on the typed text. */
1286 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1287
Douglas Gregor1982c182010-07-12 18:38:41 +00001288 for (i = 0; i != n; ++i)
1289 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001290 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001291 n = clang_codeCompleteGetNumDiagnostics(results);
1292 for (i = 0; i != n; ++i) {
1293 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1294 PrintDiagnostic(diag);
1295 clang_disposeDiagnostic(diag);
1296 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001297
1298 contexts = clang_codeCompleteGetContexts(results);
1299 print_completion_contexts(contexts, stdout);
1300
Douglas Gregor0a47d692011-07-26 15:24:30 +00001301 containerKind = clang_codeCompleteGetContainerKind(results,
1302 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001303
1304 if (containerKind != CXCursor_InvalidCode) {
1305 /* We have found a container */
1306 CXString containerUSR, containerKindSpelling;
1307 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1308 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1309 clang_disposeString(containerKindSpelling);
1310
1311 if (containerIsIncomplete) {
1312 printf("Container is incomplete\n");
1313 }
1314 else {
1315 printf("Container is complete\n");
1316 }
1317
1318 containerUSR = clang_codeCompleteGetContainerUSR(results);
1319 printf("Container USR: %s\n", clang_getCString(containerUSR));
1320 clang_disposeString(containerUSR);
1321 }
1322
Douglas Gregor0a47d692011-07-26 15:24:30 +00001323 objCSelector = clang_codeCompleteGetObjCSelector(results);
1324 selectorString = clang_getCString(objCSelector);
1325 if (selectorString && strlen(selectorString) > 0) {
1326 printf("Objective-C selector: %s\n", selectorString);
1327 }
1328 clang_disposeString(objCSelector);
1329
Douglas Gregorec6762c2009-12-18 16:20:58 +00001330 clang_disposeCodeCompleteResults(results);
1331 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001332 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001333 clang_disposeIndex(CIdx);
1334 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001335
Douglas Gregor735df882009-12-02 09:21:34 +00001336 free_remapped_files(unsaved_files, num_unsaved_files);
1337
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001338 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001339}
1340
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001341typedef struct {
1342 char *filename;
1343 unsigned line;
1344 unsigned column;
1345} CursorSourceLocation;
1346
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001347static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001348 CXIndex CIdx;
1349 int errorCode;
1350 struct CXUnsavedFile *unsaved_files = 0;
1351 int num_unsaved_files = 0;
1352 CXTranslationUnit TU;
1353 CXCursor Cursor;
1354 CursorSourceLocation *Locations = 0;
1355 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001356 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001357 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001358
Ted Kremeneke68fff62010-02-17 00:41:32 +00001359 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001360 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1361 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001362
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001363 /* Parse the locations. */
1364 assert(NumLocations > 0 && "Unable to count locations?");
1365 Locations = (CursorSourceLocation *)malloc(
1366 NumLocations * sizeof(CursorSourceLocation));
1367 for (Loc = 0; Loc < NumLocations; ++Loc) {
1368 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001369 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1370 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001371 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001372 return errorCode;
1373 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001374
1375 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001376 &num_unsaved_files))
1377 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001378
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001379 if (getenv("CINDEXTEST_EDITING"))
1380 Repeats = 5;
1381
1382 /* Parse the translation unit. When we're testing clang_getCursor() after
1383 reparsing, don't remap unsaved files until the second parse. */
1384 CIdx = clang_createIndex(1, 1);
1385 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1386 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001387 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001388 unsaved_files,
1389 Repeats > 1? 0 : num_unsaved_files,
1390 getDefaultParsingOptions());
1391
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001392 if (!TU) {
1393 fprintf(stderr, "unable to parse input\n");
1394 return -1;
1395 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001396
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001397 if (checkForErrors(TU) != 0)
1398 return -1;
1399
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001400 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001401 if (Repeats > 1 &&
1402 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1403 clang_defaultReparseOptions(TU))) {
1404 clang_disposeTranslationUnit(TU);
1405 return 1;
1406 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001407
1408 if (checkForErrors(TU) != 0)
1409 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001410
1411 for (Loc = 0; Loc < NumLocations; ++Loc) {
1412 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1413 if (!file)
1414 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001415
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001416 Cursor = clang_getCursor(TU,
1417 clang_getLocation(TU, file, Locations[Loc].line,
1418 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001419
1420 if (checkForErrors(TU) != 0)
1421 return -1;
1422
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001423 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001424 CXCompletionString completionString = clang_getCursorCompletionString(
1425 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001426 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001427 if (completionString != NULL) {
1428 printf("\nCompletion string: ");
1429 print_completion_string(completionString, stdout);
1430 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001431 printf("\n");
1432 free(Locations[Loc].filename);
1433 }
1434 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001435 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001436
Douglas Gregora88084b2010-02-18 18:08:43 +00001437 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001438 clang_disposeTranslationUnit(TU);
1439 clang_disposeIndex(CIdx);
1440 free(Locations);
1441 free_remapped_files(unsaved_files, num_unsaved_files);
1442 return 0;
1443}
1444
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001445static enum CXVisitorResult findFileRefsVisit(void *context,
1446 CXCursor cursor, CXSourceRange range) {
1447 if (clang_Range_isNull(range))
1448 return CXVisit_Continue;
1449
1450 PrintCursor(cursor);
1451 PrintRange(range, "");
1452 printf("\n");
1453 return CXVisit_Continue;
1454}
1455
1456static int find_file_refs_at(int argc, const char **argv) {
1457 CXIndex CIdx;
1458 int errorCode;
1459 struct CXUnsavedFile *unsaved_files = 0;
1460 int num_unsaved_files = 0;
1461 CXTranslationUnit TU;
1462 CXCursor Cursor;
1463 CursorSourceLocation *Locations = 0;
1464 unsigned NumLocations = 0, Loc;
1465 unsigned Repeats = 1;
1466 unsigned I;
1467
1468 /* Count the number of locations. */
1469 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1470 ++NumLocations;
1471
1472 /* Parse the locations. */
1473 assert(NumLocations > 0 && "Unable to count locations?");
1474 Locations = (CursorSourceLocation *)malloc(
1475 NumLocations * sizeof(CursorSourceLocation));
1476 for (Loc = 0; Loc < NumLocations; ++Loc) {
1477 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1478 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1479 &Locations[Loc].line,
1480 &Locations[Loc].column, 0, 0)))
1481 return errorCode;
1482 }
1483
1484 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1485 &num_unsaved_files))
1486 return -1;
1487
1488 if (getenv("CINDEXTEST_EDITING"))
1489 Repeats = 5;
1490
1491 /* Parse the translation unit. When we're testing clang_getCursor() after
1492 reparsing, don't remap unsaved files until the second parse. */
1493 CIdx = clang_createIndex(1, 1);
1494 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1495 argv + num_unsaved_files + 1 + NumLocations,
1496 argc - num_unsaved_files - 2 - NumLocations,
1497 unsaved_files,
1498 Repeats > 1? 0 : num_unsaved_files,
1499 getDefaultParsingOptions());
1500
1501 if (!TU) {
1502 fprintf(stderr, "unable to parse input\n");
1503 return -1;
1504 }
1505
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001506 if (checkForErrors(TU) != 0)
1507 return -1;
1508
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001509 for (I = 0; I != Repeats; ++I) {
1510 if (Repeats > 1 &&
1511 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1512 clang_defaultReparseOptions(TU))) {
1513 clang_disposeTranslationUnit(TU);
1514 return 1;
1515 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001516
1517 if (checkForErrors(TU) != 0)
1518 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001519
1520 for (Loc = 0; Loc < NumLocations; ++Loc) {
1521 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1522 if (!file)
1523 continue;
1524
1525 Cursor = clang_getCursor(TU,
1526 clang_getLocation(TU, file, Locations[Loc].line,
1527 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001528
1529 if (checkForErrors(TU) != 0)
1530 return -1;
1531
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001532 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001533 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001534 PrintCursor(Cursor);
1535 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001536 clang_findReferencesInFile(Cursor, file, visitor);
1537 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001538
1539 if (checkForErrors(TU) != 0)
1540 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001541 }
1542 }
1543 }
1544
1545 PrintDiagnostics(TU);
1546 clang_disposeTranslationUnit(TU);
1547 clang_disposeIndex(CIdx);
1548 free(Locations);
1549 free_remapped_files(unsaved_files, num_unsaved_files);
1550 return 0;
1551}
1552
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001553typedef struct {
1554 const char *check_prefix;
1555 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001556 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001557 int abort;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001558} IndexData;
1559
1560static void printCheck(IndexData *data) {
1561 if (data->check_prefix) {
1562 if (data->first_check_printed) {
1563 printf("// %s-NEXT: ", data->check_prefix);
1564 } else {
1565 printf("// %s : ", data->check_prefix);
1566 data->first_check_printed = 1;
1567 }
1568 }
1569}
1570
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001571static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001572 CXString filename = clang_getFileName((CXFile)file);
1573 printf("%s", clang_getCString(filename));
1574 clang_disposeString(filename);
1575}
1576
1577static void printCXIndexLoc(CXIdxLoc loc) {
1578 CXString filename;
1579 const char *cname, *end;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001580 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001581 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001582 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001583
1584 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1585 if (line == 0) {
1586 printf("<null loc>");
1587 return;
1588 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001589 if (!file) {
1590 printf("<no idxfile>");
1591 return;
1592 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001593 filename = clang_getFileName((CXFile)file);
1594 cname = clang_getCString(filename);
1595 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001596 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001597
1598 if (isHeader) {
1599 printCXIndexFile(file);
1600 printf(":");
1601 }
1602 printf("%d:%d", line, column);
1603}
1604
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001605static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1606 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001607 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001608 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001609 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001610 unsigned line, column;
1611
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001612 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001613 if (!name)
1614 name = "<anon-tag>";
1615
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001616 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001617 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001618 newStr = (char *)malloc(strlen(name) + 10);
1619 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001620 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001621}
1622
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001623static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1624 CXIdxClientContainer container;
1625 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001626 if (!container)
1627 printf("[<<NULL>>]");
1628 else
1629 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001630}
1631
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001632static const char *getEntityKindString(CXIdxEntityKind kind) {
1633 switch (kind) {
1634 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1635 case CXIdxEntity_Typedef: return "typedef";
1636 case CXIdxEntity_Function: return "function";
1637 case CXIdxEntity_Variable: return "variable";
1638 case CXIdxEntity_Field: return "field";
1639 case CXIdxEntity_EnumConstant: return "enumerator";
1640 case CXIdxEntity_ObjCClass: return "objc-class";
1641 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1642 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001643 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1644 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001645 case CXIdxEntity_ObjCProperty: return "objc-property";
1646 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1647 case CXIdxEntity_Enum: return "enum";
1648 case CXIdxEntity_Struct: return "struct";
1649 case CXIdxEntity_Union: return "union";
1650 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001651 case CXIdxEntity_CXXNamespace: return "namespace";
1652 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1653 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1654 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1655 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1656 case CXIdxEntity_CXXConstructor: return "constructor";
1657 case CXIdxEntity_CXXDestructor: return "destructor";
1658 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1659 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1660 }
1661 assert(0 && "Garbage entity kind");
1662 return 0;
1663}
1664
1665static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1666 switch (kind) {
1667 case CXIdxEntity_NonTemplate: return "";
1668 case CXIdxEntity_Template: return "-template";
1669 case CXIdxEntity_TemplatePartialSpecialization:
1670 return "-template-partial-spec";
1671 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001672 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001673 assert(0 && "Garbage entity kind");
1674 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001675}
1676
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001677static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1678 switch (kind) {
1679 case CXIdxEntityLang_None: return "<none>";
1680 case CXIdxEntityLang_C: return "C";
1681 case CXIdxEntityLang_ObjC: return "ObjC";
1682 case CXIdxEntityLang_CXX: return "C++";
1683 }
1684 assert(0 && "Garbage language kind");
1685 return 0;
1686}
1687
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001688static void printEntityInfo(const char *cb,
1689 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001690 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001691 const char *name;
1692 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001693 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001694 index_data = (IndexData *)client_data;
1695 printCheck(index_data);
1696
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001697 if (!info) {
1698 printf("%s: <<NULL>>", cb);
1699 return;
1700 }
1701
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001702 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001703 if (!name)
1704 name = "<anon-tag>";
1705
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001706 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1707 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001708 printf(" | name: %s", name);
1709 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001710 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001711
1712 for (i = 0; i != info->numAttributes; ++i) {
1713 const CXIdxAttrInfo *Attr = info->attributes[i];
1714 printf(" <attribute>: ");
1715 PrintCursor(Attr->cursor);
1716 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001717}
1718
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001719static void printBaseClassInfo(CXClientData client_data,
1720 const CXIdxBaseClassInfo *info) {
1721 printEntityInfo(" <base>", client_data, info->base);
1722 printf(" | cursor: ");
1723 PrintCursor(info->cursor);
1724 printf(" | loc: ");
1725 printCXIndexLoc(info->loc);
1726}
1727
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001728static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1729 CXClientData client_data) {
1730 unsigned i;
1731 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1732 printEntityInfo(" <protocol>", client_data,
1733 ProtoInfo->protocols[i]->protocol);
1734 printf(" | cursor: ");
1735 PrintCursor(ProtoInfo->protocols[i]->cursor);
1736 printf(" | loc: ");
1737 printCXIndexLoc(ProtoInfo->protocols[i]->loc);
1738 printf("\n");
1739 }
1740}
1741
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001742static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001743 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001744 CXString str;
1745 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001746 unsigned numDiags, i;
1747 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001748 IndexData *index_data;
1749 index_data = (IndexData *)client_data;
1750 printCheck(index_data);
1751
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001752 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1753 for (i = 0; i != numDiags; ++i) {
1754 diag = clang_getDiagnosticInSet(diagSet, i);
1755 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1756 cstr = clang_getCString(str);
1757 printf("[diagnostic]: %s\n", cstr);
1758 clang_disposeString(str);
1759
1760 if (getenv("CINDEXTEST_FAILONERROR") &&
1761 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1762 index_data->fail_for_error = 1;
1763 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001764 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001765}
1766
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001767static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1768 CXFile file, void *reserved) {
1769 IndexData *index_data;
1770 index_data = (IndexData *)client_data;
1771 printCheck(index_data);
1772
1773 printf("[enteredMainFile]: ");
1774 printCXIndexFile((CXIdxClientFile)file);
1775 printf("\n");
1776
1777 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001778}
1779
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001780static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001781 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001782 IndexData *index_data;
1783 index_data = (IndexData *)client_data;
1784 printCheck(index_data);
1785
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001786 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001787 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001788 printf(" | name: \"%s\"", info->filename);
1789 printf(" | hash loc: ");
1790 printCXIndexLoc(info->hashLoc);
1791 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001792
1793 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001794}
1795
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001796static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001797 void *reserved) {
1798 IndexData *index_data;
1799 index_data = (IndexData *)client_data;
1800 printCheck(index_data);
1801
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001802 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001803 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001804}
1805
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001806static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001807 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001808 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001809 const CXIdxObjCCategoryDeclInfo *CatInfo;
1810 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001811 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001812 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001813 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001814 index_data = (IndexData *)client_data;
1815
1816 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1817 printf(" | cursor: ");
1818 PrintCursor(info->cursor);
1819 printf(" | loc: ");
1820 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001821 printf(" | semantic-container: ");
1822 printCXIndexContainer(info->semanticContainer);
1823 printf(" | lexical-container: ");
1824 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001825 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001826 printf(" | isDef: %d", info->isDefinition);
1827 printf(" | isContainer: %d", info->isContainer);
1828 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001829
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001830 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001831 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001832 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001833 PrintCursor(Attr->cursor);
1834 printf("\n");
1835 }
1836
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001837 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1838 const char *kindName = 0;
1839 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1840 switch (K) {
1841 case CXIdxObjCContainer_ForwardRef:
1842 kindName = "forward-ref"; break;
1843 case CXIdxObjCContainer_Interface:
1844 kindName = "interface"; break;
1845 case CXIdxObjCContainer_Implementation:
1846 kindName = "implementation"; break;
1847 }
1848 printCheck(index_data);
1849 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1850 }
1851
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001852 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001853 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1854 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001855 printf(" | cursor: ");
1856 PrintCursor(CatInfo->classCursor);
1857 printf(" | loc: ");
1858 printCXIndexLoc(CatInfo->classLoc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001859 printf("\n");
1860 }
1861
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001862 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1863 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001864 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001865 printf("\n");
1866 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001867 }
1868
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001869 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1870 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001871 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001872
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001873 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1874 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1875 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1876 printf("\n");
1877 }
1878 }
1879
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001880 if (info->declAsContainer)
1881 clang_index_setClientContainer(info->declAsContainer,
1882 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001883}
1884
1885static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001886 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001887 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001888 printf(" | cursor: ");
1889 PrintCursor(info->cursor);
1890 printf(" | loc: ");
1891 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001892 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001893 printf(" | container: ");
1894 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001895 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001896 switch (info->kind) {
1897 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001898 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001899 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001900 printf("\n");
1901}
1902
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001903static int index_abortQuery(CXClientData client_data, void *reserved) {
1904 IndexData *index_data;
1905 index_data = (IndexData *)client_data;
1906 return index_data->abort;
1907}
1908
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001909static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001910 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001911 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001912 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001913 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001914 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001915 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001916 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001917 index_indexEntityReference
1918};
1919
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00001920static unsigned getIndexOptions(void) {
1921 unsigned index_opts;
1922 index_opts = 0;
1923 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1924 index_opts |= CXIndexOpt_SuppressRedundantRefs;
1925 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
1926 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
1927
1928 return index_opts;
1929}
1930
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001931static int index_file(int argc, const char **argv) {
1932 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001933 CXIndex Idx;
1934 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001935 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001936 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001937 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001938
1939 check_prefix = 0;
1940 if (argc > 0) {
1941 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1942 check_prefix = argv[0] + strlen("-check-prefix=");
1943 ++argv;
1944 --argc;
1945 }
1946 }
1947
1948 if (argc == 0) {
1949 fprintf(stderr, "no compiler arguments\n");
1950 return -1;
1951 }
1952
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001953 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1954 /* displayDiagnosics=*/1))) {
1955 fprintf(stderr, "Could not create Index\n");
1956 return 1;
1957 }
1958 idxAction = 0;
1959 result = 1;
1960
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001961 index_data.check_prefix = check_prefix;
1962 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001963 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001964 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001965
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00001966 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001967 idxAction = clang_IndexAction_create(Idx);
1968 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001969 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001970 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001971 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001972 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001973
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001974 clang_IndexAction_dispose(idxAction);
1975 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001976 return result;
1977}
1978
1979static int index_tu(int argc, const char **argv) {
1980 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001981 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001982 CXTranslationUnit TU;
1983 const char *check_prefix;
1984 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001985 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001986 int result;
1987
1988 check_prefix = 0;
1989 if (argc > 0) {
1990 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1991 check_prefix = argv[0] + strlen("-check-prefix=");
1992 ++argv;
1993 --argc;
1994 }
1995 }
1996
1997 if (argc == 0) {
1998 fprintf(stderr, "no ast file\n");
1999 return -1;
2000 }
2001
2002 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2003 /* displayDiagnosics=*/1))) {
2004 fprintf(stderr, "Could not create Index\n");
2005 return 1;
2006 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002007 idxAction = 0;
2008 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002009
2010 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002011 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002012
2013 index_data.check_prefix = check_prefix;
2014 index_data.first_check_printed = 0;
2015 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002016 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002017
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002018 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002019 idxAction = clang_IndexAction_create(Idx);
2020 result = clang_indexTranslationUnit(idxAction, &index_data,
2021 &IndexCB,sizeof(IndexCB),
2022 index_opts, TU);
2023 if (index_data.fail_for_error)
2024 goto finished;
2025
2026 finished:
2027 clang_IndexAction_dispose(idxAction);
2028 clang_disposeIndex(Idx);
2029
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002030 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002031}
2032
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002033int perform_token_annotation(int argc, const char **argv) {
2034 const char *input = argv[1];
2035 char *filename = 0;
2036 unsigned line, second_line;
2037 unsigned column, second_column;
2038 CXIndex CIdx;
2039 CXTranslationUnit TU = 0;
2040 int errorCode;
2041 struct CXUnsavedFile *unsaved_files = 0;
2042 int num_unsaved_files = 0;
2043 CXToken *tokens;
2044 unsigned num_tokens;
2045 CXSourceRange range;
2046 CXSourceLocation startLoc, endLoc;
2047 CXFile file = 0;
2048 CXCursor *cursors = 0;
2049 unsigned i;
2050
2051 input += strlen("-test-annotate-tokens=");
2052 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2053 &second_line, &second_column)))
2054 return errorCode;
2055
2056 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2057 return -1;
2058
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002059 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002060 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2061 argv + num_unsaved_files + 2,
2062 argc - num_unsaved_files - 3,
2063 unsaved_files,
2064 num_unsaved_files,
2065 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002066 if (!TU) {
2067 fprintf(stderr, "unable to parse input\n");
2068 clang_disposeIndex(CIdx);
2069 free(filename);
2070 free_remapped_files(unsaved_files, num_unsaved_files);
2071 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002072 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002073 errorCode = 0;
2074
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002075 if (checkForErrors(TU) != 0)
2076 return -1;
2077
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002078 if (getenv("CINDEXTEST_EDITING")) {
2079 for (i = 0; i < 5; ++i) {
2080 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2081 clang_defaultReparseOptions(TU))) {
2082 fprintf(stderr, "Unable to reparse translation unit!\n");
2083 errorCode = -1;
2084 goto teardown;
2085 }
2086 }
2087 }
2088
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002089 if (checkForErrors(TU) != 0) {
2090 errorCode = -1;
2091 goto teardown;
2092 }
2093
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002094 file = clang_getFile(TU, filename);
2095 if (!file) {
2096 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2097 errorCode = -1;
2098 goto teardown;
2099 }
2100
2101 startLoc = clang_getLocation(TU, file, line, column);
2102 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002103 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002104 column);
2105 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002106 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002107 }
2108
2109 endLoc = clang_getLocation(TU, file, second_line, second_column);
2110 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002111 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002112 second_line, second_column);
2113 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002114 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002115 }
2116
2117 range = clang_getRange(startLoc, endLoc);
2118 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002119
2120 if (checkForErrors(TU) != 0) {
2121 errorCode = -1;
2122 goto teardown;
2123 }
2124
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002125 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2126 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002127
2128 if (checkForErrors(TU) != 0) {
2129 errorCode = -1;
2130 goto teardown;
2131 }
2132
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002133 for (i = 0; i != num_tokens; ++i) {
2134 const char *kind = "<unknown>";
2135 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2136 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2137 unsigned start_line, start_column, end_line, end_column;
2138
2139 switch (clang_getTokenKind(tokens[i])) {
2140 case CXToken_Punctuation: kind = "Punctuation"; break;
2141 case CXToken_Keyword: kind = "Keyword"; break;
2142 case CXToken_Identifier: kind = "Identifier"; break;
2143 case CXToken_Literal: kind = "Literal"; break;
2144 case CXToken_Comment: kind = "Comment"; break;
2145 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002146 clang_getSpellingLocation(clang_getRangeStart(extent),
2147 0, &start_line, &start_column, 0);
2148 clang_getSpellingLocation(clang_getRangeEnd(extent),
2149 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002150 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2151 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002152 if (!clang_isInvalid(cursors[i].kind)) {
2153 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002154 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002155 }
2156 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002157 }
2158 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002159 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002160
2161 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002162 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002163 clang_disposeTranslationUnit(TU);
2164 clang_disposeIndex(CIdx);
2165 free(filename);
2166 free_remapped_files(unsaved_files, num_unsaved_files);
2167 return errorCode;
2168}
2169
Ted Kremenek0d435192009-11-17 18:13:31 +00002170/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002171/* USR printing. */
2172/******************************************************************************/
2173
2174static int insufficient_usr(const char *kind, const char *usage) {
2175 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2176 return 1;
2177}
2178
2179static unsigned isUSR(const char *s) {
2180 return s[0] == 'c' && s[1] == ':';
2181}
2182
2183static int not_usr(const char *s, const char *arg) {
2184 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2185 return 1;
2186}
2187
2188static void print_usr(CXString usr) {
2189 const char *s = clang_getCString(usr);
2190 printf("%s\n", s);
2191 clang_disposeString(usr);
2192}
2193
2194static void display_usrs() {
2195 fprintf(stderr, "-print-usrs options:\n"
2196 " ObjCCategory <class name> <category name>\n"
2197 " ObjCClass <class name>\n"
2198 " ObjCIvar <ivar name> <class USR>\n"
2199 " ObjCMethod <selector> [0=class method|1=instance method] "
2200 "<class USR>\n"
2201 " ObjCProperty <property name> <class USR>\n"
2202 " ObjCProtocol <protocol name>\n");
2203}
2204
2205int print_usrs(const char **I, const char **E) {
2206 while (I != E) {
2207 const char *kind = *I;
2208 unsigned len = strlen(kind);
2209 switch (len) {
2210 case 8:
2211 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2212 if (I + 2 >= E)
2213 return insufficient_usr(kind, "<ivar name> <class USR>");
2214 if (!isUSR(I[2]))
2215 return not_usr("<class USR>", I[2]);
2216 else {
2217 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002218 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002219 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002220 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2221 }
2222
2223 I += 3;
2224 continue;
2225 }
2226 break;
2227 case 9:
2228 if (memcmp(kind, "ObjCClass", 9) == 0) {
2229 if (I + 1 >= E)
2230 return insufficient_usr(kind, "<class name>");
2231 print_usr(clang_constructUSR_ObjCClass(I[1]));
2232 I += 2;
2233 continue;
2234 }
2235 break;
2236 case 10:
2237 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2238 if (I + 3 >= E)
2239 return insufficient_usr(kind, "<method selector> "
2240 "[0=class method|1=instance method] <class USR>");
2241 if (!isUSR(I[3]))
2242 return not_usr("<class USR>", I[3]);
2243 else {
2244 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002245 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002246 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002247 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2248 }
2249 I += 4;
2250 continue;
2251 }
2252 break;
2253 case 12:
2254 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2255 if (I + 2 >= E)
2256 return insufficient_usr(kind, "<class name> <category name>");
2257 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2258 I += 3;
2259 continue;
2260 }
2261 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2262 if (I + 1 >= E)
2263 return insufficient_usr(kind, "<protocol name>");
2264 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2265 I += 2;
2266 continue;
2267 }
2268 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2269 if (I + 2 >= E)
2270 return insufficient_usr(kind, "<property name> <class USR>");
2271 if (!isUSR(I[2]))
2272 return not_usr("<class USR>", I[2]);
2273 else {
2274 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002275 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002276 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002277 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2278 }
2279 I += 3;
2280 continue;
2281 }
2282 break;
2283 default:
2284 break;
2285 }
2286 break;
2287 }
2288
2289 if (I != E) {
2290 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2291 display_usrs();
2292 return 1;
2293 }
2294 return 0;
2295}
2296
2297int print_usrs_file(const char *file_name) {
2298 char line[2048];
2299 const char *args[128];
2300 unsigned numChars = 0;
2301
2302 FILE *fp = fopen(file_name, "r");
2303 if (!fp) {
2304 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2305 return 1;
2306 }
2307
2308 /* This code is not really all that safe, but it works fine for testing. */
2309 while (!feof(fp)) {
2310 char c = fgetc(fp);
2311 if (c == '\n') {
2312 unsigned i = 0;
2313 const char *s = 0;
2314
2315 if (numChars == 0)
2316 continue;
2317
2318 line[numChars] = '\0';
2319 numChars = 0;
2320
2321 if (line[0] == '/' && line[1] == '/')
2322 continue;
2323
2324 s = strtok(line, " ");
2325 while (s) {
2326 args[i] = s;
2327 ++i;
2328 s = strtok(0, " ");
2329 }
2330 if (print_usrs(&args[0], &args[i]))
2331 return 1;
2332 }
2333 else
2334 line[numChars++] = c;
2335 }
2336
2337 fclose(fp);
2338 return 0;
2339}
2340
2341/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002342/* Command line processing. */
2343/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002344int write_pch_file(const char *filename, int argc, const char *argv[]) {
2345 CXIndex Idx;
2346 CXTranslationUnit TU;
2347 struct CXUnsavedFile *unsaved_files = 0;
2348 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002349 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002350
2351 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2352
2353 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2354 clang_disposeIndex(Idx);
2355 return -1;
2356 }
2357
2358 TU = clang_parseTranslationUnit(Idx, 0,
2359 argv + num_unsaved_files,
2360 argc - num_unsaved_files,
2361 unsaved_files,
2362 num_unsaved_files,
2363 CXTranslationUnit_Incomplete);
2364 if (!TU) {
2365 fprintf(stderr, "Unable to load translation unit!\n");
2366 free_remapped_files(unsaved_files, num_unsaved_files);
2367 clang_disposeIndex(Idx);
2368 return 1;
2369 }
2370
Douglas Gregor39c411f2011-07-06 16:43:36 +00002371 switch (clang_saveTranslationUnit(TU, filename,
2372 clang_defaultSaveOptions(TU))) {
2373 case CXSaveError_None:
2374 break;
2375
2376 case CXSaveError_TranslationErrors:
2377 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2378 filename);
2379 result = 2;
2380 break;
2381
2382 case CXSaveError_InvalidTU:
2383 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2384 filename);
2385 result = 3;
2386 break;
2387
2388 case CXSaveError_Unknown:
2389 default:
2390 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2391 result = 1;
2392 break;
2393 }
2394
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002395 clang_disposeTranslationUnit(TU);
2396 free_remapped_files(unsaved_files, num_unsaved_files);
2397 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002398 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002399}
2400
2401/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002402/* Serialized diagnostics. */
2403/******************************************************************************/
2404
2405static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2406 switch (error) {
2407 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2408 case CXLoadDiag_None: break;
2409 case CXLoadDiag_Unknown: return "Unknown";
2410 case CXLoadDiag_InvalidFile: return "Invalid File";
2411 }
2412 return "None";
2413}
2414
2415static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2416 switch (severity) {
2417 case CXDiagnostic_Note: return "note";
2418 case CXDiagnostic_Error: return "error";
2419 case CXDiagnostic_Fatal: return "fatal";
2420 case CXDiagnostic_Ignored: return "ignored";
2421 case CXDiagnostic_Warning: return "warning";
2422 }
2423 return "unknown";
2424}
2425
2426static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002427 if (indent == 0)
2428 return;
2429 fprintf(stderr, "+");
2430 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002431 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002432 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002433 --indent;
2434 }
2435}
2436
2437static void printLocation(CXSourceLocation L) {
2438 CXFile File;
2439 CXString FileName;
2440 unsigned line, column, offset;
2441
2442 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2443 FileName = clang_getFileName(File);
2444
2445 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2446 clang_disposeString(FileName);
2447}
2448
2449static void printRanges(CXDiagnostic D, unsigned indent) {
2450 unsigned i, n = clang_getDiagnosticNumRanges(D);
2451
2452 for (i = 0; i < n; ++i) {
2453 CXSourceLocation Start, End;
2454 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2455 Start = clang_getRangeStart(SR);
2456 End = clang_getRangeEnd(SR);
2457
2458 printIndent(indent);
2459 fprintf(stderr, "Range: ");
2460 printLocation(Start);
2461 fprintf(stderr, " ");
2462 printLocation(End);
2463 fprintf(stderr, "\n");
2464 }
2465}
2466
2467static void printFixIts(CXDiagnostic D, unsigned indent) {
2468 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2469 for (i = 0 ; i < n; ++i) {
2470 CXSourceRange ReplacementRange;
2471 CXString text;
2472 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2473
2474 printIndent(indent);
2475 fprintf(stderr, "FIXIT: (");
2476 printLocation(clang_getRangeStart(ReplacementRange));
2477 fprintf(stderr, " - ");
2478 printLocation(clang_getRangeEnd(ReplacementRange));
2479 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2480 clang_disposeString(text);
2481 }
2482}
2483
2484static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002485 unsigned i, n;
2486
Ted Kremenek15322172011-11-10 08:43:12 +00002487 if (!Diags)
2488 return;
2489
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002490 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002491 for (i = 0; i < n; ++i) {
2492 CXSourceLocation DiagLoc;
2493 CXDiagnostic D;
2494 CXFile File;
2495 CXString FileName, DiagSpelling, DiagOption;
2496 unsigned line, column, offset;
2497 const char *DiagOptionStr = 0;
2498
2499 D = clang_getDiagnosticInSet(Diags, i);
2500 DiagLoc = clang_getDiagnosticLocation(D);
2501 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2502 FileName = clang_getFileName(File);
2503 DiagSpelling = clang_getDiagnosticSpelling(D);
2504
2505 printIndent(indent);
2506
2507 fprintf(stderr, "%s:%d:%d: %s: %s",
2508 clang_getCString(FileName),
2509 line,
2510 column,
2511 getSeverityString(clang_getDiagnosticSeverity(D)),
2512 clang_getCString(DiagSpelling));
2513
2514 DiagOption = clang_getDiagnosticOption(D, 0);
2515 DiagOptionStr = clang_getCString(DiagOption);
2516 if (DiagOptionStr) {
2517 fprintf(stderr, " [%s]", DiagOptionStr);
2518 }
2519
2520 fprintf(stderr, "\n");
2521
2522 printRanges(D, indent);
2523 printFixIts(D, indent);
2524
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002525 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002526 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2527
2528 clang_disposeString(FileName);
2529 clang_disposeString(DiagSpelling);
2530 clang_disposeString(DiagOption);
2531 }
2532}
2533
2534static int read_diagnostics(const char *filename) {
2535 enum CXLoadDiag_Error error;
2536 CXString errorString;
2537 CXDiagnosticSet Diags = 0;
2538
2539 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2540 if (!Diags) {
2541 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2542 getDiagnosticCodeStr(error),
2543 clang_getCString(errorString));
2544 clang_disposeString(errorString);
2545 return 1;
2546 }
2547
2548 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002549 fprintf(stderr, "Number of diagnostics: %d\n",
2550 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002551 clang_disposeDiagnosticSet(Diags);
2552 return 0;
2553}
2554
2555/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002556/* Command line processing. */
2557/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002558
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002559static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002560 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002561 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002562 if (strcmp(s, "-usrs") == 0)
2563 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002564 if (strncmp(s, "-memory-usage", 13) == 0)
2565 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002566 return NULL;
2567}
2568
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002569static void print_usage(void) {
2570 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002571 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002572 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002573 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002574 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002575 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002576 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002577 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002578 "[FileCheck prefix]\n");
2579 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002580 " c-index-test -test-load-tu <AST file> <symbol filter> "
2581 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002582 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2583 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002584 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002585 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002586 " c-index-test -test-load-source-memory-usage "
2587 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002588 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2589 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002590 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002591 " c-index-test -test-load-source-usrs-memory-usage "
2592 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002593 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2594 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002595 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002596 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002597 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002598 " c-index-test -test-print-typekind {<args>}*\n"
2599 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002600 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002601 " c-index-test -write-pch <file> <compiler arguments>\n");
2602 fprintf(stderr,
2603 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002604 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002605 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002606 " all - load all symbols, including those from PCH\n"
2607 " local - load all symbols except those in PCH\n"
2608 " category - only load ObjC categories (non-PCH)\n"
2609 " interface - only load ObjC interfaces (non-PCH)\n"
2610 " protocol - only load ObjC protocols (non-PCH)\n"
2611 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002612 " typedef - only load typdefs (non-PCH)\n"
2613 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002614}
2615
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002616/***/
2617
2618int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002619 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002620 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2621 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002622 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002623 return perform_code_completion(argc, argv, 0);
2624 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2625 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002626 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2627 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002628 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2629 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002630 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2631 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002632 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2633 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002634 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002635 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002636 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002637 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2638 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002639 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002640 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2641 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2642 if (I) {
2643 int trials = atoi(argv[2]);
2644 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2645 NULL);
2646 }
2647 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002648 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002649 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002650
2651 PostVisitTU postVisit = 0;
2652 if (strstr(argv[1], "-memory-usage"))
2653 postVisit = PrintMemoryUsage;
2654
Ted Kremenek7d405622010-01-12 23:34:26 +00002655 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002656 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2657 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002658 }
2659 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002660 return perform_file_scan(argv[2], argv[3],
2661 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002662 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2663 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002664 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2665 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2666 PrintInclusionStack);
2667 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2668 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2669 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002670 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2671 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2672 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002673 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2674 return perform_test_load_source(argc - 2, argv + 2, "all",
2675 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002676 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2677 if (argc > 2)
2678 return print_usrs(argv + 2, argv + argc);
2679 else {
2680 display_usrs();
2681 return 1;
2682 }
2683 }
2684 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2685 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002686 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2687 return write_pch_file(argv[2], argc - 3, argv + 3);
2688
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002689 print_usage();
2690 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002691}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002692
2693/***/
2694
2695/* We intentionally run in a separate thread to ensure we at least minimal
2696 * testing of a multithreaded environment (for example, having a reduced stack
2697 * size). */
2698
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002699typedef struct thread_info {
2700 int argc;
2701 const char **argv;
2702 int result;
2703} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002704void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002705 thread_info *client_data = client_data_v;
2706 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002707}
2708
2709int main(int argc, const char **argv) {
2710 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002711
Douglas Gregor61605982010-10-27 16:00:01 +00002712 if (getenv("CINDEXTEST_NOTHREADS"))
2713 return cindextest_main(argc, argv);
2714
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002715 client_data.argc = argc;
2716 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002717 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002718 return client_data.result;
2719}