blob: 548c40e176fefbd2c8ac39c53f4d7b0d6c0eaa30 [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
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000421void PrintDiagnosticSet(CXDiagnosticSet Set) {
422 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
423 for ( ; i != n ; ++i) {
424 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
425 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000426 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000427 if (ChildDiags)
428 PrintDiagnosticSet(ChildDiags);
429 }
430}
431
432void PrintDiagnostics(CXTranslationUnit TU) {
433 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
434 PrintDiagnosticSet(TUSet);
435 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000436}
437
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000438void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000439 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000440 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000441 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000442 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000443 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000444 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000445 unsigned long amount = usage.entries[i].amount;
446 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000447 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000448 ((double) amount)/(1024*1024));
449 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000450 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000451 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000452 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000453}
454
Ted Kremenekce2ae882010-01-26 17:59:48 +0000455/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000456/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000457/******************************************************************************/
458
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000459static const char *FileCheckPrefix = "CHECK";
460
Douglas Gregora7bde202010-01-19 00:34:46 +0000461static void PrintCursorExtent(CXCursor C) {
462 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000463 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000464}
465
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000466/* Data used by all of the visitors. */
467typedef struct {
468 CXTranslationUnit TU;
469 enum CXCursorKind *Filter;
470} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000471
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000472
Ted Kremeneke68fff62010-02-17 00:41:32 +0000473enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000474 CXCursor Parent,
475 CXClientData ClientData) {
476 VisitorData *Data = (VisitorData *)ClientData;
477 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000478 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000479 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000480 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000481 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000482 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000483 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000484 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000485 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000486 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000487 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000488
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000489 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000490}
Steve Naroff50398192009-08-28 15:28:48 +0000491
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000493 CXCursor Parent,
494 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495 const char *startBuf, *endBuf;
496 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
497 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000498 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000499
Douglas Gregorb6998662010-01-19 19:34:47 +0000500 if (Cursor.kind != CXCursor_FunctionDecl ||
501 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000502 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000503
504 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
505 &startLine, &startColumn,
506 &endLine, &endColumn);
507 /* Probe the entire body, looking for both decls and refs. */
508 curLine = startLine;
509 curColumn = startColumn;
510
511 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000512 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000513 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000514 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000516 if (*startBuf == '\n') {
517 startBuf++;
518 curLine++;
519 curColumn = 1;
520 } else if (*startBuf != '\t')
521 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000522
Douglas Gregor98258af2010-01-18 22:46:11 +0000523 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000524 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000525
Douglas Gregor1db19de2010-01-19 21:36:55 +0000526 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000527 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000528 CXSourceLocation RefLoc
529 = clang_getLocation(Data->TU, file, curLine, curColumn);
530 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000531 if (Ref.kind == CXCursor_NoDeclFound) {
532 /* Nothing found here; that's fine. */
533 } else if (Ref.kind != CXCursor_FunctionDecl) {
534 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
535 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000536 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000537 printf("\n");
538 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000539 }
Ted Kremenek74844072010-02-17 00:41:20 +0000540 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000541 startBuf++;
542 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000543
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000544 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000545}
546
Ted Kremenek7d405622010-01-12 23:34:26 +0000547/******************************************************************************/
548/* USR testing. */
549/******************************************************************************/
550
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000551enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
552 CXClientData ClientData) {
553 VisitorData *Data = (VisitorData *)ClientData;
554 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000555 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000556 const char *cstr = clang_getCString(USR);
557 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000558 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000559 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000560 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000561 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
562
Douglas Gregora7bde202010-01-19 00:34:46 +0000563 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000564 printf("\n");
565 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000566
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000567 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000568 }
569
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000570 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000571}
572
573/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000574/* Inclusion stack testing. */
575/******************************************************************************/
576
577void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
578 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579
Ted Kremenek16b55a72010-01-26 19:31:51 +0000580 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000581 CXString fname;
582
583 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000584 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000585 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586
Ted Kremenek16b55a72010-01-26 19:31:51 +0000587 for (i = 0; i < includeStackLen; ++i) {
588 CXFile includingFile;
589 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000590 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
591 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000592 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000593 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000594 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000595 }
596 printf("\n");
597}
598
599void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000600 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000601}
602
603/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000604/* Linkage testing. */
605/******************************************************************************/
606
607static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
608 CXClientData d) {
609 const char *linkage = 0;
610
611 if (clang_isInvalid(clang_getCursorKind(cursor)))
612 return CXChildVisit_Recurse;
613
614 switch (clang_getCursorLinkage(cursor)) {
615 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000616 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
617 case CXLinkage_Internal: linkage = "Internal"; break;
618 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
619 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000620 }
621
622 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000623 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000624 printf("linkage=%s\n", linkage);
625 }
626
627 return CXChildVisit_Recurse;
628}
629
630/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000631/* Typekind testing. */
632/******************************************************************************/
633
634static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
635 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000636 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
637 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000638 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000639 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000640 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000641 if (clang_isConstQualifiedType(T))
642 printf(" const");
643 if (clang_isVolatileQualifiedType(T))
644 printf(" volatile");
645 if (clang_isRestrictQualifiedType(T))
646 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000647 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000648 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000649 {
650 CXType CT = clang_getCanonicalType(T);
651 if (!clang_equalTypes(T, CT)) {
652 CXString CS = clang_getTypeKindSpelling(CT.kind);
653 printf(" [canonical=%s]", clang_getCString(CS));
654 clang_disposeString(CS);
655 }
656 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000657 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000658 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000659 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660 if (RT.kind != CXType_Invalid) {
661 CXString RS = clang_getTypeKindSpelling(RT.kind);
662 printf(" [result=%s]", clang_getCString(RS));
663 clang_disposeString(RS);
664 }
665 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000666 /* Print if this is a non-POD type. */
667 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000668
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000669 printf("\n");
670 }
671 return CXChildVisit_Recurse;
672}
673
674
675/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000676/* Loading ASTs/source. */
677/******************************************************************************/
678
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000679static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000680 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000681 CXCursorVisitor Visitor,
682 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000684 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000685 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000686
687 if (Visitor) {
688 enum CXCursorKind K = CXCursor_NotImplemented;
689 enum CXCursorKind *ck = &K;
690 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000691
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000692 /* Perform some simple filtering. */
693 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000694 else if (!strcmp(filter, "all-display") ||
695 !strcmp(filter, "local-display")) {
696 ck = NULL;
697 want_display_name = 1;
698 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000699 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000700 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
701 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
702 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
703 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
704 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
705 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
706 else {
707 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
708 return 1;
709 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000710
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000711 Data.TU = TU;
712 Data.Filter = ck;
713 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000714 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000715
Ted Kremenekce2ae882010-01-26 17:59:48 +0000716 if (PV)
717 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000718
Douglas Gregora88084b2010-02-18 18:08:43 +0000719 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +0000720 if (checkForErrors(TU) != 0) {
721 clang_disposeTranslationUnit(TU);
722 return -1;
723 }
724
Ted Kremenek0d435192009-11-17 18:13:31 +0000725 clang_disposeTranslationUnit(TU);
726 return 0;
727}
728
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000729int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000730 const char *prefix, CXCursorVisitor Visitor,
731 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000732 CXIndex Idx;
733 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000734 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000735 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000736 !strcmp(filter, "local") ? 1 : 0,
737 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000738
Ted Kremenek020a0952010-02-11 07:41:25 +0000739 if (!CreateTranslationUnit(Idx, file, &TU)) {
740 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000741 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000742 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000743
Ted Kremenek020a0952010-02-11 07:41:25 +0000744 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
745 clang_disposeIndex(Idx);
746 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000747}
748
Ted Kremenekce2ae882010-01-26 17:59:48 +0000749int perform_test_load_source(int argc, const char **argv,
750 const char *filter, CXCursorVisitor Visitor,
751 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000752 CXIndex Idx;
753 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000754 struct CXUnsavedFile *unsaved_files = 0;
755 int num_unsaved_files = 0;
756 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000757
Daniel Dunbarada487d2009-12-01 02:03:10 +0000758 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000759 (!strcmp(filter, "local") ||
760 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000761 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000762
Ted Kremenek020a0952010-02-11 07:41:25 +0000763 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
764 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000765 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000766 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000767
Douglas Gregordca8ee82011-05-06 16:33:08 +0000768 TU = clang_parseTranslationUnit(Idx, 0,
769 argv + num_unsaved_files,
770 argc - num_unsaved_files,
771 unsaved_files, num_unsaved_files,
772 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000773 if (!TU) {
774 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000775 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000776 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000777 return 1;
778 }
779
Ted Kremenekce2ae882010-01-26 17:59:48 +0000780 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000781 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000782 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000783 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000784}
785
Douglas Gregorabc563f2010-07-19 21:46:24 +0000786int perform_test_reparse_source(int argc, const char **argv, int trials,
787 const char *filter, CXCursorVisitor Visitor,
788 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000789 CXIndex Idx;
790 CXTranslationUnit TU;
791 struct CXUnsavedFile *unsaved_files = 0;
792 int num_unsaved_files = 0;
793 int result;
794 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000795 int remap_after_trial = 0;
796 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000797
798 Idx = clang_createIndex(/* excludeDeclsFromPCH */
799 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000800 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000801
Douglas Gregorabc563f2010-07-19 21:46:24 +0000802 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
803 clang_disposeIndex(Idx);
804 return -1;
805 }
806
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000807 /* Load the initial translation unit -- we do this without honoring remapped
808 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000809 TU = clang_parseTranslationUnit(Idx, 0,
810 argv + num_unsaved_files,
811 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000812 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000813 if (!TU) {
814 fprintf(stderr, "Unable to load translation unit!\n");
815 free_remapped_files(unsaved_files, num_unsaved_files);
816 clang_disposeIndex(Idx);
817 return 1;
818 }
819
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000820 if (checkForErrors(TU) != 0)
821 return -1;
822
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000823 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
824 remap_after_trial =
825 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
826 }
827
Douglas Gregorabc563f2010-07-19 21:46:24 +0000828 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000829 if (clang_reparseTranslationUnit(TU,
830 trial >= remap_after_trial ? num_unsaved_files : 0,
831 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000832 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000833 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000834 clang_disposeTranslationUnit(TU);
835 free_remapped_files(unsaved_files, num_unsaved_files);
836 clang_disposeIndex(Idx);
837 return -1;
838 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000839
840 if (checkForErrors(TU) != 0)
841 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000842 }
843
844 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +0000845
Douglas Gregorabc563f2010-07-19 21:46:24 +0000846 free_remapped_files(unsaved_files, num_unsaved_files);
847 clang_disposeIndex(Idx);
848 return result;
849}
850
Ted Kremenek0d435192009-11-17 18:13:31 +0000851/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000852/* Logic for testing clang_getCursor(). */
853/******************************************************************************/
854
Douglas Gregordd3e5542011-05-04 00:14:37 +0000855static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000856 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000857 unsigned end_line, unsigned end_col,
858 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000859 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000860 if (prefix)
861 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000862 PrintExtent(stdout, start_line, start_col, end_line, end_col);
863 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000864 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000865 printf("\n");
866}
867
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000868static int perform_file_scan(const char *ast_file, const char *source_file,
869 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000870 CXIndex Idx;
871 CXTranslationUnit TU;
872 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000873 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000874 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000875 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000876 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000877
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000878 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
879 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000880 fprintf(stderr, "Could not create Index\n");
881 return 1;
882 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883
Ted Kremenek1c6da172009-11-17 19:37:36 +0000884 if (!CreateTranslationUnit(Idx, ast_file, &TU))
885 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000886
Ted Kremenek1c6da172009-11-17 19:37:36 +0000887 if ((fp = fopen(source_file, "r")) == NULL) {
888 fprintf(stderr, "Could not open '%s'\n", source_file);
889 return 1;
890 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000891
Douglas Gregorb9790342010-01-22 21:44:22 +0000892 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000893 for (;;) {
894 CXCursor cursor;
895 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000896
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000897 if (c == '\n') {
898 ++line;
899 col = 1;
900 } else
901 ++col;
902
903 /* Check the cursor at this position, and dump the previous one if we have
904 * found something new.
905 */
906 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
907 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
908 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000909 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000910 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000911 start_line = line;
912 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000913 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000914 if (c == EOF)
915 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000916
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000917 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000918 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000919
Ted Kremenek1c6da172009-11-17 19:37:36 +0000920 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000921 clang_disposeTranslationUnit(TU);
922 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000923 return 0;
924}
925
926/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000927/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000928/******************************************************************************/
929
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000930/* Parse file:line:column from the input string. Returns 0 on success, non-zero
931 on failure. If successful, the pointer *filename will contain newly-allocated
932 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000933int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000934 unsigned *column, unsigned *second_line,
935 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000936 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000937 const char *last_colon = strrchr(input, ':');
938 unsigned values[4], i;
939 unsigned num_values = (second_line && second_column)? 4 : 2;
940
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000941 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000942 if (!last_colon || last_colon == input) {
943 if (num_values == 4)
944 fprintf(stderr, "could not parse filename:line:column:line:column in "
945 "'%s'\n", input);
946 else
947 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000948 return 1;
949 }
950
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 for (i = 0; i != num_values; ++i) {
952 const char *prev_colon;
953
954 /* Parse the next line or column. */
955 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
956 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000957 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000958 (i % 2 ? "column" : "line"), input);
959 return 1;
960 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000961
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000962 if (i + 1 == num_values)
963 break;
964
965 /* Find the previous colon. */
966 prev_colon = last_colon - 1;
967 while (prev_colon != input && *prev_colon != ':')
968 --prev_colon;
969 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000970 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000971 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000973 }
974
975 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000976 }
977
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000978 *line = values[0];
979 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000980
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000981 if (second_line && second_column) {
982 *second_line = values[2];
983 *second_column = values[3];
984 }
985
Douglas Gregor88d23952009-11-09 18:19:57 +0000986 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000987 *filename = (char*)malloc(last_colon - input + 1);
988 memcpy(*filename, input, last_colon - input);
989 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000990 return 0;
991}
992
993const char *
994clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
995 switch (Kind) {
996 case CXCompletionChunk_Optional: return "Optional";
997 case CXCompletionChunk_TypedText: return "TypedText";
998 case CXCompletionChunk_Text: return "Text";
999 case CXCompletionChunk_Placeholder: return "Placeholder";
1000 case CXCompletionChunk_Informative: return "Informative";
1001 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1002 case CXCompletionChunk_LeftParen: return "LeftParen";
1003 case CXCompletionChunk_RightParen: return "RightParen";
1004 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1005 case CXCompletionChunk_RightBracket: return "RightBracket";
1006 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1007 case CXCompletionChunk_RightBrace: return "RightBrace";
1008 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1009 case CXCompletionChunk_RightAngle: return "RightAngle";
1010 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001011 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001012 case CXCompletionChunk_Colon: return "Colon";
1013 case CXCompletionChunk_SemiColon: return "SemiColon";
1014 case CXCompletionChunk_Equal: return "Equal";
1015 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1016 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001017 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001018
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001019 return "Unknown";
1020}
1021
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001022static int checkForErrors(CXTranslationUnit TU) {
1023 unsigned Num, i;
1024 CXDiagnostic Diag;
1025 CXString DiagStr;
1026
1027 if (!getenv("CINDEXTEST_FAILONERROR"))
1028 return 0;
1029
1030 Num = clang_getNumDiagnostics(TU);
1031 for (i = 0; i != Num; ++i) {
1032 Diag = clang_getDiagnostic(TU, i);
1033 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1034 DiagStr = clang_formatDiagnostic(Diag,
1035 clang_defaultDiagnosticDisplayOptions());
1036 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1037 clang_disposeString(DiagStr);
1038 clang_disposeDiagnostic(Diag);
1039 return -1;
1040 }
1041 clang_disposeDiagnostic(Diag);
1042 }
1043
1044 return 0;
1045}
1046
Douglas Gregor3ac73852009-11-09 16:04:45 +00001047void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001048 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001049
Douglas Gregor3ac73852009-11-09 16:04:45 +00001050 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001051 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001052 CXString text;
1053 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001054 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001055 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001056
Douglas Gregor3ac73852009-11-09 16:04:45 +00001057 if (Kind == CXCompletionChunk_Optional) {
1058 fprintf(file, "{Optional ");
1059 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001060 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001061 file);
1062 fprintf(file, "}");
1063 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001064 }
1065
1066 if (Kind == CXCompletionChunk_VerticalSpace) {
1067 fprintf(file, "{VerticalSpace }");
1068 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001069 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001070
Douglas Gregord5a20892009-11-09 17:05:28 +00001071 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001072 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001073 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001074 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001075 cstr ? cstr : "");
1076 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001077 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001078
Douglas Gregor3ac73852009-11-09 16:04:45 +00001079}
1080
1081void print_completion_result(CXCompletionResult *completion_result,
1082 CXClientData client_data) {
1083 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001084 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001085 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001086
1087 fprintf(file, "%s:", clang_getCString(ks));
1088 clang_disposeString(ks);
1089
Douglas Gregor3ac73852009-11-09 16:04:45 +00001090 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001091 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001092 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001093 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1094 case CXAvailability_Available:
1095 break;
1096
1097 case CXAvailability_Deprecated:
1098 fprintf(file, " (deprecated)");
1099 break;
1100
1101 case CXAvailability_NotAvailable:
1102 fprintf(file, " (unavailable)");
1103 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001104
1105 case CXAvailability_NotAccessible:
1106 fprintf(file, " (inaccessible)");
1107 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001108 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001109
1110 annotationCount = clang_getCompletionNumAnnotations(
1111 completion_result->CompletionString);
1112 if (annotationCount) {
1113 unsigned i;
1114 fprintf(file, " (");
1115 for (i = 0; i < annotationCount; ++i) {
1116 if (i != 0)
1117 fprintf(file, ", ");
1118 fprintf(file, "\"%s\"",
1119 clang_getCString(clang_getCompletionAnnotation(
1120 completion_result->CompletionString, i)));
1121 }
1122 fprintf(file, ")");
1123 }
1124
Douglas Gregor58ddb602010-08-23 23:00:57 +00001125 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001126}
1127
Douglas Gregor3da626b2011-07-07 16:03:39 +00001128void print_completion_contexts(unsigned long long contexts, FILE *file) {
1129 fprintf(file, "Completion contexts:\n");
1130 if (contexts == CXCompletionContext_Unknown) {
1131 fprintf(file, "Unknown\n");
1132 }
1133 if (contexts & CXCompletionContext_AnyType) {
1134 fprintf(file, "Any type\n");
1135 }
1136 if (contexts & CXCompletionContext_AnyValue) {
1137 fprintf(file, "Any value\n");
1138 }
1139 if (contexts & CXCompletionContext_ObjCObjectValue) {
1140 fprintf(file, "Objective-C object value\n");
1141 }
1142 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1143 fprintf(file, "Objective-C selector value\n");
1144 }
1145 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1146 fprintf(file, "C++ class type value\n");
1147 }
1148 if (contexts & CXCompletionContext_DotMemberAccess) {
1149 fprintf(file, "Dot member access\n");
1150 }
1151 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1152 fprintf(file, "Arrow member access\n");
1153 }
1154 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1155 fprintf(file, "Objective-C property access\n");
1156 }
1157 if (contexts & CXCompletionContext_EnumTag) {
1158 fprintf(file, "Enum tag\n");
1159 }
1160 if (contexts & CXCompletionContext_UnionTag) {
1161 fprintf(file, "Union tag\n");
1162 }
1163 if (contexts & CXCompletionContext_StructTag) {
1164 fprintf(file, "Struct tag\n");
1165 }
1166 if (contexts & CXCompletionContext_ClassTag) {
1167 fprintf(file, "Class name\n");
1168 }
1169 if (contexts & CXCompletionContext_Namespace) {
1170 fprintf(file, "Namespace or namespace alias\n");
1171 }
1172 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1173 fprintf(file, "Nested name specifier\n");
1174 }
1175 if (contexts & CXCompletionContext_ObjCInterface) {
1176 fprintf(file, "Objective-C interface\n");
1177 }
1178 if (contexts & CXCompletionContext_ObjCProtocol) {
1179 fprintf(file, "Objective-C protocol\n");
1180 }
1181 if (contexts & CXCompletionContext_ObjCCategory) {
1182 fprintf(file, "Objective-C category\n");
1183 }
1184 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1185 fprintf(file, "Objective-C instance method\n");
1186 }
1187 if (contexts & CXCompletionContext_ObjCClassMessage) {
1188 fprintf(file, "Objective-C class method\n");
1189 }
1190 if (contexts & CXCompletionContext_ObjCSelectorName) {
1191 fprintf(file, "Objective-C selector name\n");
1192 }
1193 if (contexts & CXCompletionContext_MacroName) {
1194 fprintf(file, "Macro name\n");
1195 }
1196 if (contexts & CXCompletionContext_NaturalLanguage) {
1197 fprintf(file, "Natural language\n");
1198 }
1199}
1200
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001201int my_stricmp(const char *s1, const char *s2) {
1202 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001203 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001204 if (c1 < c2)
1205 return -1;
1206 else if (c1 > c2)
1207 return 1;
1208
1209 ++s1;
1210 ++s2;
1211 }
1212
1213 if (*s1)
1214 return 1;
1215 else if (*s2)
1216 return -1;
1217 return 0;
1218}
1219
Douglas Gregor1982c182010-07-12 18:38:41 +00001220int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001221 const char *input = argv[1];
1222 char *filename = 0;
1223 unsigned line;
1224 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001225 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001226 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001227 struct CXUnsavedFile *unsaved_files = 0;
1228 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001229 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001230 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001231 unsigned I, Repeats = 1;
1232 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1233
1234 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1235 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001236
Douglas Gregor1982c182010-07-12 18:38:41 +00001237 if (timing_only)
1238 input += strlen("-code-completion-timing=");
1239 else
1240 input += strlen("-code-completion-at=");
1241
Ted Kremeneke68fff62010-02-17 00:41:32 +00001242 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001243 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001244 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001245
Douglas Gregor735df882009-12-02 09:21:34 +00001246 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1247 return -1;
1248
Douglas Gregor32be4a52010-10-11 21:37:58 +00001249 CIdx = clang_createIndex(0, 0);
1250
1251 if (getenv("CINDEXTEST_EDITING"))
1252 Repeats = 5;
1253
1254 TU = clang_parseTranslationUnit(CIdx, 0,
1255 argv + num_unsaved_files + 2,
1256 argc - num_unsaved_files - 2,
1257 0, 0, getDefaultParsingOptions());
1258 if (!TU) {
1259 fprintf(stderr, "Unable to load translation unit!\n");
1260 return 1;
1261 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001262
1263 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1264 fprintf(stderr, "Unable to reparse translation init!\n");
1265 return 1;
1266 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001267
1268 for (I = 0; I != Repeats; ++I) {
1269 results = clang_codeCompleteAt(TU, filename, line, column,
1270 unsaved_files, num_unsaved_files,
1271 completionOptions);
1272 if (!results) {
1273 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001274 return 1;
1275 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001276 if (I != Repeats-1)
1277 clang_disposeCodeCompleteResults(results);
1278 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001279
Douglas Gregorec6762c2009-12-18 16:20:58 +00001280 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001281 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001282 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001283 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001284 CXString objCSelector;
1285 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001286 if (!timing_only) {
1287 /* Sort the code-completion results based on the typed text. */
1288 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1289
Douglas Gregor1982c182010-07-12 18:38:41 +00001290 for (i = 0; i != n; ++i)
1291 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001292 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001293 n = clang_codeCompleteGetNumDiagnostics(results);
1294 for (i = 0; i != n; ++i) {
1295 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1296 PrintDiagnostic(diag);
1297 clang_disposeDiagnostic(diag);
1298 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001299
1300 contexts = clang_codeCompleteGetContexts(results);
1301 print_completion_contexts(contexts, stdout);
1302
Douglas Gregor0a47d692011-07-26 15:24:30 +00001303 containerKind = clang_codeCompleteGetContainerKind(results,
1304 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001305
1306 if (containerKind != CXCursor_InvalidCode) {
1307 /* We have found a container */
1308 CXString containerUSR, containerKindSpelling;
1309 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1310 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1311 clang_disposeString(containerKindSpelling);
1312
1313 if (containerIsIncomplete) {
1314 printf("Container is incomplete\n");
1315 }
1316 else {
1317 printf("Container is complete\n");
1318 }
1319
1320 containerUSR = clang_codeCompleteGetContainerUSR(results);
1321 printf("Container USR: %s\n", clang_getCString(containerUSR));
1322 clang_disposeString(containerUSR);
1323 }
1324
Douglas Gregor0a47d692011-07-26 15:24:30 +00001325 objCSelector = clang_codeCompleteGetObjCSelector(results);
1326 selectorString = clang_getCString(objCSelector);
1327 if (selectorString && strlen(selectorString) > 0) {
1328 printf("Objective-C selector: %s\n", selectorString);
1329 }
1330 clang_disposeString(objCSelector);
1331
Douglas Gregorec6762c2009-12-18 16:20:58 +00001332 clang_disposeCodeCompleteResults(results);
1333 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001334 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001335 clang_disposeIndex(CIdx);
1336 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001337
Douglas Gregor735df882009-12-02 09:21:34 +00001338 free_remapped_files(unsaved_files, num_unsaved_files);
1339
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001340 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001341}
1342
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001343typedef struct {
1344 char *filename;
1345 unsigned line;
1346 unsigned column;
1347} CursorSourceLocation;
1348
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001349static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001350 CXIndex CIdx;
1351 int errorCode;
1352 struct CXUnsavedFile *unsaved_files = 0;
1353 int num_unsaved_files = 0;
1354 CXTranslationUnit TU;
1355 CXCursor Cursor;
1356 CursorSourceLocation *Locations = 0;
1357 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001358 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001359 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001360
Ted Kremeneke68fff62010-02-17 00:41:32 +00001361 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001362 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1363 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001364
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001365 /* Parse the locations. */
1366 assert(NumLocations > 0 && "Unable to count locations?");
1367 Locations = (CursorSourceLocation *)malloc(
1368 NumLocations * sizeof(CursorSourceLocation));
1369 for (Loc = 0; Loc < NumLocations; ++Loc) {
1370 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001371 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1372 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001373 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001374 return errorCode;
1375 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001376
1377 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001378 &num_unsaved_files))
1379 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001380
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001381 if (getenv("CINDEXTEST_EDITING"))
1382 Repeats = 5;
1383
1384 /* Parse the translation unit. When we're testing clang_getCursor() after
1385 reparsing, don't remap unsaved files until the second parse. */
1386 CIdx = clang_createIndex(1, 1);
1387 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1388 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001389 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001390 unsaved_files,
1391 Repeats > 1? 0 : num_unsaved_files,
1392 getDefaultParsingOptions());
1393
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001394 if (!TU) {
1395 fprintf(stderr, "unable to parse input\n");
1396 return -1;
1397 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001398
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001399 if (checkForErrors(TU) != 0)
1400 return -1;
1401
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001402 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001403 if (Repeats > 1 &&
1404 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1405 clang_defaultReparseOptions(TU))) {
1406 clang_disposeTranslationUnit(TU);
1407 return 1;
1408 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001409
1410 if (checkForErrors(TU) != 0)
1411 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001412
1413 for (Loc = 0; Loc < NumLocations; ++Loc) {
1414 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1415 if (!file)
1416 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001417
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001418 Cursor = clang_getCursor(TU,
1419 clang_getLocation(TU, file, Locations[Loc].line,
1420 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001421
1422 if (checkForErrors(TU) != 0)
1423 return -1;
1424
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001425 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001426 CXCompletionString completionString = clang_getCursorCompletionString(
1427 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001428 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001429 if (completionString != NULL) {
1430 printf("\nCompletion string: ");
1431 print_completion_string(completionString, stdout);
1432 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001433 printf("\n");
1434 free(Locations[Loc].filename);
1435 }
1436 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001437 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001438
Douglas Gregora88084b2010-02-18 18:08:43 +00001439 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001440 clang_disposeTranslationUnit(TU);
1441 clang_disposeIndex(CIdx);
1442 free(Locations);
1443 free_remapped_files(unsaved_files, num_unsaved_files);
1444 return 0;
1445}
1446
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001447static enum CXVisitorResult findFileRefsVisit(void *context,
1448 CXCursor cursor, CXSourceRange range) {
1449 if (clang_Range_isNull(range))
1450 return CXVisit_Continue;
1451
1452 PrintCursor(cursor);
1453 PrintRange(range, "");
1454 printf("\n");
1455 return CXVisit_Continue;
1456}
1457
1458static int find_file_refs_at(int argc, const char **argv) {
1459 CXIndex CIdx;
1460 int errorCode;
1461 struct CXUnsavedFile *unsaved_files = 0;
1462 int num_unsaved_files = 0;
1463 CXTranslationUnit TU;
1464 CXCursor Cursor;
1465 CursorSourceLocation *Locations = 0;
1466 unsigned NumLocations = 0, Loc;
1467 unsigned Repeats = 1;
1468 unsigned I;
1469
1470 /* Count the number of locations. */
1471 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1472 ++NumLocations;
1473
1474 /* Parse the locations. */
1475 assert(NumLocations > 0 && "Unable to count locations?");
1476 Locations = (CursorSourceLocation *)malloc(
1477 NumLocations * sizeof(CursorSourceLocation));
1478 for (Loc = 0; Loc < NumLocations; ++Loc) {
1479 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1480 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1481 &Locations[Loc].line,
1482 &Locations[Loc].column, 0, 0)))
1483 return errorCode;
1484 }
1485
1486 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1487 &num_unsaved_files))
1488 return -1;
1489
1490 if (getenv("CINDEXTEST_EDITING"))
1491 Repeats = 5;
1492
1493 /* Parse the translation unit. When we're testing clang_getCursor() after
1494 reparsing, don't remap unsaved files until the second parse. */
1495 CIdx = clang_createIndex(1, 1);
1496 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1497 argv + num_unsaved_files + 1 + NumLocations,
1498 argc - num_unsaved_files - 2 - NumLocations,
1499 unsaved_files,
1500 Repeats > 1? 0 : num_unsaved_files,
1501 getDefaultParsingOptions());
1502
1503 if (!TU) {
1504 fprintf(stderr, "unable to parse input\n");
1505 return -1;
1506 }
1507
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001508 if (checkForErrors(TU) != 0)
1509 return -1;
1510
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001511 for (I = 0; I != Repeats; ++I) {
1512 if (Repeats > 1 &&
1513 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1514 clang_defaultReparseOptions(TU))) {
1515 clang_disposeTranslationUnit(TU);
1516 return 1;
1517 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001518
1519 if (checkForErrors(TU) != 0)
1520 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001521
1522 for (Loc = 0; Loc < NumLocations; ++Loc) {
1523 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1524 if (!file)
1525 continue;
1526
1527 Cursor = clang_getCursor(TU,
1528 clang_getLocation(TU, file, Locations[Loc].line,
1529 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001530
1531 if (checkForErrors(TU) != 0)
1532 return -1;
1533
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001534 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001535 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001536 PrintCursor(Cursor);
1537 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001538 clang_findReferencesInFile(Cursor, file, visitor);
1539 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001540
1541 if (checkForErrors(TU) != 0)
1542 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001543 }
1544 }
1545 }
1546
1547 PrintDiagnostics(TU);
1548 clang_disposeTranslationUnit(TU);
1549 clang_disposeIndex(CIdx);
1550 free(Locations);
1551 free_remapped_files(unsaved_files, num_unsaved_files);
1552 return 0;
1553}
1554
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001555typedef struct {
1556 const char *check_prefix;
1557 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001558 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001559 int abort;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001560} IndexData;
1561
1562static void printCheck(IndexData *data) {
1563 if (data->check_prefix) {
1564 if (data->first_check_printed) {
1565 printf("// %s-NEXT: ", data->check_prefix);
1566 } else {
1567 printf("// %s : ", data->check_prefix);
1568 data->first_check_printed = 1;
1569 }
1570 }
1571}
1572
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001573static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001574 CXString filename = clang_getFileName((CXFile)file);
1575 printf("%s", clang_getCString(filename));
1576 clang_disposeString(filename);
1577}
1578
1579static void printCXIndexLoc(CXIdxLoc loc) {
1580 CXString filename;
1581 const char *cname, *end;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001582 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001583 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001584 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001585
1586 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1587 if (line == 0) {
1588 printf("<null loc>");
1589 return;
1590 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001591 if (!file) {
1592 printf("<no idxfile>");
1593 return;
1594 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001595 filename = clang_getFileName((CXFile)file);
1596 cname = clang_getCString(filename);
1597 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001598 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001599
1600 if (isHeader) {
1601 printCXIndexFile(file);
1602 printf(":");
1603 }
1604 printf("%d:%d", line, column);
1605}
1606
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001607static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1608 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001609 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001610 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001611 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001612 unsigned line, column;
1613
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001614 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001615 if (!name)
1616 name = "<anon-tag>";
1617
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001618 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001619 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001620 newStr = (char *)malloc(strlen(name) + 10);
1621 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001622 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001623}
1624
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001625static void printCXIndexContainer(const CXIdxContainerInfo *info) {
1626 CXIdxClientContainer container;
1627 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00001628 if (!container)
1629 printf("[<<NULL>>]");
1630 else
1631 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001632}
1633
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001634static const char *getEntityKindString(CXIdxEntityKind kind) {
1635 switch (kind) {
1636 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
1637 case CXIdxEntity_Typedef: return "typedef";
1638 case CXIdxEntity_Function: return "function";
1639 case CXIdxEntity_Variable: return "variable";
1640 case CXIdxEntity_Field: return "field";
1641 case CXIdxEntity_EnumConstant: return "enumerator";
1642 case CXIdxEntity_ObjCClass: return "objc-class";
1643 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
1644 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001645 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
1646 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001647 case CXIdxEntity_ObjCProperty: return "objc-property";
1648 case CXIdxEntity_ObjCIvar: return "objc-ivar";
1649 case CXIdxEntity_Enum: return "enum";
1650 case CXIdxEntity_Struct: return "struct";
1651 case CXIdxEntity_Union: return "union";
1652 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001653 case CXIdxEntity_CXXNamespace: return "namespace";
1654 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
1655 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
1656 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
1657 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
1658 case CXIdxEntity_CXXConstructor: return "constructor";
1659 case CXIdxEntity_CXXDestructor: return "destructor";
1660 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
1661 case CXIdxEntity_CXXTypeAlias: return "type-alias";
1662 }
1663 assert(0 && "Garbage entity kind");
1664 return 0;
1665}
1666
1667static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
1668 switch (kind) {
1669 case CXIdxEntity_NonTemplate: return "";
1670 case CXIdxEntity_Template: return "-template";
1671 case CXIdxEntity_TemplatePartialSpecialization:
1672 return "-template-partial-spec";
1673 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001674 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001675 assert(0 && "Garbage entity kind");
1676 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001677}
1678
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00001679static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
1680 switch (kind) {
1681 case CXIdxEntityLang_None: return "<none>";
1682 case CXIdxEntityLang_C: return "C";
1683 case CXIdxEntityLang_ObjC: return "ObjC";
1684 case CXIdxEntityLang_CXX: return "C++";
1685 }
1686 assert(0 && "Garbage language kind");
1687 return 0;
1688}
1689
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001690static void printEntityInfo(const char *cb,
1691 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001692 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001693 const char *name;
1694 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001695 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001696 index_data = (IndexData *)client_data;
1697 printCheck(index_data);
1698
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001699 if (!info) {
1700 printf("%s: <<NULL>>", cb);
1701 return;
1702 }
1703
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001704 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001705 if (!name)
1706 name = "<anon-tag>";
1707
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001708 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
1709 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001710 printf(" | name: %s", name);
1711 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001712 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00001713
1714 for (i = 0; i != info->numAttributes; ++i) {
1715 const CXIdxAttrInfo *Attr = info->attributes[i];
1716 printf(" <attribute>: ");
1717 PrintCursor(Attr->cursor);
1718 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001719}
1720
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001721static void printBaseClassInfo(CXClientData client_data,
1722 const CXIdxBaseClassInfo *info) {
1723 printEntityInfo(" <base>", client_data, info->base);
1724 printf(" | cursor: ");
1725 PrintCursor(info->cursor);
1726 printf(" | loc: ");
1727 printCXIndexLoc(info->loc);
1728}
1729
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001730static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
1731 CXClientData client_data) {
1732 unsigned i;
1733 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
1734 printEntityInfo(" <protocol>", client_data,
1735 ProtoInfo->protocols[i]->protocol);
1736 printf(" | cursor: ");
1737 PrintCursor(ProtoInfo->protocols[i]->cursor);
1738 printf(" | loc: ");
1739 printCXIndexLoc(ProtoInfo->protocols[i]->loc);
1740 printf("\n");
1741 }
1742}
1743
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001744static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001745 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001746 CXString str;
1747 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001748 unsigned numDiags, i;
1749 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001750 IndexData *index_data;
1751 index_data = (IndexData *)client_data;
1752 printCheck(index_data);
1753
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00001754 numDiags = clang_getNumDiagnosticsInSet(diagSet);
1755 for (i = 0; i != numDiags; ++i) {
1756 diag = clang_getDiagnosticInSet(diagSet, i);
1757 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1758 cstr = clang_getCString(str);
1759 printf("[diagnostic]: %s\n", cstr);
1760 clang_disposeString(str);
1761
1762 if (getenv("CINDEXTEST_FAILONERROR") &&
1763 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1764 index_data->fail_for_error = 1;
1765 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001766 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001767}
1768
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001769static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
1770 CXFile file, void *reserved) {
1771 IndexData *index_data;
1772 index_data = (IndexData *)client_data;
1773 printCheck(index_data);
1774
1775 printf("[enteredMainFile]: ");
1776 printCXIndexFile((CXIdxClientFile)file);
1777 printf("\n");
1778
1779 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001780}
1781
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001782static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001783 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001784 IndexData *index_data;
1785 index_data = (IndexData *)client_data;
1786 printCheck(index_data);
1787
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001788 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001789 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001790 printf(" | name: \"%s\"", info->filename);
1791 printf(" | hash loc: ");
1792 printCXIndexLoc(info->hashLoc);
1793 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001794
1795 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001796}
1797
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001798static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001799 void *reserved) {
1800 IndexData *index_data;
1801 index_data = (IndexData *)client_data;
1802 printCheck(index_data);
1803
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00001804 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001805 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001806}
1807
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001808static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001809 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001810 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001811 const CXIdxObjCCategoryDeclInfo *CatInfo;
1812 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001813 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001814 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001815 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001816 index_data = (IndexData *)client_data;
1817
1818 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
1819 printf(" | cursor: ");
1820 PrintCursor(info->cursor);
1821 printf(" | loc: ");
1822 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00001823 printf(" | semantic-container: ");
1824 printCXIndexContainer(info->semanticContainer);
1825 printf(" | lexical-container: ");
1826 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001827 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001828 printf(" | isDef: %d", info->isDefinition);
1829 printf(" | isContainer: %d", info->isContainer);
1830 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001831
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001832 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00001833 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001834 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001835 PrintCursor(Attr->cursor);
1836 printf("\n");
1837 }
1838
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001839 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
1840 const char *kindName = 0;
1841 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
1842 switch (K) {
1843 case CXIdxObjCContainer_ForwardRef:
1844 kindName = "forward-ref"; break;
1845 case CXIdxObjCContainer_Interface:
1846 kindName = "interface"; break;
1847 case CXIdxObjCContainer_Implementation:
1848 kindName = "implementation"; break;
1849 }
1850 printCheck(index_data);
1851 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
1852 }
1853
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001854 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001855 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
1856 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001857 printf(" | cursor: ");
1858 PrintCursor(CatInfo->classCursor);
1859 printf(" | loc: ");
1860 printCXIndexLoc(CatInfo->classLoc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001861 printf("\n");
1862 }
1863
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001864 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
1865 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001866 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001867 printf("\n");
1868 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001869 }
1870
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001871 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
1872 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001873 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001874
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00001875 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
1876 for (i = 0; i != CXXClassInfo->numBases; ++i) {
1877 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
1878 printf("\n");
1879 }
1880 }
1881
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001882 if (info->declAsContainer)
1883 clang_index_setClientContainer(info->declAsContainer,
1884 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001885}
1886
1887static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001888 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001889 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001890 printf(" | cursor: ");
1891 PrintCursor(info->cursor);
1892 printf(" | loc: ");
1893 printCXIndexLoc(info->loc);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001894 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001895 printf(" | container: ");
1896 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00001897 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001898 switch (info->kind) {
1899 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001900 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001901 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001902 printf("\n");
1903}
1904
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001905static int index_abortQuery(CXClientData client_data, void *reserved) {
1906 IndexData *index_data;
1907 index_data = (IndexData *)client_data;
1908 return index_data->abort;
1909}
1910
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001911static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001912 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001913 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001914 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001915 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001916 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001917 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001918 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001919 index_indexEntityReference
1920};
1921
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00001922static unsigned getIndexOptions(void) {
1923 unsigned index_opts;
1924 index_opts = 0;
1925 if (getenv("CINDEXTEST_SUPPRESSREFS"))
1926 index_opts |= CXIndexOpt_SuppressRedundantRefs;
1927 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
1928 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
1929
1930 return index_opts;
1931}
1932
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001933static int index_file(int argc, const char **argv) {
1934 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001935 CXIndex Idx;
1936 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001937 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001938 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001939 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001940
1941 check_prefix = 0;
1942 if (argc > 0) {
1943 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1944 check_prefix = argv[0] + strlen("-check-prefix=");
1945 ++argv;
1946 --argc;
1947 }
1948 }
1949
1950 if (argc == 0) {
1951 fprintf(stderr, "no compiler arguments\n");
1952 return -1;
1953 }
1954
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001955 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1956 /* displayDiagnosics=*/1))) {
1957 fprintf(stderr, "Could not create Index\n");
1958 return 1;
1959 }
1960 idxAction = 0;
1961 result = 1;
1962
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001963 index_data.check_prefix = check_prefix;
1964 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001965 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001966 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001967
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00001968 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001969 idxAction = clang_IndexAction_create(Idx);
1970 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001971 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00001972 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001973 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001974 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001975
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001976 clang_IndexAction_dispose(idxAction);
1977 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001978 return result;
1979}
1980
1981static int index_tu(int argc, const char **argv) {
1982 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00001983 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001984 CXTranslationUnit TU;
1985 const char *check_prefix;
1986 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00001987 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00001988 int result;
1989
1990 check_prefix = 0;
1991 if (argc > 0) {
1992 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1993 check_prefix = argv[0] + strlen("-check-prefix=");
1994 ++argv;
1995 --argc;
1996 }
1997 }
1998
1999 if (argc == 0) {
2000 fprintf(stderr, "no ast file\n");
2001 return -1;
2002 }
2003
2004 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2005 /* displayDiagnosics=*/1))) {
2006 fprintf(stderr, "Could not create Index\n");
2007 return 1;
2008 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002009 idxAction = 0;
2010 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002011
2012 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002013 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002014
2015 index_data.check_prefix = check_prefix;
2016 index_data.first_check_printed = 0;
2017 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002018 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002019
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002020 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002021 idxAction = clang_IndexAction_create(Idx);
2022 result = clang_indexTranslationUnit(idxAction, &index_data,
2023 &IndexCB,sizeof(IndexCB),
2024 index_opts, TU);
2025 if (index_data.fail_for_error)
2026 goto finished;
2027
2028 finished:
2029 clang_IndexAction_dispose(idxAction);
2030 clang_disposeIndex(Idx);
2031
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002032 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002033}
2034
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002035int perform_token_annotation(int argc, const char **argv) {
2036 const char *input = argv[1];
2037 char *filename = 0;
2038 unsigned line, second_line;
2039 unsigned column, second_column;
2040 CXIndex CIdx;
2041 CXTranslationUnit TU = 0;
2042 int errorCode;
2043 struct CXUnsavedFile *unsaved_files = 0;
2044 int num_unsaved_files = 0;
2045 CXToken *tokens;
2046 unsigned num_tokens;
2047 CXSourceRange range;
2048 CXSourceLocation startLoc, endLoc;
2049 CXFile file = 0;
2050 CXCursor *cursors = 0;
2051 unsigned i;
2052
2053 input += strlen("-test-annotate-tokens=");
2054 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2055 &second_line, &second_column)))
2056 return errorCode;
2057
2058 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2059 return -1;
2060
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002061 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002062 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2063 argv + num_unsaved_files + 2,
2064 argc - num_unsaved_files - 3,
2065 unsaved_files,
2066 num_unsaved_files,
2067 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002068 if (!TU) {
2069 fprintf(stderr, "unable to parse input\n");
2070 clang_disposeIndex(CIdx);
2071 free(filename);
2072 free_remapped_files(unsaved_files, num_unsaved_files);
2073 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002074 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002075 errorCode = 0;
2076
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002077 if (checkForErrors(TU) != 0)
2078 return -1;
2079
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002080 if (getenv("CINDEXTEST_EDITING")) {
2081 for (i = 0; i < 5; ++i) {
2082 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2083 clang_defaultReparseOptions(TU))) {
2084 fprintf(stderr, "Unable to reparse translation unit!\n");
2085 errorCode = -1;
2086 goto teardown;
2087 }
2088 }
2089 }
2090
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002091 if (checkForErrors(TU) != 0) {
2092 errorCode = -1;
2093 goto teardown;
2094 }
2095
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002096 file = clang_getFile(TU, filename);
2097 if (!file) {
2098 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2099 errorCode = -1;
2100 goto teardown;
2101 }
2102
2103 startLoc = clang_getLocation(TU, file, line, column);
2104 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002105 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002106 column);
2107 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002108 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002109 }
2110
2111 endLoc = clang_getLocation(TU, file, second_line, second_column);
2112 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002113 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002114 second_line, second_column);
2115 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002116 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002117 }
2118
2119 range = clang_getRange(startLoc, endLoc);
2120 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002121
2122 if (checkForErrors(TU) != 0) {
2123 errorCode = -1;
2124 goto teardown;
2125 }
2126
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002127 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2128 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002129
2130 if (checkForErrors(TU) != 0) {
2131 errorCode = -1;
2132 goto teardown;
2133 }
2134
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002135 for (i = 0; i != num_tokens; ++i) {
2136 const char *kind = "<unknown>";
2137 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2138 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2139 unsigned start_line, start_column, end_line, end_column;
2140
2141 switch (clang_getTokenKind(tokens[i])) {
2142 case CXToken_Punctuation: kind = "Punctuation"; break;
2143 case CXToken_Keyword: kind = "Keyword"; break;
2144 case CXToken_Identifier: kind = "Identifier"; break;
2145 case CXToken_Literal: kind = "Literal"; break;
2146 case CXToken_Comment: kind = "Comment"; break;
2147 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002148 clang_getSpellingLocation(clang_getRangeStart(extent),
2149 0, &start_line, &start_column, 0);
2150 clang_getSpellingLocation(clang_getRangeEnd(extent),
2151 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002152 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2153 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002154 if (!clang_isInvalid(cursors[i].kind)) {
2155 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002156 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002157 }
2158 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002159 }
2160 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002161 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002162
2163 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002164 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002165 clang_disposeTranslationUnit(TU);
2166 clang_disposeIndex(CIdx);
2167 free(filename);
2168 free_remapped_files(unsaved_files, num_unsaved_files);
2169 return errorCode;
2170}
2171
Ted Kremenek0d435192009-11-17 18:13:31 +00002172/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002173/* USR printing. */
2174/******************************************************************************/
2175
2176static int insufficient_usr(const char *kind, const char *usage) {
2177 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2178 return 1;
2179}
2180
2181static unsigned isUSR(const char *s) {
2182 return s[0] == 'c' && s[1] == ':';
2183}
2184
2185static int not_usr(const char *s, const char *arg) {
2186 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2187 return 1;
2188}
2189
2190static void print_usr(CXString usr) {
2191 const char *s = clang_getCString(usr);
2192 printf("%s\n", s);
2193 clang_disposeString(usr);
2194}
2195
2196static void display_usrs() {
2197 fprintf(stderr, "-print-usrs options:\n"
2198 " ObjCCategory <class name> <category name>\n"
2199 " ObjCClass <class name>\n"
2200 " ObjCIvar <ivar name> <class USR>\n"
2201 " ObjCMethod <selector> [0=class method|1=instance method] "
2202 "<class USR>\n"
2203 " ObjCProperty <property name> <class USR>\n"
2204 " ObjCProtocol <protocol name>\n");
2205}
2206
2207int print_usrs(const char **I, const char **E) {
2208 while (I != E) {
2209 const char *kind = *I;
2210 unsigned len = strlen(kind);
2211 switch (len) {
2212 case 8:
2213 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2214 if (I + 2 >= E)
2215 return insufficient_usr(kind, "<ivar name> <class USR>");
2216 if (!isUSR(I[2]))
2217 return not_usr("<class USR>", I[2]);
2218 else {
2219 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002220 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002221 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002222 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2223 }
2224
2225 I += 3;
2226 continue;
2227 }
2228 break;
2229 case 9:
2230 if (memcmp(kind, "ObjCClass", 9) == 0) {
2231 if (I + 1 >= E)
2232 return insufficient_usr(kind, "<class name>");
2233 print_usr(clang_constructUSR_ObjCClass(I[1]));
2234 I += 2;
2235 continue;
2236 }
2237 break;
2238 case 10:
2239 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2240 if (I + 3 >= E)
2241 return insufficient_usr(kind, "<method selector> "
2242 "[0=class method|1=instance method] <class USR>");
2243 if (!isUSR(I[3]))
2244 return not_usr("<class USR>", I[3]);
2245 else {
2246 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002247 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002248 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002249 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2250 }
2251 I += 4;
2252 continue;
2253 }
2254 break;
2255 case 12:
2256 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2257 if (I + 2 >= E)
2258 return insufficient_usr(kind, "<class name> <category name>");
2259 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2260 I += 3;
2261 continue;
2262 }
2263 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2264 if (I + 1 >= E)
2265 return insufficient_usr(kind, "<protocol name>");
2266 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2267 I += 2;
2268 continue;
2269 }
2270 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2271 if (I + 2 >= E)
2272 return insufficient_usr(kind, "<property name> <class USR>");
2273 if (!isUSR(I[2]))
2274 return not_usr("<class USR>", I[2]);
2275 else {
2276 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002277 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002278 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002279 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2280 }
2281 I += 3;
2282 continue;
2283 }
2284 break;
2285 default:
2286 break;
2287 }
2288 break;
2289 }
2290
2291 if (I != E) {
2292 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2293 display_usrs();
2294 return 1;
2295 }
2296 return 0;
2297}
2298
2299int print_usrs_file(const char *file_name) {
2300 char line[2048];
2301 const char *args[128];
2302 unsigned numChars = 0;
2303
2304 FILE *fp = fopen(file_name, "r");
2305 if (!fp) {
2306 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2307 return 1;
2308 }
2309
2310 /* This code is not really all that safe, but it works fine for testing. */
2311 while (!feof(fp)) {
2312 char c = fgetc(fp);
2313 if (c == '\n') {
2314 unsigned i = 0;
2315 const char *s = 0;
2316
2317 if (numChars == 0)
2318 continue;
2319
2320 line[numChars] = '\0';
2321 numChars = 0;
2322
2323 if (line[0] == '/' && line[1] == '/')
2324 continue;
2325
2326 s = strtok(line, " ");
2327 while (s) {
2328 args[i] = s;
2329 ++i;
2330 s = strtok(0, " ");
2331 }
2332 if (print_usrs(&args[0], &args[i]))
2333 return 1;
2334 }
2335 else
2336 line[numChars++] = c;
2337 }
2338
2339 fclose(fp);
2340 return 0;
2341}
2342
2343/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002344/* Command line processing. */
2345/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002346int write_pch_file(const char *filename, int argc, const char *argv[]) {
2347 CXIndex Idx;
2348 CXTranslationUnit TU;
2349 struct CXUnsavedFile *unsaved_files = 0;
2350 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002351 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002352
2353 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2354
2355 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2356 clang_disposeIndex(Idx);
2357 return -1;
2358 }
2359
2360 TU = clang_parseTranslationUnit(Idx, 0,
2361 argv + num_unsaved_files,
2362 argc - num_unsaved_files,
2363 unsaved_files,
2364 num_unsaved_files,
2365 CXTranslationUnit_Incomplete);
2366 if (!TU) {
2367 fprintf(stderr, "Unable to load translation unit!\n");
2368 free_remapped_files(unsaved_files, num_unsaved_files);
2369 clang_disposeIndex(Idx);
2370 return 1;
2371 }
2372
Douglas Gregor39c411f2011-07-06 16:43:36 +00002373 switch (clang_saveTranslationUnit(TU, filename,
2374 clang_defaultSaveOptions(TU))) {
2375 case CXSaveError_None:
2376 break;
2377
2378 case CXSaveError_TranslationErrors:
2379 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2380 filename);
2381 result = 2;
2382 break;
2383
2384 case CXSaveError_InvalidTU:
2385 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2386 filename);
2387 result = 3;
2388 break;
2389
2390 case CXSaveError_Unknown:
2391 default:
2392 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2393 result = 1;
2394 break;
2395 }
2396
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002397 clang_disposeTranslationUnit(TU);
2398 free_remapped_files(unsaved_files, num_unsaved_files);
2399 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002400 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002401}
2402
2403/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002404/* Serialized diagnostics. */
2405/******************************************************************************/
2406
2407static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2408 switch (error) {
2409 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2410 case CXLoadDiag_None: break;
2411 case CXLoadDiag_Unknown: return "Unknown";
2412 case CXLoadDiag_InvalidFile: return "Invalid File";
2413 }
2414 return "None";
2415}
2416
2417static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2418 switch (severity) {
2419 case CXDiagnostic_Note: return "note";
2420 case CXDiagnostic_Error: return "error";
2421 case CXDiagnostic_Fatal: return "fatal";
2422 case CXDiagnostic_Ignored: return "ignored";
2423 case CXDiagnostic_Warning: return "warning";
2424 }
2425 return "unknown";
2426}
2427
2428static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002429 if (indent == 0)
2430 return;
2431 fprintf(stderr, "+");
2432 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002433 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002434 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002435 --indent;
2436 }
2437}
2438
2439static void printLocation(CXSourceLocation L) {
2440 CXFile File;
2441 CXString FileName;
2442 unsigned line, column, offset;
2443
2444 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2445 FileName = clang_getFileName(File);
2446
2447 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2448 clang_disposeString(FileName);
2449}
2450
2451static void printRanges(CXDiagnostic D, unsigned indent) {
2452 unsigned i, n = clang_getDiagnosticNumRanges(D);
2453
2454 for (i = 0; i < n; ++i) {
2455 CXSourceLocation Start, End;
2456 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2457 Start = clang_getRangeStart(SR);
2458 End = clang_getRangeEnd(SR);
2459
2460 printIndent(indent);
2461 fprintf(stderr, "Range: ");
2462 printLocation(Start);
2463 fprintf(stderr, " ");
2464 printLocation(End);
2465 fprintf(stderr, "\n");
2466 }
2467}
2468
2469static void printFixIts(CXDiagnostic D, unsigned indent) {
2470 unsigned i, n = clang_getDiagnosticNumFixIts(D);
2471 for (i = 0 ; i < n; ++i) {
2472 CXSourceRange ReplacementRange;
2473 CXString text;
2474 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2475
2476 printIndent(indent);
2477 fprintf(stderr, "FIXIT: (");
2478 printLocation(clang_getRangeStart(ReplacementRange));
2479 fprintf(stderr, " - ");
2480 printLocation(clang_getRangeEnd(ReplacementRange));
2481 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2482 clang_disposeString(text);
2483 }
2484}
2485
2486static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002487 unsigned i, n;
2488
Ted Kremenek15322172011-11-10 08:43:12 +00002489 if (!Diags)
2490 return;
2491
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002492 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002493 for (i = 0; i < n; ++i) {
2494 CXSourceLocation DiagLoc;
2495 CXDiagnostic D;
2496 CXFile File;
2497 CXString FileName, DiagSpelling, DiagOption;
2498 unsigned line, column, offset;
2499 const char *DiagOptionStr = 0;
2500
2501 D = clang_getDiagnosticInSet(Diags, i);
2502 DiagLoc = clang_getDiagnosticLocation(D);
2503 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
2504 FileName = clang_getFileName(File);
2505 DiagSpelling = clang_getDiagnosticSpelling(D);
2506
2507 printIndent(indent);
2508
2509 fprintf(stderr, "%s:%d:%d: %s: %s",
2510 clang_getCString(FileName),
2511 line,
2512 column,
2513 getSeverityString(clang_getDiagnosticSeverity(D)),
2514 clang_getCString(DiagSpelling));
2515
2516 DiagOption = clang_getDiagnosticOption(D, 0);
2517 DiagOptionStr = clang_getCString(DiagOption);
2518 if (DiagOptionStr) {
2519 fprintf(stderr, " [%s]", DiagOptionStr);
2520 }
2521
2522 fprintf(stderr, "\n");
2523
2524 printRanges(D, indent);
2525 printFixIts(D, indent);
2526
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00002527 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00002528 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
2529
2530 clang_disposeString(FileName);
2531 clang_disposeString(DiagSpelling);
2532 clang_disposeString(DiagOption);
2533 }
2534}
2535
2536static int read_diagnostics(const char *filename) {
2537 enum CXLoadDiag_Error error;
2538 CXString errorString;
2539 CXDiagnosticSet Diags = 0;
2540
2541 Diags = clang_loadDiagnostics(filename, &error, &errorString);
2542 if (!Diags) {
2543 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
2544 getDiagnosticCodeStr(error),
2545 clang_getCString(errorString));
2546 clang_disposeString(errorString);
2547 return 1;
2548 }
2549
2550 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002551 fprintf(stderr, "Number of diagnostics: %d\n",
2552 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00002553 clang_disposeDiagnosticSet(Diags);
2554 return 0;
2555}
2556
2557/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002558/* Command line processing. */
2559/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002560
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002561static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002562 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002563 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002564 if (strcmp(s, "-usrs") == 0)
2565 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002566 if (strncmp(s, "-memory-usage", 13) == 0)
2567 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002568 return NULL;
2569}
2570
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002571static void print_usage(void) {
2572 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002573 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002574 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002575 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002576 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002577 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002578 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002579 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002580 "[FileCheck prefix]\n");
2581 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002582 " c-index-test -test-load-tu <AST file> <symbol filter> "
2583 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002584 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2585 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002586 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002587 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002588 " c-index-test -test-load-source-memory-usage "
2589 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002590 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2591 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002592 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002593 " c-index-test -test-load-source-usrs-memory-usage "
2594 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002595 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2596 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002597 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002598 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002599 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002600 " c-index-test -test-print-typekind {<args>}*\n"
2601 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002602 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00002603 " c-index-test -write-pch <file> <compiler arguments>\n");
2604 fprintf(stderr,
2605 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002606 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002607 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002608 " all - load all symbols, including those from PCH\n"
2609 " local - load all symbols except those in PCH\n"
2610 " category - only load ObjC categories (non-PCH)\n"
2611 " interface - only load ObjC interfaces (non-PCH)\n"
2612 " protocol - only load ObjC protocols (non-PCH)\n"
2613 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002614 " typedef - only load typdefs (non-PCH)\n"
2615 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002616}
2617
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002618/***/
2619
2620int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002621 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00002622 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
2623 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002624 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002625 return perform_code_completion(argc, argv, 0);
2626 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2627 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002628 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2629 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002630 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2631 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002632 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2633 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002634 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
2635 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002636 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002637 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002638 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002639 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2640 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002641 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002642 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2643 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2644 if (I) {
2645 int trials = atoi(argv[2]);
2646 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2647 NULL);
2648 }
2649 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002650 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002651 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002652
2653 PostVisitTU postVisit = 0;
2654 if (strstr(argv[1], "-memory-usage"))
2655 postVisit = PrintMemoryUsage;
2656
Ted Kremenek7d405622010-01-12 23:34:26 +00002657 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002658 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2659 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002660 }
2661 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002662 return perform_file_scan(argv[2], argv[3],
2663 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002664 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2665 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002666 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2667 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2668 PrintInclusionStack);
2669 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2670 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2671 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002672 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2673 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2674 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002675 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2676 return perform_test_load_source(argc - 2, argv + 2, "all",
2677 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002678 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2679 if (argc > 2)
2680 return print_usrs(argv + 2, argv + argc);
2681 else {
2682 display_usrs();
2683 return 1;
2684 }
2685 }
2686 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2687 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002688 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2689 return write_pch_file(argv[2], argc - 3, argv + 3);
2690
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002691 print_usage();
2692 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002693}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002694
2695/***/
2696
2697/* We intentionally run in a separate thread to ensure we at least minimal
2698 * testing of a multithreaded environment (for example, having a reduced stack
2699 * size). */
2700
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002701typedef struct thread_info {
2702 int argc;
2703 const char **argv;
2704 int result;
2705} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002706void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002707 thread_info *client_data = client_data_v;
2708 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002709}
2710
2711int main(int argc, const char **argv) {
2712 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002713
Douglas Gregor61605982010-10-27 16:00:01 +00002714 if (getenv("CINDEXTEST_NOTHREADS"))
2715 return cindextest_main(argc, argv);
2716
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002717 client_data.argc = argc;
2718 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002719 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002720 return client_data.result;
2721}