blob: 1f78a6ffaa0eb263926befb13a024ba5e17f63fd [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
Douglas Gregordd3e5542011-05-04 00:14:37 +0000599 VisitorData *Data = (VisitorData *)d;
600
Ted Kremenek3bed5272010-03-03 06:37:58 +0000601 if (clang_isInvalid(clang_getCursorKind(cursor)))
602 return CXChildVisit_Recurse;
603
604 switch (clang_getCursorLinkage(cursor)) {
605 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000606 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
607 case CXLinkage_Internal: linkage = "Internal"; break;
608 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
609 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000610 }
611
612 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000613 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000614 printf("linkage=%s\n", linkage);
615 }
616
617 return CXChildVisit_Recurse;
618}
619
620/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000621/* Typekind testing. */
622/******************************************************************************/
623
624static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
625 CXClientData d) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000626 VisitorData *Data = (VisitorData *)d;
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000627
628 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
629 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000630 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000631 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000632 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000633 if (clang_isConstQualifiedType(T))
634 printf(" const");
635 if (clang_isVolatileQualifiedType(T))
636 printf(" volatile");
637 if (clang_isRestrictQualifiedType(T))
638 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000639 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000640 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000641 {
642 CXType CT = clang_getCanonicalType(T);
643 if (!clang_equalTypes(T, CT)) {
644 CXString CS = clang_getTypeKindSpelling(CT.kind);
645 printf(" [canonical=%s]", clang_getCString(CS));
646 clang_disposeString(CS);
647 }
648 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000649 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000650 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000651 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000652 if (RT.kind != CXType_Invalid) {
653 CXString RS = clang_getTypeKindSpelling(RT.kind);
654 printf(" [result=%s]", clang_getCString(RS));
655 clang_disposeString(RS);
656 }
657 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000658 /* Print if this is a non-POD type. */
659 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000660
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000661 printf("\n");
662 }
663 return CXChildVisit_Recurse;
664}
665
666
667/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000668/* Loading ASTs/source. */
669/******************************************************************************/
670
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000671static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000672 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000673 CXCursorVisitor Visitor,
674 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000676 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000677 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000678
679 if (Visitor) {
680 enum CXCursorKind K = CXCursor_NotImplemented;
681 enum CXCursorKind *ck = &K;
682 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000684 /* Perform some simple filtering. */
685 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000686 else if (!strcmp(filter, "all-display") ||
687 !strcmp(filter, "local-display")) {
688 ck = NULL;
689 want_display_name = 1;
690 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000691 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000692 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
693 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
694 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
695 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
696 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
697 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
698 else {
699 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
700 return 1;
701 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000702
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000703 Data.TU = TU;
704 Data.Filter = ck;
705 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000706 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707
Ted Kremenekce2ae882010-01-26 17:59:48 +0000708 if (PV)
709 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000710
Douglas Gregora88084b2010-02-18 18:08:43 +0000711 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000712 clang_disposeTranslationUnit(TU);
713 return 0;
714}
715
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000716int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000717 const char *prefix, CXCursorVisitor Visitor,
718 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000719 CXIndex Idx;
720 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000721 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000722 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000723 !strcmp(filter, "local") ? 1 : 0,
724 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000725
Ted Kremenek020a0952010-02-11 07:41:25 +0000726 if (!CreateTranslationUnit(Idx, file, &TU)) {
727 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000728 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000729 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000730
Ted Kremenek020a0952010-02-11 07:41:25 +0000731 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
732 clang_disposeIndex(Idx);
733 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000734}
735
Ted Kremenekce2ae882010-01-26 17:59:48 +0000736int perform_test_load_source(int argc, const char **argv,
737 const char *filter, CXCursorVisitor Visitor,
738 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000739 CXIndex Idx;
740 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000741 struct CXUnsavedFile *unsaved_files = 0;
742 int num_unsaved_files = 0;
743 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000744
Daniel Dunbarada487d2009-12-01 02:03:10 +0000745 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000746 (!strcmp(filter, "local") ||
747 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000748 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000749
Ted Kremenek020a0952010-02-11 07:41:25 +0000750 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
751 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000752 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000753 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000754
Douglas Gregordca8ee82011-05-06 16:33:08 +0000755 TU = clang_parseTranslationUnit(Idx, 0,
756 argv + num_unsaved_files,
757 argc - num_unsaved_files,
758 unsaved_files, num_unsaved_files,
759 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000760 if (!TU) {
761 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000762 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000763 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000764 return 1;
765 }
766
Ted Kremenekce2ae882010-01-26 17:59:48 +0000767 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000768 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000769 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000770 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000771}
772
Douglas Gregorabc563f2010-07-19 21:46:24 +0000773int perform_test_reparse_source(int argc, const char **argv, int trials,
774 const char *filter, CXCursorVisitor Visitor,
775 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000776 CXIndex Idx;
777 CXTranslationUnit TU;
778 struct CXUnsavedFile *unsaved_files = 0;
779 int num_unsaved_files = 0;
780 int result;
781 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000782 int remap_after_trial = 0;
783 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000784
785 Idx = clang_createIndex(/* excludeDeclsFromPCH */
786 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000787 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000788
Douglas Gregorabc563f2010-07-19 21:46:24 +0000789 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
790 clang_disposeIndex(Idx);
791 return -1;
792 }
793
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000794 /* Load the initial translation unit -- we do this without honoring remapped
795 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000796 TU = clang_parseTranslationUnit(Idx, 0,
797 argv + num_unsaved_files,
798 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000799 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000800 if (!TU) {
801 fprintf(stderr, "Unable to load translation unit!\n");
802 free_remapped_files(unsaved_files, num_unsaved_files);
803 clang_disposeIndex(Idx);
804 return 1;
805 }
806
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000807 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
808 remap_after_trial =
809 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
810 }
811
Douglas Gregorabc563f2010-07-19 21:46:24 +0000812 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +0000813 if (clang_reparseTranslationUnit(TU,
814 trial >= remap_after_trial ? num_unsaved_files : 0,
815 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000816 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000817 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000818 clang_disposeTranslationUnit(TU);
819 free_remapped_files(unsaved_files, num_unsaved_files);
820 clang_disposeIndex(Idx);
821 return -1;
822 }
823 }
824
825 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
826 free_remapped_files(unsaved_files, num_unsaved_files);
827 clang_disposeIndex(Idx);
828 return result;
829}
830
Ted Kremenek0d435192009-11-17 18:13:31 +0000831/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000832/* Logic for testing clang_getCursor(). */
833/******************************************************************************/
834
Douglas Gregordd3e5542011-05-04 00:14:37 +0000835static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000836 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000837 unsigned end_line, unsigned end_col,
838 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000839 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000840 if (prefix)
841 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000842 PrintExtent(stdout, start_line, start_col, end_line, end_col);
843 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000844 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000845 printf("\n");
846}
847
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000848static int perform_file_scan(const char *ast_file, const char *source_file,
849 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000850 CXIndex Idx;
851 CXTranslationUnit TU;
852 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000853 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000854 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000855 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000856 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000857
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000858 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
859 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000860 fprintf(stderr, "Could not create Index\n");
861 return 1;
862 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000863
Ted Kremenek1c6da172009-11-17 19:37:36 +0000864 if (!CreateTranslationUnit(Idx, ast_file, &TU))
865 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000866
Ted Kremenek1c6da172009-11-17 19:37:36 +0000867 if ((fp = fopen(source_file, "r")) == NULL) {
868 fprintf(stderr, "Could not open '%s'\n", source_file);
869 return 1;
870 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000871
Douglas Gregorb9790342010-01-22 21:44:22 +0000872 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000873 for (;;) {
874 CXCursor cursor;
875 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000876
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000877 if (c == '\n') {
878 ++line;
879 col = 1;
880 } else
881 ++col;
882
883 /* Check the cursor at this position, and dump the previous one if we have
884 * found something new.
885 */
886 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
887 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
888 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000889 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000890 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000891 start_line = line;
892 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000893 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000894 if (c == EOF)
895 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000896
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000897 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000898 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000899
Ted Kremenek1c6da172009-11-17 19:37:36 +0000900 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000901 clang_disposeTranslationUnit(TU);
902 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000903 return 0;
904}
905
906/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000907/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000908/******************************************************************************/
909
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000910/* Parse file:line:column from the input string. Returns 0 on success, non-zero
911 on failure. If successful, the pointer *filename will contain newly-allocated
912 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000913int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000914 unsigned *column, unsigned *second_line,
915 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000916 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000917 const char *last_colon = strrchr(input, ':');
918 unsigned values[4], i;
919 unsigned num_values = (second_line && second_column)? 4 : 2;
920
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000921 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000922 if (!last_colon || last_colon == input) {
923 if (num_values == 4)
924 fprintf(stderr, "could not parse filename:line:column:line:column in "
925 "'%s'\n", input);
926 else
927 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000928 return 1;
929 }
930
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000931 for (i = 0; i != num_values; ++i) {
932 const char *prev_colon;
933
934 /* Parse the next line or column. */
935 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
936 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000937 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000938 (i % 2 ? "column" : "line"), input);
939 return 1;
940 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000941
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000942 if (i + 1 == num_values)
943 break;
944
945 /* Find the previous colon. */
946 prev_colon = last_colon - 1;
947 while (prev_colon != input && *prev_colon != ':')
948 --prev_colon;
949 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000950 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000952 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000953 }
954
955 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000956 }
957
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000958 *line = values[0];
959 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000960
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000961 if (second_line && second_column) {
962 *second_line = values[2];
963 *second_column = values[3];
964 }
965
Douglas Gregor88d23952009-11-09 18:19:57 +0000966 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000967 *filename = (char*)malloc(last_colon - input + 1);
968 memcpy(*filename, input, last_colon - input);
969 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000970 return 0;
971}
972
973const char *
974clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
975 switch (Kind) {
976 case CXCompletionChunk_Optional: return "Optional";
977 case CXCompletionChunk_TypedText: return "TypedText";
978 case CXCompletionChunk_Text: return "Text";
979 case CXCompletionChunk_Placeholder: return "Placeholder";
980 case CXCompletionChunk_Informative: return "Informative";
981 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
982 case CXCompletionChunk_LeftParen: return "LeftParen";
983 case CXCompletionChunk_RightParen: return "RightParen";
984 case CXCompletionChunk_LeftBracket: return "LeftBracket";
985 case CXCompletionChunk_RightBracket: return "RightBracket";
986 case CXCompletionChunk_LeftBrace: return "LeftBrace";
987 case CXCompletionChunk_RightBrace: return "RightBrace";
988 case CXCompletionChunk_LeftAngle: return "LeftAngle";
989 case CXCompletionChunk_RightAngle: return "RightAngle";
990 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000991 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000992 case CXCompletionChunk_Colon: return "Colon";
993 case CXCompletionChunk_SemiColon: return "SemiColon";
994 case CXCompletionChunk_Equal: return "Equal";
995 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
996 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000997 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000998
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000999 return "Unknown";
1000}
1001
Douglas Gregor3ac73852009-11-09 16:04:45 +00001002void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001003 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001004
Douglas Gregor3ac73852009-11-09 16:04:45 +00001005 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001006 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001007 CXString text;
1008 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001009 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001010 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001011
Douglas Gregor3ac73852009-11-09 16:04:45 +00001012 if (Kind == CXCompletionChunk_Optional) {
1013 fprintf(file, "{Optional ");
1014 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001015 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001016 file);
1017 fprintf(file, "}");
1018 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001019 }
1020
1021 if (Kind == CXCompletionChunk_VerticalSpace) {
1022 fprintf(file, "{VerticalSpace }");
1023 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001024 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001025
Douglas Gregord5a20892009-11-09 17:05:28 +00001026 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001027 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001028 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001029 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001030 cstr ? cstr : "");
1031 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001032 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001033
Douglas Gregor3ac73852009-11-09 16:04:45 +00001034}
1035
1036void print_completion_result(CXCompletionResult *completion_result,
1037 CXClientData client_data) {
1038 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001039 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
1040
1041 fprintf(file, "%s:", clang_getCString(ks));
1042 clang_disposeString(ks);
1043
Douglas Gregor3ac73852009-11-09 16:04:45 +00001044 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001045 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001046 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001047 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1048 case CXAvailability_Available:
1049 break;
1050
1051 case CXAvailability_Deprecated:
1052 fprintf(file, " (deprecated)");
1053 break;
1054
1055 case CXAvailability_NotAvailable:
1056 fprintf(file, " (unavailable)");
1057 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001058
1059 case CXAvailability_NotAccessible:
1060 fprintf(file, " (inaccessible)");
1061 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001062 }
1063 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001064}
1065
Douglas Gregor3da626b2011-07-07 16:03:39 +00001066void print_completion_contexts(unsigned long long contexts, FILE *file) {
1067 fprintf(file, "Completion contexts:\n");
1068 if (contexts == CXCompletionContext_Unknown) {
1069 fprintf(file, "Unknown\n");
1070 }
1071 if (contexts & CXCompletionContext_AnyType) {
1072 fprintf(file, "Any type\n");
1073 }
1074 if (contexts & CXCompletionContext_AnyValue) {
1075 fprintf(file, "Any value\n");
1076 }
1077 if (contexts & CXCompletionContext_ObjCObjectValue) {
1078 fprintf(file, "Objective-C object value\n");
1079 }
1080 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1081 fprintf(file, "Objective-C selector value\n");
1082 }
1083 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1084 fprintf(file, "C++ class type value\n");
1085 }
1086 if (contexts & CXCompletionContext_DotMemberAccess) {
1087 fprintf(file, "Dot member access\n");
1088 }
1089 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1090 fprintf(file, "Arrow member access\n");
1091 }
1092 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1093 fprintf(file, "Objective-C property access\n");
1094 }
1095 if (contexts & CXCompletionContext_EnumTag) {
1096 fprintf(file, "Enum tag\n");
1097 }
1098 if (contexts & CXCompletionContext_UnionTag) {
1099 fprintf(file, "Union tag\n");
1100 }
1101 if (contexts & CXCompletionContext_StructTag) {
1102 fprintf(file, "Struct tag\n");
1103 }
1104 if (contexts & CXCompletionContext_ClassTag) {
1105 fprintf(file, "Class name\n");
1106 }
1107 if (contexts & CXCompletionContext_Namespace) {
1108 fprintf(file, "Namespace or namespace alias\n");
1109 }
1110 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1111 fprintf(file, "Nested name specifier\n");
1112 }
1113 if (contexts & CXCompletionContext_ObjCInterface) {
1114 fprintf(file, "Objective-C interface\n");
1115 }
1116 if (contexts & CXCompletionContext_ObjCProtocol) {
1117 fprintf(file, "Objective-C protocol\n");
1118 }
1119 if (contexts & CXCompletionContext_ObjCCategory) {
1120 fprintf(file, "Objective-C category\n");
1121 }
1122 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1123 fprintf(file, "Objective-C instance method\n");
1124 }
1125 if (contexts & CXCompletionContext_ObjCClassMessage) {
1126 fprintf(file, "Objective-C class method\n");
1127 }
1128 if (contexts & CXCompletionContext_ObjCSelectorName) {
1129 fprintf(file, "Objective-C selector name\n");
1130 }
1131 if (contexts & CXCompletionContext_MacroName) {
1132 fprintf(file, "Macro name\n");
1133 }
1134 if (contexts & CXCompletionContext_NaturalLanguage) {
1135 fprintf(file, "Natural language\n");
1136 }
1137}
1138
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001139int my_stricmp(const char *s1, const char *s2) {
1140 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001141 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001142 if (c1 < c2)
1143 return -1;
1144 else if (c1 > c2)
1145 return 1;
1146
1147 ++s1;
1148 ++s2;
1149 }
1150
1151 if (*s1)
1152 return 1;
1153 else if (*s2)
1154 return -1;
1155 return 0;
1156}
1157
Douglas Gregor1982c182010-07-12 18:38:41 +00001158int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001159 const char *input = argv[1];
1160 char *filename = 0;
1161 unsigned line;
1162 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001163 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001164 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001165 struct CXUnsavedFile *unsaved_files = 0;
1166 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001167 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001168 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001169 unsigned I, Repeats = 1;
1170 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1171
1172 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1173 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001174
Douglas Gregor1982c182010-07-12 18:38:41 +00001175 if (timing_only)
1176 input += strlen("-code-completion-timing=");
1177 else
1178 input += strlen("-code-completion-at=");
1179
Ted Kremeneke68fff62010-02-17 00:41:32 +00001180 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001181 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001182 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001183
Douglas Gregor735df882009-12-02 09:21:34 +00001184 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1185 return -1;
1186
Douglas Gregor32be4a52010-10-11 21:37:58 +00001187 CIdx = clang_createIndex(0, 0);
1188
1189 if (getenv("CINDEXTEST_EDITING"))
1190 Repeats = 5;
1191
1192 TU = clang_parseTranslationUnit(CIdx, 0,
1193 argv + num_unsaved_files + 2,
1194 argc - num_unsaved_files - 2,
1195 0, 0, getDefaultParsingOptions());
1196 if (!TU) {
1197 fprintf(stderr, "Unable to load translation unit!\n");
1198 return 1;
1199 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001200
1201 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1202 fprintf(stderr, "Unable to reparse translation init!\n");
1203 return 1;
1204 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001205
1206 for (I = 0; I != Repeats; ++I) {
1207 results = clang_codeCompleteAt(TU, filename, line, column,
1208 unsaved_files, num_unsaved_files,
1209 completionOptions);
1210 if (!results) {
1211 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001212 return 1;
1213 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001214 if (I != Repeats-1)
1215 clang_disposeCodeCompleteResults(results);
1216 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001217
Douglas Gregorec6762c2009-12-18 16:20:58 +00001218 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001219 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001220 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001221 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001222 CXString objCSelector;
1223 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001224 if (!timing_only) {
1225 /* Sort the code-completion results based on the typed text. */
1226 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1227
Douglas Gregor1982c182010-07-12 18:38:41 +00001228 for (i = 0; i != n; ++i)
1229 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001230 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001231 n = clang_codeCompleteGetNumDiagnostics(results);
1232 for (i = 0; i != n; ++i) {
1233 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1234 PrintDiagnostic(diag);
1235 clang_disposeDiagnostic(diag);
1236 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001237
1238 contexts = clang_codeCompleteGetContexts(results);
1239 print_completion_contexts(contexts, stdout);
1240
Douglas Gregor0a47d692011-07-26 15:24:30 +00001241 containerKind = clang_codeCompleteGetContainerKind(results,
1242 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001243
1244 if (containerKind != CXCursor_InvalidCode) {
1245 /* We have found a container */
1246 CXString containerUSR, containerKindSpelling;
1247 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1248 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1249 clang_disposeString(containerKindSpelling);
1250
1251 if (containerIsIncomplete) {
1252 printf("Container is incomplete\n");
1253 }
1254 else {
1255 printf("Container is complete\n");
1256 }
1257
1258 containerUSR = clang_codeCompleteGetContainerUSR(results);
1259 printf("Container USR: %s\n", clang_getCString(containerUSR));
1260 clang_disposeString(containerUSR);
1261 }
1262
Douglas Gregor0a47d692011-07-26 15:24:30 +00001263 objCSelector = clang_codeCompleteGetObjCSelector(results);
1264 selectorString = clang_getCString(objCSelector);
1265 if (selectorString && strlen(selectorString) > 0) {
1266 printf("Objective-C selector: %s\n", selectorString);
1267 }
1268 clang_disposeString(objCSelector);
1269
Douglas Gregorec6762c2009-12-18 16:20:58 +00001270 clang_disposeCodeCompleteResults(results);
1271 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001272 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001273 clang_disposeIndex(CIdx);
1274 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001275
Douglas Gregor735df882009-12-02 09:21:34 +00001276 free_remapped_files(unsaved_files, num_unsaved_files);
1277
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001278 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001279}
1280
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001281typedef struct {
1282 char *filename;
1283 unsigned line;
1284 unsigned column;
1285} CursorSourceLocation;
1286
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001287static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001288 CXIndex CIdx;
1289 int errorCode;
1290 struct CXUnsavedFile *unsaved_files = 0;
1291 int num_unsaved_files = 0;
1292 CXTranslationUnit TU;
1293 CXCursor Cursor;
1294 CursorSourceLocation *Locations = 0;
1295 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001296 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001297 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001298
Ted Kremeneke68fff62010-02-17 00:41:32 +00001299 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001300 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1301 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001302
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001303 /* Parse the locations. */
1304 assert(NumLocations > 0 && "Unable to count locations?");
1305 Locations = (CursorSourceLocation *)malloc(
1306 NumLocations * sizeof(CursorSourceLocation));
1307 for (Loc = 0; Loc < NumLocations; ++Loc) {
1308 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001309 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1310 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001311 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001312 return errorCode;
1313 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001314
1315 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001316 &num_unsaved_files))
1317 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001318
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001319 if (getenv("CINDEXTEST_EDITING"))
1320 Repeats = 5;
1321
1322 /* Parse the translation unit. When we're testing clang_getCursor() after
1323 reparsing, don't remap unsaved files until the second parse. */
1324 CIdx = clang_createIndex(1, 1);
1325 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1326 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001327 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001328 unsaved_files,
1329 Repeats > 1? 0 : num_unsaved_files,
1330 getDefaultParsingOptions());
1331
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001332 if (!TU) {
1333 fprintf(stderr, "unable to parse input\n");
1334 return -1;
1335 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001336
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001337 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001338 if (Repeats > 1 &&
1339 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1340 clang_defaultReparseOptions(TU))) {
1341 clang_disposeTranslationUnit(TU);
1342 return 1;
1343 }
1344
1345 for (Loc = 0; Loc < NumLocations; ++Loc) {
1346 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1347 if (!file)
1348 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001349
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001350 Cursor = clang_getCursor(TU,
1351 clang_getLocation(TU, file, Locations[Loc].line,
1352 Locations[Loc].column));
1353 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001354 CXCompletionString completionString = clang_getCursorCompletionString(
1355 Cursor);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001356 PrintCursor(Cursor);
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001357 if (completionString != NULL) {
1358 printf("\nCompletion string: ");
1359 print_completion_string(completionString, stdout);
1360 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001361 printf("\n");
1362 free(Locations[Loc].filename);
1363 }
1364 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001365 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001366
Douglas Gregora88084b2010-02-18 18:08:43 +00001367 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001368 clang_disposeTranslationUnit(TU);
1369 clang_disposeIndex(CIdx);
1370 free(Locations);
1371 free_remapped_files(unsaved_files, num_unsaved_files);
1372 return 0;
1373}
1374
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001375static enum CXVisitorResult findFileRefsVisit(void *context,
1376 CXCursor cursor, CXSourceRange range) {
1377 if (clang_Range_isNull(range))
1378 return CXVisit_Continue;
1379
1380 PrintCursor(cursor);
1381 PrintRange(range, "");
1382 printf("\n");
1383 return CXVisit_Continue;
1384}
1385
1386static int find_file_refs_at(int argc, const char **argv) {
1387 CXIndex CIdx;
1388 int errorCode;
1389 struct CXUnsavedFile *unsaved_files = 0;
1390 int num_unsaved_files = 0;
1391 CXTranslationUnit TU;
1392 CXCursor Cursor;
1393 CursorSourceLocation *Locations = 0;
1394 unsigned NumLocations = 0, Loc;
1395 unsigned Repeats = 1;
1396 unsigned I;
1397
1398 /* Count the number of locations. */
1399 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1400 ++NumLocations;
1401
1402 /* Parse the locations. */
1403 assert(NumLocations > 0 && "Unable to count locations?");
1404 Locations = (CursorSourceLocation *)malloc(
1405 NumLocations * sizeof(CursorSourceLocation));
1406 for (Loc = 0; Loc < NumLocations; ++Loc) {
1407 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1408 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1409 &Locations[Loc].line,
1410 &Locations[Loc].column, 0, 0)))
1411 return errorCode;
1412 }
1413
1414 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1415 &num_unsaved_files))
1416 return -1;
1417
1418 if (getenv("CINDEXTEST_EDITING"))
1419 Repeats = 5;
1420
1421 /* Parse the translation unit. When we're testing clang_getCursor() after
1422 reparsing, don't remap unsaved files until the second parse. */
1423 CIdx = clang_createIndex(1, 1);
1424 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1425 argv + num_unsaved_files + 1 + NumLocations,
1426 argc - num_unsaved_files - 2 - NumLocations,
1427 unsaved_files,
1428 Repeats > 1? 0 : num_unsaved_files,
1429 getDefaultParsingOptions());
1430
1431 if (!TU) {
1432 fprintf(stderr, "unable to parse input\n");
1433 return -1;
1434 }
1435
1436 for (I = 0; I != Repeats; ++I) {
1437 if (Repeats > 1 &&
1438 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1439 clang_defaultReparseOptions(TU))) {
1440 clang_disposeTranslationUnit(TU);
1441 return 1;
1442 }
1443
1444 for (Loc = 0; Loc < NumLocations; ++Loc) {
1445 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1446 if (!file)
1447 continue;
1448
1449 Cursor = clang_getCursor(TU,
1450 clang_getLocation(TU, file, Locations[Loc].line,
1451 Locations[Loc].column));
1452 if (I + 1 == Repeats) {
1453 PrintCursor(Cursor);
1454 printf("\n");
1455 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
1456 clang_findReferencesInFile(Cursor, file, visitor);
1457 free(Locations[Loc].filename);
1458 }
1459 }
1460 }
1461
1462 PrintDiagnostics(TU);
1463 clang_disposeTranslationUnit(TU);
1464 clang_disposeIndex(CIdx);
1465 free(Locations);
1466 free_remapped_files(unsaved_files, num_unsaved_files);
1467 return 0;
1468}
1469
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001470int perform_token_annotation(int argc, const char **argv) {
1471 const char *input = argv[1];
1472 char *filename = 0;
1473 unsigned line, second_line;
1474 unsigned column, second_column;
1475 CXIndex CIdx;
1476 CXTranslationUnit TU = 0;
1477 int errorCode;
1478 struct CXUnsavedFile *unsaved_files = 0;
1479 int num_unsaved_files = 0;
1480 CXToken *tokens;
1481 unsigned num_tokens;
1482 CXSourceRange range;
1483 CXSourceLocation startLoc, endLoc;
1484 CXFile file = 0;
1485 CXCursor *cursors = 0;
1486 unsigned i;
1487
1488 input += strlen("-test-annotate-tokens=");
1489 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1490 &second_line, &second_column)))
1491 return errorCode;
1492
1493 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1494 return -1;
1495
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001496 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00001497 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1498 argv + num_unsaved_files + 2,
1499 argc - num_unsaved_files - 3,
1500 unsaved_files,
1501 num_unsaved_files,
1502 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001503 if (!TU) {
1504 fprintf(stderr, "unable to parse input\n");
1505 clang_disposeIndex(CIdx);
1506 free(filename);
1507 free_remapped_files(unsaved_files, num_unsaved_files);
1508 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001509 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001510 errorCode = 0;
1511
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00001512 if (getenv("CINDEXTEST_EDITING")) {
1513 for (i = 0; i < 5; ++i) {
1514 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1515 clang_defaultReparseOptions(TU))) {
1516 fprintf(stderr, "Unable to reparse translation unit!\n");
1517 errorCode = -1;
1518 goto teardown;
1519 }
1520 }
1521 }
1522
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001523 file = clang_getFile(TU, filename);
1524 if (!file) {
1525 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1526 errorCode = -1;
1527 goto teardown;
1528 }
1529
1530 startLoc = clang_getLocation(TU, file, line, column);
1531 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001532 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001533 column);
1534 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001535 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001536 }
1537
1538 endLoc = clang_getLocation(TU, file, second_line, second_column);
1539 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001540 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001541 second_line, second_column);
1542 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001543 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001544 }
1545
1546 range = clang_getRange(startLoc, endLoc);
1547 clang_tokenize(TU, range, &tokens, &num_tokens);
1548 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1549 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1550 for (i = 0; i != num_tokens; ++i) {
1551 const char *kind = "<unknown>";
1552 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1553 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1554 unsigned start_line, start_column, end_line, end_column;
1555
1556 switch (clang_getTokenKind(tokens[i])) {
1557 case CXToken_Punctuation: kind = "Punctuation"; break;
1558 case CXToken_Keyword: kind = "Keyword"; break;
1559 case CXToken_Identifier: kind = "Identifier"; break;
1560 case CXToken_Literal: kind = "Literal"; break;
1561 case CXToken_Comment: kind = "Comment"; break;
1562 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00001563 clang_getSpellingLocation(clang_getRangeStart(extent),
1564 0, &start_line, &start_column, 0);
1565 clang_getSpellingLocation(clang_getRangeEnd(extent),
1566 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001567 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1568 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001569 if (!clang_isInvalid(cursors[i].kind)) {
1570 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001571 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001572 }
1573 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001574 }
1575 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00001576 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001577
1578 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001579 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001580 clang_disposeTranslationUnit(TU);
1581 clang_disposeIndex(CIdx);
1582 free(filename);
1583 free_remapped_files(unsaved_files, num_unsaved_files);
1584 return errorCode;
1585}
1586
Ted Kremenek0d435192009-11-17 18:13:31 +00001587/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001588/* USR printing. */
1589/******************************************************************************/
1590
1591static int insufficient_usr(const char *kind, const char *usage) {
1592 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1593 return 1;
1594}
1595
1596static unsigned isUSR(const char *s) {
1597 return s[0] == 'c' && s[1] == ':';
1598}
1599
1600static int not_usr(const char *s, const char *arg) {
1601 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1602 return 1;
1603}
1604
1605static void print_usr(CXString usr) {
1606 const char *s = clang_getCString(usr);
1607 printf("%s\n", s);
1608 clang_disposeString(usr);
1609}
1610
1611static void display_usrs() {
1612 fprintf(stderr, "-print-usrs options:\n"
1613 " ObjCCategory <class name> <category name>\n"
1614 " ObjCClass <class name>\n"
1615 " ObjCIvar <ivar name> <class USR>\n"
1616 " ObjCMethod <selector> [0=class method|1=instance method] "
1617 "<class USR>\n"
1618 " ObjCProperty <property name> <class USR>\n"
1619 " ObjCProtocol <protocol name>\n");
1620}
1621
1622int print_usrs(const char **I, const char **E) {
1623 while (I != E) {
1624 const char *kind = *I;
1625 unsigned len = strlen(kind);
1626 switch (len) {
1627 case 8:
1628 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1629 if (I + 2 >= E)
1630 return insufficient_usr(kind, "<ivar name> <class USR>");
1631 if (!isUSR(I[2]))
1632 return not_usr("<class USR>", I[2]);
1633 else {
1634 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001635 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001636 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001637 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1638 }
1639
1640 I += 3;
1641 continue;
1642 }
1643 break;
1644 case 9:
1645 if (memcmp(kind, "ObjCClass", 9) == 0) {
1646 if (I + 1 >= E)
1647 return insufficient_usr(kind, "<class name>");
1648 print_usr(clang_constructUSR_ObjCClass(I[1]));
1649 I += 2;
1650 continue;
1651 }
1652 break;
1653 case 10:
1654 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1655 if (I + 3 >= E)
1656 return insufficient_usr(kind, "<method selector> "
1657 "[0=class method|1=instance method] <class USR>");
1658 if (!isUSR(I[3]))
1659 return not_usr("<class USR>", I[3]);
1660 else {
1661 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001662 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00001663 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001664 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1665 }
1666 I += 4;
1667 continue;
1668 }
1669 break;
1670 case 12:
1671 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1672 if (I + 2 >= E)
1673 return insufficient_usr(kind, "<class name> <category name>");
1674 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1675 I += 3;
1676 continue;
1677 }
1678 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1679 if (I + 1 >= E)
1680 return insufficient_usr(kind, "<protocol name>");
1681 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1682 I += 2;
1683 continue;
1684 }
1685 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1686 if (I + 2 >= E)
1687 return insufficient_usr(kind, "<property name> <class USR>");
1688 if (!isUSR(I[2]))
1689 return not_usr("<class USR>", I[2]);
1690 else {
1691 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001692 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001693 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001694 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1695 }
1696 I += 3;
1697 continue;
1698 }
1699 break;
1700 default:
1701 break;
1702 }
1703 break;
1704 }
1705
1706 if (I != E) {
1707 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1708 display_usrs();
1709 return 1;
1710 }
1711 return 0;
1712}
1713
1714int print_usrs_file(const char *file_name) {
1715 char line[2048];
1716 const char *args[128];
1717 unsigned numChars = 0;
1718
1719 FILE *fp = fopen(file_name, "r");
1720 if (!fp) {
1721 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1722 return 1;
1723 }
1724
1725 /* This code is not really all that safe, but it works fine for testing. */
1726 while (!feof(fp)) {
1727 char c = fgetc(fp);
1728 if (c == '\n') {
1729 unsigned i = 0;
1730 const char *s = 0;
1731
1732 if (numChars == 0)
1733 continue;
1734
1735 line[numChars] = '\0';
1736 numChars = 0;
1737
1738 if (line[0] == '/' && line[1] == '/')
1739 continue;
1740
1741 s = strtok(line, " ");
1742 while (s) {
1743 args[i] = s;
1744 ++i;
1745 s = strtok(0, " ");
1746 }
1747 if (print_usrs(&args[0], &args[i]))
1748 return 1;
1749 }
1750 else
1751 line[numChars++] = c;
1752 }
1753
1754 fclose(fp);
1755 return 0;
1756}
1757
1758/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001759/* Command line processing. */
1760/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001761int write_pch_file(const char *filename, int argc, const char *argv[]) {
1762 CXIndex Idx;
1763 CXTranslationUnit TU;
1764 struct CXUnsavedFile *unsaved_files = 0;
1765 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00001766 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001767
1768 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1769
1770 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1771 clang_disposeIndex(Idx);
1772 return -1;
1773 }
1774
1775 TU = clang_parseTranslationUnit(Idx, 0,
1776 argv + num_unsaved_files,
1777 argc - num_unsaved_files,
1778 unsaved_files,
1779 num_unsaved_files,
1780 CXTranslationUnit_Incomplete);
1781 if (!TU) {
1782 fprintf(stderr, "Unable to load translation unit!\n");
1783 free_remapped_files(unsaved_files, num_unsaved_files);
1784 clang_disposeIndex(Idx);
1785 return 1;
1786 }
1787
Douglas Gregor39c411f2011-07-06 16:43:36 +00001788 switch (clang_saveTranslationUnit(TU, filename,
1789 clang_defaultSaveOptions(TU))) {
1790 case CXSaveError_None:
1791 break;
1792
1793 case CXSaveError_TranslationErrors:
1794 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
1795 filename);
1796 result = 2;
1797 break;
1798
1799 case CXSaveError_InvalidTU:
1800 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
1801 filename);
1802 result = 3;
1803 break;
1804
1805 case CXSaveError_Unknown:
1806 default:
1807 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
1808 result = 1;
1809 break;
1810 }
1811
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001812 clang_disposeTranslationUnit(TU);
1813 free_remapped_files(unsaved_files, num_unsaved_files);
1814 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00001815 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001816}
1817
1818/******************************************************************************/
1819/* Command line processing. */
1820/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001821
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001822static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001823 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001824 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001825 if (strcmp(s, "-usrs") == 0)
1826 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001827 if (strncmp(s, "-memory-usage", 13) == 0)
1828 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001829 return NULL;
1830}
1831
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001832static void print_usage(void) {
1833 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001834 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001835 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001836 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001837 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001838 " c-index-test -test-file-scan <AST file> <source file> "
1839 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001840 " c-index-test -test-load-tu <AST file> <symbol filter> "
1841 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001842 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1843 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001844 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001845 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001846 " c-index-test -test-load-source-memory-usage "
1847 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00001848 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1849 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001850 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001851 " c-index-test -test-load-source-usrs-memory-usage "
1852 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001853 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1854 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001855 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00001856 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001857 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001858 " c-index-test -test-print-typekind {<args>}*\n"
1859 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001860 " c-index-test -print-usr-file <file>\n"
1861 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001862 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001863 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001864 " all - load all symbols, including those from PCH\n"
1865 " local - load all symbols except those in PCH\n"
1866 " category - only load ObjC categories (non-PCH)\n"
1867 " interface - only load ObjC interfaces (non-PCH)\n"
1868 " protocol - only load ObjC protocols (non-PCH)\n"
1869 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001870 " typedef - only load typdefs (non-PCH)\n"
1871 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001872}
1873
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001874/***/
1875
1876int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001877 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001878 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001879 return perform_code_completion(argc, argv, 0);
1880 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1881 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001882 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1883 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001884 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
1885 return find_file_refs_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001886 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001887 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001888 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001889 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1890 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001891 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001892 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1893 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1894 if (I) {
1895 int trials = atoi(argv[2]);
1896 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1897 NULL);
1898 }
1899 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001900 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001901 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001902
1903 PostVisitTU postVisit = 0;
1904 if (strstr(argv[1], "-memory-usage"))
1905 postVisit = PrintMemoryUsage;
1906
Ted Kremenek7d405622010-01-12 23:34:26 +00001907 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001908 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
1909 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00001910 }
1911 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001912 return perform_file_scan(argv[2], argv[3],
1913 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001914 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1915 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001916 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1917 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1918 PrintInclusionStack);
1919 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1920 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1921 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001922 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1923 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1924 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001925 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1926 return perform_test_load_source(argc - 2, argv + 2, "all",
1927 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001928 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1929 if (argc > 2)
1930 return print_usrs(argv + 2, argv + argc);
1931 else {
1932 display_usrs();
1933 return 1;
1934 }
1935 }
1936 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1937 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001938 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1939 return write_pch_file(argv[2], argc - 3, argv + 3);
1940
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001941 print_usage();
1942 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001943}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001944
1945/***/
1946
1947/* We intentionally run in a separate thread to ensure we at least minimal
1948 * testing of a multithreaded environment (for example, having a reduced stack
1949 * size). */
1950
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001951typedef struct thread_info {
1952 int argc;
1953 const char **argv;
1954 int result;
1955} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00001956void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001957 thread_info *client_data = client_data_v;
1958 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001959}
1960
1961int main(int argc, const char **argv) {
1962 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001963
Douglas Gregor61605982010-10-27 16:00:01 +00001964 if (getenv("CINDEXTEST_NOTHREADS"))
1965 return cindextest_main(argc, argv);
1966
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001967 client_data.argc = argc;
1968 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00001969 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001970 return client_data.result;
1971}