blob: 80068b61568b8e9db2212c7046ea79c74eccdfdd [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;
Douglas Gregor44c181a2010-07-23 00:33:23 +000042
43 return options;
44}
45
Daniel Dunbar51b058c2010-02-14 08:32:24 +000046static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
47 unsigned end_line, unsigned end_column) {
48 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000049 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000050}
51
Ted Kremenek1c6da172009-11-17 19:37:36 +000052static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
53 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000054
Douglas Gregora88084b2010-02-18 18:08:43 +000055 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000056 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000057 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
58 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000059 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000060 return 1;
61}
62
Douglas Gregor4db64a42010-01-23 00:14:00 +000063void free_remapped_files(struct CXUnsavedFile *unsaved_files,
64 int num_unsaved_files) {
65 int i;
66 for (i = 0; i != num_unsaved_files; ++i) {
67 free((char *)unsaved_files[i].Filename);
68 free((char *)unsaved_files[i].Contents);
69 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000070 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000071}
72
73int parse_remapped_files(int argc, const char **argv, int start_arg,
74 struct CXUnsavedFile **unsaved_files,
75 int *num_unsaved_files) {
76 int i;
77 int arg;
78 int prefix_len = strlen("-remap-file=");
79 *unsaved_files = 0;
80 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000081
Douglas Gregor4db64a42010-01-23 00:14:00 +000082 /* Count the number of remapped files. */
83 for (arg = start_arg; arg < argc; ++arg) {
84 if (strncmp(argv[arg], "-remap-file=", prefix_len))
85 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000086
Douglas Gregor4db64a42010-01-23 00:14:00 +000087 ++*num_unsaved_files;
88 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000089
Douglas Gregor4db64a42010-01-23 00:14:00 +000090 if (*num_unsaved_files == 0)
91 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000092
Douglas Gregor4db64a42010-01-23 00:14:00 +000093 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000094 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
95 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000096 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
97 struct CXUnsavedFile *unsaved = *unsaved_files + i;
98 const char *arg_string = argv[arg] + prefix_len;
99 int filename_len;
100 char *filename;
101 char *contents;
102 FILE *to_file;
103 const char *semi = strchr(arg_string, ';');
104 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000105 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000106 "error: -remap-file=from;to argument is missing semicolon\n");
107 free_remapped_files(*unsaved_files, i);
108 *unsaved_files = 0;
109 *num_unsaved_files = 0;
110 return -1;
111 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000112
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000114 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000115 if (!to_file) {
116 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
117 semi + 1);
118 free_remapped_files(*unsaved_files, i);
119 *unsaved_files = 0;
120 *num_unsaved_files = 0;
121 return -1;
122 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000123
Douglas Gregor4db64a42010-01-23 00:14:00 +0000124 /* Determine the length of the file we're remapping to. */
125 fseek(to_file, 0, SEEK_END);
126 unsaved->Length = ftell(to_file);
127 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000128
Douglas Gregor4db64a42010-01-23 00:14:00 +0000129 /* Read the contents of the file we're remapping to. */
130 contents = (char *)malloc(unsaved->Length + 1);
131 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
132 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
133 (feof(to_file) ? "EOF" : "error"), semi + 1);
134 fclose(to_file);
135 free_remapped_files(*unsaved_files, i);
136 *unsaved_files = 0;
137 *num_unsaved_files = 0;
138 return -1;
139 }
140 contents[unsaved->Length] = 0;
141 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000142
Douglas Gregor4db64a42010-01-23 00:14:00 +0000143 /* Close the file. */
144 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000145
Douglas Gregor4db64a42010-01-23 00:14:00 +0000146 /* Copy the file name that we're remapping from. */
147 filename_len = semi - arg_string;
148 filename = (char *)malloc(filename_len + 1);
149 memcpy(filename, arg_string, filename_len);
150 filename[filename_len] = 0;
151 unsaved->Filename = filename;
152 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000153
Douglas Gregor4db64a42010-01-23 00:14:00 +0000154 return 0;
155}
156
Ted Kremenek0d435192009-11-17 18:13:31 +0000157/******************************************************************************/
158/* Pretty-printing. */
159/******************************************************************************/
160
Douglas Gregor430d7a12011-07-25 17:48:11 +0000161static void PrintRange(CXSourceRange R, const char *str) {
162 CXFile begin_file, end_file;
163 unsigned begin_line, begin_column, end_line, end_column;
164
165 clang_getSpellingLocation(clang_getRangeStart(R),
166 &begin_file, &begin_line, &begin_column, 0);
167 clang_getSpellingLocation(clang_getRangeEnd(R),
168 &end_file, &end_line, &end_column, 0);
169 if (!begin_file || !end_file)
170 return;
171
172 printf(" %s=", str);
173 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
174}
175
Douglas Gregor358559d2010-10-02 22:49:11 +0000176int want_display_name = 0;
177
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000178static void PrintCursor(CXCursor Cursor) {
179 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000180 if (clang_isInvalid(Cursor.kind)) {
181 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
182 printf("Invalid Cursor => %s", clang_getCString(ks));
183 clang_disposeString(ks);
184 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000185 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000186 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000187 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000188 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000189 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000190 CXCursor *overridden;
191 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000192 unsigned RefNameRangeNr;
193 CXSourceRange CursorExtent;
194 CXSourceRange RefNameRange;
Douglas Gregor9f592342010-10-01 20:25:15 +0000195
Ted Kremeneke68fff62010-02-17 00:41:32 +0000196 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000197 string = want_display_name? clang_getCursorDisplayName(Cursor)
198 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000199 printf("%s=%s", clang_getCString(ks),
200 clang_getCString(string));
201 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000202 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000203
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000204 Referenced = clang_getCursorReferenced(Cursor);
205 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000206 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
207 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
208 printf("[");
209 for (I = 0; I != N; ++I) {
210 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000211 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000212 if (I)
213 printf(", ");
214
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000215 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000216 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000217 printf("%d:%d", line, column);
218 }
219 printf("]");
220 } else {
221 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000222 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000223 printf(":%d:%d", line, column);
224 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000225 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000226
227 if (clang_isCursorDefinition(Cursor))
228 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000229
230 switch (clang_getCursorAvailability(Cursor)) {
231 case CXAvailability_Available:
232 break;
233
234 case CXAvailability_Deprecated:
235 printf(" (deprecated)");
236 break;
237
238 case CXAvailability_NotAvailable:
239 printf(" (unavailable)");
240 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000241
242 case CXAvailability_NotAccessible:
243 printf(" (inaccessible)");
244 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000245 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000246
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000247 if (clang_CXXMethod_isStatic(Cursor))
248 printf(" (static)");
249 if (clang_CXXMethod_isVirtual(Cursor))
250 printf(" (virtual)");
251
Ted Kremenek95f33552010-08-26 01:42:22 +0000252 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
253 CXType T =
254 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
255 CXString S = clang_getTypeKindSpelling(T.kind);
256 printf(" [IBOutletCollection=%s]", clang_getCString(S));
257 clang_disposeString(S);
258 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000259
260 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
261 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
262 unsigned isVirtual = clang_isVirtualBase(Cursor);
263 const char *accessStr = 0;
264
265 switch (access) {
266 case CX_CXXInvalidAccessSpecifier:
267 accessStr = "invalid"; break;
268 case CX_CXXPublic:
269 accessStr = "public"; break;
270 case CX_CXXProtected:
271 accessStr = "protected"; break;
272 case CX_CXXPrivate:
273 accessStr = "private"; break;
274 }
275
276 printf(" [access=%s isVirtual=%s]", accessStr,
277 isVirtual ? "true" : "false");
278 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000279
280 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
281 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
282 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
283 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000284 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000285 printf(" [Specialization of %s:%d:%d]",
286 clang_getCString(Name), line, column);
287 clang_disposeString(Name);
288 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000289
290 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
291 if (num_overridden) {
292 unsigned I;
293 printf(" [Overrides ");
294 for (I = 0; I != num_overridden; ++I) {
295 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000296 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000297 if (I)
298 printf(", ");
299 printf("@%d:%d", line, column);
300 }
301 printf("]");
302 clang_disposeOverriddenCursors(overridden);
303 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000304
305 if (Cursor.kind == CXCursor_InclusionDirective) {
306 CXFile File = clang_getIncludedFile(Cursor);
307 CXString Included = clang_getFileName(File);
308 printf(" (%s)", clang_getCString(Included));
309 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000310
311 if (clang_isFileMultipleIncludeGuarded(TU, File))
312 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000313 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000314
315 CursorExtent = clang_getCursorExtent(Cursor);
316 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
317 CXNameRange_WantQualifier
318 | CXNameRange_WantSinglePiece
319 | CXNameRange_WantTemplateArgs,
320 0);
321 if (!clang_equalRanges(CursorExtent, RefNameRange))
322 PrintRange(RefNameRange, "SingleRefName");
323
324 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
325 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
326 CXNameRange_WantQualifier
327 | CXNameRange_WantTemplateArgs,
328 RefNameRangeNr);
329 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
330 break;
331 if (!clang_equalRanges(CursorExtent, RefNameRange))
332 PrintRange(RefNameRange, "RefName");
333 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000334 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000335}
Steve Naroff89922f82009-08-31 00:59:03 +0000336
Ted Kremeneke68fff62010-02-17 00:41:32 +0000337static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000338 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000339 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000340 CXFile file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000341 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000343 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000344 clang_disposeString(source);
345 return "<invalid loc>";
346 }
347 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000348 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000349 clang_disposeString(source);
350 return b;
351 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000352}
353
Ted Kremenek0d435192009-11-17 18:13:31 +0000354/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000355/* Callbacks. */
356/******************************************************************************/
357
358typedef void (*PostVisitTU)(CXTranslationUnit);
359
Douglas Gregora88084b2010-02-18 18:08:43 +0000360void PrintDiagnostic(CXDiagnostic Diagnostic) {
361 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000362 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000363 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000364 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000365 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
366 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000367 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000368
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000369 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000370 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000371
Douglas Gregor274f1902010-02-22 23:17:23 +0000372 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
373 fprintf(stderr, "%s\n", clang_getCString(Msg));
374 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000375
Douglas Gregora9b06d42010-11-09 06:24:54 +0000376 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
377 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000378 if (!file)
379 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000380
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000381 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
382 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000383 CXSourceRange range;
384 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
385 CXSourceLocation start = clang_getRangeStart(range);
386 CXSourceLocation end = clang_getRangeEnd(range);
387 unsigned start_line, start_column, end_line, end_column;
388 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000389 clang_getSpellingLocation(start, &start_file, &start_line,
390 &start_column, 0);
391 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000392 if (clang_equalLocations(start, end)) {
393 /* Insertion. */
394 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000395 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000396 clang_getCString(insertion_text), start_line, start_column);
397 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
398 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000399 if (start_file == file && end_file == file) {
400 fprintf(out, "FIX-IT: Remove ");
401 PrintExtent(out, start_line, start_column, end_line, end_column);
402 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000403 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000404 } else {
405 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000406 if (start_file == end_file) {
407 fprintf(out, "FIX-IT: Replace ");
408 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000409 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000410 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000411 break;
412 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000413 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000414 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000415}
416
Douglas Gregora88084b2010-02-18 18:08:43 +0000417void PrintDiagnostics(CXTranslationUnit TU) {
418 int i, n = clang_getNumDiagnostics(TU);
419 for (i = 0; i != n; ++i) {
420 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
421 PrintDiagnostic(Diag);
422 clang_disposeDiagnostic(Diag);
423 }
424}
425
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000426void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000427 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000428 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000429 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000430 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000431 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000432 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000433 unsigned long amount = usage.entries[i].amount;
434 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000435 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000436 ((double) amount)/(1024*1024));
437 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000438 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000439 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000440 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000441}
442
Ted Kremenekce2ae882010-01-26 17:59:48 +0000443/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000444/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000445/******************************************************************************/
446
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000447static const char *FileCheckPrefix = "CHECK";
448
Douglas Gregora7bde202010-01-19 00:34:46 +0000449static void PrintCursorExtent(CXCursor C) {
450 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000451 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000452}
453
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000454/* Data used by all of the visitors. */
455typedef struct {
456 CXTranslationUnit TU;
457 enum CXCursorKind *Filter;
458} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000459
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000460
Ted Kremeneke68fff62010-02-17 00:41:32 +0000461enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000462 CXCursor Parent,
463 CXClientData ClientData) {
464 VisitorData *Data = (VisitorData *)ClientData;
465 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000466 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000467 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000468 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000469 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000470 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000471 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000472 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000473 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000474 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000475 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000476
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000477 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000478}
Steve Naroff50398192009-08-28 15:28:48 +0000479
Ted Kremeneke68fff62010-02-17 00:41:32 +0000480static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000481 CXCursor Parent,
482 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000483 const char *startBuf, *endBuf;
484 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
485 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000486 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000487
Douglas Gregorb6998662010-01-19 19:34:47 +0000488 if (Cursor.kind != CXCursor_FunctionDecl ||
489 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000490 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000491
492 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
493 &startLine, &startColumn,
494 &endLine, &endColumn);
495 /* Probe the entire body, looking for both decls and refs. */
496 curLine = startLine;
497 curColumn = startColumn;
498
499 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000500 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000501 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000502 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000503
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000504 if (*startBuf == '\n') {
505 startBuf++;
506 curLine++;
507 curColumn = 1;
508 } else if (*startBuf != '\t')
509 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000510
Douglas Gregor98258af2010-01-18 22:46:11 +0000511 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000512 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000513
Douglas Gregor1db19de2010-01-19 21:36:55 +0000514 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000516 CXSourceLocation RefLoc
517 = clang_getLocation(Data->TU, file, curLine, curColumn);
518 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000519 if (Ref.kind == CXCursor_NoDeclFound) {
520 /* Nothing found here; that's fine. */
521 } else if (Ref.kind != CXCursor_FunctionDecl) {
522 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
523 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000524 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000525 printf("\n");
526 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000527 }
Ted Kremenek74844072010-02-17 00:41:20 +0000528 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000529 startBuf++;
530 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000531
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000532 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000533}
534
Ted Kremenek7d405622010-01-12 23:34:26 +0000535/******************************************************************************/
536/* USR testing. */
537/******************************************************************************/
538
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000539enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
540 CXClientData ClientData) {
541 VisitorData *Data = (VisitorData *)ClientData;
542 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000543 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000544 const char *cstr = clang_getCString(USR);
545 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000546 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000547 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000548 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000549 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
550
Douglas Gregora7bde202010-01-19 00:34:46 +0000551 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000552 printf("\n");
553 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000554
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000555 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000556 }
557
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000558 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000559}
560
561/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000562/* Inclusion stack testing. */
563/******************************************************************************/
564
565void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
566 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000567
Ted Kremenek16b55a72010-01-26 19:31:51 +0000568 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000569 CXString fname;
570
571 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000572 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000573 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000574
Ted Kremenek16b55a72010-01-26 19:31:51 +0000575 for (i = 0; i < includeStackLen; ++i) {
576 CXFile includingFile;
577 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000578 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
579 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000580 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000581 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000582 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000583 }
584 printf("\n");
585}
586
587void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000588 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000589}
590
591/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000592/* Linkage testing. */
593/******************************************************************************/
594
595static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
596 CXClientData d) {
597 const char *linkage = 0;
598
599 if (clang_isInvalid(clang_getCursorKind(cursor)))
600 return CXChildVisit_Recurse;
601
602 switch (clang_getCursorLinkage(cursor)) {
603 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000604 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
605 case CXLinkage_Internal: linkage = "Internal"; break;
606 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
607 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000608 }
609
610 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000611 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000612 printf("linkage=%s\n", linkage);
613 }
614
615 return CXChildVisit_Recurse;
616}
617
618/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000619/* Typekind testing. */
620/******************************************************************************/
621
622static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
623 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000624 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
625 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000626 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000627 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000628 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000629 if (clang_isConstQualifiedType(T))
630 printf(" const");
631 if (clang_isVolatileQualifiedType(T))
632 printf(" volatile");
633 if (clang_isRestrictQualifiedType(T))
634 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000635 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000636 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000637 {
638 CXType CT = clang_getCanonicalType(T);
639 if (!clang_equalTypes(T, CT)) {
640 CXString CS = clang_getTypeKindSpelling(CT.kind);
641 printf(" [canonical=%s]", clang_getCString(CS));
642 clang_disposeString(CS);
643 }
644 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000645 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000646 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000647 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000648 if (RT.kind != CXType_Invalid) {
649 CXString RS = clang_getTypeKindSpelling(RT.kind);
650 printf(" [result=%s]", clang_getCString(RS));
651 clang_disposeString(RS);
652 }
653 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000654 /* Print if this is a non-POD type. */
655 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000656
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000657 printf("\n");
658 }
659 return CXChildVisit_Recurse;
660}
661
662
663/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000664/* Loading ASTs/source. */
665/******************************************************************************/
666
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000667static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000668 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000669 CXCursorVisitor Visitor,
670 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000671
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000672 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000673 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000674
675 if (Visitor) {
676 enum CXCursorKind K = CXCursor_NotImplemented;
677 enum CXCursorKind *ck = &K;
678 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000679
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000680 /* Perform some simple filtering. */
681 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000682 else if (!strcmp(filter, "all-display") ||
683 !strcmp(filter, "local-display")) {
684 ck = NULL;
685 want_display_name = 1;
686 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000687 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000688 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
689 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
690 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
691 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
692 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
693 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
694 else {
695 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
696 return 1;
697 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000698
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000699 Data.TU = TU;
700 Data.Filter = ck;
701 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000702 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000703
Ted Kremenekce2ae882010-01-26 17:59:48 +0000704 if (PV)
705 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000706
Douglas Gregora88084b2010-02-18 18:08:43 +0000707 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000708 clang_disposeTranslationUnit(TU);
709 return 0;
710}
711
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000712int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000713 const char *prefix, CXCursorVisitor Visitor,
714 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000715 CXIndex Idx;
716 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000717 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000718 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000719 !strcmp(filter, "local") ? 1 : 0,
720 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000721
Ted Kremenek020a0952010-02-11 07:41:25 +0000722 if (!CreateTranslationUnit(Idx, file, &TU)) {
723 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000724 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000725 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000726
Ted Kremenek020a0952010-02-11 07:41:25 +0000727 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
728 clang_disposeIndex(Idx);
729 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000730}
731
Ted Kremenekce2ae882010-01-26 17:59:48 +0000732int perform_test_load_source(int argc, const char **argv,
733 const char *filter, CXCursorVisitor Visitor,
734 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000735 CXIndex Idx;
736 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000737 struct CXUnsavedFile *unsaved_files = 0;
738 int num_unsaved_files = 0;
739 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000740
Daniel Dunbarada487d2009-12-01 02:03:10 +0000741 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000742 (!strcmp(filter, "local") ||
743 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000744 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000745
Ted Kremenek020a0952010-02-11 07:41:25 +0000746 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
747 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000748 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000749 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000750
Douglas Gregordca8ee82011-05-06 16:33:08 +0000751 TU = clang_parseTranslationUnit(Idx, 0,
752 argv + num_unsaved_files,
753 argc - num_unsaved_files,
754 unsaved_files, num_unsaved_files,
755 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000756 if (!TU) {
757 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000758 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000759 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000760 return 1;
761 }
762
Ted Kremenekce2ae882010-01-26 17:59:48 +0000763 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000764 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000765 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000766 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000767}
768
Douglas Gregorabc563f2010-07-19 21:46:24 +0000769int perform_test_reparse_source(int argc, const char **argv, int trials,
770 const char *filter, CXCursorVisitor Visitor,
771 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000772 CXIndex Idx;
773 CXTranslationUnit TU;
774 struct CXUnsavedFile *unsaved_files = 0;
775 int num_unsaved_files = 0;
776 int result;
777 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000778 int remap_after_trial = 0;
779 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000780
781 Idx = clang_createIndex(/* excludeDeclsFromPCH */
782 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000783 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000784
Douglas Gregorabc563f2010-07-19 21:46:24 +0000785 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
786 clang_disposeIndex(Idx);
787 return -1;
788 }
789
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000790 /* Load the initial translation unit -- we do this without honoring remapped
791 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000792 TU = clang_parseTranslationUnit(Idx, 0,
793 argv + num_unsaved_files,
794 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000795 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000796 if (!TU) {
797 fprintf(stderr, "Unable to load translation unit!\n");
798 free_remapped_files(unsaved_files, num_unsaved_files);
799 clang_disposeIndex(Idx);
800 return 1;
801 }
802
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000803 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
804 remap_after_trial =
805 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
806 }
807
Douglas Gregorabc563f2010-07-19 21:46:24 +0000808 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000809 if (clang_reparseTranslationUnit(TU,
810 trial >= remap_after_trial ? num_unsaved_files : 0,
811 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000812 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000813 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000814 clang_disposeTranslationUnit(TU);
815 free_remapped_files(unsaved_files, num_unsaved_files);
816 clang_disposeIndex(Idx);
817 return -1;
818 }
819 }
820
821 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
822 free_remapped_files(unsaved_files, num_unsaved_files);
823 clang_disposeIndex(Idx);
824 return result;
825}
826
Ted Kremenek0d435192009-11-17 18:13:31 +0000827/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000828/* Logic for testing clang_getCursor(). */
829/******************************************************************************/
830
Douglas Gregordd3e5542011-05-04 00:14:37 +0000831static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000832 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000833 unsigned end_line, unsigned end_col,
834 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000835 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000836 if (prefix)
837 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000838 PrintExtent(stdout, start_line, start_col, end_line, end_col);
839 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000840 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000841 printf("\n");
842}
843
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000844static int perform_file_scan(const char *ast_file, const char *source_file,
845 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000846 CXIndex Idx;
847 CXTranslationUnit TU;
848 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000849 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000850 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000851 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000852 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000853
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000854 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
855 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000856 fprintf(stderr, "Could not create Index\n");
857 return 1;
858 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000859
Ted Kremenek1c6da172009-11-17 19:37:36 +0000860 if (!CreateTranslationUnit(Idx, ast_file, &TU))
861 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000862
Ted Kremenek1c6da172009-11-17 19:37:36 +0000863 if ((fp = fopen(source_file, "r")) == NULL) {
864 fprintf(stderr, "Could not open '%s'\n", source_file);
865 return 1;
866 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000867
Douglas Gregorb9790342010-01-22 21:44:22 +0000868 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000869 for (;;) {
870 CXCursor cursor;
871 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000872
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000873 if (c == '\n') {
874 ++line;
875 col = 1;
876 } else
877 ++col;
878
879 /* Check the cursor at this position, and dump the previous one if we have
880 * found something new.
881 */
882 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
883 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
884 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000885 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000886 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000887 start_line = line;
888 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000889 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000890 if (c == EOF)
891 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000892
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000893 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000894 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000895
Ted Kremenek1c6da172009-11-17 19:37:36 +0000896 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000897 clang_disposeTranslationUnit(TU);
898 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000899 return 0;
900}
901
902/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000903/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000904/******************************************************************************/
905
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000906/* Parse file:line:column from the input string. Returns 0 on success, non-zero
907 on failure. If successful, the pointer *filename will contain newly-allocated
908 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000909int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000910 unsigned *column, unsigned *second_line,
911 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000912 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000913 const char *last_colon = strrchr(input, ':');
914 unsigned values[4], i;
915 unsigned num_values = (second_line && second_column)? 4 : 2;
916
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000917 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000918 if (!last_colon || last_colon == input) {
919 if (num_values == 4)
920 fprintf(stderr, "could not parse filename:line:column:line:column in "
921 "'%s'\n", input);
922 else
923 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000924 return 1;
925 }
926
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000927 for (i = 0; i != num_values; ++i) {
928 const char *prev_colon;
929
930 /* Parse the next line or column. */
931 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
932 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000933 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000934 (i % 2 ? "column" : "line"), input);
935 return 1;
936 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000937
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000938 if (i + 1 == num_values)
939 break;
940
941 /* Find the previous colon. */
942 prev_colon = last_colon - 1;
943 while (prev_colon != input && *prev_colon != ':')
944 --prev_colon;
945 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000946 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000947 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000948 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000949 }
950
951 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000952 }
953
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000954 *line = values[0];
955 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000956
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000957 if (second_line && second_column) {
958 *second_line = values[2];
959 *second_column = values[3];
960 }
961
Douglas Gregor88d23952009-11-09 18:19:57 +0000962 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000963 *filename = (char*)malloc(last_colon - input + 1);
964 memcpy(*filename, input, last_colon - input);
965 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000966 return 0;
967}
968
969const char *
970clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
971 switch (Kind) {
972 case CXCompletionChunk_Optional: return "Optional";
973 case CXCompletionChunk_TypedText: return "TypedText";
974 case CXCompletionChunk_Text: return "Text";
975 case CXCompletionChunk_Placeholder: return "Placeholder";
976 case CXCompletionChunk_Informative: return "Informative";
977 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
978 case CXCompletionChunk_LeftParen: return "LeftParen";
979 case CXCompletionChunk_RightParen: return "RightParen";
980 case CXCompletionChunk_LeftBracket: return "LeftBracket";
981 case CXCompletionChunk_RightBracket: return "RightBracket";
982 case CXCompletionChunk_LeftBrace: return "LeftBrace";
983 case CXCompletionChunk_RightBrace: return "RightBrace";
984 case CXCompletionChunk_LeftAngle: return "LeftAngle";
985 case CXCompletionChunk_RightAngle: return "RightAngle";
986 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000987 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000988 case CXCompletionChunk_Colon: return "Colon";
989 case CXCompletionChunk_SemiColon: return "SemiColon";
990 case CXCompletionChunk_Equal: return "Equal";
991 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
992 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000993 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000994
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000995 return "Unknown";
996}
997
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +0000998static int checkForErrors(CXTranslationUnit TU) {
999 unsigned Num, i;
1000 CXDiagnostic Diag;
1001 CXString DiagStr;
1002
1003 if (!getenv("CINDEXTEST_FAILONERROR"))
1004 return 0;
1005
1006 Num = clang_getNumDiagnostics(TU);
1007 for (i = 0; i != Num; ++i) {
1008 Diag = clang_getDiagnostic(TU, i);
1009 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1010 DiagStr = clang_formatDiagnostic(Diag,
1011 clang_defaultDiagnosticDisplayOptions());
1012 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1013 clang_disposeString(DiagStr);
1014 clang_disposeDiagnostic(Diag);
1015 return -1;
1016 }
1017 clang_disposeDiagnostic(Diag);
1018 }
1019
1020 return 0;
1021}
1022
Douglas Gregor3ac73852009-11-09 16:04:45 +00001023void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001024 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001025
Douglas Gregor3ac73852009-11-09 16:04:45 +00001026 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001027 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001028 CXString text;
1029 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001030 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001031 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001032
Douglas Gregor3ac73852009-11-09 16:04:45 +00001033 if (Kind == CXCompletionChunk_Optional) {
1034 fprintf(file, "{Optional ");
1035 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001036 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001037 file);
1038 fprintf(file, "}");
1039 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001040 }
1041
1042 if (Kind == CXCompletionChunk_VerticalSpace) {
1043 fprintf(file, "{VerticalSpace }");
1044 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001045 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001046
Douglas Gregord5a20892009-11-09 17:05:28 +00001047 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001048 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001049 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001050 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001051 cstr ? cstr : "");
1052 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001053 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001054
Douglas Gregor3ac73852009-11-09 16:04:45 +00001055}
1056
1057void print_completion_result(CXCompletionResult *completion_result,
1058 CXClientData client_data) {
1059 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001060 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001061 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001062
1063 fprintf(file, "%s:", clang_getCString(ks));
1064 clang_disposeString(ks);
1065
Douglas Gregor3ac73852009-11-09 16:04:45 +00001066 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001067 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001068 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001069 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1070 case CXAvailability_Available:
1071 break;
1072
1073 case CXAvailability_Deprecated:
1074 fprintf(file, " (deprecated)");
1075 break;
1076
1077 case CXAvailability_NotAvailable:
1078 fprintf(file, " (unavailable)");
1079 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001080
1081 case CXAvailability_NotAccessible:
1082 fprintf(file, " (inaccessible)");
1083 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001084 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001085
1086 annotationCount = clang_getCompletionNumAnnotations(
1087 completion_result->CompletionString);
1088 if (annotationCount) {
1089 unsigned i;
1090 fprintf(file, " (");
1091 for (i = 0; i < annotationCount; ++i) {
1092 if (i != 0)
1093 fprintf(file, ", ");
1094 fprintf(file, "\"%s\"",
1095 clang_getCString(clang_getCompletionAnnotation(
1096 completion_result->CompletionString, i)));
1097 }
1098 fprintf(file, ")");
1099 }
1100
Douglas Gregor58ddb602010-08-23 23:00:57 +00001101 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001102}
1103
Douglas Gregor3da626b2011-07-07 16:03:39 +00001104void print_completion_contexts(unsigned long long contexts, FILE *file) {
1105 fprintf(file, "Completion contexts:\n");
1106 if (contexts == CXCompletionContext_Unknown) {
1107 fprintf(file, "Unknown\n");
1108 }
1109 if (contexts & CXCompletionContext_AnyType) {
1110 fprintf(file, "Any type\n");
1111 }
1112 if (contexts & CXCompletionContext_AnyValue) {
1113 fprintf(file, "Any value\n");
1114 }
1115 if (contexts & CXCompletionContext_ObjCObjectValue) {
1116 fprintf(file, "Objective-C object value\n");
1117 }
1118 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1119 fprintf(file, "Objective-C selector value\n");
1120 }
1121 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1122 fprintf(file, "C++ class type value\n");
1123 }
1124 if (contexts & CXCompletionContext_DotMemberAccess) {
1125 fprintf(file, "Dot member access\n");
1126 }
1127 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1128 fprintf(file, "Arrow member access\n");
1129 }
1130 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1131 fprintf(file, "Objective-C property access\n");
1132 }
1133 if (contexts & CXCompletionContext_EnumTag) {
1134 fprintf(file, "Enum tag\n");
1135 }
1136 if (contexts & CXCompletionContext_UnionTag) {
1137 fprintf(file, "Union tag\n");
1138 }
1139 if (contexts & CXCompletionContext_StructTag) {
1140 fprintf(file, "Struct tag\n");
1141 }
1142 if (contexts & CXCompletionContext_ClassTag) {
1143 fprintf(file, "Class name\n");
1144 }
1145 if (contexts & CXCompletionContext_Namespace) {
1146 fprintf(file, "Namespace or namespace alias\n");
1147 }
1148 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1149 fprintf(file, "Nested name specifier\n");
1150 }
1151 if (contexts & CXCompletionContext_ObjCInterface) {
1152 fprintf(file, "Objective-C interface\n");
1153 }
1154 if (contexts & CXCompletionContext_ObjCProtocol) {
1155 fprintf(file, "Objective-C protocol\n");
1156 }
1157 if (contexts & CXCompletionContext_ObjCCategory) {
1158 fprintf(file, "Objective-C category\n");
1159 }
1160 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1161 fprintf(file, "Objective-C instance method\n");
1162 }
1163 if (contexts & CXCompletionContext_ObjCClassMessage) {
1164 fprintf(file, "Objective-C class method\n");
1165 }
1166 if (contexts & CXCompletionContext_ObjCSelectorName) {
1167 fprintf(file, "Objective-C selector name\n");
1168 }
1169 if (contexts & CXCompletionContext_MacroName) {
1170 fprintf(file, "Macro name\n");
1171 }
1172 if (contexts & CXCompletionContext_NaturalLanguage) {
1173 fprintf(file, "Natural language\n");
1174 }
1175}
1176
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001177int my_stricmp(const char *s1, const char *s2) {
1178 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001179 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001180 if (c1 < c2)
1181 return -1;
1182 else if (c1 > c2)
1183 return 1;
1184
1185 ++s1;
1186 ++s2;
1187 }
1188
1189 if (*s1)
1190 return 1;
1191 else if (*s2)
1192 return -1;
1193 return 0;
1194}
1195
Douglas Gregor1982c182010-07-12 18:38:41 +00001196int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001197 const char *input = argv[1];
1198 char *filename = 0;
1199 unsigned line;
1200 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001201 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001202 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001203 struct CXUnsavedFile *unsaved_files = 0;
1204 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001205 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001206 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001207 unsigned I, Repeats = 1;
1208 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1209
1210 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1211 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001212
Douglas Gregor1982c182010-07-12 18:38:41 +00001213 if (timing_only)
1214 input += strlen("-code-completion-timing=");
1215 else
1216 input += strlen("-code-completion-at=");
1217
Ted Kremeneke68fff62010-02-17 00:41:32 +00001218 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001219 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001220 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001221
Douglas Gregor735df882009-12-02 09:21:34 +00001222 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1223 return -1;
1224
Douglas Gregor32be4a52010-10-11 21:37:58 +00001225 CIdx = clang_createIndex(0, 0);
1226
1227 if (getenv("CINDEXTEST_EDITING"))
1228 Repeats = 5;
1229
1230 TU = clang_parseTranslationUnit(CIdx, 0,
1231 argv + num_unsaved_files + 2,
1232 argc - num_unsaved_files - 2,
1233 0, 0, getDefaultParsingOptions());
1234 if (!TU) {
1235 fprintf(stderr, "Unable to load translation unit!\n");
1236 return 1;
1237 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001238
1239 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1240 fprintf(stderr, "Unable to reparse translation init!\n");
1241 return 1;
1242 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001243
1244 for (I = 0; I != Repeats; ++I) {
1245 results = clang_codeCompleteAt(TU, filename, line, column,
1246 unsaved_files, num_unsaved_files,
1247 completionOptions);
1248 if (!results) {
1249 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001250 return 1;
1251 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001252 if (I != Repeats-1)
1253 clang_disposeCodeCompleteResults(results);
1254 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001255
Douglas Gregorec6762c2009-12-18 16:20:58 +00001256 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001257 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001258 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001259 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001260 CXString objCSelector;
1261 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001262 if (!timing_only) {
1263 /* Sort the code-completion results based on the typed text. */
1264 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1265
Douglas Gregor1982c182010-07-12 18:38:41 +00001266 for (i = 0; i != n; ++i)
1267 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001268 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001269 n = clang_codeCompleteGetNumDiagnostics(results);
1270 for (i = 0; i != n; ++i) {
1271 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1272 PrintDiagnostic(diag);
1273 clang_disposeDiagnostic(diag);
1274 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001275
1276 contexts = clang_codeCompleteGetContexts(results);
1277 print_completion_contexts(contexts, stdout);
1278
Douglas Gregor0a47d692011-07-26 15:24:30 +00001279 containerKind = clang_codeCompleteGetContainerKind(results,
1280 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001281
1282 if (containerKind != CXCursor_InvalidCode) {
1283 /* We have found a container */
1284 CXString containerUSR, containerKindSpelling;
1285 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1286 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1287 clang_disposeString(containerKindSpelling);
1288
1289 if (containerIsIncomplete) {
1290 printf("Container is incomplete\n");
1291 }
1292 else {
1293 printf("Container is complete\n");
1294 }
1295
1296 containerUSR = clang_codeCompleteGetContainerUSR(results);
1297 printf("Container USR: %s\n", clang_getCString(containerUSR));
1298 clang_disposeString(containerUSR);
1299 }
1300
Douglas Gregor0a47d692011-07-26 15:24:30 +00001301 objCSelector = clang_codeCompleteGetObjCSelector(results);
1302 selectorString = clang_getCString(objCSelector);
1303 if (selectorString && strlen(selectorString) > 0) {
1304 printf("Objective-C selector: %s\n", selectorString);
1305 }
1306 clang_disposeString(objCSelector);
1307
Douglas Gregorec6762c2009-12-18 16:20:58 +00001308 clang_disposeCodeCompleteResults(results);
1309 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001310 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001311 clang_disposeIndex(CIdx);
1312 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001313
Douglas Gregor735df882009-12-02 09:21:34 +00001314 free_remapped_files(unsaved_files, num_unsaved_files);
1315
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001316 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001317}
1318
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001319typedef struct {
1320 char *filename;
1321 unsigned line;
1322 unsigned column;
1323} CursorSourceLocation;
1324
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001325static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001326 CXIndex CIdx;
1327 int errorCode;
1328 struct CXUnsavedFile *unsaved_files = 0;
1329 int num_unsaved_files = 0;
1330 CXTranslationUnit TU;
1331 CXCursor Cursor;
1332 CursorSourceLocation *Locations = 0;
1333 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001334 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001335 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001336
Ted Kremeneke68fff62010-02-17 00:41:32 +00001337 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001338 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1339 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001340
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001341 /* Parse the locations. */
1342 assert(NumLocations > 0 && "Unable to count locations?");
1343 Locations = (CursorSourceLocation *)malloc(
1344 NumLocations * sizeof(CursorSourceLocation));
1345 for (Loc = 0; Loc < NumLocations; ++Loc) {
1346 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001347 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1348 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001349 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001350 return errorCode;
1351 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001352
1353 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001354 &num_unsaved_files))
1355 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001356
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001357 if (getenv("CINDEXTEST_EDITING"))
1358 Repeats = 5;
1359
1360 /* Parse the translation unit. When we're testing clang_getCursor() after
1361 reparsing, don't remap unsaved files until the second parse. */
1362 CIdx = clang_createIndex(1, 1);
1363 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1364 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001365 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001366 unsaved_files,
1367 Repeats > 1? 0 : num_unsaved_files,
1368 getDefaultParsingOptions());
1369
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001370 if (!TU) {
1371 fprintf(stderr, "unable to parse input\n");
1372 return -1;
1373 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001374
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001375 if (checkForErrors(TU) != 0)
1376 return -1;
1377
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001378 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001379 if (Repeats > 1 &&
1380 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1381 clang_defaultReparseOptions(TU))) {
1382 clang_disposeTranslationUnit(TU);
1383 return 1;
1384 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001385
1386 if (checkForErrors(TU) != 0)
1387 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001388
1389 for (Loc = 0; Loc < NumLocations; ++Loc) {
1390 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1391 if (!file)
1392 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001393
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001394 Cursor = clang_getCursor(TU,
1395 clang_getLocation(TU, file, Locations[Loc].line,
1396 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001397
1398 if (checkForErrors(TU) != 0)
1399 return -1;
1400
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001401 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001402 CXCompletionString completionString = clang_getCursorCompletionString(
1403 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001404 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001405 if (completionString != NULL) {
1406 printf("\nCompletion string: ");
1407 print_completion_string(completionString, stdout);
1408 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001409 printf("\n");
1410 free(Locations[Loc].filename);
1411 }
1412 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001413 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001414
Douglas Gregora88084b2010-02-18 18:08:43 +00001415 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001416 clang_disposeTranslationUnit(TU);
1417 clang_disposeIndex(CIdx);
1418 free(Locations);
1419 free_remapped_files(unsaved_files, num_unsaved_files);
1420 return 0;
1421}
1422
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001423static enum CXVisitorResult findFileRefsVisit(void *context,
1424 CXCursor cursor, CXSourceRange range) {
1425 if (clang_Range_isNull(range))
1426 return CXVisit_Continue;
1427
1428 PrintCursor(cursor);
1429 PrintRange(range, "");
1430 printf("\n");
1431 return CXVisit_Continue;
1432}
1433
1434static int find_file_refs_at(int argc, const char **argv) {
1435 CXIndex CIdx;
1436 int errorCode;
1437 struct CXUnsavedFile *unsaved_files = 0;
1438 int num_unsaved_files = 0;
1439 CXTranslationUnit TU;
1440 CXCursor Cursor;
1441 CursorSourceLocation *Locations = 0;
1442 unsigned NumLocations = 0, Loc;
1443 unsigned Repeats = 1;
1444 unsigned I;
1445
1446 /* Count the number of locations. */
1447 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1448 ++NumLocations;
1449
1450 /* Parse the locations. */
1451 assert(NumLocations > 0 && "Unable to count locations?");
1452 Locations = (CursorSourceLocation *)malloc(
1453 NumLocations * sizeof(CursorSourceLocation));
1454 for (Loc = 0; Loc < NumLocations; ++Loc) {
1455 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1456 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1457 &Locations[Loc].line,
1458 &Locations[Loc].column, 0, 0)))
1459 return errorCode;
1460 }
1461
1462 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1463 &num_unsaved_files))
1464 return -1;
1465
1466 if (getenv("CINDEXTEST_EDITING"))
1467 Repeats = 5;
1468
1469 /* Parse the translation unit. When we're testing clang_getCursor() after
1470 reparsing, don't remap unsaved files until the second parse. */
1471 CIdx = clang_createIndex(1, 1);
1472 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1473 argv + num_unsaved_files + 1 + NumLocations,
1474 argc - num_unsaved_files - 2 - NumLocations,
1475 unsaved_files,
1476 Repeats > 1? 0 : num_unsaved_files,
1477 getDefaultParsingOptions());
1478
1479 if (!TU) {
1480 fprintf(stderr, "unable to parse input\n");
1481 return -1;
1482 }
1483
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001484 if (checkForErrors(TU) != 0)
1485 return -1;
1486
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001487 for (I = 0; I != Repeats; ++I) {
1488 if (Repeats > 1 &&
1489 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1490 clang_defaultReparseOptions(TU))) {
1491 clang_disposeTranslationUnit(TU);
1492 return 1;
1493 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001494
1495 if (checkForErrors(TU) != 0)
1496 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001497
1498 for (Loc = 0; Loc < NumLocations; ++Loc) {
1499 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1500 if (!file)
1501 continue;
1502
1503 Cursor = clang_getCursor(TU,
1504 clang_getLocation(TU, file, Locations[Loc].line,
1505 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001506
1507 if (checkForErrors(TU) != 0)
1508 return -1;
1509
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001510 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001511 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001512 PrintCursor(Cursor);
1513 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001514 clang_findReferencesInFile(Cursor, file, visitor);
1515 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001516
1517 if (checkForErrors(TU) != 0)
1518 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001519 }
1520 }
1521 }
1522
1523 PrintDiagnostics(TU);
1524 clang_disposeTranslationUnit(TU);
1525 clang_disposeIndex(CIdx);
1526 free(Locations);
1527 free_remapped_files(unsaved_files, num_unsaved_files);
1528 return 0;
1529}
1530
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001531typedef struct {
1532 const char *check_prefix;
1533 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001534 int fail_for_error;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001535} IndexData;
1536
1537static void printCheck(IndexData *data) {
1538 if (data->check_prefix) {
1539 if (data->first_check_printed) {
1540 printf("// %s-NEXT: ", data->check_prefix);
1541 } else {
1542 printf("// %s : ", data->check_prefix);
1543 data->first_check_printed = 1;
1544 }
1545 }
1546}
1547
1548static void printCXIndexFile(CXIdxFile file) {
1549 CXString filename = clang_getFileName((CXFile)file);
1550 printf("%s", clang_getCString(filename));
1551 clang_disposeString(filename);
1552}
1553
1554static void printCXIndexLoc(CXIdxLoc loc) {
1555 CXString filename;
1556 const char *cname, *end;
1557 CXIdxFile file;
1558 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001559 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001560
1561 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1562 if (line == 0) {
1563 printf("<null loc>");
1564 return;
1565 }
1566 filename = clang_getFileName((CXFile)file);
1567 cname = clang_getCString(filename);
1568 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001569 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001570
1571 if (isHeader) {
1572 printCXIndexFile(file);
1573 printf(":");
1574 }
1575 printf("%d:%d", line, column);
1576}
1577
1578static CXIdxEntity makeCXIndexEntity(CXIdxIndexedEntityInfo *info) {
1579 const char *name;
1580 CXIdxLoc loc;
1581 char *newStr;
1582 CXIdxFile file;
1583 unsigned line, column;
1584
1585 name = info->entityInfo->name;
1586 if (!name)
1587 name = "<anon-tag>";
1588
1589 loc = info->declInfo->loc;
1590 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001591 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001592 newStr = (char *)malloc(strlen(name) + 10);
1593 sprintf(newStr, "%s:%d:%d", name, line, column);
1594 return (CXIdxEntity)newStr;
1595}
1596
1597static CXIdxContainer makeCXIndexContainer(CXIdxEntity entity) {
1598 return (CXIdxContainer)entity;
1599}
1600
1601static void printCXIndexEntity(CXIdxEntity entity) {
1602 printf("{%s}", (const char *)entity);
1603}
1604
1605static void printCXIndexContainer(CXIdxContainer container) {
1606 printf("[%s]", (const char *)container);
1607}
1608
1609static void printIndexedDeclInfo(CXIdxIndexedDeclInfo *info) {
1610 printf(" | cursor: ");
1611 PrintCursor(info->cursor);
1612 printf(" | loc: ");
1613 printCXIndexLoc(info->loc);
1614 printf(" | container: ");
1615 printCXIndexContainer(info->container);
1616}
1617
1618static void printIndexedEntityInfo(const char *cb,
1619 CXClientData client_data,
1620 CXIdxIndexedEntityInfo *info) {
1621 const char *name;
1622 IndexData *index_data;
1623 index_data = (IndexData *)client_data;
1624 printCheck(index_data);
1625
1626 name = info->entityInfo->name;
1627 if (!name)
1628 name = "<anon-tag>";
1629
1630 printf("%s: %s", cb, info->entityInfo->name);
1631 printIndexedDeclInfo(info->declInfo);
1632 printf(" | USR: %s", info->entityInfo->USR);
1633}
1634
1635static void printIndexedRedeclInfo(const char *cb,
1636 CXClientData client_data,
1637 CXIdxIndexedRedeclInfo *info) {
1638 IndexData *index_data;
1639 index_data = (IndexData *)client_data;
1640 printCheck(index_data);
1641
1642 printf("%s redeclaration: ", cb);
1643 printCXIndexEntity(info->entity);
1644 printIndexedDeclInfo(info->declInfo);
1645}
1646
1647static void printStartedContainerInfo(const char *cb,
1648 CXClientData client_data,
1649 CXIdxContainerInfo *info) {
1650 IndexData *index_data;
1651 index_data = (IndexData *)client_data;
1652 printCheck(index_data);
1653
1654 printf("started %s: ", cb);
1655 printCXIndexEntity(info->entity);
1656 printf(" | cursor: ");
1657 PrintCursor(info->cursor);
1658 printf(" | loc: ");
1659 printCXIndexLoc(info->loc);
1660}
1661
1662static void index_diagnostic(CXClientData client_data,
1663 CXDiagnostic diag, void *reserved) {
1664 CXString str;
1665 const char *cstr;
1666 IndexData *index_data;
1667 index_data = (IndexData *)client_data;
1668 printCheck(index_data);
1669
1670 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1671 cstr = clang_getCString(str);
Argyrios Kyrtzidisc0f5b752011-10-18 15:13:14 +00001672 printf("diagnostic: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001673 clang_disposeString(str);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001674
1675 if (getenv("CINDEXTEST_FAILONERROR") &&
1676 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
1677 index_data->fail_for_error = 1;
1678 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001679}
1680
1681static CXIdxFile index_recordFile(CXClientData client_data,
1682 CXFile file, void *reserved) {
1683 return (CXIdxFile)file;
1684}
1685
1686static void index_ppIncludedFile(CXClientData client_data,
1687 CXIdxIncludedFileInfo *info) {
1688 IndexData *index_data;
1689 index_data = (IndexData *)client_data;
1690 printCheck(index_data);
1691
1692 printf("included file: ");
1693 printCXIndexFile(info->file);
1694 printf(" | name: \"%s\"", info->filename);
1695 printf(" | hash loc: ");
1696 printCXIndexLoc(info->hashLoc);
1697 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
1698}
1699
1700static CXIdxMacro index_ppMacroDefined(CXClientData client_data,
1701 CXIdxMacroDefinedInfo *info) {
1702 IndexData *index_data;
1703 index_data = (IndexData *)client_data;
1704 printCheck(index_data);
1705
1706 printf("macro defined: %s", info->macroInfo->name);
1707 printf(" | loc: ");
1708 printCXIndexLoc(info->macroInfo->loc);
1709 printf(" | defBegin: ");
1710 printCXIndexLoc(info->defBegin);
1711 printf(" | length: %d\n", info->defLength);
1712
1713 return (CXIdxMacro)info->macroInfo->name;
1714}
1715
1716static void index_ppMacroUndefined(CXClientData client_data,
1717 CXIdxMacroUndefinedInfo *info) {
1718 IndexData *index_data;
1719 index_data = (IndexData *)client_data;
1720 printCheck(index_data);
1721
1722 printf("macro undefined: %s", info->name);
1723 printf(" | loc: ");
1724 printCXIndexLoc(info->loc);
1725 printf("\n");
1726}
1727
1728static void index_ppMacroExpanded(CXClientData client_data,
1729 CXIdxMacroExpandedInfo *info) {
1730 IndexData *index_data;
1731 index_data = (IndexData *)client_data;
1732 printCheck(index_data);
1733
1734 printf("macro expanded: %s", info->name);
1735 printf(" | loc: ");
1736 printCXIndexLoc(info->loc);
1737 printf("\n");
1738}
1739
1740static CXIdxEntity index_importedEntity(CXClientData client_data,
1741 CXIdxImportedEntityInfo *info) {
1742 IndexData *index_data;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001743 CXIdxIndexedDeclInfo DeclInfo;
1744 CXIdxIndexedEntityInfo EntityInfo;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001745 const char *name;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001746 DeclInfo.cursor = info->cursor;
1747 DeclInfo.loc = info->loc;
1748 DeclInfo.container = 0;
1749 EntityInfo.entityInfo = info->entityInfo;
1750 EntityInfo.declInfo = &DeclInfo;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001751 index_data = (IndexData *)client_data;
1752 printCheck(index_data);
1753
1754 name = info->entityInfo->name;
1755 if (!name)
1756 name = "<anon-tag>";
1757
1758 printf("imported entity: %s", name);
1759 printf(" | cursor: ");
1760 PrintCursor(info->cursor);
1761 printf(" | loc: ");
1762 printCXIndexLoc(info->loc);
1763 printf("\n");
1764
1765 return makeCXIndexEntity(&EntityInfo);
1766}
1767
1768static CXIdxContainer index_startedTranslationUnit(CXClientData client_data,
1769 void *reserved) {
1770 IndexData *index_data;
1771 index_data = (IndexData *)client_data;
1772 printCheck(index_data);
1773
1774 printf("started TU\n");
1775 return (CXIdxContainer)"TU";
1776}
1777
1778static CXIdxEntity index_indexTypedef(CXClientData client_data,
1779 CXIdxTypedefInfo *info) {
1780 printIndexedEntityInfo("typedef", client_data, info->indexedEntityInfo);
1781 printf("\n");
1782
1783 return makeCXIndexEntity(info->indexedEntityInfo);
1784}
1785
1786static CXIdxEntity index_indexFunction(CXClientData client_data,
1787 CXIdxFunctionInfo *info) {
1788 printIndexedEntityInfo("function", client_data, info->indexedEntityInfo);
1789 printf(" | isDefinition: %d\n", info->isDefinition);
1790
1791 return makeCXIndexEntity(info->indexedEntityInfo);
1792}
1793
1794static void index_indexFunctionRedeclaration(CXClientData client_data,
1795 CXIdxFunctionRedeclInfo *info) {
1796 printIndexedRedeclInfo("function", client_data, info->indexedRedeclInfo);
1797 printf(" | isDefinition: %d\n", info->isDefinition);
1798}
1799
1800static CXIdxEntity index_indexVariable(CXClientData client_data,
1801 CXIdxVariableInfo *info) {
1802 printIndexedEntityInfo("variable", client_data, info->indexedEntityInfo);
1803 printf(" | isDefinition: %d\n", info->isDefinition);
1804
1805 return makeCXIndexEntity(info->indexedEntityInfo);
1806}
1807
1808static void index_indexVariableRedeclaration(CXClientData client_data,
1809 CXIdxVariableRedeclInfo *info) {
1810 printIndexedRedeclInfo("variable", client_data, info->indexedRedeclInfo);
1811 printf(" | isDefinition: %d\n", info->isDefinition);
1812}
1813
1814static CXIdxEntity index_indexTagType(CXClientData client_data,
1815 CXIdxTagTypeInfo *info) {
1816 printIndexedEntityInfo("tag type", client_data, info->indexedEntityInfo);
1817 printf(" | isDefinition: %d | anon: %d\n",
1818 info->isDefinition, info->isAnonymous);
1819
1820 return makeCXIndexEntity(info->indexedEntityInfo);
1821}
1822
1823static void index_indexTagTypeRedeclaration(CXClientData client_data,
1824 CXIdxTagTypeRedeclInfo *info) {
1825 printIndexedRedeclInfo("tag type", client_data, info->indexedRedeclInfo);
1826 printf(" | isDefinition: %d\n", info->isDefinition);
1827}
1828
1829static CXIdxEntity index_indexField(CXClientData client_data,
1830 CXIdxFieldInfo *info) {
1831 printIndexedEntityInfo("field", client_data, info->indexedEntityInfo);
1832 printf("\n");
1833
1834 return makeCXIndexEntity(info->indexedEntityInfo);
1835}
1836
1837static CXIdxEntity index_indexEnumerator(CXClientData client_data,
1838 CXIdxEnumeratorInfo *info) {
1839 printIndexedEntityInfo("enumerator", client_data, info->indexedEntityInfo);
1840 printf("\n");
1841
1842 return makeCXIndexEntity(info->indexedEntityInfo);
1843}
1844
1845static CXIdxContainer
1846index_startedTagTypeDefinition(CXClientData client_data,
1847 CXIdxTagTypeDefinitionInfo *info) {
1848 printStartedContainerInfo("tag type definition", client_data,
1849 info->containerInfo);
1850 printf("\n");
1851
1852 return makeCXIndexContainer(info->containerInfo->entity);
1853}
1854
1855static CXIdxEntity index_indexObjCClass(CXClientData client_data,
1856 CXIdxObjCClassInfo *info) {
1857 printIndexedEntityInfo("ObjC class", client_data, info->indexedEntityInfo);
1858 printf(" | forward ref: %d\n", info->isForwardRef);
1859
1860 return makeCXIndexEntity(info->indexedEntityInfo);
1861}
1862
1863static CXIdxEntity index_indexObjCProtocol(CXClientData client_data,
1864 CXIdxObjCProtocolInfo *info) {
1865 printIndexedEntityInfo("ObjC protocol", client_data,
1866 info->indexedEntityInfo);
1867 printf(" | forward ref: %d\n", info->isForwardRef);
1868
1869 return makeCXIndexEntity(info->indexedEntityInfo);
1870}
1871
1872static CXIdxEntity index_indexObjCCategory(CXClientData client_data,
1873 CXIdxObjCCategoryInfo *info) {
1874 printIndexedEntityInfo("ObjC category", client_data,
1875 info->indexedEntityInfo);
1876 printf(" | class: ");
1877 printCXIndexEntity(info->objcClass);
1878 printf("\n");
1879
1880 return makeCXIndexEntity(info->indexedEntityInfo);
1881}
1882
1883static CXIdxEntity index_indexObjCMethod(CXClientData client_data,
1884 CXIdxObjCMethodInfo *info) {
1885 printIndexedEntityInfo("ObjC Method", client_data, info->indexedEntityInfo);
1886 printf(" | isDefinition: %d\n", info->isDefinition);
1887
1888 return makeCXIndexEntity(info->indexedEntityInfo);
1889}
1890
1891static CXIdxEntity index_indexObjCProperty(CXClientData client_data,
1892 CXIdxObjCPropertyInfo *info) {
1893 printIndexedEntityInfo("ObjC property", client_data, info->indexedEntityInfo);
1894 printf("\n");
1895
1896 return makeCXIndexEntity(info->indexedEntityInfo);
1897}
1898
1899static void index_indexObjCMethodRedeclaration(CXClientData client_data,
1900 CXIdxObjCMethodRedeclInfo *info) {
1901 printIndexedRedeclInfo("ObjC Method", client_data, info->indexedRedeclInfo);
1902 printf(" | isDefinition: %d\n", info->isDefinition);
1903}
1904
1905static CXIdxContainer
1906index_startedStatementBody(CXClientData client_data,
1907 CXIdxStmtBodyInfo *info) {
1908 printStartedContainerInfo("body", client_data, info->containerInfo);
1909 printf(" | body: ");
1910 printCXIndexLoc(info->bodyBegin);
1911 printf("\n");
1912
1913 return makeCXIndexContainer(info->containerInfo->entity);
1914}
1915
1916static CXIdxContainer
1917index_startedObjCContainer(CXClientData client_data,
1918 CXIdxObjCContainerInfo *info) {
1919 printStartedContainerInfo("ObjC container", client_data, info->containerInfo);
1920 printf("\n");
1921
1922 return makeCXIndexContainer(info->containerInfo->entity);
1923}
1924
1925static void index_defineObjCClass(CXClientData client_data,
1926 CXIdxObjCClassDefineInfo *info) {
1927 IndexData *index_data;
1928 index_data = (IndexData *)client_data;
1929 printCheck(index_data);
1930
1931 printf("define objc class: ");
1932 printCXIndexEntity(info->objcClass);
1933 printf(" | cursor: ");
1934 PrintCursor(info->cursor);
1935 printf(" | container: ");
1936 printCXIndexContainer(info->container);
1937
1938 if (info->baseInfo) {
1939 printf(" | base: ");
1940 printCXIndexEntity(info->baseInfo->objcClass);
1941 printf(" | base loc: ");
1942 printCXIndexLoc(info->baseInfo->loc);
1943 }
1944
1945 printf("\n");
1946}
1947
1948static void index_endedContainer(CXClientData client_data,
1949 CXIdxEndContainerInfo *info) {
1950 IndexData *index_data;
1951 index_data = (IndexData *)client_data;
1952 printCheck(index_data);
1953
1954 printf("ended container: ");
1955 printCXIndexContainer(info->container);
1956 printf(" | end: ");
1957 printCXIndexLoc(info->endLoc);
1958 printf("\n");
1959}
1960
1961static void index_indexEntityReference(CXClientData client_data,
1962 CXIdxEntityRefInfo *info) {
1963 IndexData *index_data;
1964 index_data = (IndexData *)client_data;
1965 printCheck(index_data);
1966
1967 printf("reference: ");
1968 printCXIndexEntity(info->referencedEntity);
1969 printf(" | cursor: ");
1970 PrintCursor(info->cursor);
1971 printf(" | loc: ");
1972 printCXIndexLoc(info->loc);
1973 printf(" | parent: ");
1974 printCXIndexEntity(info->parentEntity);
1975 printf(" | container: ");
1976 printCXIndexContainer(info->container);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001977 printf(" | kind: ");
1978 switch (info->kind) {
1979 case CXIdxEntityRef_Direct: printf("direct"); break;
1980 case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break;
1981 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001982 printf("\n");
1983}
1984
1985static IndexerCallbacks IndexCB = {
1986 index_diagnostic,
1987 index_recordFile,
1988 index_ppIncludedFile,
1989 index_ppMacroDefined,
1990 index_ppMacroUndefined,
1991 index_ppMacroExpanded,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001992 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001993 index_importedEntity,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001994 0,/*index_importedMacro,*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001995 index_startedTranslationUnit,
1996 index_indexTypedef,
1997 index_indexFunction,
1998 index_indexFunctionRedeclaration,
1999 index_indexVariable,
2000 index_indexVariableRedeclaration,
2001 index_indexTagType,
2002 index_indexTagTypeRedeclaration,
2003 index_indexField,
2004 index_indexEnumerator,
2005 index_startedTagTypeDefinition,
2006 index_indexObjCClass,
2007 index_indexObjCProtocol,
2008 index_indexObjCCategory,
2009 index_indexObjCMethod,
2010 index_indexObjCProperty,
2011 index_indexObjCMethodRedeclaration,
2012 index_startedStatementBody,
2013 index_startedObjCContainer,
2014 index_defineObjCClass,
2015 index_endedContainer,
2016 index_indexEntityReference
2017};
2018
2019static int index_file(int argc, const char **argv) {
2020 const char *check_prefix;
2021 CXIndex CIdx;
2022 IndexData index_data;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002023 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002024
2025 check_prefix = 0;
2026 if (argc > 0) {
2027 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2028 check_prefix = argv[0] + strlen("-check-prefix=");
2029 ++argv;
2030 --argc;
2031 }
2032 }
2033
2034 if (argc == 0) {
2035 fprintf(stderr, "no compiler arguments\n");
2036 return -1;
2037 }
2038
2039 CIdx = clang_createIndex(0, 1);
2040 index_data.check_prefix = check_prefix;
2041 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002042 index_data.fail_for_error = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002043
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002044 result = clang_indexTranslationUnit(CIdx, &index_data,
2045 &IndexCB,sizeof(IndexCB),
2046 0, 0, argv, argc, 0, 0, 0, 0);
2047 if (index_data.fail_for_error)
2048 return -1;
2049
2050 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002051}
2052
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002053int perform_token_annotation(int argc, const char **argv) {
2054 const char *input = argv[1];
2055 char *filename = 0;
2056 unsigned line, second_line;
2057 unsigned column, second_column;
2058 CXIndex CIdx;
2059 CXTranslationUnit TU = 0;
2060 int errorCode;
2061 struct CXUnsavedFile *unsaved_files = 0;
2062 int num_unsaved_files = 0;
2063 CXToken *tokens;
2064 unsigned num_tokens;
2065 CXSourceRange range;
2066 CXSourceLocation startLoc, endLoc;
2067 CXFile file = 0;
2068 CXCursor *cursors = 0;
2069 unsigned i;
2070
2071 input += strlen("-test-annotate-tokens=");
2072 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2073 &second_line, &second_column)))
2074 return errorCode;
2075
2076 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2077 return -1;
2078
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002079 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002080 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2081 argv + num_unsaved_files + 2,
2082 argc - num_unsaved_files - 3,
2083 unsaved_files,
2084 num_unsaved_files,
2085 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002086 if (!TU) {
2087 fprintf(stderr, "unable to parse input\n");
2088 clang_disposeIndex(CIdx);
2089 free(filename);
2090 free_remapped_files(unsaved_files, num_unsaved_files);
2091 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002092 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002093 errorCode = 0;
2094
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002095 if (checkForErrors(TU) != 0)
2096 return -1;
2097
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002098 if (getenv("CINDEXTEST_EDITING")) {
2099 for (i = 0; i < 5; ++i) {
2100 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2101 clang_defaultReparseOptions(TU))) {
2102 fprintf(stderr, "Unable to reparse translation unit!\n");
2103 errorCode = -1;
2104 goto teardown;
2105 }
2106 }
2107 }
2108
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002109 if (checkForErrors(TU) != 0) {
2110 errorCode = -1;
2111 goto teardown;
2112 }
2113
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002114 file = clang_getFile(TU, filename);
2115 if (!file) {
2116 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2117 errorCode = -1;
2118 goto teardown;
2119 }
2120
2121 startLoc = clang_getLocation(TU, file, line, column);
2122 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002123 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002124 column);
2125 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002126 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002127 }
2128
2129 endLoc = clang_getLocation(TU, file, second_line, second_column);
2130 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002131 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002132 second_line, second_column);
2133 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002134 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002135 }
2136
2137 range = clang_getRange(startLoc, endLoc);
2138 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002139
2140 if (checkForErrors(TU) != 0) {
2141 errorCode = -1;
2142 goto teardown;
2143 }
2144
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002145 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2146 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002147
2148 if (checkForErrors(TU) != 0) {
2149 errorCode = -1;
2150 goto teardown;
2151 }
2152
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002153 for (i = 0; i != num_tokens; ++i) {
2154 const char *kind = "<unknown>";
2155 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2156 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2157 unsigned start_line, start_column, end_line, end_column;
2158
2159 switch (clang_getTokenKind(tokens[i])) {
2160 case CXToken_Punctuation: kind = "Punctuation"; break;
2161 case CXToken_Keyword: kind = "Keyword"; break;
2162 case CXToken_Identifier: kind = "Identifier"; break;
2163 case CXToken_Literal: kind = "Literal"; break;
2164 case CXToken_Comment: kind = "Comment"; break;
2165 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002166 clang_getSpellingLocation(clang_getRangeStart(extent),
2167 0, &start_line, &start_column, 0);
2168 clang_getSpellingLocation(clang_getRangeEnd(extent),
2169 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002170 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2171 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002172 if (!clang_isInvalid(cursors[i].kind)) {
2173 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002174 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002175 }
2176 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002177 }
2178 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002179 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002180
2181 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002182 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002183 clang_disposeTranslationUnit(TU);
2184 clang_disposeIndex(CIdx);
2185 free(filename);
2186 free_remapped_files(unsaved_files, num_unsaved_files);
2187 return errorCode;
2188}
2189
Ted Kremenek0d435192009-11-17 18:13:31 +00002190/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002191/* USR printing. */
2192/******************************************************************************/
2193
2194static int insufficient_usr(const char *kind, const char *usage) {
2195 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2196 return 1;
2197}
2198
2199static unsigned isUSR(const char *s) {
2200 return s[0] == 'c' && s[1] == ':';
2201}
2202
2203static int not_usr(const char *s, const char *arg) {
2204 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2205 return 1;
2206}
2207
2208static void print_usr(CXString usr) {
2209 const char *s = clang_getCString(usr);
2210 printf("%s\n", s);
2211 clang_disposeString(usr);
2212}
2213
2214static void display_usrs() {
2215 fprintf(stderr, "-print-usrs options:\n"
2216 " ObjCCategory <class name> <category name>\n"
2217 " ObjCClass <class name>\n"
2218 " ObjCIvar <ivar name> <class USR>\n"
2219 " ObjCMethod <selector> [0=class method|1=instance method] "
2220 "<class USR>\n"
2221 " ObjCProperty <property name> <class USR>\n"
2222 " ObjCProtocol <protocol name>\n");
2223}
2224
2225int print_usrs(const char **I, const char **E) {
2226 while (I != E) {
2227 const char *kind = *I;
2228 unsigned len = strlen(kind);
2229 switch (len) {
2230 case 8:
2231 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2232 if (I + 2 >= E)
2233 return insufficient_usr(kind, "<ivar name> <class USR>");
2234 if (!isUSR(I[2]))
2235 return not_usr("<class USR>", I[2]);
2236 else {
2237 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002238 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002239 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002240 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2241 }
2242
2243 I += 3;
2244 continue;
2245 }
2246 break;
2247 case 9:
2248 if (memcmp(kind, "ObjCClass", 9) == 0) {
2249 if (I + 1 >= E)
2250 return insufficient_usr(kind, "<class name>");
2251 print_usr(clang_constructUSR_ObjCClass(I[1]));
2252 I += 2;
2253 continue;
2254 }
2255 break;
2256 case 10:
2257 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2258 if (I + 3 >= E)
2259 return insufficient_usr(kind, "<method selector> "
2260 "[0=class method|1=instance method] <class USR>");
2261 if (!isUSR(I[3]))
2262 return not_usr("<class USR>", I[3]);
2263 else {
2264 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002265 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002266 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002267 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2268 }
2269 I += 4;
2270 continue;
2271 }
2272 break;
2273 case 12:
2274 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2275 if (I + 2 >= E)
2276 return insufficient_usr(kind, "<class name> <category name>");
2277 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2278 I += 3;
2279 continue;
2280 }
2281 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2282 if (I + 1 >= E)
2283 return insufficient_usr(kind, "<protocol name>");
2284 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2285 I += 2;
2286 continue;
2287 }
2288 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2289 if (I + 2 >= E)
2290 return insufficient_usr(kind, "<property name> <class USR>");
2291 if (!isUSR(I[2]))
2292 return not_usr("<class USR>", I[2]);
2293 else {
2294 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002295 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002296 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002297 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2298 }
2299 I += 3;
2300 continue;
2301 }
2302 break;
2303 default:
2304 break;
2305 }
2306 break;
2307 }
2308
2309 if (I != E) {
2310 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2311 display_usrs();
2312 return 1;
2313 }
2314 return 0;
2315}
2316
2317int print_usrs_file(const char *file_name) {
2318 char line[2048];
2319 const char *args[128];
2320 unsigned numChars = 0;
2321
2322 FILE *fp = fopen(file_name, "r");
2323 if (!fp) {
2324 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2325 return 1;
2326 }
2327
2328 /* This code is not really all that safe, but it works fine for testing. */
2329 while (!feof(fp)) {
2330 char c = fgetc(fp);
2331 if (c == '\n') {
2332 unsigned i = 0;
2333 const char *s = 0;
2334
2335 if (numChars == 0)
2336 continue;
2337
2338 line[numChars] = '\0';
2339 numChars = 0;
2340
2341 if (line[0] == '/' && line[1] == '/')
2342 continue;
2343
2344 s = strtok(line, " ");
2345 while (s) {
2346 args[i] = s;
2347 ++i;
2348 s = strtok(0, " ");
2349 }
2350 if (print_usrs(&args[0], &args[i]))
2351 return 1;
2352 }
2353 else
2354 line[numChars++] = c;
2355 }
2356
2357 fclose(fp);
2358 return 0;
2359}
2360
2361/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002362/* Command line processing. */
2363/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002364int write_pch_file(const char *filename, int argc, const char *argv[]) {
2365 CXIndex Idx;
2366 CXTranslationUnit TU;
2367 struct CXUnsavedFile *unsaved_files = 0;
2368 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002369 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002370
2371 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2372
2373 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2374 clang_disposeIndex(Idx);
2375 return -1;
2376 }
2377
2378 TU = clang_parseTranslationUnit(Idx, 0,
2379 argv + num_unsaved_files,
2380 argc - num_unsaved_files,
2381 unsaved_files,
2382 num_unsaved_files,
2383 CXTranslationUnit_Incomplete);
2384 if (!TU) {
2385 fprintf(stderr, "Unable to load translation unit!\n");
2386 free_remapped_files(unsaved_files, num_unsaved_files);
2387 clang_disposeIndex(Idx);
2388 return 1;
2389 }
2390
Douglas Gregor39c411f2011-07-06 16:43:36 +00002391 switch (clang_saveTranslationUnit(TU, filename,
2392 clang_defaultSaveOptions(TU))) {
2393 case CXSaveError_None:
2394 break;
2395
2396 case CXSaveError_TranslationErrors:
2397 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2398 filename);
2399 result = 2;
2400 break;
2401
2402 case CXSaveError_InvalidTU:
2403 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2404 filename);
2405 result = 3;
2406 break;
2407
2408 case CXSaveError_Unknown:
2409 default:
2410 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2411 result = 1;
2412 break;
2413 }
2414
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002415 clang_disposeTranslationUnit(TU);
2416 free_remapped_files(unsaved_files, num_unsaved_files);
2417 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002418 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002419}
2420
2421/******************************************************************************/
2422/* Command line processing. */
2423/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002424
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002425static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002426 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002427 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002428 if (strcmp(s, "-usrs") == 0)
2429 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002430 if (strncmp(s, "-memory-usage", 13) == 0)
2431 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002432 return NULL;
2433}
2434
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002435static void print_usage(void) {
2436 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002437 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002438 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002439 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002440 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002441 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002442 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002443 "[FileCheck prefix]\n");
2444 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002445 " c-index-test -test-load-tu <AST file> <symbol filter> "
2446 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002447 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2448 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002449 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002450 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002451 " c-index-test -test-load-source-memory-usage "
2452 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002453 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2454 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002455 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002456 " c-index-test -test-load-source-usrs-memory-usage "
2457 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002458 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2459 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002460 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002461 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002462 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002463 " c-index-test -test-print-typekind {<args>}*\n"
2464 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002465 " c-index-test -print-usr-file <file>\n"
2466 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002467 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002468 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002469 " all - load all symbols, including those from PCH\n"
2470 " local - load all symbols except those in PCH\n"
2471 " category - only load ObjC categories (non-PCH)\n"
2472 " interface - only load ObjC interfaces (non-PCH)\n"
2473 " protocol - only load ObjC protocols (non-PCH)\n"
2474 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002475 " typedef - only load typdefs (non-PCH)\n"
2476 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002477}
2478
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002479/***/
2480
2481int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002482 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002483 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002484 return perform_code_completion(argc, argv, 0);
2485 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2486 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002487 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2488 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002489 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2490 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002491 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2492 return index_file(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002493 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002494 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002495 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002496 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2497 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002498 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002499 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2500 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2501 if (I) {
2502 int trials = atoi(argv[2]);
2503 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2504 NULL);
2505 }
2506 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002507 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002508 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002509
2510 PostVisitTU postVisit = 0;
2511 if (strstr(argv[1], "-memory-usage"))
2512 postVisit = PrintMemoryUsage;
2513
Ted Kremenek7d405622010-01-12 23:34:26 +00002514 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002515 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2516 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002517 }
2518 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002519 return perform_file_scan(argv[2], argv[3],
2520 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002521 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2522 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002523 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2524 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2525 PrintInclusionStack);
2526 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2527 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2528 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002529 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2530 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2531 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002532 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2533 return perform_test_load_source(argc - 2, argv + 2, "all",
2534 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002535 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2536 if (argc > 2)
2537 return print_usrs(argv + 2, argv + argc);
2538 else {
2539 display_usrs();
2540 return 1;
2541 }
2542 }
2543 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2544 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002545 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2546 return write_pch_file(argv[2], argc - 3, argv + 3);
2547
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002548 print_usage();
2549 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002550}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002551
2552/***/
2553
2554/* We intentionally run in a separate thread to ensure we at least minimal
2555 * testing of a multithreaded environment (for example, having a reduced stack
2556 * size). */
2557
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002558typedef struct thread_info {
2559 int argc;
2560 const char **argv;
2561 int result;
2562} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002563void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002564 thread_info *client_data = client_data_v;
2565 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002566}
2567
2568int main(int argc, const char **argv) {
2569 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002570
Douglas Gregor61605982010-10-27 16:00:01 +00002571 if (getenv("CINDEXTEST_NOTHREADS"))
2572 return cindextest_main(argc, argv);
2573
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002574 client_data.argc = argc;
2575 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002576 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002577 return client_data.result;
2578}