blob: 2121bcbd98519e721cf516004c1393047bc8723f [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);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001036 unsigned annotationCount;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001037
1038 fprintf(file, "%s:", clang_getCString(ks));
1039 clang_disposeString(ks);
1040
Douglas Gregor3ac73852009-11-09 16:04:45 +00001041 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001042 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001043 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001044 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1045 case CXAvailability_Available:
1046 break;
1047
1048 case CXAvailability_Deprecated:
1049 fprintf(file, " (deprecated)");
1050 break;
1051
1052 case CXAvailability_NotAvailable:
1053 fprintf(file, " (unavailable)");
1054 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001055
1056 case CXAvailability_NotAccessible:
1057 fprintf(file, " (inaccessible)");
1058 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001059 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001060
1061 annotationCount = clang_getCompletionNumAnnotations(
1062 completion_result->CompletionString);
1063 if (annotationCount) {
1064 unsigned i;
1065 fprintf(file, " (");
1066 for (i = 0; i < annotationCount; ++i) {
1067 if (i != 0)
1068 fprintf(file, ", ");
1069 fprintf(file, "\"%s\"",
1070 clang_getCString(clang_getCompletionAnnotation(
1071 completion_result->CompletionString, i)));
1072 }
1073 fprintf(file, ")");
1074 }
1075
Douglas Gregor58ddb602010-08-23 23:00:57 +00001076 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001077}
1078
Douglas Gregor3da626b2011-07-07 16:03:39 +00001079void print_completion_contexts(unsigned long long contexts, FILE *file) {
1080 fprintf(file, "Completion contexts:\n");
1081 if (contexts == CXCompletionContext_Unknown) {
1082 fprintf(file, "Unknown\n");
1083 }
1084 if (contexts & CXCompletionContext_AnyType) {
1085 fprintf(file, "Any type\n");
1086 }
1087 if (contexts & CXCompletionContext_AnyValue) {
1088 fprintf(file, "Any value\n");
1089 }
1090 if (contexts & CXCompletionContext_ObjCObjectValue) {
1091 fprintf(file, "Objective-C object value\n");
1092 }
1093 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1094 fprintf(file, "Objective-C selector value\n");
1095 }
1096 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1097 fprintf(file, "C++ class type value\n");
1098 }
1099 if (contexts & CXCompletionContext_DotMemberAccess) {
1100 fprintf(file, "Dot member access\n");
1101 }
1102 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1103 fprintf(file, "Arrow member access\n");
1104 }
1105 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1106 fprintf(file, "Objective-C property access\n");
1107 }
1108 if (contexts & CXCompletionContext_EnumTag) {
1109 fprintf(file, "Enum tag\n");
1110 }
1111 if (contexts & CXCompletionContext_UnionTag) {
1112 fprintf(file, "Union tag\n");
1113 }
1114 if (contexts & CXCompletionContext_StructTag) {
1115 fprintf(file, "Struct tag\n");
1116 }
1117 if (contexts & CXCompletionContext_ClassTag) {
1118 fprintf(file, "Class name\n");
1119 }
1120 if (contexts & CXCompletionContext_Namespace) {
1121 fprintf(file, "Namespace or namespace alias\n");
1122 }
1123 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1124 fprintf(file, "Nested name specifier\n");
1125 }
1126 if (contexts & CXCompletionContext_ObjCInterface) {
1127 fprintf(file, "Objective-C interface\n");
1128 }
1129 if (contexts & CXCompletionContext_ObjCProtocol) {
1130 fprintf(file, "Objective-C protocol\n");
1131 }
1132 if (contexts & CXCompletionContext_ObjCCategory) {
1133 fprintf(file, "Objective-C category\n");
1134 }
1135 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1136 fprintf(file, "Objective-C instance method\n");
1137 }
1138 if (contexts & CXCompletionContext_ObjCClassMessage) {
1139 fprintf(file, "Objective-C class method\n");
1140 }
1141 if (contexts & CXCompletionContext_ObjCSelectorName) {
1142 fprintf(file, "Objective-C selector name\n");
1143 }
1144 if (contexts & CXCompletionContext_MacroName) {
1145 fprintf(file, "Macro name\n");
1146 }
1147 if (contexts & CXCompletionContext_NaturalLanguage) {
1148 fprintf(file, "Natural language\n");
1149 }
1150}
1151
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001152int my_stricmp(const char *s1, const char *s2) {
1153 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001154 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001155 if (c1 < c2)
1156 return -1;
1157 else if (c1 > c2)
1158 return 1;
1159
1160 ++s1;
1161 ++s2;
1162 }
1163
1164 if (*s1)
1165 return 1;
1166 else if (*s2)
1167 return -1;
1168 return 0;
1169}
1170
Douglas Gregor1982c182010-07-12 18:38:41 +00001171int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001172 const char *input = argv[1];
1173 char *filename = 0;
1174 unsigned line;
1175 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001176 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001177 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001178 struct CXUnsavedFile *unsaved_files = 0;
1179 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001180 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001181 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001182 unsigned I, Repeats = 1;
1183 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1184
1185 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1186 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001187
Douglas Gregor1982c182010-07-12 18:38:41 +00001188 if (timing_only)
1189 input += strlen("-code-completion-timing=");
1190 else
1191 input += strlen("-code-completion-at=");
1192
Ted Kremeneke68fff62010-02-17 00:41:32 +00001193 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001194 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001195 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001196
Douglas Gregor735df882009-12-02 09:21:34 +00001197 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1198 return -1;
1199
Douglas Gregor32be4a52010-10-11 21:37:58 +00001200 CIdx = clang_createIndex(0, 0);
1201
1202 if (getenv("CINDEXTEST_EDITING"))
1203 Repeats = 5;
1204
1205 TU = clang_parseTranslationUnit(CIdx, 0,
1206 argv + num_unsaved_files + 2,
1207 argc - num_unsaved_files - 2,
1208 0, 0, getDefaultParsingOptions());
1209 if (!TU) {
1210 fprintf(stderr, "Unable to load translation unit!\n");
1211 return 1;
1212 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001213
1214 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1215 fprintf(stderr, "Unable to reparse translation init!\n");
1216 return 1;
1217 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001218
1219 for (I = 0; I != Repeats; ++I) {
1220 results = clang_codeCompleteAt(TU, filename, line, column,
1221 unsaved_files, num_unsaved_files,
1222 completionOptions);
1223 if (!results) {
1224 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001225 return 1;
1226 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001227 if (I != Repeats-1)
1228 clang_disposeCodeCompleteResults(results);
1229 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001230
Douglas Gregorec6762c2009-12-18 16:20:58 +00001231 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001232 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001233 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001234 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001235 CXString objCSelector;
1236 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001237 if (!timing_only) {
1238 /* Sort the code-completion results based on the typed text. */
1239 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1240
Douglas Gregor1982c182010-07-12 18:38:41 +00001241 for (i = 0; i != n; ++i)
1242 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001243 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001244 n = clang_codeCompleteGetNumDiagnostics(results);
1245 for (i = 0; i != n; ++i) {
1246 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1247 PrintDiagnostic(diag);
1248 clang_disposeDiagnostic(diag);
1249 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001250
1251 contexts = clang_codeCompleteGetContexts(results);
1252 print_completion_contexts(contexts, stdout);
1253
Douglas Gregor0a47d692011-07-26 15:24:30 +00001254 containerKind = clang_codeCompleteGetContainerKind(results,
1255 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001256
1257 if (containerKind != CXCursor_InvalidCode) {
1258 /* We have found a container */
1259 CXString containerUSR, containerKindSpelling;
1260 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1261 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1262 clang_disposeString(containerKindSpelling);
1263
1264 if (containerIsIncomplete) {
1265 printf("Container is incomplete\n");
1266 }
1267 else {
1268 printf("Container is complete\n");
1269 }
1270
1271 containerUSR = clang_codeCompleteGetContainerUSR(results);
1272 printf("Container USR: %s\n", clang_getCString(containerUSR));
1273 clang_disposeString(containerUSR);
1274 }
1275
Douglas Gregor0a47d692011-07-26 15:24:30 +00001276 objCSelector = clang_codeCompleteGetObjCSelector(results);
1277 selectorString = clang_getCString(objCSelector);
1278 if (selectorString && strlen(selectorString) > 0) {
1279 printf("Objective-C selector: %s\n", selectorString);
1280 }
1281 clang_disposeString(objCSelector);
1282
Douglas Gregorec6762c2009-12-18 16:20:58 +00001283 clang_disposeCodeCompleteResults(results);
1284 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001285 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001286 clang_disposeIndex(CIdx);
1287 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001288
Douglas Gregor735df882009-12-02 09:21:34 +00001289 free_remapped_files(unsaved_files, num_unsaved_files);
1290
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001291 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001292}
1293
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001294typedef struct {
1295 char *filename;
1296 unsigned line;
1297 unsigned column;
1298} CursorSourceLocation;
1299
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001300static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001301 CXIndex CIdx;
1302 int errorCode;
1303 struct CXUnsavedFile *unsaved_files = 0;
1304 int num_unsaved_files = 0;
1305 CXTranslationUnit TU;
1306 CXCursor Cursor;
1307 CursorSourceLocation *Locations = 0;
1308 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001309 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001310 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001311
Ted Kremeneke68fff62010-02-17 00:41:32 +00001312 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001313 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1314 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001315
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001316 /* Parse the locations. */
1317 assert(NumLocations > 0 && "Unable to count locations?");
1318 Locations = (CursorSourceLocation *)malloc(
1319 NumLocations * sizeof(CursorSourceLocation));
1320 for (Loc = 0; Loc < NumLocations; ++Loc) {
1321 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001322 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1323 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001324 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001325 return errorCode;
1326 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001327
1328 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001329 &num_unsaved_files))
1330 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001331
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001332 if (getenv("CINDEXTEST_EDITING"))
1333 Repeats = 5;
1334
1335 /* Parse the translation unit. When we're testing clang_getCursor() after
1336 reparsing, don't remap unsaved files until the second parse. */
1337 CIdx = clang_createIndex(1, 1);
1338 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1339 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001340 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001341 unsaved_files,
1342 Repeats > 1? 0 : num_unsaved_files,
1343 getDefaultParsingOptions());
1344
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001345 if (!TU) {
1346 fprintf(stderr, "unable to parse input\n");
1347 return -1;
1348 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001349
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001350 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001351 if (Repeats > 1 &&
1352 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1353 clang_defaultReparseOptions(TU))) {
1354 clang_disposeTranslationUnit(TU);
1355 return 1;
1356 }
1357
1358 for (Loc = 0; Loc < NumLocations; ++Loc) {
1359 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1360 if (!file)
1361 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001362
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001363 Cursor = clang_getCursor(TU,
1364 clang_getLocation(TU, file, Locations[Loc].line,
1365 Locations[Loc].column));
1366 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001367 CXCompletionString completionString = clang_getCursorCompletionString(
1368 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001369 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001370 if (completionString != NULL) {
1371 printf("\nCompletion string: ");
1372 print_completion_string(completionString, stdout);
1373 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001374 printf("\n");
1375 free(Locations[Loc].filename);
1376 }
1377 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001378 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001379
Douglas Gregora88084b2010-02-18 18:08:43 +00001380 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001381 clang_disposeTranslationUnit(TU);
1382 clang_disposeIndex(CIdx);
1383 free(Locations);
1384 free_remapped_files(unsaved_files, num_unsaved_files);
1385 return 0;
1386}
1387
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001388static enum CXVisitorResult findFileRefsVisit(void *context,
1389 CXCursor cursor, CXSourceRange range) {
1390 if (clang_Range_isNull(range))
1391 return CXVisit_Continue;
1392
1393 PrintCursor(cursor);
1394 PrintRange(range, "");
1395 printf("\n");
1396 return CXVisit_Continue;
1397}
1398
1399static int find_file_refs_at(int argc, const char **argv) {
1400 CXIndex CIdx;
1401 int errorCode;
1402 struct CXUnsavedFile *unsaved_files = 0;
1403 int num_unsaved_files = 0;
1404 CXTranslationUnit TU;
1405 CXCursor Cursor;
1406 CursorSourceLocation *Locations = 0;
1407 unsigned NumLocations = 0, Loc;
1408 unsigned Repeats = 1;
1409 unsigned I;
1410
1411 /* Count the number of locations. */
1412 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1413 ++NumLocations;
1414
1415 /* Parse the locations. */
1416 assert(NumLocations > 0 && "Unable to count locations?");
1417 Locations = (CursorSourceLocation *)malloc(
1418 NumLocations * sizeof(CursorSourceLocation));
1419 for (Loc = 0; Loc < NumLocations; ++Loc) {
1420 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1421 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1422 &Locations[Loc].line,
1423 &Locations[Loc].column, 0, 0)))
1424 return errorCode;
1425 }
1426
1427 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1428 &num_unsaved_files))
1429 return -1;
1430
1431 if (getenv("CINDEXTEST_EDITING"))
1432 Repeats = 5;
1433
1434 /* Parse the translation unit. When we're testing clang_getCursor() after
1435 reparsing, don't remap unsaved files until the second parse. */
1436 CIdx = clang_createIndex(1, 1);
1437 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1438 argv + num_unsaved_files + 1 + NumLocations,
1439 argc - num_unsaved_files - 2 - NumLocations,
1440 unsaved_files,
1441 Repeats > 1? 0 : num_unsaved_files,
1442 getDefaultParsingOptions());
1443
1444 if (!TU) {
1445 fprintf(stderr, "unable to parse input\n");
1446 return -1;
1447 }
1448
1449 for (I = 0; I != Repeats; ++I) {
1450 if (Repeats > 1 &&
1451 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1452 clang_defaultReparseOptions(TU))) {
1453 clang_disposeTranslationUnit(TU);
1454 return 1;
1455 }
1456
1457 for (Loc = 0; Loc < NumLocations; ++Loc) {
1458 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1459 if (!file)
1460 continue;
1461
1462 Cursor = clang_getCursor(TU,
1463 clang_getLocation(TU, file, Locations[Loc].line,
1464 Locations[Loc].column));
1465 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001466 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001467 PrintCursor(Cursor);
1468 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001469 clang_findReferencesInFile(Cursor, file, visitor);
1470 free(Locations[Loc].filename);
1471 }
1472 }
1473 }
1474
1475 PrintDiagnostics(TU);
1476 clang_disposeTranslationUnit(TU);
1477 clang_disposeIndex(CIdx);
1478 free(Locations);
1479 free_remapped_files(unsaved_files, num_unsaved_files);
1480 return 0;
1481}
1482
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001483typedef struct {
1484 const char *check_prefix;
1485 int first_check_printed;
1486} IndexData;
1487
1488static void printCheck(IndexData *data) {
1489 if (data->check_prefix) {
1490 if (data->first_check_printed) {
1491 printf("// %s-NEXT: ", data->check_prefix);
1492 } else {
1493 printf("// %s : ", data->check_prefix);
1494 data->first_check_printed = 1;
1495 }
1496 }
1497}
1498
1499static void printCXIndexFile(CXIdxFile file) {
1500 CXString filename = clang_getFileName((CXFile)file);
1501 printf("%s", clang_getCString(filename));
1502 clang_disposeString(filename);
1503}
1504
1505static void printCXIndexLoc(CXIdxLoc loc) {
1506 CXString filename;
1507 const char *cname, *end;
1508 CXIdxFile file;
1509 unsigned line, column;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001510 int isHeader;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001511
1512 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1513 if (line == 0) {
1514 printf("<null loc>");
1515 return;
1516 }
1517 filename = clang_getFileName((CXFile)file);
1518 cname = clang_getCString(filename);
1519 end = cname + strlen(cname);
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001520 isHeader = (end[-2] == '.' && end[-1] == 'h');
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001521
1522 if (isHeader) {
1523 printCXIndexFile(file);
1524 printf(":");
1525 }
1526 printf("%d:%d", line, column);
1527}
1528
1529static CXIdxEntity makeCXIndexEntity(CXIdxIndexedEntityInfo *info) {
1530 const char *name;
1531 CXIdxLoc loc;
1532 char *newStr;
1533 CXIdxFile file;
1534 unsigned line, column;
1535
1536 name = info->entityInfo->name;
1537 if (!name)
1538 name = "<anon-tag>";
1539
1540 loc = info->declInfo->loc;
1541 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001542 /* FIXME: free these.*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001543 newStr = (char *)malloc(strlen(name) + 10);
1544 sprintf(newStr, "%s:%d:%d", name, line, column);
1545 return (CXIdxEntity)newStr;
1546}
1547
1548static CXIdxContainer makeCXIndexContainer(CXIdxEntity entity) {
1549 return (CXIdxContainer)entity;
1550}
1551
1552static void printCXIndexEntity(CXIdxEntity entity) {
1553 printf("{%s}", (const char *)entity);
1554}
1555
1556static void printCXIndexContainer(CXIdxContainer container) {
1557 printf("[%s]", (const char *)container);
1558}
1559
1560static void printIndexedDeclInfo(CXIdxIndexedDeclInfo *info) {
1561 printf(" | cursor: ");
1562 PrintCursor(info->cursor);
1563 printf(" | loc: ");
1564 printCXIndexLoc(info->loc);
1565 printf(" | container: ");
1566 printCXIndexContainer(info->container);
1567}
1568
1569static void printIndexedEntityInfo(const char *cb,
1570 CXClientData client_data,
1571 CXIdxIndexedEntityInfo *info) {
1572 const char *name;
1573 IndexData *index_data;
1574 index_data = (IndexData *)client_data;
1575 printCheck(index_data);
1576
1577 name = info->entityInfo->name;
1578 if (!name)
1579 name = "<anon-tag>";
1580
1581 printf("%s: %s", cb, info->entityInfo->name);
1582 printIndexedDeclInfo(info->declInfo);
1583 printf(" | USR: %s", info->entityInfo->USR);
1584}
1585
1586static void printIndexedRedeclInfo(const char *cb,
1587 CXClientData client_data,
1588 CXIdxIndexedRedeclInfo *info) {
1589 IndexData *index_data;
1590 index_data = (IndexData *)client_data;
1591 printCheck(index_data);
1592
1593 printf("%s redeclaration: ", cb);
1594 printCXIndexEntity(info->entity);
1595 printIndexedDeclInfo(info->declInfo);
1596}
1597
1598static void printStartedContainerInfo(const char *cb,
1599 CXClientData client_data,
1600 CXIdxContainerInfo *info) {
1601 IndexData *index_data;
1602 index_data = (IndexData *)client_data;
1603 printCheck(index_data);
1604
1605 printf("started %s: ", cb);
1606 printCXIndexEntity(info->entity);
1607 printf(" | cursor: ");
1608 PrintCursor(info->cursor);
1609 printf(" | loc: ");
1610 printCXIndexLoc(info->loc);
1611}
1612
1613static void index_diagnostic(CXClientData client_data,
1614 CXDiagnostic diag, void *reserved) {
1615 CXString str;
1616 const char *cstr;
1617 IndexData *index_data;
1618 index_data = (IndexData *)client_data;
1619 printCheck(index_data);
1620
1621 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
1622 cstr = clang_getCString(str);
Argyrios Kyrtzidisc0f5b752011-10-18 15:13:14 +00001623 printf("diagnostic: %s\n", cstr);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001624 clang_disposeString(str);
1625}
1626
1627static CXIdxFile index_recordFile(CXClientData client_data,
1628 CXFile file, void *reserved) {
1629 return (CXIdxFile)file;
1630}
1631
1632static void index_ppIncludedFile(CXClientData client_data,
1633 CXIdxIncludedFileInfo *info) {
1634 IndexData *index_data;
1635 index_data = (IndexData *)client_data;
1636 printCheck(index_data);
1637
1638 printf("included file: ");
1639 printCXIndexFile(info->file);
1640 printf(" | name: \"%s\"", info->filename);
1641 printf(" | hash loc: ");
1642 printCXIndexLoc(info->hashLoc);
1643 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
1644}
1645
1646static CXIdxMacro index_ppMacroDefined(CXClientData client_data,
1647 CXIdxMacroDefinedInfo *info) {
1648 IndexData *index_data;
1649 index_data = (IndexData *)client_data;
1650 printCheck(index_data);
1651
1652 printf("macro defined: %s", info->macroInfo->name);
1653 printf(" | loc: ");
1654 printCXIndexLoc(info->macroInfo->loc);
1655 printf(" | defBegin: ");
1656 printCXIndexLoc(info->defBegin);
1657 printf(" | length: %d\n", info->defLength);
1658
1659 return (CXIdxMacro)info->macroInfo->name;
1660}
1661
1662static void index_ppMacroUndefined(CXClientData client_data,
1663 CXIdxMacroUndefinedInfo *info) {
1664 IndexData *index_data;
1665 index_data = (IndexData *)client_data;
1666 printCheck(index_data);
1667
1668 printf("macro undefined: %s", info->name);
1669 printf(" | loc: ");
1670 printCXIndexLoc(info->loc);
1671 printf("\n");
1672}
1673
1674static void index_ppMacroExpanded(CXClientData client_data,
1675 CXIdxMacroExpandedInfo *info) {
1676 IndexData *index_data;
1677 index_data = (IndexData *)client_data;
1678 printCheck(index_data);
1679
1680 printf("macro expanded: %s", info->name);
1681 printf(" | loc: ");
1682 printCXIndexLoc(info->loc);
1683 printf("\n");
1684}
1685
1686static CXIdxEntity index_importedEntity(CXClientData client_data,
1687 CXIdxImportedEntityInfo *info) {
1688 IndexData *index_data;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001689 CXIdxIndexedDeclInfo DeclInfo;
1690 CXIdxIndexedEntityInfo EntityInfo;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001691 const char *name;
Argyrios Kyrtzidis36180f32011-10-17 22:12:24 +00001692 DeclInfo.cursor = info->cursor;
1693 DeclInfo.loc = info->loc;
1694 DeclInfo.container = 0;
1695 EntityInfo.entityInfo = info->entityInfo;
1696 EntityInfo.declInfo = &DeclInfo;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001697 index_data = (IndexData *)client_data;
1698 printCheck(index_data);
1699
1700 name = info->entityInfo->name;
1701 if (!name)
1702 name = "<anon-tag>";
1703
1704 printf("imported entity: %s", name);
1705 printf(" | cursor: ");
1706 PrintCursor(info->cursor);
1707 printf(" | loc: ");
1708 printCXIndexLoc(info->loc);
1709 printf("\n");
1710
1711 return makeCXIndexEntity(&EntityInfo);
1712}
1713
1714static CXIdxContainer index_startedTranslationUnit(CXClientData client_data,
1715 void *reserved) {
1716 IndexData *index_data;
1717 index_data = (IndexData *)client_data;
1718 printCheck(index_data);
1719
1720 printf("started TU\n");
1721 return (CXIdxContainer)"TU";
1722}
1723
1724static CXIdxEntity index_indexTypedef(CXClientData client_data,
1725 CXIdxTypedefInfo *info) {
1726 printIndexedEntityInfo("typedef", client_data, info->indexedEntityInfo);
1727 printf("\n");
1728
1729 return makeCXIndexEntity(info->indexedEntityInfo);
1730}
1731
1732static CXIdxEntity index_indexFunction(CXClientData client_data,
1733 CXIdxFunctionInfo *info) {
1734 printIndexedEntityInfo("function", client_data, info->indexedEntityInfo);
1735 printf(" | isDefinition: %d\n", info->isDefinition);
1736
1737 return makeCXIndexEntity(info->indexedEntityInfo);
1738}
1739
1740static void index_indexFunctionRedeclaration(CXClientData client_data,
1741 CXIdxFunctionRedeclInfo *info) {
1742 printIndexedRedeclInfo("function", client_data, info->indexedRedeclInfo);
1743 printf(" | isDefinition: %d\n", info->isDefinition);
1744}
1745
1746static CXIdxEntity index_indexVariable(CXClientData client_data,
1747 CXIdxVariableInfo *info) {
1748 printIndexedEntityInfo("variable", client_data, info->indexedEntityInfo);
1749 printf(" | isDefinition: %d\n", info->isDefinition);
1750
1751 return makeCXIndexEntity(info->indexedEntityInfo);
1752}
1753
1754static void index_indexVariableRedeclaration(CXClientData client_data,
1755 CXIdxVariableRedeclInfo *info) {
1756 printIndexedRedeclInfo("variable", client_data, info->indexedRedeclInfo);
1757 printf(" | isDefinition: %d\n", info->isDefinition);
1758}
1759
1760static CXIdxEntity index_indexTagType(CXClientData client_data,
1761 CXIdxTagTypeInfo *info) {
1762 printIndexedEntityInfo("tag type", client_data, info->indexedEntityInfo);
1763 printf(" | isDefinition: %d | anon: %d\n",
1764 info->isDefinition, info->isAnonymous);
1765
1766 return makeCXIndexEntity(info->indexedEntityInfo);
1767}
1768
1769static void index_indexTagTypeRedeclaration(CXClientData client_data,
1770 CXIdxTagTypeRedeclInfo *info) {
1771 printIndexedRedeclInfo("tag type", client_data, info->indexedRedeclInfo);
1772 printf(" | isDefinition: %d\n", info->isDefinition);
1773}
1774
1775static CXIdxEntity index_indexField(CXClientData client_data,
1776 CXIdxFieldInfo *info) {
1777 printIndexedEntityInfo("field", client_data, info->indexedEntityInfo);
1778 printf("\n");
1779
1780 return makeCXIndexEntity(info->indexedEntityInfo);
1781}
1782
1783static CXIdxEntity index_indexEnumerator(CXClientData client_data,
1784 CXIdxEnumeratorInfo *info) {
1785 printIndexedEntityInfo("enumerator", client_data, info->indexedEntityInfo);
1786 printf("\n");
1787
1788 return makeCXIndexEntity(info->indexedEntityInfo);
1789}
1790
1791static CXIdxContainer
1792index_startedTagTypeDefinition(CXClientData client_data,
1793 CXIdxTagTypeDefinitionInfo *info) {
1794 printStartedContainerInfo("tag type definition", client_data,
1795 info->containerInfo);
1796 printf("\n");
1797
1798 return makeCXIndexContainer(info->containerInfo->entity);
1799}
1800
1801static CXIdxEntity index_indexObjCClass(CXClientData client_data,
1802 CXIdxObjCClassInfo *info) {
1803 printIndexedEntityInfo("ObjC class", client_data, info->indexedEntityInfo);
1804 printf(" | forward ref: %d\n", info->isForwardRef);
1805
1806 return makeCXIndexEntity(info->indexedEntityInfo);
1807}
1808
1809static CXIdxEntity index_indexObjCProtocol(CXClientData client_data,
1810 CXIdxObjCProtocolInfo *info) {
1811 printIndexedEntityInfo("ObjC protocol", client_data,
1812 info->indexedEntityInfo);
1813 printf(" | forward ref: %d\n", info->isForwardRef);
1814
1815 return makeCXIndexEntity(info->indexedEntityInfo);
1816}
1817
1818static CXIdxEntity index_indexObjCCategory(CXClientData client_data,
1819 CXIdxObjCCategoryInfo *info) {
1820 printIndexedEntityInfo("ObjC category", client_data,
1821 info->indexedEntityInfo);
1822 printf(" | class: ");
1823 printCXIndexEntity(info->objcClass);
1824 printf("\n");
1825
1826 return makeCXIndexEntity(info->indexedEntityInfo);
1827}
1828
1829static CXIdxEntity index_indexObjCMethod(CXClientData client_data,
1830 CXIdxObjCMethodInfo *info) {
1831 printIndexedEntityInfo("ObjC Method", client_data, info->indexedEntityInfo);
1832 printf(" | isDefinition: %d\n", info->isDefinition);
1833
1834 return makeCXIndexEntity(info->indexedEntityInfo);
1835}
1836
1837static CXIdxEntity index_indexObjCProperty(CXClientData client_data,
1838 CXIdxObjCPropertyInfo *info) {
1839 printIndexedEntityInfo("ObjC property", client_data, info->indexedEntityInfo);
1840 printf("\n");
1841
1842 return makeCXIndexEntity(info->indexedEntityInfo);
1843}
1844
1845static void index_indexObjCMethodRedeclaration(CXClientData client_data,
1846 CXIdxObjCMethodRedeclInfo *info) {
1847 printIndexedRedeclInfo("ObjC Method", client_data, info->indexedRedeclInfo);
1848 printf(" | isDefinition: %d\n", info->isDefinition);
1849}
1850
1851static CXIdxContainer
1852index_startedStatementBody(CXClientData client_data,
1853 CXIdxStmtBodyInfo *info) {
1854 printStartedContainerInfo("body", client_data, info->containerInfo);
1855 printf(" | body: ");
1856 printCXIndexLoc(info->bodyBegin);
1857 printf("\n");
1858
1859 return makeCXIndexContainer(info->containerInfo->entity);
1860}
1861
1862static CXIdxContainer
1863index_startedObjCContainer(CXClientData client_data,
1864 CXIdxObjCContainerInfo *info) {
1865 printStartedContainerInfo("ObjC container", client_data, info->containerInfo);
1866 printf("\n");
1867
1868 return makeCXIndexContainer(info->containerInfo->entity);
1869}
1870
1871static void index_defineObjCClass(CXClientData client_data,
1872 CXIdxObjCClassDefineInfo *info) {
1873 IndexData *index_data;
1874 index_data = (IndexData *)client_data;
1875 printCheck(index_data);
1876
1877 printf("define objc class: ");
1878 printCXIndexEntity(info->objcClass);
1879 printf(" | cursor: ");
1880 PrintCursor(info->cursor);
1881 printf(" | container: ");
1882 printCXIndexContainer(info->container);
1883
1884 if (info->baseInfo) {
1885 printf(" | base: ");
1886 printCXIndexEntity(info->baseInfo->objcClass);
1887 printf(" | base loc: ");
1888 printCXIndexLoc(info->baseInfo->loc);
1889 }
1890
1891 printf("\n");
1892}
1893
1894static void index_endedContainer(CXClientData client_data,
1895 CXIdxEndContainerInfo *info) {
1896 IndexData *index_data;
1897 index_data = (IndexData *)client_data;
1898 printCheck(index_data);
1899
1900 printf("ended container: ");
1901 printCXIndexContainer(info->container);
1902 printf(" | end: ");
1903 printCXIndexLoc(info->endLoc);
1904 printf("\n");
1905}
1906
1907static void index_indexEntityReference(CXClientData client_data,
1908 CXIdxEntityRefInfo *info) {
1909 IndexData *index_data;
1910 index_data = (IndexData *)client_data;
1911 printCheck(index_data);
1912
1913 printf("reference: ");
1914 printCXIndexEntity(info->referencedEntity);
1915 printf(" | cursor: ");
1916 PrintCursor(info->cursor);
1917 printf(" | loc: ");
1918 printCXIndexLoc(info->loc);
1919 printf(" | parent: ");
1920 printCXIndexEntity(info->parentEntity);
1921 printf(" | container: ");
1922 printCXIndexContainer(info->container);
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00001923 printf(" | kind: ");
1924 switch (info->kind) {
1925 case CXIdxEntityRef_Direct: printf("direct"); break;
1926 case CXIdxEntityRef_ImplicitProperty: printf("implicit prop"); break;
1927 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001928 printf("\n");
1929}
1930
1931static IndexerCallbacks IndexCB = {
1932 index_diagnostic,
1933 index_recordFile,
1934 index_ppIncludedFile,
1935 index_ppMacroDefined,
1936 index_ppMacroUndefined,
1937 index_ppMacroExpanded,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001938 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001939 index_importedEntity,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00001940 0,/*index_importedMacro,*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001941 index_startedTranslationUnit,
1942 index_indexTypedef,
1943 index_indexFunction,
1944 index_indexFunctionRedeclaration,
1945 index_indexVariable,
1946 index_indexVariableRedeclaration,
1947 index_indexTagType,
1948 index_indexTagTypeRedeclaration,
1949 index_indexField,
1950 index_indexEnumerator,
1951 index_startedTagTypeDefinition,
1952 index_indexObjCClass,
1953 index_indexObjCProtocol,
1954 index_indexObjCCategory,
1955 index_indexObjCMethod,
1956 index_indexObjCProperty,
1957 index_indexObjCMethodRedeclaration,
1958 index_startedStatementBody,
1959 index_startedObjCContainer,
1960 index_defineObjCClass,
1961 index_endedContainer,
1962 index_indexEntityReference
1963};
1964
1965static int index_file(int argc, const char **argv) {
1966 const char *check_prefix;
1967 CXIndex CIdx;
1968 IndexData index_data;
1969
1970 check_prefix = 0;
1971 if (argc > 0) {
1972 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
1973 check_prefix = argv[0] + strlen("-check-prefix=");
1974 ++argv;
1975 --argc;
1976 }
1977 }
1978
1979 if (argc == 0) {
1980 fprintf(stderr, "no compiler arguments\n");
1981 return -1;
1982 }
1983
1984 CIdx = clang_createIndex(0, 1);
1985 index_data.check_prefix = check_prefix;
1986 index_data.first_check_printed = 0;
1987
1988 return clang_indexTranslationUnit(CIdx, &index_data, &IndexCB,sizeof(IndexCB),
1989 0, 0, argv, argc, 0, 0, 0, 0);
1990}
1991
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001992int perform_token_annotation(int argc, const char **argv) {
1993 const char *input = argv[1];
1994 char *filename = 0;
1995 unsigned line, second_line;
1996 unsigned column, second_column;
1997 CXIndex CIdx;
1998 CXTranslationUnit TU = 0;
1999 int errorCode;
2000 struct CXUnsavedFile *unsaved_files = 0;
2001 int num_unsaved_files = 0;
2002 CXToken *tokens;
2003 unsigned num_tokens;
2004 CXSourceRange range;
2005 CXSourceLocation startLoc, endLoc;
2006 CXFile file = 0;
2007 CXCursor *cursors = 0;
2008 unsigned i;
2009
2010 input += strlen("-test-annotate-tokens=");
2011 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2012 &second_line, &second_column)))
2013 return errorCode;
2014
2015 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2016 return -1;
2017
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002018 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002019 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2020 argv + num_unsaved_files + 2,
2021 argc - num_unsaved_files - 3,
2022 unsaved_files,
2023 num_unsaved_files,
2024 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002025 if (!TU) {
2026 fprintf(stderr, "unable to parse input\n");
2027 clang_disposeIndex(CIdx);
2028 free(filename);
2029 free_remapped_files(unsaved_files, num_unsaved_files);
2030 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002031 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002032 errorCode = 0;
2033
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002034 if (getenv("CINDEXTEST_EDITING")) {
2035 for (i = 0; i < 5; ++i) {
2036 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2037 clang_defaultReparseOptions(TU))) {
2038 fprintf(stderr, "Unable to reparse translation unit!\n");
2039 errorCode = -1;
2040 goto teardown;
2041 }
2042 }
2043 }
2044
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002045 file = clang_getFile(TU, filename);
2046 if (!file) {
2047 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2048 errorCode = -1;
2049 goto teardown;
2050 }
2051
2052 startLoc = clang_getLocation(TU, file, line, column);
2053 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002054 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002055 column);
2056 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002057 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002058 }
2059
2060 endLoc = clang_getLocation(TU, file, second_line, second_column);
2061 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002062 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002063 second_line, second_column);
2064 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002065 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002066 }
2067
2068 range = clang_getRange(startLoc, endLoc);
2069 clang_tokenize(TU, range, &tokens, &num_tokens);
2070 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2071 clang_annotateTokens(TU, tokens, num_tokens, cursors);
2072 for (i = 0; i != num_tokens; ++i) {
2073 const char *kind = "<unknown>";
2074 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2075 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2076 unsigned start_line, start_column, end_line, end_column;
2077
2078 switch (clang_getTokenKind(tokens[i])) {
2079 case CXToken_Punctuation: kind = "Punctuation"; break;
2080 case CXToken_Keyword: kind = "Keyword"; break;
2081 case CXToken_Identifier: kind = "Identifier"; break;
2082 case CXToken_Literal: kind = "Literal"; break;
2083 case CXToken_Comment: kind = "Comment"; break;
2084 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002085 clang_getSpellingLocation(clang_getRangeStart(extent),
2086 0, &start_line, &start_column, 0);
2087 clang_getSpellingLocation(clang_getRangeEnd(extent),
2088 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002089 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
2090 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002091 if (!clang_isInvalid(cursors[i].kind)) {
2092 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002093 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002094 }
2095 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002096 }
2097 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002098 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002099
2100 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002101 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002102 clang_disposeTranslationUnit(TU);
2103 clang_disposeIndex(CIdx);
2104 free(filename);
2105 free_remapped_files(unsaved_files, num_unsaved_files);
2106 return errorCode;
2107}
2108
Ted Kremenek0d435192009-11-17 18:13:31 +00002109/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002110/* USR printing. */
2111/******************************************************************************/
2112
2113static int insufficient_usr(const char *kind, const char *usage) {
2114 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2115 return 1;
2116}
2117
2118static unsigned isUSR(const char *s) {
2119 return s[0] == 'c' && s[1] == ':';
2120}
2121
2122static int not_usr(const char *s, const char *arg) {
2123 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2124 return 1;
2125}
2126
2127static void print_usr(CXString usr) {
2128 const char *s = clang_getCString(usr);
2129 printf("%s\n", s);
2130 clang_disposeString(usr);
2131}
2132
2133static void display_usrs() {
2134 fprintf(stderr, "-print-usrs options:\n"
2135 " ObjCCategory <class name> <category name>\n"
2136 " ObjCClass <class name>\n"
2137 " ObjCIvar <ivar name> <class USR>\n"
2138 " ObjCMethod <selector> [0=class method|1=instance method] "
2139 "<class USR>\n"
2140 " ObjCProperty <property name> <class USR>\n"
2141 " ObjCProtocol <protocol name>\n");
2142}
2143
2144int print_usrs(const char **I, const char **E) {
2145 while (I != E) {
2146 const char *kind = *I;
2147 unsigned len = strlen(kind);
2148 switch (len) {
2149 case 8:
2150 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2151 if (I + 2 >= E)
2152 return insufficient_usr(kind, "<ivar name> <class USR>");
2153 if (!isUSR(I[2]))
2154 return not_usr("<class USR>", I[2]);
2155 else {
2156 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002157 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002158 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002159 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2160 }
2161
2162 I += 3;
2163 continue;
2164 }
2165 break;
2166 case 9:
2167 if (memcmp(kind, "ObjCClass", 9) == 0) {
2168 if (I + 1 >= E)
2169 return insufficient_usr(kind, "<class name>");
2170 print_usr(clang_constructUSR_ObjCClass(I[1]));
2171 I += 2;
2172 continue;
2173 }
2174 break;
2175 case 10:
2176 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2177 if (I + 3 >= E)
2178 return insufficient_usr(kind, "<method selector> "
2179 "[0=class method|1=instance method] <class USR>");
2180 if (!isUSR(I[3]))
2181 return not_usr("<class USR>", I[3]);
2182 else {
2183 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002184 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002185 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002186 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2187 }
2188 I += 4;
2189 continue;
2190 }
2191 break;
2192 case 12:
2193 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2194 if (I + 2 >= E)
2195 return insufficient_usr(kind, "<class name> <category name>");
2196 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2197 I += 3;
2198 continue;
2199 }
2200 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2201 if (I + 1 >= E)
2202 return insufficient_usr(kind, "<protocol name>");
2203 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2204 I += 2;
2205 continue;
2206 }
2207 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2208 if (I + 2 >= E)
2209 return insufficient_usr(kind, "<property name> <class USR>");
2210 if (!isUSR(I[2]))
2211 return not_usr("<class USR>", I[2]);
2212 else {
2213 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002214 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002215 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002216 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2217 }
2218 I += 3;
2219 continue;
2220 }
2221 break;
2222 default:
2223 break;
2224 }
2225 break;
2226 }
2227
2228 if (I != E) {
2229 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2230 display_usrs();
2231 return 1;
2232 }
2233 return 0;
2234}
2235
2236int print_usrs_file(const char *file_name) {
2237 char line[2048];
2238 const char *args[128];
2239 unsigned numChars = 0;
2240
2241 FILE *fp = fopen(file_name, "r");
2242 if (!fp) {
2243 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2244 return 1;
2245 }
2246
2247 /* This code is not really all that safe, but it works fine for testing. */
2248 while (!feof(fp)) {
2249 char c = fgetc(fp);
2250 if (c == '\n') {
2251 unsigned i = 0;
2252 const char *s = 0;
2253
2254 if (numChars == 0)
2255 continue;
2256
2257 line[numChars] = '\0';
2258 numChars = 0;
2259
2260 if (line[0] == '/' && line[1] == '/')
2261 continue;
2262
2263 s = strtok(line, " ");
2264 while (s) {
2265 args[i] = s;
2266 ++i;
2267 s = strtok(0, " ");
2268 }
2269 if (print_usrs(&args[0], &args[i]))
2270 return 1;
2271 }
2272 else
2273 line[numChars++] = c;
2274 }
2275
2276 fclose(fp);
2277 return 0;
2278}
2279
2280/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002281/* Command line processing. */
2282/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002283int write_pch_file(const char *filename, int argc, const char *argv[]) {
2284 CXIndex Idx;
2285 CXTranslationUnit TU;
2286 struct CXUnsavedFile *unsaved_files = 0;
2287 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002288 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002289
2290 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2291
2292 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2293 clang_disposeIndex(Idx);
2294 return -1;
2295 }
2296
2297 TU = clang_parseTranslationUnit(Idx, 0,
2298 argv + num_unsaved_files,
2299 argc - num_unsaved_files,
2300 unsaved_files,
2301 num_unsaved_files,
2302 CXTranslationUnit_Incomplete);
2303 if (!TU) {
2304 fprintf(stderr, "Unable to load translation unit!\n");
2305 free_remapped_files(unsaved_files, num_unsaved_files);
2306 clang_disposeIndex(Idx);
2307 return 1;
2308 }
2309
Douglas Gregor39c411f2011-07-06 16:43:36 +00002310 switch (clang_saveTranslationUnit(TU, filename,
2311 clang_defaultSaveOptions(TU))) {
2312 case CXSaveError_None:
2313 break;
2314
2315 case CXSaveError_TranslationErrors:
2316 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2317 filename);
2318 result = 2;
2319 break;
2320
2321 case CXSaveError_InvalidTU:
2322 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2323 filename);
2324 result = 3;
2325 break;
2326
2327 case CXSaveError_Unknown:
2328 default:
2329 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2330 result = 1;
2331 break;
2332 }
2333
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002334 clang_disposeTranslationUnit(TU);
2335 free_remapped_files(unsaved_files, num_unsaved_files);
2336 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002337 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002338}
2339
2340/******************************************************************************/
2341/* Command line processing. */
2342/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002343
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002344static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00002345 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002346 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00002347 if (strcmp(s, "-usrs") == 0)
2348 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002349 if (strncmp(s, "-memory-usage", 13) == 0)
2350 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002351 return NULL;
2352}
2353
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002354static void print_usage(void) {
2355 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00002356 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002357 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002358 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002359 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002360 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002361 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002362 "[FileCheck prefix]\n");
2363 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00002364 " c-index-test -test-load-tu <AST file> <symbol filter> "
2365 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00002366 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
2367 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002368 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002369 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002370 " c-index-test -test-load-source-memory-usage "
2371 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00002372 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
2373 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00002374 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002375 " c-index-test -test-load-source-usrs-memory-usage "
2376 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00002377 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
2378 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002379 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00002380 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00002381 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002382 " c-index-test -test-print-typekind {<args>}*\n"
2383 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002384 " c-index-test -print-usr-file <file>\n"
2385 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00002386 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00002387 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00002388 " all - load all symbols, including those from PCH\n"
2389 " local - load all symbols except those in PCH\n"
2390 " category - only load ObjC categories (non-PCH)\n"
2391 " interface - only load ObjC interfaces (non-PCH)\n"
2392 " protocol - only load ObjC protocols (non-PCH)\n"
2393 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00002394 " typedef - only load typdefs (non-PCH)\n"
2395 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002396}
2397
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002398/***/
2399
2400int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002401 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002402 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00002403 return perform_code_completion(argc, argv, 0);
2404 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
2405 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002406 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
2407 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002408 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
2409 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002410 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
2411 return index_file(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00002412 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002413 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00002414 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00002415 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
2416 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00002417 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00002418 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
2419 CXCursorVisitor I = GetVisitor(argv[1] + 25);
2420 if (I) {
2421 int trials = atoi(argv[2]);
2422 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
2423 NULL);
2424 }
2425 }
Ted Kremenek7d405622010-01-12 23:34:26 +00002426 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00002427 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002428
2429 PostVisitTU postVisit = 0;
2430 if (strstr(argv[1], "-memory-usage"))
2431 postVisit = PrintMemoryUsage;
2432
Ted Kremenek7d405622010-01-12 23:34:26 +00002433 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00002434 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
2435 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00002436 }
2437 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00002438 return perform_file_scan(argv[2], argv[3],
2439 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002440 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
2441 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00002442 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
2443 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
2444 PrintInclusionStack);
2445 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
2446 return perform_test_load_tu(argv[2], "all", NULL, NULL,
2447 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00002448 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
2449 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
2450 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00002451 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
2452 return perform_test_load_source(argc - 2, argv + 2, "all",
2453 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002454 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
2455 if (argc > 2)
2456 return print_usrs(argv + 2, argv + argc);
2457 else {
2458 display_usrs();
2459 return 1;
2460 }
2461 }
2462 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
2463 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002464 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
2465 return write_pch_file(argv[2], argc - 3, argv + 3);
2466
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002467 print_usage();
2468 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00002469}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002470
2471/***/
2472
2473/* We intentionally run in a separate thread to ensure we at least minimal
2474 * testing of a multithreaded environment (for example, having a reduced stack
2475 * size). */
2476
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002477typedef struct thread_info {
2478 int argc;
2479 const char **argv;
2480 int result;
2481} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00002482void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002483 thread_info *client_data = client_data_v;
2484 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002485}
2486
2487int main(int argc, const char **argv) {
2488 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002489
Douglas Gregor61605982010-10-27 16:00:01 +00002490 if (getenv("CINDEXTEST_NOTHREADS"))
2491 return cindextest_main(argc, argv);
2492
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002493 client_data.argc = argc;
2494 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00002495 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00002496 return client_data.result;
2497}