blob: 063c4adc9033ddbfdf5fa022bc1048134ff6bfeb [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
Douglas Gregor3ac73852009-11-09 16:04:45 +0000998void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000999 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001000
Douglas Gregor3ac73852009-11-09 16:04:45 +00001001 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001002 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001003 CXString text;
1004 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001005 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001006 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001007
Douglas Gregor3ac73852009-11-09 16:04:45 +00001008 if (Kind == CXCompletionChunk_Optional) {
1009 fprintf(file, "{Optional ");
1010 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001011 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001012 file);
1013 fprintf(file, "}");
1014 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001015 }
1016
1017 if (Kind == CXCompletionChunk_VerticalSpace) {
1018 fprintf(file, "{VerticalSpace }");
1019 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001020 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001021
Douglas Gregord5a20892009-11-09 17:05:28 +00001022 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001023 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001024 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001025 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001026 cstr ? cstr : "");
1027 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001028 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001029
Douglas Gregor3ac73852009-11-09 16:04:45 +00001030}
1031
1032void print_completion_result(CXCompletionResult *completion_result,
1033 CXClientData client_data) {
1034 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001035 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
1036
1037 fprintf(file, "%s:", clang_getCString(ks));
1038 clang_disposeString(ks);
1039
Douglas Gregor3ac73852009-11-09 16:04:45 +00001040 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001041 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001042 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001043 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1044 case CXAvailability_Available:
1045 break;
1046
1047 case CXAvailability_Deprecated:
1048 fprintf(file, " (deprecated)");
1049 break;
1050
1051 case CXAvailability_NotAvailable:
1052 fprintf(file, " (unavailable)");
1053 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001054
1055 case CXAvailability_NotAccessible:
1056 fprintf(file, " (inaccessible)");
1057 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001058 }
1059 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001060}
1061
Douglas Gregor3da626b2011-07-07 16:03:39 +00001062void print_completion_contexts(unsigned long long contexts, FILE *file) {
1063 fprintf(file, "Completion contexts:\n");
1064 if (contexts == CXCompletionContext_Unknown) {
1065 fprintf(file, "Unknown\n");
1066 }
1067 if (contexts & CXCompletionContext_AnyType) {
1068 fprintf(file, "Any type\n");
1069 }
1070 if (contexts & CXCompletionContext_AnyValue) {
1071 fprintf(file, "Any value\n");
1072 }
1073 if (contexts & CXCompletionContext_ObjCObjectValue) {
1074 fprintf(file, "Objective-C object value\n");
1075 }
1076 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1077 fprintf(file, "Objective-C selector value\n");
1078 }
1079 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1080 fprintf(file, "C++ class type value\n");
1081 }
1082 if (contexts & CXCompletionContext_DotMemberAccess) {
1083 fprintf(file, "Dot member access\n");
1084 }
1085 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1086 fprintf(file, "Arrow member access\n");
1087 }
1088 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1089 fprintf(file, "Objective-C property access\n");
1090 }
1091 if (contexts & CXCompletionContext_EnumTag) {
1092 fprintf(file, "Enum tag\n");
1093 }
1094 if (contexts & CXCompletionContext_UnionTag) {
1095 fprintf(file, "Union tag\n");
1096 }
1097 if (contexts & CXCompletionContext_StructTag) {
1098 fprintf(file, "Struct tag\n");
1099 }
1100 if (contexts & CXCompletionContext_ClassTag) {
1101 fprintf(file, "Class name\n");
1102 }
1103 if (contexts & CXCompletionContext_Namespace) {
1104 fprintf(file, "Namespace or namespace alias\n");
1105 }
1106 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1107 fprintf(file, "Nested name specifier\n");
1108 }
1109 if (contexts & CXCompletionContext_ObjCInterface) {
1110 fprintf(file, "Objective-C interface\n");
1111 }
1112 if (contexts & CXCompletionContext_ObjCProtocol) {
1113 fprintf(file, "Objective-C protocol\n");
1114 }
1115 if (contexts & CXCompletionContext_ObjCCategory) {
1116 fprintf(file, "Objective-C category\n");
1117 }
1118 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1119 fprintf(file, "Objective-C instance method\n");
1120 }
1121 if (contexts & CXCompletionContext_ObjCClassMessage) {
1122 fprintf(file, "Objective-C class method\n");
1123 }
1124 if (contexts & CXCompletionContext_ObjCSelectorName) {
1125 fprintf(file, "Objective-C selector name\n");
1126 }
1127 if (contexts & CXCompletionContext_MacroName) {
1128 fprintf(file, "Macro name\n");
1129 }
1130 if (contexts & CXCompletionContext_NaturalLanguage) {
1131 fprintf(file, "Natural language\n");
1132 }
1133}
1134
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001135int my_stricmp(const char *s1, const char *s2) {
1136 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001137 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001138 if (c1 < c2)
1139 return -1;
1140 else if (c1 > c2)
1141 return 1;
1142
1143 ++s1;
1144 ++s2;
1145 }
1146
1147 if (*s1)
1148 return 1;
1149 else if (*s2)
1150 return -1;
1151 return 0;
1152}
1153
Douglas Gregor1982c182010-07-12 18:38:41 +00001154int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001155 const char *input = argv[1];
1156 char *filename = 0;
1157 unsigned line;
1158 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001159 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001160 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001161 struct CXUnsavedFile *unsaved_files = 0;
1162 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001163 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001164 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001165 unsigned I, Repeats = 1;
1166 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1167
1168 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1169 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001170
Douglas Gregor1982c182010-07-12 18:38:41 +00001171 if (timing_only)
1172 input += strlen("-code-completion-timing=");
1173 else
1174 input += strlen("-code-completion-at=");
1175
Ted Kremeneke68fff62010-02-17 00:41:32 +00001176 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001177 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001178 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001179
Douglas Gregor735df882009-12-02 09:21:34 +00001180 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1181 return -1;
1182
Douglas Gregor32be4a52010-10-11 21:37:58 +00001183 CIdx = clang_createIndex(0, 0);
1184
1185 if (getenv("CINDEXTEST_EDITING"))
1186 Repeats = 5;
1187
1188 TU = clang_parseTranslationUnit(CIdx, 0,
1189 argv + num_unsaved_files + 2,
1190 argc - num_unsaved_files - 2,
1191 0, 0, getDefaultParsingOptions());
1192 if (!TU) {
1193 fprintf(stderr, "Unable to load translation unit!\n");
1194 return 1;
1195 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001196
1197 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1198 fprintf(stderr, "Unable to reparse translation init!\n");
1199 return 1;
1200 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001201
1202 for (I = 0; I != Repeats; ++I) {
1203 results = clang_codeCompleteAt(TU, filename, line, column,
1204 unsaved_files, num_unsaved_files,
1205 completionOptions);
1206 if (!results) {
1207 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001208 return 1;
1209 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001210 if (I != Repeats-1)
1211 clang_disposeCodeCompleteResults(results);
1212 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001213
Douglas Gregorec6762c2009-12-18 16:20:58 +00001214 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001215 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001216 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001217 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001218 CXString objCSelector;
1219 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001220 if (!timing_only) {
1221 /* Sort the code-completion results based on the typed text. */
1222 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1223
Douglas Gregor1982c182010-07-12 18:38:41 +00001224 for (i = 0; i != n; ++i)
1225 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001226 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001227 n = clang_codeCompleteGetNumDiagnostics(results);
1228 for (i = 0; i != n; ++i) {
1229 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1230 PrintDiagnostic(diag);
1231 clang_disposeDiagnostic(diag);
1232 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001233
1234 contexts = clang_codeCompleteGetContexts(results);
1235 print_completion_contexts(contexts, stdout);
1236
Douglas Gregor0a47d692011-07-26 15:24:30 +00001237 containerKind = clang_codeCompleteGetContainerKind(results,
1238 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001239
1240 if (containerKind != CXCursor_InvalidCode) {
1241 /* We have found a container */
1242 CXString containerUSR, containerKindSpelling;
1243 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1244 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1245 clang_disposeString(containerKindSpelling);
1246
1247 if (containerIsIncomplete) {
1248 printf("Container is incomplete\n");
1249 }
1250 else {
1251 printf("Container is complete\n");
1252 }
1253
1254 containerUSR = clang_codeCompleteGetContainerUSR(results);
1255 printf("Container USR: %s\n", clang_getCString(containerUSR));
1256 clang_disposeString(containerUSR);
1257 }
1258
Douglas Gregor0a47d692011-07-26 15:24:30 +00001259 objCSelector = clang_codeCompleteGetObjCSelector(results);
1260 selectorString = clang_getCString(objCSelector);
1261 if (selectorString && strlen(selectorString) > 0) {
1262 printf("Objective-C selector: %s\n", selectorString);
1263 }
1264 clang_disposeString(objCSelector);
1265
Douglas Gregorec6762c2009-12-18 16:20:58 +00001266 clang_disposeCodeCompleteResults(results);
1267 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001268 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001269 clang_disposeIndex(CIdx);
1270 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001271
Douglas Gregor735df882009-12-02 09:21:34 +00001272 free_remapped_files(unsaved_files, num_unsaved_files);
1273
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001274 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001275}
1276
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001277typedef struct {
1278 char *filename;
1279 unsigned line;
1280 unsigned column;
1281} CursorSourceLocation;
1282
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001283static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001284 CXIndex CIdx;
1285 int errorCode;
1286 struct CXUnsavedFile *unsaved_files = 0;
1287 int num_unsaved_files = 0;
1288 CXTranslationUnit TU;
1289 CXCursor Cursor;
1290 CursorSourceLocation *Locations = 0;
1291 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001292 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001293 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001294
Ted Kremeneke68fff62010-02-17 00:41:32 +00001295 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001296 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1297 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001298
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001299 /* Parse the locations. */
1300 assert(NumLocations > 0 && "Unable to count locations?");
1301 Locations = (CursorSourceLocation *)malloc(
1302 NumLocations * sizeof(CursorSourceLocation));
1303 for (Loc = 0; Loc < NumLocations; ++Loc) {
1304 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001305 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1306 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001307 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001308 return errorCode;
1309 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001310
1311 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001312 &num_unsaved_files))
1313 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001314
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001315 if (getenv("CINDEXTEST_EDITING"))
1316 Repeats = 5;
1317
1318 /* Parse the translation unit. When we're testing clang_getCursor() after
1319 reparsing, don't remap unsaved files until the second parse. */
1320 CIdx = clang_createIndex(1, 1);
1321 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1322 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001323 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001324 unsaved_files,
1325 Repeats > 1? 0 : num_unsaved_files,
1326 getDefaultParsingOptions());
1327
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001328 if (!TU) {
1329 fprintf(stderr, "unable to parse input\n");
1330 return -1;
1331 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001332
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001333 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001334 if (Repeats > 1 &&
1335 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1336 clang_defaultReparseOptions(TU))) {
1337 clang_disposeTranslationUnit(TU);
1338 return 1;
1339 }
1340
1341 for (Loc = 0; Loc < NumLocations; ++Loc) {
1342 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1343 if (!file)
1344 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001345
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001346 Cursor = clang_getCursor(TU,
1347 clang_getLocation(TU, file, Locations[Loc].line,
1348 Locations[Loc].column));
1349 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001350 CXCompletionString completionString = clang_getCursorCompletionString(
1351 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001352 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001353 if (completionString != NULL) {
1354 printf("\nCompletion string: ");
1355 print_completion_string(completionString, stdout);
1356 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001357 printf("\n");
1358 free(Locations[Loc].filename);
1359 }
1360 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001361 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001362
Douglas Gregora88084b2010-02-18 18:08:43 +00001363 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001364 clang_disposeTranslationUnit(TU);
1365 clang_disposeIndex(CIdx);
1366 free(Locations);
1367 free_remapped_files(unsaved_files, num_unsaved_files);
1368 return 0;
1369}
1370
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001371static enum CXVisitorResult findFileRefsVisit(void *context,
1372 CXCursor cursor, CXSourceRange range) {
1373 if (clang_Range_isNull(range))
1374 return CXVisit_Continue;
1375
1376 PrintCursor(cursor);
1377 PrintRange(range, "");
1378 printf("\n");
1379 return CXVisit_Continue;
1380}
1381
1382static int find_file_refs_at(int argc, const char **argv) {
1383 CXIndex CIdx;
1384 int errorCode;
1385 struct CXUnsavedFile *unsaved_files = 0;
1386 int num_unsaved_files = 0;
1387 CXTranslationUnit TU;
1388 CXCursor Cursor;
1389 CursorSourceLocation *Locations = 0;
1390 unsigned NumLocations = 0, Loc;
1391 unsigned Repeats = 1;
1392 unsigned I;
1393
1394 /* Count the number of locations. */
1395 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1396 ++NumLocations;
1397
1398 /* Parse the locations. */
1399 assert(NumLocations > 0 && "Unable to count locations?");
1400 Locations = (CursorSourceLocation *)malloc(
1401 NumLocations * sizeof(CursorSourceLocation));
1402 for (Loc = 0; Loc < NumLocations; ++Loc) {
1403 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1404 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1405 &Locations[Loc].line,
1406 &Locations[Loc].column, 0, 0)))
1407 return errorCode;
1408 }
1409
1410 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1411 &num_unsaved_files))
1412 return -1;
1413
1414 if (getenv("CINDEXTEST_EDITING"))
1415 Repeats = 5;
1416
1417 /* Parse the translation unit. When we're testing clang_getCursor() after
1418 reparsing, don't remap unsaved files until the second parse. */
1419 CIdx = clang_createIndex(1, 1);
1420 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1421 argv + num_unsaved_files + 1 + NumLocations,
1422 argc - num_unsaved_files - 2 - NumLocations,
1423 unsaved_files,
1424 Repeats > 1? 0 : num_unsaved_files,
1425 getDefaultParsingOptions());
1426
1427 if (!TU) {
1428 fprintf(stderr, "unable to parse input\n");
1429 return -1;
1430 }
1431
1432 for (I = 0; I != Repeats; ++I) {
1433 if (Repeats > 1 &&
1434 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1435 clang_defaultReparseOptions(TU))) {
1436 clang_disposeTranslationUnit(TU);
1437 return 1;
1438 }
1439
1440 for (Loc = 0; Loc < NumLocations; ++Loc) {
1441 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1442 if (!file)
1443 continue;
1444
1445 Cursor = clang_getCursor(TU,
1446 clang_getLocation(TU, file, Locations[Loc].line,
1447 Locations[Loc].column));
1448 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001449 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001450 PrintCursor(Cursor);
1451 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001452 clang_findReferencesInFile(Cursor, file, visitor);
1453 free(Locations[Loc].filename);
1454 }
1455 }
1456 }
1457
1458 PrintDiagnostics(TU);
1459 clang_disposeTranslationUnit(TU);
1460 clang_disposeIndex(CIdx);
1461 free(Locations);
1462 free_remapped_files(unsaved_files, num_unsaved_files);
1463 return 0;
1464}
1465
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001466int perform_token_annotation(int argc, const char **argv) {
1467 const char *input = argv[1];
1468 char *filename = 0;
1469 unsigned line, second_line;
1470 unsigned column, second_column;
1471 CXIndex CIdx;
1472 CXTranslationUnit TU = 0;
1473 int errorCode;
1474 struct CXUnsavedFile *unsaved_files = 0;
1475 int num_unsaved_files = 0;
1476 CXToken *tokens;
1477 unsigned num_tokens;
1478 CXSourceRange range;
1479 CXSourceLocation startLoc, endLoc;
1480 CXFile file = 0;
1481 CXCursor *cursors = 0;
1482 unsigned i;
1483
1484 input += strlen("-test-annotate-tokens=");
1485 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1486 &second_line, &second_column)))
1487 return errorCode;
1488
1489 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1490 return -1;
1491
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001492 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00001493 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1494 argv + num_unsaved_files + 2,
1495 argc - num_unsaved_files - 3,
1496 unsaved_files,
1497 num_unsaved_files,
1498 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001499 if (!TU) {
1500 fprintf(stderr, "unable to parse input\n");
1501 clang_disposeIndex(CIdx);
1502 free(filename);
1503 free_remapped_files(unsaved_files, num_unsaved_files);
1504 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001505 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001506 errorCode = 0;
1507
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00001508 if (getenv("CINDEXTEST_EDITING")) {
1509 for (i = 0; i < 5; ++i) {
1510 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1511 clang_defaultReparseOptions(TU))) {
1512 fprintf(stderr, "Unable to reparse translation unit!\n");
1513 errorCode = -1;
1514 goto teardown;
1515 }
1516 }
1517 }
1518
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001519 file = clang_getFile(TU, filename);
1520 if (!file) {
1521 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1522 errorCode = -1;
1523 goto teardown;
1524 }
1525
1526 startLoc = clang_getLocation(TU, file, line, column);
1527 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001528 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001529 column);
1530 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001531 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001532 }
1533
1534 endLoc = clang_getLocation(TU, file, second_line, second_column);
1535 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001536 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001537 second_line, second_column);
1538 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001539 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001540 }
1541
1542 range = clang_getRange(startLoc, endLoc);
1543 clang_tokenize(TU, range, &tokens, &num_tokens);
1544 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1545 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1546 for (i = 0; i != num_tokens; ++i) {
1547 const char *kind = "<unknown>";
1548 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1549 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1550 unsigned start_line, start_column, end_line, end_column;
1551
1552 switch (clang_getTokenKind(tokens[i])) {
1553 case CXToken_Punctuation: kind = "Punctuation"; break;
1554 case CXToken_Keyword: kind = "Keyword"; break;
1555 case CXToken_Identifier: kind = "Identifier"; break;
1556 case CXToken_Literal: kind = "Literal"; break;
1557 case CXToken_Comment: kind = "Comment"; break;
1558 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00001559 clang_getSpellingLocation(clang_getRangeStart(extent),
1560 0, &start_line, &start_column, 0);
1561 clang_getSpellingLocation(clang_getRangeEnd(extent),
1562 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001563 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1564 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001565 if (!clang_isInvalid(cursors[i].kind)) {
1566 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001567 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001568 }
1569 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001570 }
1571 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00001572 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001573
1574 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001575 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001576 clang_disposeTranslationUnit(TU);
1577 clang_disposeIndex(CIdx);
1578 free(filename);
1579 free_remapped_files(unsaved_files, num_unsaved_files);
1580 return errorCode;
1581}
1582
Ted Kremenek0d435192009-11-17 18:13:31 +00001583/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001584/* USR printing. */
1585/******************************************************************************/
1586
1587static int insufficient_usr(const char *kind, const char *usage) {
1588 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1589 return 1;
1590}
1591
1592static unsigned isUSR(const char *s) {
1593 return s[0] == 'c' && s[1] == ':';
1594}
1595
1596static int not_usr(const char *s, const char *arg) {
1597 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1598 return 1;
1599}
1600
1601static void print_usr(CXString usr) {
1602 const char *s = clang_getCString(usr);
1603 printf("%s\n", s);
1604 clang_disposeString(usr);
1605}
1606
1607static void display_usrs() {
1608 fprintf(stderr, "-print-usrs options:\n"
1609 " ObjCCategory <class name> <category name>\n"
1610 " ObjCClass <class name>\n"
1611 " ObjCIvar <ivar name> <class USR>\n"
1612 " ObjCMethod <selector> [0=class method|1=instance method] "
1613 "<class USR>\n"
1614 " ObjCProperty <property name> <class USR>\n"
1615 " ObjCProtocol <protocol name>\n");
1616}
1617
1618int print_usrs(const char **I, const char **E) {
1619 while (I != E) {
1620 const char *kind = *I;
1621 unsigned len = strlen(kind);
1622 switch (len) {
1623 case 8:
1624 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1625 if (I + 2 >= E)
1626 return insufficient_usr(kind, "<ivar name> <class USR>");
1627 if (!isUSR(I[2]))
1628 return not_usr("<class USR>", I[2]);
1629 else {
1630 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001631 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001632 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001633 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1634 }
1635
1636 I += 3;
1637 continue;
1638 }
1639 break;
1640 case 9:
1641 if (memcmp(kind, "ObjCClass", 9) == 0) {
1642 if (I + 1 >= E)
1643 return insufficient_usr(kind, "<class name>");
1644 print_usr(clang_constructUSR_ObjCClass(I[1]));
1645 I += 2;
1646 continue;
1647 }
1648 break;
1649 case 10:
1650 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1651 if (I + 3 >= E)
1652 return insufficient_usr(kind, "<method selector> "
1653 "[0=class method|1=instance method] <class USR>");
1654 if (!isUSR(I[3]))
1655 return not_usr("<class USR>", I[3]);
1656 else {
1657 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001658 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00001659 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001660 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1661 }
1662 I += 4;
1663 continue;
1664 }
1665 break;
1666 case 12:
1667 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1668 if (I + 2 >= E)
1669 return insufficient_usr(kind, "<class name> <category name>");
1670 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1671 I += 3;
1672 continue;
1673 }
1674 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1675 if (I + 1 >= E)
1676 return insufficient_usr(kind, "<protocol name>");
1677 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1678 I += 2;
1679 continue;
1680 }
1681 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1682 if (I + 2 >= E)
1683 return insufficient_usr(kind, "<property name> <class USR>");
1684 if (!isUSR(I[2]))
1685 return not_usr("<class USR>", I[2]);
1686 else {
1687 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001688 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001689 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001690 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1691 }
1692 I += 3;
1693 continue;
1694 }
1695 break;
1696 default:
1697 break;
1698 }
1699 break;
1700 }
1701
1702 if (I != E) {
1703 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1704 display_usrs();
1705 return 1;
1706 }
1707 return 0;
1708}
1709
1710int print_usrs_file(const char *file_name) {
1711 char line[2048];
1712 const char *args[128];
1713 unsigned numChars = 0;
1714
1715 FILE *fp = fopen(file_name, "r");
1716 if (!fp) {
1717 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1718 return 1;
1719 }
1720
1721 /* This code is not really all that safe, but it works fine for testing. */
1722 while (!feof(fp)) {
1723 char c = fgetc(fp);
1724 if (c == '\n') {
1725 unsigned i = 0;
1726 const char *s = 0;
1727
1728 if (numChars == 0)
1729 continue;
1730
1731 line[numChars] = '\0';
1732 numChars = 0;
1733
1734 if (line[0] == '/' && line[1] == '/')
1735 continue;
1736
1737 s = strtok(line, " ");
1738 while (s) {
1739 args[i] = s;
1740 ++i;
1741 s = strtok(0, " ");
1742 }
1743 if (print_usrs(&args[0], &args[i]))
1744 return 1;
1745 }
1746 else
1747 line[numChars++] = c;
1748 }
1749
1750 fclose(fp);
1751 return 0;
1752}
1753
1754/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001755/* Command line processing. */
1756/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001757int write_pch_file(const char *filename, int argc, const char *argv[]) {
1758 CXIndex Idx;
1759 CXTranslationUnit TU;
1760 struct CXUnsavedFile *unsaved_files = 0;
1761 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00001762 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001763
1764 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1765
1766 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1767 clang_disposeIndex(Idx);
1768 return -1;
1769 }
1770
1771 TU = clang_parseTranslationUnit(Idx, 0,
1772 argv + num_unsaved_files,
1773 argc - num_unsaved_files,
1774 unsaved_files,
1775 num_unsaved_files,
1776 CXTranslationUnit_Incomplete);
1777 if (!TU) {
1778 fprintf(stderr, "Unable to load translation unit!\n");
1779 free_remapped_files(unsaved_files, num_unsaved_files);
1780 clang_disposeIndex(Idx);
1781 return 1;
1782 }
1783
Douglas Gregor39c411f2011-07-06 16:43:36 +00001784 switch (clang_saveTranslationUnit(TU, filename,
1785 clang_defaultSaveOptions(TU))) {
1786 case CXSaveError_None:
1787 break;
1788
1789 case CXSaveError_TranslationErrors:
1790 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
1791 filename);
1792 result = 2;
1793 break;
1794
1795 case CXSaveError_InvalidTU:
1796 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
1797 filename);
1798 result = 3;
1799 break;
1800
1801 case CXSaveError_Unknown:
1802 default:
1803 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
1804 result = 1;
1805 break;
1806 }
1807
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001808 clang_disposeTranslationUnit(TU);
1809 free_remapped_files(unsaved_files, num_unsaved_files);
1810 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00001811 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001812}
1813
1814/******************************************************************************/
1815/* Command line processing. */
1816/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001817
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001818static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001819 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001820 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001821 if (strcmp(s, "-usrs") == 0)
1822 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001823 if (strncmp(s, "-memory-usage", 13) == 0)
1824 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001825 return NULL;
1826}
1827
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001828static void print_usage(void) {
1829 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001830 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001831 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001832 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001833 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001834 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001835 "[FileCheck prefix]\n");
1836 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001837 " c-index-test -test-load-tu <AST file> <symbol filter> "
1838 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001839 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1840 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001841 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001842 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001843 " c-index-test -test-load-source-memory-usage "
1844 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00001845 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1846 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001847 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001848 " c-index-test -test-load-source-usrs-memory-usage "
1849 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001850 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1851 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001852 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00001853 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001854 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001855 " c-index-test -test-print-typekind {<args>}*\n"
1856 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001857 " c-index-test -print-usr-file <file>\n"
1858 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001859 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001860 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001861 " all - load all symbols, including those from PCH\n"
1862 " local - load all symbols except those in PCH\n"
1863 " category - only load ObjC categories (non-PCH)\n"
1864 " interface - only load ObjC interfaces (non-PCH)\n"
1865 " protocol - only load ObjC protocols (non-PCH)\n"
1866 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001867 " typedef - only load typdefs (non-PCH)\n"
1868 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001869}
1870
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001871/***/
1872
1873int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001874 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001875 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001876 return perform_code_completion(argc, argv, 0);
1877 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1878 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001879 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1880 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001881 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
1882 return find_file_refs_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001883 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001884 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001885 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001886 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1887 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001888 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001889 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1890 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1891 if (I) {
1892 int trials = atoi(argv[2]);
1893 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1894 NULL);
1895 }
1896 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001897 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001898 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001899
1900 PostVisitTU postVisit = 0;
1901 if (strstr(argv[1], "-memory-usage"))
1902 postVisit = PrintMemoryUsage;
1903
Ted Kremenek7d405622010-01-12 23:34:26 +00001904 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001905 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
1906 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00001907 }
1908 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001909 return perform_file_scan(argv[2], argv[3],
1910 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001911 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1912 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001913 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1914 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1915 PrintInclusionStack);
1916 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1917 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1918 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001919 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1920 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1921 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001922 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1923 return perform_test_load_source(argc - 2, argv + 2, "all",
1924 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001925 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1926 if (argc > 2)
1927 return print_usrs(argv + 2, argv + argc);
1928 else {
1929 display_usrs();
1930 return 1;
1931 }
1932 }
1933 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1934 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001935 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1936 return write_pch_file(argv[2], argc - 3, argv + 3);
1937
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001938 print_usage();
1939 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001940}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001941
1942/***/
1943
1944/* We intentionally run in a separate thread to ensure we at least minimal
1945 * testing of a multithreaded environment (for example, having a reduced stack
1946 * size). */
1947
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001948typedef struct thread_info {
1949 int argc;
1950 const char **argv;
1951 int result;
1952} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00001953void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001954 thread_info *client_data = client_data_v;
1955 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001956}
1957
1958int main(int argc, const char **argv) {
1959 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001960
Douglas Gregor61605982010-10-27 16:00:01 +00001961 if (getenv("CINDEXTEST_NOTHREADS"))
1962 return cindextest_main(argc, argv);
1963
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001964 client_data.argc = argc;
1965 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00001966 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001967 return client_data.result;
1968}