blob: 472c0b0e95013b32e0dab65c25164d58271e908b [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;
Douglas Gregordca8ee82011-05-06 16:33:08 +000040 if (getenv("CINDEXTEST_NESTED_MACROS"))
Chandler Carruthba7537f2011-07-14 09:02:10 +000041 options |= CXTranslationUnit_NestedMacroExpansions;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000042 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
43 options &= ~CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000044
45 return options;
46}
47
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000048static int checkForErrors(CXTranslationUnit TU);
49
Daniel Dunbar51b058c2010-02-14 08:32:24 +000050static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
51 unsigned end_line, unsigned end_column) {
52 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000053 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000054}
55
Ted Kremenek1c6da172009-11-17 19:37:36 +000056static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
57 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000058
Douglas Gregora88084b2010-02-18 18:08:43 +000059 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000060 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000061 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
62 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000063 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000064 return 1;
65}
66
Douglas Gregor4db64a42010-01-23 00:14:00 +000067void free_remapped_files(struct CXUnsavedFile *unsaved_files,
68 int num_unsaved_files) {
69 int i;
70 for (i = 0; i != num_unsaved_files; ++i) {
71 free((char *)unsaved_files[i].Filename);
72 free((char *)unsaved_files[i].Contents);
73 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000074 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000075}
76
77int parse_remapped_files(int argc, const char **argv, int start_arg,
78 struct CXUnsavedFile **unsaved_files,
79 int *num_unsaved_files) {
80 int i;
81 int arg;
82 int prefix_len = strlen("-remap-file=");
83 *unsaved_files = 0;
84 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000085
Douglas Gregor4db64a42010-01-23 00:14:00 +000086 /* Count the number of remapped files. */
87 for (arg = start_arg; arg < argc; ++arg) {
88 if (strncmp(argv[arg], "-remap-file=", prefix_len))
89 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 ++*num_unsaved_files;
92 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000093
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 if (*num_unsaved_files == 0)
95 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000096
Douglas Gregor4db64a42010-01-23 00:14:00 +000097 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000098 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
99 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
101 struct CXUnsavedFile *unsaved = *unsaved_files + i;
102 const char *arg_string = argv[arg] + prefix_len;
103 int filename_len;
104 char *filename;
105 char *contents;
106 FILE *to_file;
107 const char *semi = strchr(arg_string, ';');
108 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000109 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000110 "error: -remap-file=from;to argument is missing semicolon\n");
111 free_remapped_files(*unsaved_files, i);
112 *unsaved_files = 0;
113 *num_unsaved_files = 0;
114 return -1;
115 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000118 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 if (!to_file) {
120 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
121 semi + 1);
122 free_remapped_files(*unsaved_files, i);
123 *unsaved_files = 0;
124 *num_unsaved_files = 0;
125 return -1;
126 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000127
Douglas Gregor4db64a42010-01-23 00:14:00 +0000128 /* Determine the length of the file we're remapping to. */
129 fseek(to_file, 0, SEEK_END);
130 unsaved->Length = ftell(to_file);
131 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000132
Douglas Gregor4db64a42010-01-23 00:14:00 +0000133 /* Read the contents of the file we're remapping to. */
134 contents = (char *)malloc(unsaved->Length + 1);
135 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
136 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
137 (feof(to_file) ? "EOF" : "error"), semi + 1);
138 fclose(to_file);
139 free_remapped_files(*unsaved_files, i);
140 *unsaved_files = 0;
141 *num_unsaved_files = 0;
142 return -1;
143 }
144 contents[unsaved->Length] = 0;
145 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000146
Douglas Gregor4db64a42010-01-23 00:14:00 +0000147 /* Close the file. */
148 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 /* Copy the file name that we're remapping from. */
151 filename_len = semi - arg_string;
152 filename = (char *)malloc(filename_len + 1);
153 memcpy(filename, arg_string, filename_len);
154 filename[filename_len] = 0;
155 unsaved->Filename = filename;
156 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000157
Douglas Gregor4db64a42010-01-23 00:14:00 +0000158 return 0;
159}
160
Ted Kremenek0d435192009-11-17 18:13:31 +0000161/******************************************************************************/
162/* Pretty-printing. */
163/******************************************************************************/
164
Douglas Gregor430d7a12011-07-25 17:48:11 +0000165static void PrintRange(CXSourceRange R, const char *str) {
166 CXFile begin_file, end_file;
167 unsigned begin_line, begin_column, end_line, end_column;
168
169 clang_getSpellingLocation(clang_getRangeStart(R),
170 &begin_file, &begin_line, &begin_column, 0);
171 clang_getSpellingLocation(clang_getRangeEnd(R),
172 &end_file, &end_line, &end_column, 0);
173 if (!begin_file || !end_file)
174 return;
175
176 printf(" %s=", str);
177 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
178}
179
Douglas Gregor358559d2010-10-02 22:49:11 +0000180int want_display_name = 0;
181
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000182static void PrintCursor(CXCursor Cursor) {
183 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000184 if (clang_isInvalid(Cursor.kind)) {
185 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
186 printf("Invalid Cursor => %s", clang_getCString(ks));
187 clang_disposeString(ks);
188 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000189 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000190 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000191 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000192 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000193 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000194 CXCursor *overridden;
195 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000196 unsigned RefNameRangeNr;
197 CXSourceRange CursorExtent;
198 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000199
Ted Kremeneke68fff62010-02-17 00:41:32 +0000200 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000201 string = want_display_name? clang_getCursorDisplayName(Cursor)
202 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000203 printf("%s=%s", clang_getCString(ks),
204 clang_getCString(string));
205 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000206 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000207
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000208 Referenced = clang_getCursorReferenced(Cursor);
209 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000210 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
211 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
212 printf("[");
213 for (I = 0; I != N; ++I) {
214 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000215 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000216 if (I)
217 printf(", ");
218
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000219 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000220 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000221 printf("%d:%d", line, column);
222 }
223 printf("]");
224 } else {
225 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000226 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000227 printf(":%d:%d", line, column);
228 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000229 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000230
231 if (clang_isCursorDefinition(Cursor))
232 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000233
234 switch (clang_getCursorAvailability(Cursor)) {
235 case CXAvailability_Available:
236 break;
237
238 case CXAvailability_Deprecated:
239 printf(" (deprecated)");
240 break;
241
242 case CXAvailability_NotAvailable:
243 printf(" (unavailable)");
244 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000245
246 case CXAvailability_NotAccessible:
247 printf(" (inaccessible)");
248 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000249 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000250
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000251 if (clang_CXXMethod_isStatic(Cursor))
252 printf(" (static)");
253 if (clang_CXXMethod_isVirtual(Cursor))
254 printf(" (virtual)");
255
Ted Kremenek95f33552010-08-26 01:42:22 +0000256 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
257 CXType T =
258 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
259 CXString S = clang_getTypeKindSpelling(T.kind);
260 printf(" [IBOutletCollection=%s]", clang_getCString(S));
261 clang_disposeString(S);
262 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000263
264 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
265 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
266 unsigned isVirtual = clang_isVirtualBase(Cursor);
267 const char *accessStr = 0;
268
269 switch (access) {
270 case CX_CXXInvalidAccessSpecifier:
271 accessStr = "invalid"; break;
272 case CX_CXXPublic:
273 accessStr = "public"; break;
274 case CX_CXXProtected:
275 accessStr = "protected"; break;
276 case CX_CXXPrivate:
277 accessStr = "private"; break;
278 }
279
280 printf(" [access=%s isVirtual=%s]", accessStr,
281 isVirtual ? "true" : "false");
282 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000283
284 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
285 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
286 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
287 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000288 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000289 printf(" [Specialization of %s:%d:%d]",
290 clang_getCString(Name), line, column);
291 clang_disposeString(Name);
292 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000293
294 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
295 if (num_overridden) {
296 unsigned I;
297 printf(" [Overrides ");
298 for (I = 0; I != num_overridden; ++I) {
299 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000300 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000301 if (I)
302 printf(", ");
303 printf("@%d:%d", line, column);
304 }
305 printf("]");
306 clang_disposeOverriddenCursors(overridden);
307 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000308
309 if (Cursor.kind == CXCursor_InclusionDirective) {
310 CXFile File = clang_getIncludedFile(Cursor);
311 CXString Included = clang_getFileName(File);
312 printf(" (%s)", clang_getCString(Included));
313 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000314
315 if (clang_isFileMultipleIncludeGuarded(TU, File))
316 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000317 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000318
319 CursorExtent = clang_getCursorExtent(Cursor);
320 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
321 CXNameRange_WantQualifier
322 | CXNameRange_WantSinglePiece
323 | CXNameRange_WantTemplateArgs,
324 0);
325 if (!clang_equalRanges(CursorExtent, RefNameRange))
326 PrintRange(RefNameRange, "SingleRefName");
327
328 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
329 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
330 CXNameRange_WantQualifier
331 | CXNameRange_WantTemplateArgs,
332 RefNameRangeNr);
333 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
334 break;
335 if (!clang_equalRanges(CursorExtent, RefNameRange))
336 PrintRange(RefNameRange, "RefName");
337 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000338 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000339}
Steve Naroff89922f82009-08-31 00:59:03 +0000340
Ted Kremeneke68fff62010-02-17 00:41:32 +0000341static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000343 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000345 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000346 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000347 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000348 clang_disposeString(source);
349 return "<invalid loc>";
350 }
351 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000352 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000353 clang_disposeString(source);
354 return b;
355 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000356}
357
Ted Kremenek0d435192009-11-17 18:13:31 +0000358/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000359/* Callbacks. */
360/******************************************************************************/
361
362typedef void (*PostVisitTU)(CXTranslationUnit);
363
Douglas Gregora88084b2010-02-18 18:08:43 +0000364void PrintDiagnostic(CXDiagnostic Diagnostic) {
365 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000366 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000367 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000368 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000369 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
370 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000371 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000372
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000373 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000374 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000375
Douglas Gregor274f1902010-02-22 23:17:23 +0000376 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
377 fprintf(stderr, "%s\n", clang_getCString(Msg));
378 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000379
Douglas Gregora9b06d42010-11-09 06:24:54 +0000380 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
381 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000382 if (!file)
383 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000384
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000385 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
386 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000387 CXSourceRange range;
388 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
389 CXSourceLocation start = clang_getRangeStart(range);
390 CXSourceLocation end = clang_getRangeEnd(range);
391 unsigned start_line, start_column, end_line, end_column;
392 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000393 clang_getSpellingLocation(start, &start_file, &start_line,
394 &start_column, 0);
395 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000396 if (clang_equalLocations(start, end)) {
397 /* Insertion. */
398 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000399 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000400 clang_getCString(insertion_text), start_line, start_column);
401 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
402 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000403 if (start_file == file && end_file == file) {
404 fprintf(out, "FIX-IT: Remove ");
405 PrintExtent(out, start_line, start_column, end_line, end_column);
406 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000407 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000408 } else {
409 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000410 if (start_file == end_file) {
411 fprintf(out, "FIX-IT: Replace ");
412 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000413 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000414 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000415 break;
416 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000417 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000418 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000419}
420
Douglas Gregora88084b2010-02-18 18:08:43 +0000421void PrintDiagnostics(CXTranslationUnit TU) {
422 int i, n = clang_getNumDiagnostics(TU);
423 for (i = 0; i != n; ++i) {
424 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
425 PrintDiagnostic(Diag);
426 clang_disposeDiagnostic(Diag);
427 }
428}
429
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000430void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000431 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000432 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000433 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000434 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000435 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000436 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000437 unsigned long amount = usage.entries[i].amount;
438 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000439 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000440 ((double) amount)/(1024*1024));
441 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000442 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000443 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000444 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000445}
446
Ted Kremenekce2ae882010-01-26 17:59:48 +0000447/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000448/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000449/******************************************************************************/
450
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000451static const char *FileCheckPrefix = "CHECK";
452
Douglas Gregora7bde202010-01-19 00:34:46 +0000453static void PrintCursorExtent(CXCursor C) {
454 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000455 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000456}
457
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000458/* Data used by all of the visitors. */
459typedef struct {
460 CXTranslationUnit TU;
461 enum CXCursorKind *Filter;
462} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000463
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000464
Ted Kremeneke68fff62010-02-17 00:41:32 +0000465enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000466 CXCursor Parent,
467 CXClientData ClientData) {
468 VisitorData *Data = (VisitorData *)ClientData;
469 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000470 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000471 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000472 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000473 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000474 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000475 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000476 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000477 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000478 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000479 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000480
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000481 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000482}
Steve Naroff50398192009-08-28 15:28:48 +0000483
Ted Kremeneke68fff62010-02-17 00:41:32 +0000484static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000485 CXCursor Parent,
486 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000487 const char *startBuf, *endBuf;
488 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
489 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000490 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000491
Douglas Gregorb6998662010-01-19 19:34:47 +0000492 if (Cursor.kind != CXCursor_FunctionDecl ||
493 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000494 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495
496 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
497 &startLine, &startColumn,
498 &endLine, &endColumn);
499 /* Probe the entire body, looking for both decls and refs. */
500 curLine = startLine;
501 curColumn = startColumn;
502
503 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000504 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000505 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000506 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000507
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000508 if (*startBuf == '\n') {
509 startBuf++;
510 curLine++;
511 curColumn = 1;
512 } else if (*startBuf != '\t')
513 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000514
Douglas Gregor98258af2010-01-18 22:46:11 +0000515 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000516 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517
Douglas Gregor1db19de2010-01-19 21:36:55 +0000518 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000519 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000520 CXSourceLocation RefLoc
521 = clang_getLocation(Data->TU, file, curLine, curColumn);
522 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000523 if (Ref.kind == CXCursor_NoDeclFound) {
524 /* Nothing found here; that's fine. */
525 } else if (Ref.kind != CXCursor_FunctionDecl) {
526 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
527 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000528 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000529 printf("\n");
530 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000531 }
Ted Kremenek74844072010-02-17 00:41:20 +0000532 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000533 startBuf++;
534 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000535
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000536 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000537}
538
Ted Kremenek7d405622010-01-12 23:34:26 +0000539/******************************************************************************/
540/* USR testing. */
541/******************************************************************************/
542
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000543enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
544 CXClientData ClientData) {
545 VisitorData *Data = (VisitorData *)ClientData;
546 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000547 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000548 const char *cstr = clang_getCString(USR);
549 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000550 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000551 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000552 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000553 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
554
Douglas Gregora7bde202010-01-19 00:34:46 +0000555 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000556 printf("\n");
557 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000558
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000559 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000560 }
561
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000562 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000563}
564
565/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000566/* Inclusion stack testing. */
567/******************************************************************************/
568
569void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
570 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000571
Ted Kremenek16b55a72010-01-26 19:31:51 +0000572 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000573 CXString fname;
574
575 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000576 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000577 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578
Ted Kremenek16b55a72010-01-26 19:31:51 +0000579 for (i = 0; i < includeStackLen; ++i) {
580 CXFile includingFile;
581 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000582 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
583 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000584 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000585 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000586 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000587 }
588 printf("\n");
589}
590
591void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000592 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000593}
594
595/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000596/* Linkage testing. */
597/******************************************************************************/
598
599static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
600 CXClientData d) {
601 const char *linkage = 0;
602
603 if (clang_isInvalid(clang_getCursorKind(cursor)))
604 return CXChildVisit_Recurse;
605
606 switch (clang_getCursorLinkage(cursor)) {
607 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000608 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
609 case CXLinkage_Internal: linkage = "Internal"; break;
610 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
611 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000612 }
613
614 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000615 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000616 printf("linkage=%s\n", linkage);
617 }
618
619 return CXChildVisit_Recurse;
620}
621
622/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000623/* Typekind testing. */
624/******************************************************************************/
625
626static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
627 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000628 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
629 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000630 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000631 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000632 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000633 if (clang_isConstQualifiedType(T))
634 printf(" const");
635 if (clang_isVolatileQualifiedType(T))
636 printf(" volatile");
637 if (clang_isRestrictQualifiedType(T))
638 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000639 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000640 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000641 {
642 CXType CT = clang_getCanonicalType(T);
643 if (!clang_equalTypes(T, CT)) {
644 CXString CS = clang_getTypeKindSpelling(CT.kind);
645 printf(" [canonical=%s]", clang_getCString(CS));
646 clang_disposeString(CS);
647 }
648 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000649 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000650 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000651 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000652 if (RT.kind != CXType_Invalid) {
653 CXString RS = clang_getTypeKindSpelling(RT.kind);
654 printf(" [result=%s]", clang_getCString(RS));
655 clang_disposeString(RS);
656 }
657 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000658 /* Print if this is a non-POD type. */
659 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000661 printf("\n");
662 }
663 return CXChildVisit_Recurse;
664}
665
666
667/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000668/* Loading ASTs/source. */
669/******************************************************************************/
670
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000671static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000672 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000673 CXCursorVisitor Visitor,
674 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000676 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000677 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000678
679 if (Visitor) {
680 enum CXCursorKind K = CXCursor_NotImplemented;
681 enum CXCursorKind *ck = &K;
682 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000684 /* Perform some simple filtering. */
685 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000686 else if (!strcmp(filter, "all-display") ||
687 !strcmp(filter, "local-display")) {
688 ck = NULL;
689 want_display_name = 1;
690 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000691 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000692 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
693 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
694 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
695 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
696 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
697 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
698 else {
699 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
700 return 1;
701 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000702
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000703 Data.TU = TU;
704 Data.Filter = ck;
705 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000706 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707
Ted Kremenekce2ae882010-01-26 17:59:48 +0000708 if (PV)
709 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000710
Douglas Gregora88084b2010-02-18 18:08:43 +0000711 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000712 if (checkForErrors(TU) != 0) {
713 clang_disposeTranslationUnit(TU);
714 return -1;
715 }
716
Ted Kremenek0d435192009-11-17 18:13:31 +0000717 clang_disposeTranslationUnit(TU);
718 return 0;
719}
720
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000721int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000722 const char *prefix, CXCursorVisitor Visitor,
723 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000724 CXIndex Idx;
725 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000726 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000727 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000728 !strcmp(filter, "local") ? 1 : 0,
729 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000730
Ted Kremenek020a0952010-02-11 07:41:25 +0000731 if (!CreateTranslationUnit(Idx, file, &TU)) {
732 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000733 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000734 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000735
Ted Kremenek020a0952010-02-11 07:41:25 +0000736 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
737 clang_disposeIndex(Idx);
738 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000739}
740
Ted Kremenekce2ae882010-01-26 17:59:48 +0000741int perform_test_load_source(int argc, const char **argv,
742 const char *filter, CXCursorVisitor Visitor,
743 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000744 CXIndex Idx;
745 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000746 struct CXUnsavedFile *unsaved_files = 0;
747 int num_unsaved_files = 0;
748 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000749
Daniel Dunbarada487d2009-12-01 02:03:10 +0000750 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000751 (!strcmp(filter, "local") ||
752 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000753 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000754
Ted Kremenek020a0952010-02-11 07:41:25 +0000755 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
756 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000757 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000758 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000759
Douglas Gregordca8ee82011-05-06 16:33:08 +0000760 TU = clang_parseTranslationUnit(Idx, 0,
761 argv + num_unsaved_files,
762 argc - num_unsaved_files,
763 unsaved_files, num_unsaved_files,
764 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000765 if (!TU) {
766 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000767 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000768 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000769 return 1;
770 }
771
Ted Kremenekce2ae882010-01-26 17:59:48 +0000772 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000773 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000774 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000775 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000776}
777
Douglas Gregorabc563f2010-07-19 21:46:24 +0000778int perform_test_reparse_source(int argc, const char **argv, int trials,
779 const char *filter, CXCursorVisitor Visitor,
780 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000781 CXIndex Idx;
782 CXTranslationUnit TU;
783 struct CXUnsavedFile *unsaved_files = 0;
784 int num_unsaved_files = 0;
785 int result;
786 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000787 int remap_after_trial = 0;
788 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000789
790 Idx = clang_createIndex(/* excludeDeclsFromPCH */
791 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000792 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000793
Douglas Gregorabc563f2010-07-19 21:46:24 +0000794 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
795 clang_disposeIndex(Idx);
796 return -1;
797 }
798
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000799 /* Load the initial translation unit -- we do this without honoring remapped
800 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000801 TU = clang_parseTranslationUnit(Idx, 0,
802 argv + num_unsaved_files,
803 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000804 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000805 if (!TU) {
806 fprintf(stderr, "Unable to load translation unit!\n");
807 free_remapped_files(unsaved_files, num_unsaved_files);
808 clang_disposeIndex(Idx);
809 return 1;
810 }
811
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000812 if (checkForErrors(TU) != 0)
813 return -1;
814
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000815 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
816 remap_after_trial =
817 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
818 }
819
Douglas Gregorabc563f2010-07-19 21:46:24 +0000820 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000821 if (clang_reparseTranslationUnit(TU,
822 trial >= remap_after_trial ? num_unsaved_files : 0,
823 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000824 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000825 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000826 clang_disposeTranslationUnit(TU);
827 free_remapped_files(unsaved_files, num_unsaved_files);
828 clang_disposeIndex(Idx);
829 return -1;
830 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000831
832 if (checkForErrors(TU) != 0)
833 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000834 }
835
836 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000837
Douglas Gregorabc563f2010-07-19 21:46:24 +0000838 free_remapped_files(unsaved_files, num_unsaved_files);
839 clang_disposeIndex(Idx);
840 return result;
841}
842
Ted Kremenek0d435192009-11-17 18:13:31 +0000843/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000844/* Logic for testing clang_getCursor(). */
845/******************************************************************************/
846
Douglas Gregordd3e5542011-05-04 00:14:37 +0000847static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000848 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000849 unsigned end_line, unsigned end_col,
850 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000851 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000852 if (prefix)
853 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000854 PrintExtent(stdout, start_line, start_col, end_line, end_col);
855 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000856 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000857 printf("\n");
858}
859
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000860static int perform_file_scan(const char *ast_file, const char *source_file,
861 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000862 CXIndex Idx;
863 CXTranslationUnit TU;
864 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000865 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000866 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000867 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000868 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000869
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000870 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
871 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000872 fprintf(stderr, "Could not create Index\n");
873 return 1;
874 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875
Ted Kremenek1c6da172009-11-17 19:37:36 +0000876 if (!CreateTranslationUnit(Idx, ast_file, &TU))
877 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000878
Ted Kremenek1c6da172009-11-17 19:37:36 +0000879 if ((fp = fopen(source_file, "r")) == NULL) {
880 fprintf(stderr, "Could not open '%s'\n", source_file);
881 return 1;
882 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883
Douglas Gregorb9790342010-01-22 21:44:22 +0000884 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000885 for (;;) {
886 CXCursor cursor;
887 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000888
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000889 if (c == '\n') {
890 ++line;
891 col = 1;
892 } else
893 ++col;
894
895 /* Check the cursor at this position, and dump the previous one if we have
896 * found something new.
897 */
898 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
899 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
900 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000901 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000902 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000903 start_line = line;
904 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000905 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000906 if (c == EOF)
907 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000908
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000909 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000910 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000911
Ted Kremenek1c6da172009-11-17 19:37:36 +0000912 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000913 clang_disposeTranslationUnit(TU);
914 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000915 return 0;
916}
917
918/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000919/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000920/******************************************************************************/
921
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000922/* Parse file:line:column from the input string. Returns 0 on success, non-zero
923 on failure. If successful, the pointer *filename will contain newly-allocated
924 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000925int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000926 unsigned *column, unsigned *second_line,
927 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000928 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000929 const char *last_colon = strrchr(input, ':');
930 unsigned values[4], i;
931 unsigned num_values = (second_line && second_column)? 4 : 2;
932
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000933 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000934 if (!last_colon || last_colon == input) {
935 if (num_values == 4)
936 fprintf(stderr, "could not parse filename:line:column:line:column in "
937 "'%s'\n", input);
938 else
939 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000940 return 1;
941 }
942
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000943 for (i = 0; i != num_values; ++i) {
944 const char *prev_colon;
945
946 /* Parse the next line or column. */
947 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
948 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000949 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000950 (i % 2 ? "column" : "line"), input);
951 return 1;
952 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000953
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000954 if (i + 1 == num_values)
955 break;
956
957 /* Find the previous colon. */
958 prev_colon = last_colon - 1;
959 while (prev_colon != input && *prev_colon != ':')
960 --prev_colon;
961 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000963 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000964 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000965 }
966
967 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000968 }
969
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000970 *line = values[0];
971 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000973 if (second_line && second_column) {
974 *second_line = values[2];
975 *second_column = values[3];
976 }
977
Douglas Gregor88d23952009-11-09 18:19:57 +0000978 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000979 *filename = (char*)malloc(last_colon - input + 1);
980 memcpy(*filename, input, last_colon - input);
981 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000982 return 0;
983}
984
985const char *
986clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
987 switch (Kind) {
988 case CXCompletionChunk_Optional: return "Optional";
989 case CXCompletionChunk_TypedText: return "TypedText";
990 case CXCompletionChunk_Text: return "Text";
991 case CXCompletionChunk_Placeholder: return "Placeholder";
992 case CXCompletionChunk_Informative: return "Informative";
993 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
994 case CXCompletionChunk_LeftParen: return "LeftParen";
995 case CXCompletionChunk_RightParen: return "RightParen";
996 case CXCompletionChunk_LeftBracket: return "LeftBracket";
997 case CXCompletionChunk_RightBracket: return "RightBracket";
998 case CXCompletionChunk_LeftBrace: return "LeftBrace";
999 case CXCompletionChunk_RightBrace: return "RightBrace";
1000 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1001 case CXCompletionChunk_RightAngle: return "RightAngle";
1002 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001003 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001004 case CXCompletionChunk_Colon: return "Colon";
1005 case CXCompletionChunk_SemiColon: return "SemiColon";
1006 case CXCompletionChunk_Equal: return "Equal";
1007 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1008 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001009 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001010
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001011 return "Unknown";
1012}
1013
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001014static int checkForErrors(CXTranslationUnit TU) {
1015 unsigned Num, i;
1016 CXDiagnostic Diag;
1017 CXString DiagStr;
1018
1019 if (!getenv("CINDEXTEST_FAILONERROR"))
1020 return 0;
1021
1022 Num = clang_getNumDiagnostics(TU);
1023 for (i = 0; i != Num; ++i) {
1024 Diag = clang_getDiagnostic(TU, i);
1025 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1026 DiagStr = clang_formatDiagnostic(Diag,
1027 clang_defaultDiagnosticDisplayOptions());
1028 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1029 clang_disposeString(DiagStr);
1030 clang_disposeDiagnostic(Diag);
1031 return -1;
1032 }
1033 clang_disposeDiagnostic(Diag);
1034 }
1035
1036 return 0;
1037}
1038
Douglas Gregor3ac73852009-11-09 16:04:45 +00001039void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001040 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001041
Douglas Gregor3ac73852009-11-09 16:04:45 +00001042 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001043 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001044 CXString text;
1045 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001046 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001047 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001048
Douglas Gregor3ac73852009-11-09 16:04:45 +00001049 if (Kind == CXCompletionChunk_Optional) {
1050 fprintf(file, "{Optional ");
1051 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001052 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001053 file);
1054 fprintf(file, "}");
1055 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001056 }
1057
1058 if (Kind == CXCompletionChunk_VerticalSpace) {
1059 fprintf(file, "{VerticalSpace }");
1060 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001061 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001062
Douglas Gregord5a20892009-11-09 17:05:28 +00001063 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001064 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001065 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001066 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001067 cstr ? cstr : "");
1068 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001069 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001070
Douglas Gregor3ac73852009-11-09 16:04:45 +00001071}
1072
1073void print_completion_result(CXCompletionResult *completion_result,
1074 CXClientData client_data) {
1075 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001076 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001077 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001078
1079 fprintf(file, "%s:", clang_getCString(ks));
1080 clang_disposeString(ks);
1081
Douglas Gregor3ac73852009-11-09 16:04:45 +00001082 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001083 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001084 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001085 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1086 case CXAvailability_Available:
1087 break;
1088
1089 case CXAvailability_Deprecated:
1090 fprintf(file, " (deprecated)");
1091 break;
1092
1093 case CXAvailability_NotAvailable:
1094 fprintf(file, " (unavailable)");
1095 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001096
1097 case CXAvailability_NotAccessible:
1098 fprintf(file, " (inaccessible)");
1099 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001100 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001101
1102 annotationCount = clang_getCompletionNumAnnotations(
1103 completion_result->CompletionString);
1104 if (annotationCount) {
1105 unsigned i;
1106 fprintf(file, " (");
1107 for (i = 0; i < annotationCount; ++i) {
1108 if (i != 0)
1109 fprintf(file, ", ");
1110 fprintf(file, "\"%s\"",
1111 clang_getCString(clang_getCompletionAnnotation(
1112 completion_result->CompletionString, i)));
1113 }
1114 fprintf(file, ")");
1115 }
1116
Douglas Gregor58ddb602010-08-23 23:00:57 +00001117 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001118}
1119
Douglas Gregor3da626b2011-07-07 16:03:39 +00001120void print_completion_contexts(unsigned long long contexts, FILE *file) {
1121 fprintf(file, "Completion contexts:\n");
1122 if (contexts == CXCompletionContext_Unknown) {
1123 fprintf(file, "Unknown\n");
1124 }
1125 if (contexts & CXCompletionContext_AnyType) {
1126 fprintf(file, "Any type\n");
1127 }
1128 if (contexts & CXCompletionContext_AnyValue) {
1129 fprintf(file, "Any value\n");
1130 }
1131 if (contexts & CXCompletionContext_ObjCObjectValue) {
1132 fprintf(file, "Objective-C object value\n");
1133 }
1134 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1135 fprintf(file, "Objective-C selector value\n");
1136 }
1137 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1138 fprintf(file, "C++ class type value\n");
1139 }
1140 if (contexts & CXCompletionContext_DotMemberAccess) {
1141 fprintf(file, "Dot member access\n");
1142 }
1143 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1144 fprintf(file, "Arrow member access\n");
1145 }
1146 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1147 fprintf(file, "Objective-C property access\n");
1148 }
1149 if (contexts & CXCompletionContext_EnumTag) {
1150 fprintf(file, "Enum tag\n");
1151 }
1152 if (contexts & CXCompletionContext_UnionTag) {
1153 fprintf(file, "Union tag\n");
1154 }
1155 if (contexts & CXCompletionContext_StructTag) {
1156 fprintf(file, "Struct tag\n");
1157 }
1158 if (contexts & CXCompletionContext_ClassTag) {
1159 fprintf(file, "Class name\n");
1160 }
1161 if (contexts & CXCompletionContext_Namespace) {
1162 fprintf(file, "Namespace or namespace alias\n");
1163 }
1164 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1165 fprintf(file, "Nested name specifier\n");
1166 }
1167 if (contexts & CXCompletionContext_ObjCInterface) {
1168 fprintf(file, "Objective-C interface\n");
1169 }
1170 if (contexts & CXCompletionContext_ObjCProtocol) {
1171 fprintf(file, "Objective-C protocol\n");
1172 }
1173 if (contexts & CXCompletionContext_ObjCCategory) {
1174 fprintf(file, "Objective-C category\n");
1175 }
1176 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1177 fprintf(file, "Objective-C instance method\n");
1178 }
1179 if (contexts & CXCompletionContext_ObjCClassMessage) {
1180 fprintf(file, "Objective-C class method\n");
1181 }
1182 if (contexts & CXCompletionContext_ObjCSelectorName) {
1183 fprintf(file, "Objective-C selector name\n");
1184 }
1185 if (contexts & CXCompletionContext_MacroName) {
1186 fprintf(file, "Macro name\n");
1187 }
1188 if (contexts & CXCompletionContext_NaturalLanguage) {
1189 fprintf(file, "Natural language\n");
1190 }
1191}
1192
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001193int my_stricmp(const char *s1, const char *s2) {
1194 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001195 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001196 if (c1 < c2)
1197 return -1;
1198 else if (c1 > c2)
1199 return 1;
1200
1201 ++s1;
1202 ++s2;
1203 }
1204
1205 if (*s1)
1206 return 1;
1207 else if (*s2)
1208 return -1;
1209 return 0;
1210}
1211
Douglas Gregor1982c182010-07-12 18:38:41 +00001212int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001213 const char *input = argv[1];
1214 char *filename = 0;
1215 unsigned line;
1216 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001217 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001218 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001219 struct CXUnsavedFile *unsaved_files = 0;
1220 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001221 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001222 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001223 unsigned I, Repeats = 1;
1224 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1225
1226 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1227 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001228
Douglas Gregor1982c182010-07-12 18:38:41 +00001229 if (timing_only)
1230 input += strlen("-code-completion-timing=");
1231 else
1232 input += strlen("-code-completion-at=");
1233
Ted Kremeneke68fff62010-02-17 00:41:32 +00001234 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001235 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001236 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001237
Douglas Gregor735df882009-12-02 09:21:34 +00001238 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1239 return -1;
1240
Douglas Gregor32be4a52010-10-11 21:37:58 +00001241 CIdx = clang_createIndex(0, 0);
1242
1243 if (getenv("CINDEXTEST_EDITING"))
1244 Repeats = 5;
1245
1246 TU = clang_parseTranslationUnit(CIdx, 0,
1247 argv + num_unsaved_files + 2,
1248 argc - num_unsaved_files - 2,
1249 0, 0, getDefaultParsingOptions());
1250 if (!TU) {
1251 fprintf(stderr, "Unable to load translation unit!\n");
1252 return 1;
1253 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001254
1255 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1256 fprintf(stderr, "Unable to reparse translation init!\n");
1257 return 1;
1258 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001259
1260 for (I = 0; I != Repeats; ++I) {
1261 results = clang_codeCompleteAt(TU, filename, line, column,
1262 unsaved_files, num_unsaved_files,
1263 completionOptions);
1264 if (!results) {
1265 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001266 return 1;
1267 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001268 if (I != Repeats-1)
1269 clang_disposeCodeCompleteResults(results);
1270 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001271
Douglas Gregorec6762c2009-12-18 16:20:58 +00001272 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001273 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001274 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001275 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001276 CXString objCSelector;
1277 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001278 if (!timing_only) {
1279 /* Sort the code-completion results based on the typed text. */
1280 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1281
Douglas Gregor1982c182010-07-12 18:38:41 +00001282 for (i = 0; i != n; ++i)
1283 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001284 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001285 n = clang_codeCompleteGetNumDiagnostics(results);
1286 for (i = 0; i != n; ++i) {
1287 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1288 PrintDiagnostic(diag);
1289 clang_disposeDiagnostic(diag);
1290 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001291
1292 contexts = clang_codeCompleteGetContexts(results);
1293 print_completion_contexts(contexts, stdout);
1294
Douglas Gregor0a47d692011-07-26 15:24:30 +00001295 containerKind = clang_codeCompleteGetContainerKind(results,
1296 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001297
1298 if (containerKind != CXCursor_InvalidCode) {
1299 /* We have found a container */
1300 CXString containerUSR, containerKindSpelling;
1301 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1302 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1303 clang_disposeString(containerKindSpelling);
1304
1305 if (containerIsIncomplete) {
1306 printf("Container is incomplete\n");
1307 }
1308 else {
1309 printf("Container is complete\n");
1310 }
1311
1312 containerUSR = clang_codeCompleteGetContainerUSR(results);
1313 printf("Container USR: %s\n", clang_getCString(containerUSR));
1314 clang_disposeString(containerUSR);
1315 }
1316
Douglas Gregor0a47d692011-07-26 15:24:30 +00001317 objCSelector = clang_codeCompleteGetObjCSelector(results);
1318 selectorString = clang_getCString(objCSelector);
1319 if (selectorString && strlen(selectorString) > 0) {
1320 printf("Objective-C selector: %s\n", selectorString);
1321 }
1322 clang_disposeString(objCSelector);
1323
Douglas Gregorec6762c2009-12-18 16:20:58 +00001324 clang_disposeCodeCompleteResults(results);
1325 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001326 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001327 clang_disposeIndex(CIdx);
1328 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001329
Douglas Gregor735df882009-12-02 09:21:34 +00001330 free_remapped_files(unsaved_files, num_unsaved_files);
1331
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001332 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001333}
1334
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001335typedef struct {
1336 char *filename;
1337 unsigned line;
1338 unsigned column;
1339} CursorSourceLocation;
1340
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001341static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001342 CXIndex CIdx;
1343 int errorCode;
1344 struct CXUnsavedFile *unsaved_files = 0;
1345 int num_unsaved_files = 0;
1346 CXTranslationUnit TU;
1347 CXCursor Cursor;
1348 CursorSourceLocation *Locations = 0;
1349 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001350 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001351 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001352
Ted Kremeneke68fff62010-02-17 00:41:32 +00001353 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001354 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1355 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001356
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001357 /* Parse the locations. */
1358 assert(NumLocations > 0 && "Unable to count locations?");
1359 Locations = (CursorSourceLocation *)malloc(
1360 NumLocations * sizeof(CursorSourceLocation));
1361 for (Loc = 0; Loc < NumLocations; ++Loc) {
1362 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001363 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1364 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001365 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001366 return errorCode;
1367 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368
1369 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001370 &num_unsaved_files))
1371 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001372
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001373 if (getenv("CINDEXTEST_EDITING"))
1374 Repeats = 5;
1375
1376 /* Parse the translation unit. When we're testing clang_getCursor() after
1377 reparsing, don't remap unsaved files until the second parse. */
1378 CIdx = clang_createIndex(1, 1);
1379 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1380 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001381 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001382 unsaved_files,
1383 Repeats > 1? 0 : num_unsaved_files,
1384 getDefaultParsingOptions());
1385
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001386 if (!TU) {
1387 fprintf(stderr, "unable to parse input\n");
1388 return -1;
1389 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001390
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001391 if (checkForErrors(TU) != 0)
1392 return -1;
1393
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001394 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001395 if (Repeats > 1 &&
1396 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1397 clang_defaultReparseOptions(TU))) {
1398 clang_disposeTranslationUnit(TU);
1399 return 1;
1400 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001401
1402 if (checkForErrors(TU) != 0)
1403 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001404
1405 for (Loc = 0; Loc < NumLocations; ++Loc) {
1406 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1407 if (!file)
1408 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001409
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001410 Cursor = clang_getCursor(TU,
1411 clang_getLocation(TU, file, Locations[Loc].line,
1412 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001413
1414 if (checkForErrors(TU) != 0)
1415 return -1;
1416
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001417 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001418 CXCompletionString completionString = clang_getCursorCompletionString(
1419 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001420 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001421 if (completionString != NULL) {
1422 printf("\nCompletion string: ");
1423 print_completion_string(completionString, stdout);
1424 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001425 printf("\n");
1426 free(Locations[Loc].filename);
1427 }
1428 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001429 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001430
Douglas Gregora88084b2010-02-18 18:08:43 +00001431 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001432 clang_disposeTranslationUnit(TU);
1433 clang_disposeIndex(CIdx);
1434 free(Locations);
1435 free_remapped_files(unsaved_files, num_unsaved_files);
1436 return 0;
1437}
1438
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001439static enum CXVisitorResult findFileRefsVisit(void *context,
1440 CXCursor cursor, CXSourceRange range) {
1441 if (clang_Range_isNull(range))
1442 return CXVisit_Continue;
1443
1444 PrintCursor(cursor);
1445 PrintRange(range, "");
1446 printf("\n");
1447 return CXVisit_Continue;
1448}
1449
1450static int find_file_refs_at(int argc, const char **argv) {
1451 CXIndex CIdx;
1452 int errorCode;
1453 struct CXUnsavedFile *unsaved_files = 0;
1454 int num_unsaved_files = 0;
1455 CXTranslationUnit TU;
1456 CXCursor Cursor;
1457 CursorSourceLocation *Locations = 0;
1458 unsigned NumLocations = 0, Loc;
1459 unsigned Repeats = 1;
1460 unsigned I;
1461
1462 /* Count the number of locations. */
1463 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1464 ++NumLocations;
1465
1466 /* Parse the locations. */
1467 assert(NumLocations > 0 && "Unable to count locations?");
1468 Locations = (CursorSourceLocation *)malloc(
1469 NumLocations * sizeof(CursorSourceLocation));
1470 for (Loc = 0; Loc < NumLocations; ++Loc) {
1471 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1472 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1473 &Locations[Loc].line,
1474 &Locations[Loc].column, 0, 0)))
1475 return errorCode;
1476 }
1477
1478 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1479 &num_unsaved_files))
1480 return -1;
1481
1482 if (getenv("CINDEXTEST_EDITING"))
1483 Repeats = 5;
1484
1485 /* Parse the translation unit. When we're testing clang_getCursor() after
1486 reparsing, don't remap unsaved files until the second parse. */
1487 CIdx = clang_createIndex(1, 1);
1488 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1489 argv + num_unsaved_files + 1 + NumLocations,
1490 argc - num_unsaved_files - 2 - NumLocations,
1491 unsaved_files,
1492 Repeats > 1? 0 : num_unsaved_files,
1493 getDefaultParsingOptions());
1494
1495 if (!TU) {
1496 fprintf(stderr, "unable to parse input\n");
1497 return -1;
1498 }
1499
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001500 if (checkForErrors(TU) != 0)
1501 return -1;
1502
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001503 for (I = 0; I != Repeats; ++I) {
1504 if (Repeats > 1 &&
1505 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1506 clang_defaultReparseOptions(TU))) {
1507 clang_disposeTranslationUnit(TU);
1508 return 1;
1509 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001510
1511 if (checkForErrors(TU) != 0)
1512 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001513
1514 for (Loc = 0; Loc < NumLocations; ++Loc) {
1515 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1516 if (!file)
1517 continue;
1518
1519 Cursor = clang_getCursor(TU,
1520 clang_getLocation(TU, file, Locations[Loc].line,
1521 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001522
1523 if (checkForErrors(TU) != 0)
1524 return -1;
1525
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001526 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001527 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001528 PrintCursor(Cursor);
1529 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001530 clang_findReferencesInFile(Cursor, file, visitor);
1531 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001532
1533 if (checkForErrors(TU) != 0)
1534 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001535 }
1536 }
1537 }
1538
1539 PrintDiagnostics(TU);
1540 clang_disposeTranslationUnit(TU);
1541 clang_disposeIndex(CIdx);
1542 free(Locations);
1543 free_remapped_files(unsaved_files, num_unsaved_files);
1544 return 0;
1545}
1546
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001547typedef struct {
1548 const char *check_prefix;
1549 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001550 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001551 int abort;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001552} IndexData;
1553
1554static void printCheck(IndexData *data) {
1555 if (data->check_prefix) {
1556 if (data->first_check_printed) {
1557 printf("// %s-NEXT: ", data->check_prefix);
1558 } else {
1559 printf("// %s : ", data->check_prefix);
1560 data->first_check_printed = 1;
1561 }
1562 }
1563}
1564
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001565static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001566 CXString filename = clang_getFileName((CXFile)file);
1567 printf("%s", clang_getCString(filename));
1568 clang_disposeString(filename);
1569}
1570
1571static void printCXIndexLoc(CXIdxLoc loc) {
1572 CXString filename;
1573 const char *cname, *end;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001574 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001575 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001576 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001577
1578 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1579 if (line == 0) {
1580 printf("<null loc>");
1581 return;
1582 }
1583 filename = clang_getFileName((CXFile)file);
1584 cname = clang_getCString(filename);
1585 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001586 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001587
1588 if (isHeader) {
1589 printCXIndexFile(file);
1590 printf(":");
1591 }
1592 printf("%d:%d", line, column);
1593}
1594
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001595static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1596 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001597 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001598 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001599 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001600 unsigned line, column;
1601
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001602 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001603 if (!name)
1604 name = "<anon-tag>";
1605
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001606 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001607 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001608 newStr = (char *)malloc(strlen(name) + 10);
1609 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001610 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001611}
1612
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001613static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1614 CXIdxClientContainer container;
1615 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001616 if (!container)
1617 printf("[<<NULL>>]");
1618 else
1619 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001620}
1621
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001622static const char *getEntityKindString(CXIdxEntityKind kind) {
1623 switch (kind) {
1624 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1625 case CXIdxEntity_Typedef: return "typedef";
1626 case CXIdxEntity_Function: return "function";
1627 case CXIdxEntity_Variable: return "variable";
1628 case CXIdxEntity_Field: return "field";
1629 case CXIdxEntity_EnumConstant: return "enumerator";
1630 case CXIdxEntity_ObjCClass: return "objc-class";
1631 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1632 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001633 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1634 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001635 case CXIdxEntity_ObjCProperty: return "objc-property";
1636 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1637 case CXIdxEntity_Enum: return "enum";
1638 case CXIdxEntity_Struct: return "struct";
1639 case CXIdxEntity_Union: return "union";
1640 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001641 case CXIdxEntity_CXXNamespace: return "namespace";
1642 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1643 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1644 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1645 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1646 case CXIdxEntity_CXXConstructor: return "constructor";
1647 case CXIdxEntity_CXXDestructor: return "destructor";
1648 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1649 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1650 }
1651 assert(0 && "Garbage entity kind");
1652 return 0;
1653}
1654
1655static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1656 switch (kind) {
1657 case CXIdxEntity_NonTemplate: return "";
1658 case CXIdxEntity_Template: return "-template";
1659 case CXIdxEntity_TemplatePartialSpecialization:
1660 return "-template-partial-spec";
1661 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001662 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001663 assert(0 && "Garbage entity kind");
1664 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001665}
1666
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001667static void printEntityInfo(const char *cb,
1668 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001669 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001670 const char *name;
1671 IndexData *index_data;
1672 index_data = (IndexData *)client_data;
1673 printCheck(index_data);
1674
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001675 if (!info) {
1676 printf("%s: <<NULL>>", cb);
1677 return;
1678 }
1679
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001680 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001681 if (!name)
1682 name = "<anon-tag>";
1683
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001684 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1685 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001686 printf(" | name: %s", name);
1687 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001688}
1689
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001690static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1691 CXClientData client_data) {
1692 unsigned i;
1693 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1694 printEntityInfo(" <protocol>", client_data,
1695 ProtoInfo->protocols[i]->protocol);
1696 printf(" | cursor: ");
1697 PrintCursor(ProtoInfo->protocols[i]->cursor);
1698 printf(" | loc: ");
1699 printCXIndexLoc(ProtoInfo->protocols[i]->loc);
1700 printf("\n");
1701 }
1702}
1703
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001704static void index_diagnostic(CXClientData client_data,
1705 CXDiagnostic diag, void *reserved) {
1706 CXString str;
1707 const char *cstr;
1708 IndexData *index_data;
1709 index_data = (IndexData *)client_data;
1710 printCheck(index_data);
1711
1712 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1713 cstr = clang_getCString(str);
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001714 printf("[diagnostic]: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001715 clang_disposeString(str);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001716
1717 if (getenv("CINDEXTEST_FAILONERROR") &&
1718 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1719 index_data->fail_for_error = 1;
1720 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001721}
1722
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001723static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1724 CXFile file, void *reserved) {
1725 IndexData *index_data;
1726 index_data = (IndexData *)client_data;
1727 printCheck(index_data);
1728
1729 printf("[enteredMainFile]: ");
1730 printCXIndexFile((CXIdxClientFile)file);
1731 printf("\n");
1732
1733 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001734}
1735
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001736static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001737 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001738 IndexData *index_data;
1739 index_data = (IndexData *)client_data;
1740 printCheck(index_data);
1741
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001742 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001743 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001744 printf(" | name: \"%s\"", info->filename);
1745 printf(" | hash loc: ");
1746 printCXIndexLoc(info->hashLoc);
1747 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001748
1749 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001750}
1751
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001752static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001753 void *reserved) {
1754 IndexData *index_data;
1755 index_data = (IndexData *)client_data;
1756 printCheck(index_data);
1757
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001758 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001759 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001760}
1761
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001762static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001763 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001764 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001765 const CXIdxObjCCategoryDeclInfo *CatInfo;
1766 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001767 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001768 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001769 index_data = (IndexData *)client_data;
1770
1771 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1772 printf(" | cursor: ");
1773 PrintCursor(info->cursor);
1774 printf(" | loc: ");
1775 printCXIndexLoc(info->loc);
1776 printf(" | container: ");
1777 printCXIndexContainer(info->container);
1778 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001779 printf(" | isDef: %d", info->isDefinition);
1780 printf(" | isContainer: %d", info->isContainer);
1781 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001782
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001783 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001784 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001785 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001786 PrintCursor(Attr->cursor);
1787 printf("\n");
1788 }
1789
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001790 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1791 const char *kindName = 0;
1792 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1793 switch (K) {
1794 case CXIdxObjCContainer_ForwardRef:
1795 kindName = "forward-ref"; break;
1796 case CXIdxObjCContainer_Interface:
1797 kindName = "interface"; break;
1798 case CXIdxObjCContainer_Implementation:
1799 kindName = "implementation"; break;
1800 }
1801 printCheck(index_data);
1802 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1803 }
1804
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001805 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001806 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1807 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001808 printf(" | cursor: ");
1809 PrintCursor(CatInfo->classCursor);
1810 printf(" | loc: ");
1811 printCXIndexLoc(CatInfo->classLoc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001812 printf("\n");
1813 }
1814
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001815 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1816 if (InterInfo->superInfo) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001817 printEntityInfo(" <base>", client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001818 InterInfo->superInfo->base);
1819 printf(" | cursor: ");
1820 PrintCursor(InterInfo->superInfo->cursor);
1821 printf(" | loc: ");
1822 printCXIndexLoc(InterInfo->superInfo->loc);
1823 printf("\n");
1824 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001825 }
1826
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001827 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1828 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001829 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001830
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001831 if (info->declAsContainer)
1832 clang_index_setClientContainer(info->declAsContainer,
1833 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001834}
1835
1836static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001837 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001838 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001839 printf(" | cursor: ");
1840 PrintCursor(info->cursor);
1841 printf(" | loc: ");
1842 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001843 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001844 printf(" | container: ");
1845 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001846 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001847 switch (info->kind) {
1848 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001849 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001850 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001851 printf("\n");
1852}
1853
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001854static int index_abortQuery(CXClientData client_data, void *reserved) {
1855 IndexData *index_data;
1856 index_data = (IndexData *)client_data;
1857 return index_data->abort;
1858}
1859
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001860static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001861 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001862 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001863 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001864 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001865 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001866 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001867 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001868 index_indexEntityReference
1869};
1870
1871static int index_file(int argc, const char **argv) {
1872 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001873 CXIndex Idx;
1874 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001875 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001876 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001877 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001878
1879 check_prefix = 0;
1880 if (argc > 0) {
1881 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1882 check_prefix = argv[0] + strlen("-check-prefix=");
1883 ++argv;
1884 --argc;
1885 }
1886 }
1887
1888 if (argc == 0) {
1889 fprintf(stderr, "no compiler arguments\n");
1890 return -1;
1891 }
1892
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001893 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1894 /* displayDiagnosics=*/1))) {
1895 fprintf(stderr, "Could not create Index\n");
1896 return 1;
1897 }
1898 idxAction = 0;
1899 result = 1;
1900
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001901 index_data.check_prefix = check_prefix;
1902 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001903 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001904 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001905
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001906 index_opts = 0;
1907 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1908 index_opts |= CXIndexOpt_SuppressRedundantRefs;
1909
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001910 idxAction = clang_IndexAction_create(Idx);
1911 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001912 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001913 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001914 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001915 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001916
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001917 clang_IndexAction_dispose(idxAction);
1918 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001919 return result;
1920}
1921
1922static int index_tu(int argc, const char **argv) {
1923 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001924 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001925 CXTranslationUnit TU;
1926 const char *check_prefix;
1927 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001928 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001929 int result;
1930
1931 check_prefix = 0;
1932 if (argc > 0) {
1933 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1934 check_prefix = argv[0] + strlen("-check-prefix=");
1935 ++argv;
1936 --argc;
1937 }
1938 }
1939
1940 if (argc == 0) {
1941 fprintf(stderr, "no ast file\n");
1942 return -1;
1943 }
1944
1945 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1946 /* displayDiagnosics=*/1))) {
1947 fprintf(stderr, "Could not create Index\n");
1948 return 1;
1949 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001950 idxAction = 0;
1951 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001952
1953 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001954 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001955
1956 index_data.check_prefix = check_prefix;
1957 index_data.first_check_printed = 0;
1958 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001959 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001960
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001961 index_opts = 0;
1962 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1963 index_opts |= CXIndexOpt_SuppressRedundantRefs;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001964
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001965 idxAction = clang_IndexAction_create(Idx);
1966 result = clang_indexTranslationUnit(idxAction, &index_data,
1967 &IndexCB,sizeof(IndexCB),
1968 index_opts, TU);
1969 if (index_data.fail_for_error)
1970 goto finished;
1971
1972 finished:
1973 clang_IndexAction_dispose(idxAction);
1974 clang_disposeIndex(Idx);
1975
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001976 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001977}
1978
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001979int perform_token_annotation(int argc, const char **argv) {
1980 const char *input = argv[1];
1981 char *filename = 0;
1982 unsigned line, second_line;
1983 unsigned column, second_column;
1984 CXIndex CIdx;
1985 CXTranslationUnit TU = 0;
1986 int errorCode;
1987 struct CXUnsavedFile *unsaved_files = 0;
1988 int num_unsaved_files = 0;
1989 CXToken *tokens;
1990 unsigned num_tokens;
1991 CXSourceRange range;
1992 CXSourceLocation startLoc, endLoc;
1993 CXFile file = 0;
1994 CXCursor *cursors = 0;
1995 unsigned i;
1996
1997 input += strlen("-test-annotate-tokens=");
1998 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1999 &second_line, &second_column)))
2000 return errorCode;
2001
2002 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2003 return -1;
2004
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002005 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002006 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2007 argv + num_unsaved_files + 2,
2008 argc - num_unsaved_files - 3,
2009 unsaved_files,
2010 num_unsaved_files,
2011 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002012 if (!TU) {
2013 fprintf(stderr, "unable to parse input\n");
2014 clang_disposeIndex(CIdx);
2015 free(filename);
2016 free_remapped_files(unsaved_files, num_unsaved_files);
2017 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002018 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002019 errorCode = 0;
2020
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002021 if (checkForErrors(TU) != 0)
2022 return -1;
2023
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002024 if (getenv("CINDEXTEST_EDITING")) {
2025 for (i = 0; i < 5; ++i) {
2026 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2027 clang_defaultReparseOptions(TU))) {
2028 fprintf(stderr, "Unable to reparse translation unit!\n");
2029 errorCode = -1;
2030 goto teardown;
2031 }
2032 }
2033 }
2034
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002035 if (checkForErrors(TU) != 0) {
2036 errorCode = -1;
2037 goto teardown;
2038 }
2039
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002040 file = clang_getFile(TU, filename);
2041 if (!file) {
2042 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2043 errorCode = -1;
2044 goto teardown;
2045 }
2046
2047 startLoc = clang_getLocation(TU, file, line, column);
2048 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002049 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002050 column);
2051 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002052 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002053 }
2054
2055 endLoc = clang_getLocation(TU, file, second_line, second_column);
2056 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002057 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002058 second_line, second_column);
2059 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002060 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002061 }
2062
2063 range = clang_getRange(startLoc, endLoc);
2064 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002065
2066 if (checkForErrors(TU) != 0) {
2067 errorCode = -1;
2068 goto teardown;
2069 }
2070
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002071 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2072 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002073
2074 if (checkForErrors(TU) != 0) {
2075 errorCode = -1;
2076 goto teardown;
2077 }
2078
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002079 for (i = 0; i != num_tokens; ++i) {
2080 const char *kind = "<unknown>";
2081 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2082 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2083 unsigned start_line, start_column, end_line, end_column;
2084
2085 switch (clang_getTokenKind(tokens[i])) {
2086 case CXToken_Punctuation: kind = "Punctuation"; break;
2087 case CXToken_Keyword: kind = "Keyword"; break;
2088 case CXToken_Identifier: kind = "Identifier"; break;
2089 case CXToken_Literal: kind = "Literal"; break;
2090 case CXToken_Comment: kind = "Comment"; break;
2091 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002092 clang_getSpellingLocation(clang_getRangeStart(extent),
2093 0, &start_line, &start_column, 0);
2094 clang_getSpellingLocation(clang_getRangeEnd(extent),
2095 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002096 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2097 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002098 if (!clang_isInvalid(cursors[i].kind)) {
2099 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002100 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002101 }
2102 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002103 }
2104 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002105 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002106
2107 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002108 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002109 clang_disposeTranslationUnit(TU);
2110 clang_disposeIndex(CIdx);
2111 free(filename);
2112 free_remapped_files(unsaved_files, num_unsaved_files);
2113 return errorCode;
2114}
2115
Ted Kremenek0d435192009-11-17 18:13:31 +00002116/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002117/* USR printing. */
2118/******************************************************************************/
2119
2120static int insufficient_usr(const char *kind, const char *usage) {
2121 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2122 return 1;
2123}
2124
2125static unsigned isUSR(const char *s) {
2126 return s[0] == 'c' && s[1] == ':';
2127}
2128
2129static int not_usr(const char *s, const char *arg) {
2130 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2131 return 1;
2132}
2133
2134static void print_usr(CXString usr) {
2135 const char *s = clang_getCString(usr);
2136 printf("%s\n", s);
2137 clang_disposeString(usr);
2138}
2139
2140static void display_usrs() {
2141 fprintf(stderr, "-print-usrs options:\n"
2142 " ObjCCategory <class name> <category name>\n"
2143 " ObjCClass <class name>\n"
2144 " ObjCIvar <ivar name> <class USR>\n"
2145 " ObjCMethod <selector> [0=class method|1=instance method] "
2146 "<class USR>\n"
2147 " ObjCProperty <property name> <class USR>\n"
2148 " ObjCProtocol <protocol name>\n");
2149}
2150
2151int print_usrs(const char **I, const char **E) {
2152 while (I != E) {
2153 const char *kind = *I;
2154 unsigned len = strlen(kind);
2155 switch (len) {
2156 case 8:
2157 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2158 if (I + 2 >= E)
2159 return insufficient_usr(kind, "<ivar name> <class USR>");
2160 if (!isUSR(I[2]))
2161 return not_usr("<class USR>", I[2]);
2162 else {
2163 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002164 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002165 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002166 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2167 }
2168
2169 I += 3;
2170 continue;
2171 }
2172 break;
2173 case 9:
2174 if (memcmp(kind, "ObjCClass", 9) == 0) {
2175 if (I + 1 >= E)
2176 return insufficient_usr(kind, "<class name>");
2177 print_usr(clang_constructUSR_ObjCClass(I[1]));
2178 I += 2;
2179 continue;
2180 }
2181 break;
2182 case 10:
2183 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2184 if (I + 3 >= E)
2185 return insufficient_usr(kind, "<method selector> "
2186 "[0=class method|1=instance method] <class USR>");
2187 if (!isUSR(I[3]))
2188 return not_usr("<class USR>", I[3]);
2189 else {
2190 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002191 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002192 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002193 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2194 }
2195 I += 4;
2196 continue;
2197 }
2198 break;
2199 case 12:
2200 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2201 if (I + 2 >= E)
2202 return insufficient_usr(kind, "<class name> <category name>");
2203 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2204 I += 3;
2205 continue;
2206 }
2207 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2208 if (I + 1 >= E)
2209 return insufficient_usr(kind, "<protocol name>");
2210 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2211 I += 2;
2212 continue;
2213 }
2214 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2215 if (I + 2 >= E)
2216 return insufficient_usr(kind, "<property name> <class USR>");
2217 if (!isUSR(I[2]))
2218 return not_usr("<class USR>", I[2]);
2219 else {
2220 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002221 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002222 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002223 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2224 }
2225 I += 3;
2226 continue;
2227 }
2228 break;
2229 default:
2230 break;
2231 }
2232 break;
2233 }
2234
2235 if (I != E) {
2236 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2237 display_usrs();
2238 return 1;
2239 }
2240 return 0;
2241}
2242
2243int print_usrs_file(const char *file_name) {
2244 char line[2048];
2245 const char *args[128];
2246 unsigned numChars = 0;
2247
2248 FILE *fp = fopen(file_name, "r");
2249 if (!fp) {
2250 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2251 return 1;
2252 }
2253
2254 /* This code is not really all that safe, but it works fine for testing. */
2255 while (!feof(fp)) {
2256 char c = fgetc(fp);
2257 if (c == '\n') {
2258 unsigned i = 0;
2259 const char *s = 0;
2260
2261 if (numChars == 0)
2262 continue;
2263
2264 line[numChars] = '\0';
2265 numChars = 0;
2266
2267 if (line[0] == '/' && line[1] == '/')
2268 continue;
2269
2270 s = strtok(line, " ");
2271 while (s) {
2272 args[i] = s;
2273 ++i;
2274 s = strtok(0, " ");
2275 }
2276 if (print_usrs(&args[0], &args[i]))
2277 return 1;
2278 }
2279 else
2280 line[numChars++] = c;
2281 }
2282
2283 fclose(fp);
2284 return 0;
2285}
2286
2287/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002288/* Command line processing. */
2289/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002290int write_pch_file(const char *filename, int argc, const char *argv[]) {
2291 CXIndex Idx;
2292 CXTranslationUnit TU;
2293 struct CXUnsavedFile *unsaved_files = 0;
2294 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002295 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002296
2297 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2298
2299 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2300 clang_disposeIndex(Idx);
2301 return -1;
2302 }
2303
2304 TU = clang_parseTranslationUnit(Idx, 0,
2305 argv + num_unsaved_files,
2306 argc - num_unsaved_files,
2307 unsaved_files,
2308 num_unsaved_files,
2309 CXTranslationUnit_Incomplete);
2310 if (!TU) {
2311 fprintf(stderr, "Unable to load translation unit!\n");
2312 free_remapped_files(unsaved_files, num_unsaved_files);
2313 clang_disposeIndex(Idx);
2314 return 1;
2315 }
2316
Douglas Gregor39c411f2011-07-06 16:43:36 +00002317 switch (clang_saveTranslationUnit(TU, filename,
2318 clang_defaultSaveOptions(TU))) {
2319 case CXSaveError_None:
2320 break;
2321
2322 case CXSaveError_TranslationErrors:
2323 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2324 filename);
2325 result = 2;
2326 break;
2327
2328 case CXSaveError_InvalidTU:
2329 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2330 filename);
2331 result = 3;
2332 break;
2333
2334 case CXSaveError_Unknown:
2335 default:
2336 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2337 result = 1;
2338 break;
2339 }
2340
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002341 clang_disposeTranslationUnit(TU);
2342 free_remapped_files(unsaved_files, num_unsaved_files);
2343 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002344 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002345}
2346
2347/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002348/* Serialized diagnostics. */
2349/******************************************************************************/
2350
2351static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2352 switch (error) {
2353 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2354 case CXLoadDiag_None: break;
2355 case CXLoadDiag_Unknown: return "Unknown";
2356 case CXLoadDiag_InvalidFile: return "Invalid File";
2357 }
2358 return "None";
2359}
2360
2361static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2362 switch (severity) {
2363 case CXDiagnostic_Note: return "note";
2364 case CXDiagnostic_Error: return "error";
2365 case CXDiagnostic_Fatal: return "fatal";
2366 case CXDiagnostic_Ignored: return "ignored";
2367 case CXDiagnostic_Warning: return "warning";
2368 }
2369 return "unknown";
2370}
2371
2372static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002373 if (indent == 0)
2374 return;
2375 fprintf(stderr, "+");
2376 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002377 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002378 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002379 --indent;
2380 }
2381}
2382
2383static void printLocation(CXSourceLocation L) {
2384 CXFile File;
2385 CXString FileName;
2386 unsigned line, column, offset;
2387
2388 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2389 FileName = clang_getFileName(File);
2390
2391 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2392 clang_disposeString(FileName);
2393}
2394
2395static void printRanges(CXDiagnostic D, unsigned indent) {
2396 unsigned i, n = clang_getDiagnosticNumRanges(D);
2397
2398 for (i = 0; i < n; ++i) {
2399 CXSourceLocation Start, End;
2400 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2401 Start = clang_getRangeStart(SR);
2402 End = clang_getRangeEnd(SR);
2403
2404 printIndent(indent);
2405 fprintf(stderr, "Range: ");
2406 printLocation(Start);
2407 fprintf(stderr, " ");
2408 printLocation(End);
2409 fprintf(stderr, "\n");
2410 }
2411}
2412
2413static void printFixIts(CXDiagnostic D, unsigned indent) {
2414 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2415 for (i = 0 ; i < n; ++i) {
2416 CXSourceRange ReplacementRange;
2417 CXString text;
2418 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2419
2420 printIndent(indent);
2421 fprintf(stderr, "FIXIT: (");
2422 printLocation(clang_getRangeStart(ReplacementRange));
2423 fprintf(stderr, " - ");
2424 printLocation(clang_getRangeEnd(ReplacementRange));
2425 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2426 clang_disposeString(text);
2427 }
2428}
2429
2430static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002431 unsigned i, n;
2432
Ted Kremenek15322172011-11-10 08:43:12 +00002433 if (!Diags)
2434 return;
2435
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002436 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002437 for (i = 0; i < n; ++i) {
2438 CXSourceLocation DiagLoc;
2439 CXDiagnostic D;
2440 CXFile File;
2441 CXString FileName, DiagSpelling, DiagOption;
2442 unsigned line, column, offset;
2443 const char *DiagOptionStr = 0;
2444
2445 D = clang_getDiagnosticInSet(Diags, i);
2446 DiagLoc = clang_getDiagnosticLocation(D);
2447 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2448 FileName = clang_getFileName(File);
2449 DiagSpelling = clang_getDiagnosticSpelling(D);
2450
2451 printIndent(indent);
2452
2453 fprintf(stderr, "%s:%d:%d: %s: %s",
2454 clang_getCString(FileName),
2455 line,
2456 column,
2457 getSeverityString(clang_getDiagnosticSeverity(D)),
2458 clang_getCString(DiagSpelling));
2459
2460 DiagOption = clang_getDiagnosticOption(D, 0);
2461 DiagOptionStr = clang_getCString(DiagOption);
2462 if (DiagOptionStr) {
2463 fprintf(stderr, " [%s]", DiagOptionStr);
2464 }
2465
2466 fprintf(stderr, "\n");
2467
2468 printRanges(D, indent);
2469 printFixIts(D, indent);
2470
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002471 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002472 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2473
2474 clang_disposeString(FileName);
2475 clang_disposeString(DiagSpelling);
2476 clang_disposeString(DiagOption);
2477 }
2478}
2479
2480static int read_diagnostics(const char *filename) {
2481 enum CXLoadDiag_Error error;
2482 CXString errorString;
2483 CXDiagnosticSet Diags = 0;
2484
2485 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2486 if (!Diags) {
2487 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2488 getDiagnosticCodeStr(error),
2489 clang_getCString(errorString));
2490 clang_disposeString(errorString);
2491 return 1;
2492 }
2493
2494 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002495 fprintf(stderr, "Number of diagnostics: %d\n",
2496 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002497 clang_disposeDiagnosticSet(Diags);
2498 return 0;
2499}
2500
2501/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002502/* Command line processing. */
2503/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002504
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002505static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002506 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002507 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002508 if (strcmp(s, "-usrs") == 0)
2509 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002510 if (strncmp(s, "-memory-usage", 13) == 0)
2511 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002512 return NULL;
2513}
2514
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002515static void print_usage(void) {
2516 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002517 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002518 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002519 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002520 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002521 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002522 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002523 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002524 "[FileCheck prefix]\n");
2525 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002526 " c-index-test -test-load-tu <AST file> <symbol filter> "
2527 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002528 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2529 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002530 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002531 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002532 " c-index-test -test-load-source-memory-usage "
2533 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002534 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2535 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002536 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002537 " c-index-test -test-load-source-usrs-memory-usage "
2538 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002539 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2540 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002541 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002542 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002543 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002544 " c-index-test -test-print-typekind {<args>}*\n"
2545 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002546 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002547 " c-index-test -write-pch <file> <compiler arguments>\n");
2548 fprintf(stderr,
2549 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002550 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002551 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002552 " all - load all symbols, including those from PCH\n"
2553 " local - load all symbols except those in PCH\n"
2554 " category - only load ObjC categories (non-PCH)\n"
2555 " interface - only load ObjC interfaces (non-PCH)\n"
2556 " protocol - only load ObjC protocols (non-PCH)\n"
2557 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002558 " typedef - only load typdefs (non-PCH)\n"
2559 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002560}
2561
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002562/***/
2563
2564int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002565 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002566 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2567 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002568 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002569 return perform_code_completion(argc, argv, 0);
2570 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2571 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002572 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2573 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002574 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2575 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002576 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2577 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002578 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2579 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002580 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002581 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002582 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002583 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2584 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002585 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002586 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2587 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2588 if (I) {
2589 int trials = atoi(argv[2]);
2590 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2591 NULL);
2592 }
2593 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002594 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002595 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002596
2597 PostVisitTU postVisit = 0;
2598 if (strstr(argv[1], "-memory-usage"))
2599 postVisit = PrintMemoryUsage;
2600
Ted Kremenek7d405622010-01-12 23:34:26 +00002601 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002602 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2603 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002604 }
2605 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002606 return perform_file_scan(argv[2], argv[3],
2607 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002608 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2609 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002610 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2611 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2612 PrintInclusionStack);
2613 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2614 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2615 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002616 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2617 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2618 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002619 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2620 return perform_test_load_source(argc - 2, argv + 2, "all",
2621 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002622 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2623 if (argc > 2)
2624 return print_usrs(argv + 2, argv + argc);
2625 else {
2626 display_usrs();
2627 return 1;
2628 }
2629 }
2630 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2631 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002632 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2633 return write_pch_file(argv[2], argc - 3, argv + 3);
2634
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002635 print_usage();
2636 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002637}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002638
2639/***/
2640
2641/* We intentionally run in a separate thread to ensure we at least minimal
2642 * testing of a multithreaded environment (for example, having a reduced stack
2643 * size). */
2644
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002645typedef struct thread_info {
2646 int argc;
2647 const char **argv;
2648 int result;
2649} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002650void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002651 thread_info *client_data = client_data_v;
2652 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002653}
2654
2655int main(int argc, const char **argv) {
2656 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002657
Douglas Gregor61605982010-10-27 16:00:01 +00002658 if (getenv("CINDEXTEST_NOTHREADS"))
2659 return cindextest_main(argc, argv);
2660
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002661 client_data.argc = argc;
2662 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002663 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002664 return client_data.result;
2665}