blob: 750b519e0417d996bedd2bd0ccd4aeff47677ddb [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 Kyrtzidis4e7064f2011-10-17 19:48:19 +00001551} IndexData;
1552
1553static void printCheck(IndexData *data) {
1554 if (data->check_prefix) {
1555 if (data->first_check_printed) {
1556 printf("// %s-NEXT: ", data->check_prefix);
1557 } else {
1558 printf("// %s : ", data->check_prefix);
1559 data->first_check_printed = 1;
1560 }
1561 }
1562}
1563
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001564static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001565 CXString filename = clang_getFileName((CXFile)file);
1566 printf("%s", clang_getCString(filename));
1567 clang_disposeString(filename);
1568}
1569
1570static void printCXIndexLoc(CXIdxLoc loc) {
1571 CXString filename;
1572 const char *cname, *end;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001573 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001574 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001575 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001576
1577 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1578 if (line == 0) {
1579 printf("<null loc>");
1580 return;
1581 }
1582 filename = clang_getFileName((CXFile)file);
1583 cname = clang_getCString(filename);
1584 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001585 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001586
1587 if (isHeader) {
1588 printCXIndexFile(file);
1589 printf(":");
1590 }
1591 printf("%d:%d", line, column);
1592}
1593
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001594static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1595 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001596 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001597 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001598 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001599 unsigned line, column;
1600
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001601 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001602 if (!name)
1603 name = "<anon-tag>";
1604
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001605 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001606 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001607 newStr = (char *)malloc(strlen(name) + 10);
1608 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001609 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001610}
1611
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001612static void printCXIndexContainer(CXIdxClientContainer container) {
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001613 if (!container)
1614 printf("[<<NULL>>]");
1615 else
1616 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001617}
1618
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001619static const char *getEntityKindString(CXIdxEntityKind kind) {
1620 switch (kind) {
1621 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1622 case CXIdxEntity_Typedef: return "typedef";
1623 case CXIdxEntity_Function: return "function";
1624 case CXIdxEntity_Variable: return "variable";
1625 case CXIdxEntity_Field: return "field";
1626 case CXIdxEntity_EnumConstant: return "enumerator";
1627 case CXIdxEntity_ObjCClass: return "objc-class";
1628 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1629 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001630 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1631 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001632 case CXIdxEntity_ObjCProperty: return "objc-property";
1633 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1634 case CXIdxEntity_Enum: return "enum";
1635 case CXIdxEntity_Struct: return "struct";
1636 case CXIdxEntity_Union: return "union";
1637 case CXIdxEntity_CXXClass: return "c++-class";
1638 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001639 assert(0 && "Garbage entity kind");
1640 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001641}
1642
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001643static void printEntityInfo(const char *cb,
1644 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001645 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001646 const char *name;
1647 IndexData *index_data;
1648 index_data = (IndexData *)client_data;
1649 printCheck(index_data);
1650
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001651 if (!info) {
1652 printf("%s: <<NULL>>", cb);
1653 return;
1654 }
1655
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001656 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001657 if (!name)
1658 name = "<anon-tag>";
1659
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001660 printf("%s: kind: %s", cb, getEntityKindString(info->kind));
1661 printf(" | name: %s", name);
1662 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001663}
1664
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001665static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1666 CXClientData client_data) {
1667 unsigned i;
1668 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1669 printEntityInfo(" <protocol>", client_data,
1670 ProtoInfo->protocols[i]->protocol);
1671 printf(" | cursor: ");
1672 PrintCursor(ProtoInfo->protocols[i]->cursor);
1673 printf(" | loc: ");
1674 printCXIndexLoc(ProtoInfo->protocols[i]->loc);
1675 printf("\n");
1676 }
1677}
1678
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001679static void index_diagnostic(CXClientData client_data,
1680 CXDiagnostic diag, void *reserved) {
1681 CXString str;
1682 const char *cstr;
1683 IndexData *index_data;
1684 index_data = (IndexData *)client_data;
1685 printCheck(index_data);
1686
1687 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1688 cstr = clang_getCString(str);
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001689 printf("[diagnostic]: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001690 clang_disposeString(str);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001691
1692 if (getenv("CINDEXTEST_FAILONERROR") &&
1693 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1694 index_data->fail_for_error = 1;
1695 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001696}
1697
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001698static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1699 CXFile file, void *reserved) {
1700 IndexData *index_data;
1701 index_data = (IndexData *)client_data;
1702 printCheck(index_data);
1703
1704 printf("[enteredMainFile]: ");
1705 printCXIndexFile((CXIdxClientFile)file);
1706 printf("\n");
1707
1708 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001709}
1710
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001711static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001712 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001713 IndexData *index_data;
1714 index_data = (IndexData *)client_data;
1715 printCheck(index_data);
1716
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001717 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001718 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001719 printf(" | name: \"%s\"", info->filename);
1720 printf(" | hash loc: ");
1721 printCXIndexLoc(info->hashLoc);
1722 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001723
1724 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001725}
1726
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001727static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001728 void *reserved) {
1729 IndexData *index_data;
1730 index_data = (IndexData *)client_data;
1731 printCheck(index_data);
1732
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001733 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001734 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001735}
1736
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001737static void index_indexDeclaration(CXClientData client_data,
1738 const CXIdxDeclInfo *info,
1739 const CXIdxDeclOut *outData) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001740 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001741 const CXIdxObjCCategoryDeclInfo *CatInfo;
1742 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001743 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001744 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001745 index_data = (IndexData *)client_data;
1746
1747 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1748 printf(" | cursor: ");
1749 PrintCursor(info->cursor);
1750 printf(" | loc: ");
1751 printCXIndexLoc(info->loc);
1752 printf(" | container: ");
1753 printCXIndexContainer(info->container);
1754 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001755 printf(" | isDef: %d", info->isDefinition);
1756 printf(" | isContainer: %d", info->isContainer);
1757 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001758
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001759 for (i = 0; i != info->numAttributes; ++i) {
1760 printf(" <attribute>: ");
1761 const CXIdxAttrInfo *Attr = info->attributes[i];
1762 PrintCursor(Attr->cursor);
1763 printf("\n");
1764 }
1765
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001766 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1767 const char *kindName = 0;
1768 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1769 switch (K) {
1770 case CXIdxObjCContainer_ForwardRef:
1771 kindName = "forward-ref"; break;
1772 case CXIdxObjCContainer_Interface:
1773 kindName = "interface"; break;
1774 case CXIdxObjCContainer_Implementation:
1775 kindName = "implementation"; break;
1776 }
1777 printCheck(index_data);
1778 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1779 }
1780
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001781 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001782 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1783 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001784 printf(" | cursor: ");
1785 PrintCursor(CatInfo->classCursor);
1786 printf(" | loc: ");
1787 printCXIndexLoc(CatInfo->classLoc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001788 printf("\n");
1789 }
1790
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001791 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1792 if (InterInfo->superInfo) {
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001793 printEntityInfo(" <base>", client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001794 InterInfo->superInfo->base);
1795 printf(" | cursor: ");
1796 PrintCursor(InterInfo->superInfo->cursor);
1797 printf(" | loc: ");
1798 printCXIndexLoc(InterInfo->superInfo->loc);
1799 printf("\n");
1800 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001801 }
1802
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001803 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1804 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001805 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001806
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001807 if (outData->outContainer)
1808 *outData->outContainer = makeClientContainer(info->entityInfo, info->loc);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001809}
1810
1811static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001812 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001813 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001814 printf(" | cursor: ");
1815 PrintCursor(info->cursor);
1816 printf(" | loc: ");
1817 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001818 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001819 printf(" | container: ");
1820 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001821 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001822 switch (info->kind) {
1823 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001824 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001825 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001826 printf("\n");
1827}
1828
1829static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001830 0, /*abortQuery*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001831 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001832 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001833 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001834 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001835 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001836 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001837 index_indexEntityReference
1838};
1839
1840static int index_file(int argc, const char **argv) {
1841 const char *check_prefix;
1842 CXIndex CIdx;
1843 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001844 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001845 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001846
1847 check_prefix = 0;
1848 if (argc > 0) {
1849 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1850 check_prefix = argv[0] + strlen("-check-prefix=");
1851 ++argv;
1852 --argc;
1853 }
1854 }
1855
1856 if (argc == 0) {
1857 fprintf(stderr, "no compiler arguments\n");
1858 return -1;
1859 }
1860
1861 CIdx = clang_createIndex(0, 1);
1862 index_data.check_prefix = check_prefix;
1863 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001864 index_data.fail_for_error = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001865
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001866 index_opts = 0;
1867 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1868 index_opts |= CXIndexOpt_SuppressRedundantRefs;
1869
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001870 result = clang_indexSourceFile(CIdx, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001871 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001872 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001873 if (index_data.fail_for_error)
1874 return -1;
1875
1876 return result;
1877}
1878
1879static int index_tu(int argc, const char **argv) {
1880 CXIndex Idx;
1881 CXTranslationUnit TU;
1882 const char *check_prefix;
1883 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001884 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001885 int result;
1886
1887 check_prefix = 0;
1888 if (argc > 0) {
1889 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1890 check_prefix = argv[0] + strlen("-check-prefix=");
1891 ++argv;
1892 --argc;
1893 }
1894 }
1895
1896 if (argc == 0) {
1897 fprintf(stderr, "no ast file\n");
1898 return -1;
1899 }
1900
1901 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1902 /* displayDiagnosics=*/1))) {
1903 fprintf(stderr, "Could not create Index\n");
1904 return 1;
1905 }
1906
1907 if (!CreateTranslationUnit(Idx, argv[0], &TU))
1908 return 1;
1909
1910 index_data.check_prefix = check_prefix;
1911 index_data.first_check_printed = 0;
1912 index_data.fail_for_error = 0;
1913
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001914 index_opts = 0;
1915 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1916 index_opts |= CXIndexOpt_SuppressRedundantRefs;
1917
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001918 result = clang_indexTranslationUnit(TU, &index_data,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001919 &IndexCB,sizeof(IndexCB),
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001920 index_opts);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001921 if (index_data.fail_for_error)
1922 return -1;
1923
1924 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001925}
1926
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001927int perform_token_annotation(int argc, const char **argv) {
1928 const char *input = argv[1];
1929 char *filename = 0;
1930 unsigned line, second_line;
1931 unsigned column, second_column;
1932 CXIndex CIdx;
1933 CXTranslationUnit TU = 0;
1934 int errorCode;
1935 struct CXUnsavedFile *unsaved_files = 0;
1936 int num_unsaved_files = 0;
1937 CXToken *tokens;
1938 unsigned num_tokens;
1939 CXSourceRange range;
1940 CXSourceLocation startLoc, endLoc;
1941 CXFile file = 0;
1942 CXCursor *cursors = 0;
1943 unsigned i;
1944
1945 input += strlen("-test-annotate-tokens=");
1946 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1947 &second_line, &second_column)))
1948 return errorCode;
1949
1950 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1951 return -1;
1952
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001953 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00001954 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1955 argv + num_unsaved_files + 2,
1956 argc - num_unsaved_files - 3,
1957 unsaved_files,
1958 num_unsaved_files,
1959 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001960 if (!TU) {
1961 fprintf(stderr, "unable to parse input\n");
1962 clang_disposeIndex(CIdx);
1963 free(filename);
1964 free_remapped_files(unsaved_files, num_unsaved_files);
1965 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001966 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001967 errorCode = 0;
1968
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001969 if (checkForErrors(TU) != 0)
1970 return -1;
1971
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00001972 if (getenv("CINDEXTEST_EDITING")) {
1973 for (i = 0; i < 5; ++i) {
1974 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1975 clang_defaultReparseOptions(TU))) {
1976 fprintf(stderr, "Unable to reparse translation unit!\n");
1977 errorCode = -1;
1978 goto teardown;
1979 }
1980 }
1981 }
1982
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001983 if (checkForErrors(TU) != 0) {
1984 errorCode = -1;
1985 goto teardown;
1986 }
1987
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001988 file = clang_getFile(TU, filename);
1989 if (!file) {
1990 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1991 errorCode = -1;
1992 goto teardown;
1993 }
1994
1995 startLoc = clang_getLocation(TU, file, line, column);
1996 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001997 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001998 column);
1999 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002000 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002001 }
2002
2003 endLoc = clang_getLocation(TU, file, second_line, second_column);
2004 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002005 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002006 second_line, second_column);
2007 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002008 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002009 }
2010
2011 range = clang_getRange(startLoc, endLoc);
2012 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002013
2014 if (checkForErrors(TU) != 0) {
2015 errorCode = -1;
2016 goto teardown;
2017 }
2018
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002019 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2020 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002021
2022 if (checkForErrors(TU) != 0) {
2023 errorCode = -1;
2024 goto teardown;
2025 }
2026
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002027 for (i = 0; i != num_tokens; ++i) {
2028 const char *kind = "<unknown>";
2029 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2030 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2031 unsigned start_line, start_column, end_line, end_column;
2032
2033 switch (clang_getTokenKind(tokens[i])) {
2034 case CXToken_Punctuation: kind = "Punctuation"; break;
2035 case CXToken_Keyword: kind = "Keyword"; break;
2036 case CXToken_Identifier: kind = "Identifier"; break;
2037 case CXToken_Literal: kind = "Literal"; break;
2038 case CXToken_Comment: kind = "Comment"; break;
2039 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002040 clang_getSpellingLocation(clang_getRangeStart(extent),
2041 0, &start_line, &start_column, 0);
2042 clang_getSpellingLocation(clang_getRangeEnd(extent),
2043 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002044 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2045 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002046 if (!clang_isInvalid(cursors[i].kind)) {
2047 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002048 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002049 }
2050 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002051 }
2052 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002053 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002054
2055 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002056 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002057 clang_disposeTranslationUnit(TU);
2058 clang_disposeIndex(CIdx);
2059 free(filename);
2060 free_remapped_files(unsaved_files, num_unsaved_files);
2061 return errorCode;
2062}
2063
Ted Kremenek0d435192009-11-17 18:13:31 +00002064/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002065/* USR printing. */
2066/******************************************************************************/
2067
2068static int insufficient_usr(const char *kind, const char *usage) {
2069 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2070 return 1;
2071}
2072
2073static unsigned isUSR(const char *s) {
2074 return s[0] == 'c' && s[1] == ':';
2075}
2076
2077static int not_usr(const char *s, const char *arg) {
2078 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2079 return 1;
2080}
2081
2082static void print_usr(CXString usr) {
2083 const char *s = clang_getCString(usr);
2084 printf("%s\n", s);
2085 clang_disposeString(usr);
2086}
2087
2088static void display_usrs() {
2089 fprintf(stderr, "-print-usrs options:\n"
2090 " ObjCCategory <class name> <category name>\n"
2091 " ObjCClass <class name>\n"
2092 " ObjCIvar <ivar name> <class USR>\n"
2093 " ObjCMethod <selector> [0=class method|1=instance method] "
2094 "<class USR>\n"
2095 " ObjCProperty <property name> <class USR>\n"
2096 " ObjCProtocol <protocol name>\n");
2097}
2098
2099int print_usrs(const char **I, const char **E) {
2100 while (I != E) {
2101 const char *kind = *I;
2102 unsigned len = strlen(kind);
2103 switch (len) {
2104 case 8:
2105 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2106 if (I + 2 >= E)
2107 return insufficient_usr(kind, "<ivar name> <class USR>");
2108 if (!isUSR(I[2]))
2109 return not_usr("<class USR>", I[2]);
2110 else {
2111 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002112 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002113 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002114 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2115 }
2116
2117 I += 3;
2118 continue;
2119 }
2120 break;
2121 case 9:
2122 if (memcmp(kind, "ObjCClass", 9) == 0) {
2123 if (I + 1 >= E)
2124 return insufficient_usr(kind, "<class name>");
2125 print_usr(clang_constructUSR_ObjCClass(I[1]));
2126 I += 2;
2127 continue;
2128 }
2129 break;
2130 case 10:
2131 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2132 if (I + 3 >= E)
2133 return insufficient_usr(kind, "<method selector> "
2134 "[0=class method|1=instance method] <class USR>");
2135 if (!isUSR(I[3]))
2136 return not_usr("<class USR>", I[3]);
2137 else {
2138 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002139 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002140 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002141 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2142 }
2143 I += 4;
2144 continue;
2145 }
2146 break;
2147 case 12:
2148 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2149 if (I + 2 >= E)
2150 return insufficient_usr(kind, "<class name> <category name>");
2151 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2152 I += 3;
2153 continue;
2154 }
2155 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2156 if (I + 1 >= E)
2157 return insufficient_usr(kind, "<protocol name>");
2158 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2159 I += 2;
2160 continue;
2161 }
2162 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2163 if (I + 2 >= E)
2164 return insufficient_usr(kind, "<property name> <class USR>");
2165 if (!isUSR(I[2]))
2166 return not_usr("<class USR>", I[2]);
2167 else {
2168 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002169 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002170 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002171 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2172 }
2173 I += 3;
2174 continue;
2175 }
2176 break;
2177 default:
2178 break;
2179 }
2180 break;
2181 }
2182
2183 if (I != E) {
2184 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2185 display_usrs();
2186 return 1;
2187 }
2188 return 0;
2189}
2190
2191int print_usrs_file(const char *file_name) {
2192 char line[2048];
2193 const char *args[128];
2194 unsigned numChars = 0;
2195
2196 FILE *fp = fopen(file_name, "r");
2197 if (!fp) {
2198 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2199 return 1;
2200 }
2201
2202 /* This code is not really all that safe, but it works fine for testing. */
2203 while (!feof(fp)) {
2204 char c = fgetc(fp);
2205 if (c == '\n') {
2206 unsigned i = 0;
2207 const char *s = 0;
2208
2209 if (numChars == 0)
2210 continue;
2211
2212 line[numChars] = '\0';
2213 numChars = 0;
2214
2215 if (line[0] == '/' && line[1] == '/')
2216 continue;
2217
2218 s = strtok(line, " ");
2219 while (s) {
2220 args[i] = s;
2221 ++i;
2222 s = strtok(0, " ");
2223 }
2224 if (print_usrs(&args[0], &args[i]))
2225 return 1;
2226 }
2227 else
2228 line[numChars++] = c;
2229 }
2230
2231 fclose(fp);
2232 return 0;
2233}
2234
2235/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002236/* Command line processing. */
2237/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002238int write_pch_file(const char *filename, int argc, const char *argv[]) {
2239 CXIndex Idx;
2240 CXTranslationUnit TU;
2241 struct CXUnsavedFile *unsaved_files = 0;
2242 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002243 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002244
2245 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2246
2247 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2248 clang_disposeIndex(Idx);
2249 return -1;
2250 }
2251
2252 TU = clang_parseTranslationUnit(Idx, 0,
2253 argv + num_unsaved_files,
2254 argc - num_unsaved_files,
2255 unsaved_files,
2256 num_unsaved_files,
2257 CXTranslationUnit_Incomplete);
2258 if (!TU) {
2259 fprintf(stderr, "Unable to load translation unit!\n");
2260 free_remapped_files(unsaved_files, num_unsaved_files);
2261 clang_disposeIndex(Idx);
2262 return 1;
2263 }
2264
Douglas Gregor39c411f2011-07-06 16:43:36 +00002265 switch (clang_saveTranslationUnit(TU, filename,
2266 clang_defaultSaveOptions(TU))) {
2267 case CXSaveError_None:
2268 break;
2269
2270 case CXSaveError_TranslationErrors:
2271 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2272 filename);
2273 result = 2;
2274 break;
2275
2276 case CXSaveError_InvalidTU:
2277 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2278 filename);
2279 result = 3;
2280 break;
2281
2282 case CXSaveError_Unknown:
2283 default:
2284 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2285 result = 1;
2286 break;
2287 }
2288
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002289 clang_disposeTranslationUnit(TU);
2290 free_remapped_files(unsaved_files, num_unsaved_files);
2291 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002292 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002293}
2294
2295/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002296/* Serialized diagnostics. */
2297/******************************************************************************/
2298
2299static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2300 switch (error) {
2301 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2302 case CXLoadDiag_None: break;
2303 case CXLoadDiag_Unknown: return "Unknown";
2304 case CXLoadDiag_InvalidFile: return "Invalid File";
2305 }
2306 return "None";
2307}
2308
2309static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2310 switch (severity) {
2311 case CXDiagnostic_Note: return "note";
2312 case CXDiagnostic_Error: return "error";
2313 case CXDiagnostic_Fatal: return "fatal";
2314 case CXDiagnostic_Ignored: return "ignored";
2315 case CXDiagnostic_Warning: return "warning";
2316 }
2317 return "unknown";
2318}
2319
2320static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002321 if (indent == 0)
2322 return;
2323 fprintf(stderr, "+");
2324 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002325 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002326 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002327 --indent;
2328 }
2329}
2330
2331static void printLocation(CXSourceLocation L) {
2332 CXFile File;
2333 CXString FileName;
2334 unsigned line, column, offset;
2335
2336 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2337 FileName = clang_getFileName(File);
2338
2339 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2340 clang_disposeString(FileName);
2341}
2342
2343static void printRanges(CXDiagnostic D, unsigned indent) {
2344 unsigned i, n = clang_getDiagnosticNumRanges(D);
2345
2346 for (i = 0; i < n; ++i) {
2347 CXSourceLocation Start, End;
2348 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2349 Start = clang_getRangeStart(SR);
2350 End = clang_getRangeEnd(SR);
2351
2352 printIndent(indent);
2353 fprintf(stderr, "Range: ");
2354 printLocation(Start);
2355 fprintf(stderr, " ");
2356 printLocation(End);
2357 fprintf(stderr, "\n");
2358 }
2359}
2360
2361static void printFixIts(CXDiagnostic D, unsigned indent) {
2362 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2363 for (i = 0 ; i < n; ++i) {
2364 CXSourceRange ReplacementRange;
2365 CXString text;
2366 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2367
2368 printIndent(indent);
2369 fprintf(stderr, "FIXIT: (");
2370 printLocation(clang_getRangeStart(ReplacementRange));
2371 fprintf(stderr, " - ");
2372 printLocation(clang_getRangeEnd(ReplacementRange));
2373 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2374 clang_disposeString(text);
2375 }
2376}
2377
2378static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002379 unsigned i, n;
2380
Ted Kremenek15322172011-11-10 08:43:12 +00002381 if (!Diags)
2382 return;
2383
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002384 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002385 for (i = 0; i < n; ++i) {
2386 CXSourceLocation DiagLoc;
2387 CXDiagnostic D;
2388 CXFile File;
2389 CXString FileName, DiagSpelling, DiagOption;
2390 unsigned line, column, offset;
2391 const char *DiagOptionStr = 0;
2392
2393 D = clang_getDiagnosticInSet(Diags, i);
2394 DiagLoc = clang_getDiagnosticLocation(D);
2395 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2396 FileName = clang_getFileName(File);
2397 DiagSpelling = clang_getDiagnosticSpelling(D);
2398
2399 printIndent(indent);
2400
2401 fprintf(stderr, "%s:%d:%d: %s: %s",
2402 clang_getCString(FileName),
2403 line,
2404 column,
2405 getSeverityString(clang_getDiagnosticSeverity(D)),
2406 clang_getCString(DiagSpelling));
2407
2408 DiagOption = clang_getDiagnosticOption(D, 0);
2409 DiagOptionStr = clang_getCString(DiagOption);
2410 if (DiagOptionStr) {
2411 fprintf(stderr, " [%s]", DiagOptionStr);
2412 }
2413
2414 fprintf(stderr, "\n");
2415
2416 printRanges(D, indent);
2417 printFixIts(D, indent);
2418
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002419 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002420 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2421
2422 clang_disposeString(FileName);
2423 clang_disposeString(DiagSpelling);
2424 clang_disposeString(DiagOption);
2425 }
2426}
2427
2428static int read_diagnostics(const char *filename) {
2429 enum CXLoadDiag_Error error;
2430 CXString errorString;
2431 CXDiagnosticSet Diags = 0;
2432
2433 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2434 if (!Diags) {
2435 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2436 getDiagnosticCodeStr(error),
2437 clang_getCString(errorString));
2438 clang_disposeString(errorString);
2439 return 1;
2440 }
2441
2442 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002443 fprintf(stderr, "Number of diagnostics: %d\n",
2444 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002445 clang_disposeDiagnosticSet(Diags);
2446 return 0;
2447}
2448
2449/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002450/* Command line processing. */
2451/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002452
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002453static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002454 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002455 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002456 if (strcmp(s, "-usrs") == 0)
2457 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002458 if (strncmp(s, "-memory-usage", 13) == 0)
2459 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002460 return NULL;
2461}
2462
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002463static void print_usage(void) {
2464 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002465 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002466 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002467 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002468 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002469 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002470 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002471 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002472 "[FileCheck prefix]\n");
2473 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002474 " c-index-test -test-load-tu <AST file> <symbol filter> "
2475 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002476 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2477 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002478 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002479 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002480 " c-index-test -test-load-source-memory-usage "
2481 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002482 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2483 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002484 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002485 " c-index-test -test-load-source-usrs-memory-usage "
2486 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002487 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2488 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002489 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002490 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002491 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002492 " c-index-test -test-print-typekind {<args>}*\n"
2493 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002494 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002495 " c-index-test -write-pch <file> <compiler arguments>\n");
2496 fprintf(stderr,
2497 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002498 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002499 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002500 " all - load all symbols, including those from PCH\n"
2501 " local - load all symbols except those in PCH\n"
2502 " category - only load ObjC categories (non-PCH)\n"
2503 " interface - only load ObjC interfaces (non-PCH)\n"
2504 " protocol - only load ObjC protocols (non-PCH)\n"
2505 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002506 " typedef - only load typdefs (non-PCH)\n"
2507 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002508}
2509
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002510/***/
2511
2512int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002513 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002514 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2515 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002516 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002517 return perform_code_completion(argc, argv, 0);
2518 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2519 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002520 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2521 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002522 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2523 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002524 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2525 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002526 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2527 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002528 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002529 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002530 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002531 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2532 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002533 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002534 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2535 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2536 if (I) {
2537 int trials = atoi(argv[2]);
2538 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2539 NULL);
2540 }
2541 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002542 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002543 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002544
2545 PostVisitTU postVisit = 0;
2546 if (strstr(argv[1], "-memory-usage"))
2547 postVisit = PrintMemoryUsage;
2548
Ted Kremenek7d405622010-01-12 23:34:26 +00002549 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002550 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2551 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002552 }
2553 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002554 return perform_file_scan(argv[2], argv[3],
2555 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002556 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2557 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002558 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2559 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2560 PrintInclusionStack);
2561 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2562 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2563 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002564 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2565 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2566 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002567 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2568 return perform_test_load_source(argc - 2, argv + 2, "all",
2569 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002570 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2571 if (argc > 2)
2572 return print_usrs(argv + 2, argv + argc);
2573 else {
2574 display_usrs();
2575 return 1;
2576 }
2577 }
2578 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2579 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002580 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2581 return write_pch_file(argv[2], argc - 3, argv + 3);
2582
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002583 print_usage();
2584 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002585}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002586
2587/***/
2588
2589/* We intentionally run in a separate thread to ensure we at least minimal
2590 * testing of a multithreaded environment (for example, having a reduced stack
2591 * size). */
2592
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002593typedef struct thread_info {
2594 int argc;
2595 const char **argv;
2596 int result;
2597} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002598void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002599 thread_info *client_data = client_data_v;
2600 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002601}
2602
2603int main(int argc, const char **argv) {
2604 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002605
Douglas Gregor61605982010-10-27 16:00:01 +00002606 if (getenv("CINDEXTEST_NOTHREADS"))
2607 return cindextest_main(argc, argv);
2608
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002609 client_data.argc = argc;
2610 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002611 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002612 return client_data.result;
2613}