blob: e43ac0699f2e675b2208b00139ef5392bb7197cf [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 Gregor44c181a2010-07-23 00:33:23 +000040
41 return options;
42}
43
Daniel Dunbar51b058c2010-02-14 08:32:24 +000044static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
45 unsigned end_line, unsigned end_column) {
46 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000047 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000048}
49
Ted Kremenek1c6da172009-11-17 19:37:36 +000050static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
51 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000052
Douglas Gregora88084b2010-02-18 18:08:43 +000053 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000054 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000055 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
56 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000057 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000058 return 1;
59}
60
Douglas Gregor4db64a42010-01-23 00:14:00 +000061void free_remapped_files(struct CXUnsavedFile *unsaved_files,
62 int num_unsaved_files) {
63 int i;
64 for (i = 0; i != num_unsaved_files; ++i) {
65 free((char *)unsaved_files[i].Filename);
66 free((char *)unsaved_files[i].Contents);
67 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000068 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000069}
70
71int parse_remapped_files(int argc, const char **argv, int start_arg,
72 struct CXUnsavedFile **unsaved_files,
73 int *num_unsaved_files) {
74 int i;
75 int arg;
76 int prefix_len = strlen("-remap-file=");
77 *unsaved_files = 0;
78 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000079
Douglas Gregor4db64a42010-01-23 00:14:00 +000080 /* Count the number of remapped files. */
81 for (arg = start_arg; arg < argc; ++arg) {
82 if (strncmp(argv[arg], "-remap-file=", prefix_len))
83 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000084
Douglas Gregor4db64a42010-01-23 00:14:00 +000085 ++*num_unsaved_files;
86 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000087
Douglas Gregor4db64a42010-01-23 00:14:00 +000088 if (*num_unsaved_files == 0)
89 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000092 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
93 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
95 struct CXUnsavedFile *unsaved = *unsaved_files + i;
96 const char *arg_string = argv[arg] + prefix_len;
97 int filename_len;
98 char *filename;
99 char *contents;
100 FILE *to_file;
101 const char *semi = strchr(arg_string, ';');
102 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000103 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000104 "error: -remap-file=from;to argument is missing semicolon\n");
105 free_remapped_files(*unsaved_files, i);
106 *unsaved_files = 0;
107 *num_unsaved_files = 0;
108 return -1;
109 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000110
Douglas Gregor4db64a42010-01-23 00:14:00 +0000111 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000112 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113 if (!to_file) {
114 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
115 semi + 1);
116 free_remapped_files(*unsaved_files, i);
117 *unsaved_files = 0;
118 *num_unsaved_files = 0;
119 return -1;
120 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000121
Douglas Gregor4db64a42010-01-23 00:14:00 +0000122 /* Determine the length of the file we're remapping to. */
123 fseek(to_file, 0, SEEK_END);
124 unsaved->Length = ftell(to_file);
125 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 /* Read the contents of the file we're remapping to. */
128 contents = (char *)malloc(unsaved->Length + 1);
129 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
130 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
131 (feof(to_file) ? "EOF" : "error"), semi + 1);
132 fclose(to_file);
133 free_remapped_files(*unsaved_files, i);
134 *unsaved_files = 0;
135 *num_unsaved_files = 0;
136 return -1;
137 }
138 contents[unsaved->Length] = 0;
139 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000140
Douglas Gregor4db64a42010-01-23 00:14:00 +0000141 /* Close the file. */
142 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000143
Douglas Gregor4db64a42010-01-23 00:14:00 +0000144 /* Copy the file name that we're remapping from. */
145 filename_len = semi - arg_string;
146 filename = (char *)malloc(filename_len + 1);
147 memcpy(filename, arg_string, filename_len);
148 filename[filename_len] = 0;
149 unsaved->Filename = filename;
150 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000151
Douglas Gregor4db64a42010-01-23 00:14:00 +0000152 return 0;
153}
154
Ted Kremenek0d435192009-11-17 18:13:31 +0000155/******************************************************************************/
156/* Pretty-printing. */
157/******************************************************************************/
158
Douglas Gregor358559d2010-10-02 22:49:11 +0000159int want_display_name = 0;
160
Douglas Gregordd3e5542011-05-04 00:14:37 +0000161static void PrintCursor(CXTranslationUnit TU, CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000162 if (clang_isInvalid(Cursor.kind)) {
163 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
164 printf("Invalid Cursor => %s", clang_getCString(ks));
165 clang_disposeString(ks);
166 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000167 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000168 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000169 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000170 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000171 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000172 CXCursor *overridden;
173 unsigned num_overridden;
174
Ted Kremeneke68fff62010-02-17 00:41:32 +0000175 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000176 string = want_display_name? clang_getCursorDisplayName(Cursor)
177 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000178 printf("%s=%s", clang_getCString(ks),
179 clang_getCString(string));
180 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000181 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000182
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000183 Referenced = clang_getCursorReferenced(Cursor);
184 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000185 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
186 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
187 printf("[");
188 for (I = 0; I != N; ++I) {
189 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000190 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000191 if (I)
192 printf(", ");
193
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000194 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000195 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000196 printf("%d:%d", line, column);
197 }
198 printf("]");
199 } else {
200 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000201 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000202 printf(":%d:%d", line, column);
203 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000204 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000205
206 if (clang_isCursorDefinition(Cursor))
207 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000208
209 switch (clang_getCursorAvailability(Cursor)) {
210 case CXAvailability_Available:
211 break;
212
213 case CXAvailability_Deprecated:
214 printf(" (deprecated)");
215 break;
216
217 case CXAvailability_NotAvailable:
218 printf(" (unavailable)");
219 break;
220 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000221
222 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
223 CXType T =
224 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
225 CXString S = clang_getTypeKindSpelling(T.kind);
226 printf(" [IBOutletCollection=%s]", clang_getCString(S));
227 clang_disposeString(S);
228 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000229
230 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
231 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
232 unsigned isVirtual = clang_isVirtualBase(Cursor);
233 const char *accessStr = 0;
234
235 switch (access) {
236 case CX_CXXInvalidAccessSpecifier:
237 accessStr = "invalid"; break;
238 case CX_CXXPublic:
239 accessStr = "public"; break;
240 case CX_CXXProtected:
241 accessStr = "protected"; break;
242 case CX_CXXPrivate:
243 accessStr = "private"; break;
244 }
245
246 printf(" [access=%s isVirtual=%s]", accessStr,
247 isVirtual ? "true" : "false");
248 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000249
250 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
251 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
252 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
253 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000254 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000255 printf(" [Specialization of %s:%d:%d]",
256 clang_getCString(Name), line, column);
257 clang_disposeString(Name);
258 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000259
260 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
261 if (num_overridden) {
262 unsigned I;
263 printf(" [Overrides ");
264 for (I = 0; I != num_overridden; ++I) {
265 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000266 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000267 if (I)
268 printf(", ");
269 printf("@%d:%d", line, column);
270 }
271 printf("]");
272 clang_disposeOverriddenCursors(overridden);
273 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000274
275 if (Cursor.kind == CXCursor_InclusionDirective) {
276 CXFile File = clang_getIncludedFile(Cursor);
277 CXString Included = clang_getFileName(File);
278 printf(" (%s)", clang_getCString(Included));
279 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000280
281 if (clang_isFileMultipleIncludeGuarded(TU, File))
282 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000283 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000284 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000285}
Steve Naroff89922f82009-08-31 00:59:03 +0000286
Ted Kremeneke68fff62010-02-17 00:41:32 +0000287static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000288 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000289 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000290 CXFile file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000291 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000292 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000293 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000294 clang_disposeString(source);
295 return "<invalid loc>";
296 }
297 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000298 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000299 clang_disposeString(source);
300 return b;
301 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000302}
303
Ted Kremenek0d435192009-11-17 18:13:31 +0000304/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000305/* Callbacks. */
306/******************************************************************************/
307
308typedef void (*PostVisitTU)(CXTranslationUnit);
309
Douglas Gregora88084b2010-02-18 18:08:43 +0000310void PrintDiagnostic(CXDiagnostic Diagnostic) {
311 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000312 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000313 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000314 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000315 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
316 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000317 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000318
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000319 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000320 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000321
Douglas Gregor274f1902010-02-22 23:17:23 +0000322 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
323 fprintf(stderr, "%s\n", clang_getCString(Msg));
324 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000325
Douglas Gregora9b06d42010-11-09 06:24:54 +0000326 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
327 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000328 if (!file)
329 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000330
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000331 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
332 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000333 CXSourceRange range;
334 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
335 CXSourceLocation start = clang_getRangeStart(range);
336 CXSourceLocation end = clang_getRangeEnd(range);
337 unsigned start_line, start_column, end_line, end_column;
338 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000339 clang_getSpellingLocation(start, &start_file, &start_line,
340 &start_column, 0);
341 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000342 if (clang_equalLocations(start, end)) {
343 /* Insertion. */
344 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000345 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000346 clang_getCString(insertion_text), start_line, start_column);
347 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
348 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000349 if (start_file == file && end_file == file) {
350 fprintf(out, "FIX-IT: Remove ");
351 PrintExtent(out, start_line, start_column, end_line, end_column);
352 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000353 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000354 } else {
355 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000356 if (start_file == end_file) {
357 fprintf(out, "FIX-IT: Replace ");
358 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000359 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000360 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000361 break;
362 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000363 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000364 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000365}
366
Douglas Gregora88084b2010-02-18 18:08:43 +0000367void PrintDiagnostics(CXTranslationUnit TU) {
368 int i, n = clang_getNumDiagnostics(TU);
369 for (i = 0; i != n; ++i) {
370 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
371 PrintDiagnostic(Diag);
372 clang_disposeDiagnostic(Diag);
373 }
374}
375
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000376void PrintMemoryUsage(CXTranslationUnit TU) {
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000377 unsigned long total = 0.0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000378 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000379 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000380 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000381 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000382 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000383 unsigned long amount = usage.entries[i].amount;
384 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000385 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000386 ((double) amount)/(1024*1024));
387 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000388 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000389 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000390 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000391}
392
Ted Kremenekce2ae882010-01-26 17:59:48 +0000393/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000394/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000395/******************************************************************************/
396
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000397static const char *FileCheckPrefix = "CHECK";
398
Douglas Gregora7bde202010-01-19 00:34:46 +0000399static void PrintCursorExtent(CXCursor C) {
400 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000401 CXFile begin_file, end_file;
402 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000403
Douglas Gregora9b06d42010-11-09 06:24:54 +0000404 clang_getSpellingLocation(clang_getRangeStart(extent),
405 &begin_file, &begin_line, &begin_column, 0);
406 clang_getSpellingLocation(clang_getRangeEnd(extent),
407 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000408 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000409 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000410
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000411 printf(" Extent=");
412 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000413}
414
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000415/* Data used by all of the visitors. */
416typedef struct {
417 CXTranslationUnit TU;
418 enum CXCursorKind *Filter;
419} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000420
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000421
Ted Kremeneke68fff62010-02-17 00:41:32 +0000422enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000423 CXCursor Parent,
424 CXClientData ClientData) {
425 VisitorData *Data = (VisitorData *)ClientData;
426 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000427 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000428 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000429 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000430 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000431 GetCursorSource(Cursor), line, column);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000432 PrintCursor(Data->TU, Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000433 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000434 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000435 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000436 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000437
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000438 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000439}
Steve Naroff50398192009-08-28 15:28:48 +0000440
Ted Kremeneke68fff62010-02-17 00:41:32 +0000441static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000442 CXCursor Parent,
443 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000444 const char *startBuf, *endBuf;
445 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
446 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000447 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000448
Douglas Gregorb6998662010-01-19 19:34:47 +0000449 if (Cursor.kind != CXCursor_FunctionDecl ||
450 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000451 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000452
453 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
454 &startLine, &startColumn,
455 &endLine, &endColumn);
456 /* Probe the entire body, looking for both decls and refs. */
457 curLine = startLine;
458 curColumn = startColumn;
459
460 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000461 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000462 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000463 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000464
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000465 if (*startBuf == '\n') {
466 startBuf++;
467 curLine++;
468 curColumn = 1;
469 } else if (*startBuf != '\t')
470 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000471
Douglas Gregor98258af2010-01-18 22:46:11 +0000472 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000473 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000474
Douglas Gregor1db19de2010-01-19 21:36:55 +0000475 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000476 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000477 CXSourceLocation RefLoc
478 = clang_getLocation(Data->TU, file, curLine, curColumn);
479 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000480 if (Ref.kind == CXCursor_NoDeclFound) {
481 /* Nothing found here; that's fine. */
482 } else if (Ref.kind != CXCursor_FunctionDecl) {
483 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
484 curLine, curColumn);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000485 PrintCursor(Data->TU, Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000486 printf("\n");
487 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000488 }
Ted Kremenek74844072010-02-17 00:41:20 +0000489 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000490 startBuf++;
491 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000493 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000494}
495
Ted Kremenek7d405622010-01-12 23:34:26 +0000496/******************************************************************************/
497/* USR testing. */
498/******************************************************************************/
499
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000500enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
501 CXClientData ClientData) {
502 VisitorData *Data = (VisitorData *)ClientData;
503 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000504 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000505 const char *cstr = clang_getCString(USR);
506 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000507 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000508 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000509 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000510 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
511
Douglas Gregora7bde202010-01-19 00:34:46 +0000512 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000513 printf("\n");
514 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000516 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517 }
518
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000519 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000520}
521
522/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000523/* Inclusion stack testing. */
524/******************************************************************************/
525
526void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
527 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000528
Ted Kremenek16b55a72010-01-26 19:31:51 +0000529 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000530 CXString fname;
531
532 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000533 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000534 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000535
Ted Kremenek16b55a72010-01-26 19:31:51 +0000536 for (i = 0; i < includeStackLen; ++i) {
537 CXFile includingFile;
538 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000539 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
540 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000541 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000542 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000543 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000544 }
545 printf("\n");
546}
547
548void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000549 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000550}
551
552/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000553/* Linkage testing. */
554/******************************************************************************/
555
556static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
557 CXClientData d) {
558 const char *linkage = 0;
559
Douglas Gregordd3e5542011-05-04 00:14:37 +0000560 VisitorData *Data = (VisitorData *)d;
561
Ted Kremenek3bed5272010-03-03 06:37:58 +0000562 if (clang_isInvalid(clang_getCursorKind(cursor)))
563 return CXChildVisit_Recurse;
564
565 switch (clang_getCursorLinkage(cursor)) {
566 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000567 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
568 case CXLinkage_Internal: linkage = "Internal"; break;
569 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
570 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000571 }
572
573 if (linkage) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000574 PrintCursor(Data->TU, cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000575 printf("linkage=%s\n", linkage);
576 }
577
578 return CXChildVisit_Recurse;
579}
580
581/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000582/* Typekind testing. */
583/******************************************************************************/
584
585static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
586 CXClientData d) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000587 VisitorData *Data = (VisitorData *)d;
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000588
589 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
590 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000591 CXString S = clang_getTypeKindSpelling(T.kind);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000592 PrintCursor(Data->TU, cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000593 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000594 if (clang_isConstQualifiedType(T))
595 printf(" const");
596 if (clang_isVolatileQualifiedType(T))
597 printf(" volatile");
598 if (clang_isRestrictQualifiedType(T))
599 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000600 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000601 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000602 {
603 CXType CT = clang_getCanonicalType(T);
604 if (!clang_equalTypes(T, CT)) {
605 CXString CS = clang_getTypeKindSpelling(CT.kind);
606 printf(" [canonical=%s]", clang_getCString(CS));
607 clang_disposeString(CS);
608 }
609 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000610 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000611 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000612 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000613 if (RT.kind != CXType_Invalid) {
614 CXString RS = clang_getTypeKindSpelling(RT.kind);
615 printf(" [result=%s]", clang_getCString(RS));
616 clang_disposeString(RS);
617 }
618 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000619 /* Print if this is a non-POD type. */
620 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000621
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000622 printf("\n");
623 }
624 return CXChildVisit_Recurse;
625}
626
627
628/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000629/* Loading ASTs/source. */
630/******************************************************************************/
631
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000632static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000633 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000634 CXCursorVisitor Visitor,
635 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000636
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000637 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000638 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000639
640 if (Visitor) {
641 enum CXCursorKind K = CXCursor_NotImplemented;
642 enum CXCursorKind *ck = &K;
643 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000644
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000645 /* Perform some simple filtering. */
646 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000647 else if (!strcmp(filter, "all-display") ||
648 !strcmp(filter, "local-display")) {
649 ck = NULL;
650 want_display_name = 1;
651 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000652 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000653 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
654 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
655 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
656 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
657 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
658 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
659 else {
660 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
661 return 1;
662 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000663
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000664 Data.TU = TU;
665 Data.Filter = ck;
666 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000667 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000668
Ted Kremenekce2ae882010-01-26 17:59:48 +0000669 if (PV)
670 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000671
Douglas Gregora88084b2010-02-18 18:08:43 +0000672 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000673 clang_disposeTranslationUnit(TU);
674 return 0;
675}
676
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000677int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000678 const char *prefix, CXCursorVisitor Visitor,
679 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000680 CXIndex Idx;
681 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000682 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000683 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000684 !strcmp(filter, "local") ? 1 : 0,
685 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000686
Ted Kremenek020a0952010-02-11 07:41:25 +0000687 if (!CreateTranslationUnit(Idx, file, &TU)) {
688 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000689 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000690 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000691
Ted Kremenek020a0952010-02-11 07:41:25 +0000692 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
693 clang_disposeIndex(Idx);
694 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000695}
696
Ted Kremenekce2ae882010-01-26 17:59:48 +0000697int perform_test_load_source(int argc, const char **argv,
698 const char *filter, CXCursorVisitor Visitor,
699 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000700 CXIndex Idx;
701 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000702 struct CXUnsavedFile *unsaved_files = 0;
703 int num_unsaved_files = 0;
704 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000705
Daniel Dunbarada487d2009-12-01 02:03:10 +0000706 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000707 (!strcmp(filter, "local") ||
708 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000709 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000710
Ted Kremenek020a0952010-02-11 07:41:25 +0000711 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
712 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000713 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000714 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000715
Ted Kremeneke68fff62010-02-17 00:41:32 +0000716 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
717 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000718 argv + num_unsaved_files,
719 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000720 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000721 if (!TU) {
722 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000723 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000724 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000725 return 1;
726 }
727
Ted Kremenekce2ae882010-01-26 17:59:48 +0000728 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000729 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000730 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000731 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000732}
733
Douglas Gregorabc563f2010-07-19 21:46:24 +0000734int perform_test_reparse_source(int argc, const char **argv, int trials,
735 const char *filter, CXCursorVisitor Visitor,
736 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000737 CXIndex Idx;
738 CXTranslationUnit TU;
739 struct CXUnsavedFile *unsaved_files = 0;
740 int num_unsaved_files = 0;
741 int result;
742 int trial;
743
744 Idx = clang_createIndex(/* excludeDeclsFromPCH */
745 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000746 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000747
Douglas Gregorabc563f2010-07-19 21:46:24 +0000748 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
749 clang_disposeIndex(Idx);
750 return -1;
751 }
752
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000753 /* Load the initial translation unit -- we do this without honoring remapped
754 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000755 TU = clang_parseTranslationUnit(Idx, 0,
756 argv + num_unsaved_files,
757 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000758 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000759 if (!TU) {
760 fprintf(stderr, "Unable to load translation unit!\n");
761 free_remapped_files(unsaved_files, num_unsaved_files);
762 clang_disposeIndex(Idx);
763 return 1;
764 }
765
766 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000767 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
768 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000769 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000770 clang_disposeTranslationUnit(TU);
771 free_remapped_files(unsaved_files, num_unsaved_files);
772 clang_disposeIndex(Idx);
773 return -1;
774 }
775 }
776
777 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
778 free_remapped_files(unsaved_files, num_unsaved_files);
779 clang_disposeIndex(Idx);
780 return result;
781}
782
Ted Kremenek0d435192009-11-17 18:13:31 +0000783/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000784/* Logic for testing clang_getCursor(). */
785/******************************************************************************/
786
Douglas Gregordd3e5542011-05-04 00:14:37 +0000787static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000788 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000789 unsigned end_line, unsigned end_col,
790 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000791 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000792 if (prefix)
793 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000794 PrintExtent(stdout, start_line, start_col, end_line, end_col);
795 printf(" ");
Douglas Gregordd3e5542011-05-04 00:14:37 +0000796 PrintCursor(TU, cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000797 printf("\n");
798}
799
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000800static int perform_file_scan(const char *ast_file, const char *source_file,
801 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000802 CXIndex Idx;
803 CXTranslationUnit TU;
804 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000805 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000806 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000807 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000808 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000809
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000810 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
811 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000812 fprintf(stderr, "Could not create Index\n");
813 return 1;
814 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000815
Ted Kremenek1c6da172009-11-17 19:37:36 +0000816 if (!CreateTranslationUnit(Idx, ast_file, &TU))
817 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000818
Ted Kremenek1c6da172009-11-17 19:37:36 +0000819 if ((fp = fopen(source_file, "r")) == NULL) {
820 fprintf(stderr, "Could not open '%s'\n", source_file);
821 return 1;
822 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000823
Douglas Gregorb9790342010-01-22 21:44:22 +0000824 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000825 for (;;) {
826 CXCursor cursor;
827 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000828
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000829 if (c == '\n') {
830 ++line;
831 col = 1;
832 } else
833 ++col;
834
835 /* Check the cursor at this position, and dump the previous one if we have
836 * found something new.
837 */
838 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
839 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
840 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000841 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000842 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000843 start_line = line;
844 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000845 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000846 if (c == EOF)
847 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000848
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000849 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000850 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000851
Ted Kremenek1c6da172009-11-17 19:37:36 +0000852 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000853 clang_disposeTranslationUnit(TU);
854 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000855 return 0;
856}
857
858/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000859/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000860/******************************************************************************/
861
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000862/* Parse file:line:column from the input string. Returns 0 on success, non-zero
863 on failure. If successful, the pointer *filename will contain newly-allocated
864 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000865int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000866 unsigned *column, unsigned *second_line,
867 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000868 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000869 const char *last_colon = strrchr(input, ':');
870 unsigned values[4], i;
871 unsigned num_values = (second_line && second_column)? 4 : 2;
872
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000873 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000874 if (!last_colon || last_colon == input) {
875 if (num_values == 4)
876 fprintf(stderr, "could not parse filename:line:column:line:column in "
877 "'%s'\n", input);
878 else
879 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000880 return 1;
881 }
882
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000883 for (i = 0; i != num_values; ++i) {
884 const char *prev_colon;
885
886 /* Parse the next line or column. */
887 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
888 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000889 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000890 (i % 2 ? "column" : "line"), input);
891 return 1;
892 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000893
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000894 if (i + 1 == num_values)
895 break;
896
897 /* Find the previous colon. */
898 prev_colon = last_colon - 1;
899 while (prev_colon != input && *prev_colon != ':')
900 --prev_colon;
901 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000902 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000903 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000904 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000905 }
906
907 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000908 }
909
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000910 *line = values[0];
911 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000912
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000913 if (second_line && second_column) {
914 *second_line = values[2];
915 *second_column = values[3];
916 }
917
Douglas Gregor88d23952009-11-09 18:19:57 +0000918 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000919 *filename = (char*)malloc(last_colon - input + 1);
920 memcpy(*filename, input, last_colon - input);
921 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000922 return 0;
923}
924
925const char *
926clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
927 switch (Kind) {
928 case CXCompletionChunk_Optional: return "Optional";
929 case CXCompletionChunk_TypedText: return "TypedText";
930 case CXCompletionChunk_Text: return "Text";
931 case CXCompletionChunk_Placeholder: return "Placeholder";
932 case CXCompletionChunk_Informative: return "Informative";
933 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
934 case CXCompletionChunk_LeftParen: return "LeftParen";
935 case CXCompletionChunk_RightParen: return "RightParen";
936 case CXCompletionChunk_LeftBracket: return "LeftBracket";
937 case CXCompletionChunk_RightBracket: return "RightBracket";
938 case CXCompletionChunk_LeftBrace: return "LeftBrace";
939 case CXCompletionChunk_RightBrace: return "RightBrace";
940 case CXCompletionChunk_LeftAngle: return "LeftAngle";
941 case CXCompletionChunk_RightAngle: return "RightAngle";
942 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000943 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000944 case CXCompletionChunk_Colon: return "Colon";
945 case CXCompletionChunk_SemiColon: return "SemiColon";
946 case CXCompletionChunk_Equal: return "Equal";
947 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
948 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000949 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000950
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000951 return "Unknown";
952}
953
Douglas Gregor3ac73852009-11-09 16:04:45 +0000954void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000955 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000956
Douglas Gregor3ac73852009-11-09 16:04:45 +0000957 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000958 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000959 CXString text;
960 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000961 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000962 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000963
Douglas Gregor3ac73852009-11-09 16:04:45 +0000964 if (Kind == CXCompletionChunk_Optional) {
965 fprintf(file, "{Optional ");
966 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000967 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000968 file);
969 fprintf(file, "}");
970 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +0000971 }
972
973 if (Kind == CXCompletionChunk_VerticalSpace) {
974 fprintf(file, "{VerticalSpace }");
975 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000976 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000977
Douglas Gregord5a20892009-11-09 17:05:28 +0000978 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000979 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000980 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000981 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000982 cstr ? cstr : "");
983 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000984 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000985
Douglas Gregor3ac73852009-11-09 16:04:45 +0000986}
987
988void print_completion_result(CXCompletionResult *completion_result,
989 CXClientData client_data) {
990 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000991 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
992
993 fprintf(file, "%s:", clang_getCString(ks));
994 clang_disposeString(ks);
995
Douglas Gregor3ac73852009-11-09 16:04:45 +0000996 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000997 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000998 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000999 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1000 case CXAvailability_Available:
1001 break;
1002
1003 case CXAvailability_Deprecated:
1004 fprintf(file, " (deprecated)");
1005 break;
1006
1007 case CXAvailability_NotAvailable:
1008 fprintf(file, " (unavailable)");
1009 break;
1010 }
1011 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001012}
1013
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001014int my_stricmp(const char *s1, const char *s2) {
1015 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001016 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001017 if (c1 < c2)
1018 return -1;
1019 else if (c1 > c2)
1020 return 1;
1021
1022 ++s1;
1023 ++s2;
1024 }
1025
1026 if (*s1)
1027 return 1;
1028 else if (*s2)
1029 return -1;
1030 return 0;
1031}
1032
Douglas Gregor1982c182010-07-12 18:38:41 +00001033int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001034 const char *input = argv[1];
1035 char *filename = 0;
1036 unsigned line;
1037 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001038 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001039 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001040 struct CXUnsavedFile *unsaved_files = 0;
1041 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001042 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001043 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001044 unsigned I, Repeats = 1;
1045 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1046
1047 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1048 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001049
Douglas Gregor1982c182010-07-12 18:38:41 +00001050 if (timing_only)
1051 input += strlen("-code-completion-timing=");
1052 else
1053 input += strlen("-code-completion-at=");
1054
Ted Kremeneke68fff62010-02-17 00:41:32 +00001055 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001056 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001057 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001058
Douglas Gregor735df882009-12-02 09:21:34 +00001059 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1060 return -1;
1061
Douglas Gregor32be4a52010-10-11 21:37:58 +00001062 CIdx = clang_createIndex(0, 0);
1063
1064 if (getenv("CINDEXTEST_EDITING"))
1065 Repeats = 5;
1066
1067 TU = clang_parseTranslationUnit(CIdx, 0,
1068 argv + num_unsaved_files + 2,
1069 argc - num_unsaved_files - 2,
1070 0, 0, getDefaultParsingOptions());
1071 if (!TU) {
1072 fprintf(stderr, "Unable to load translation unit!\n");
1073 return 1;
1074 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001075
1076 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1077 fprintf(stderr, "Unable to reparse translation init!\n");
1078 return 1;
1079 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001080
1081 for (I = 0; I != Repeats; ++I) {
1082 results = clang_codeCompleteAt(TU, filename, line, column,
1083 unsaved_files, num_unsaved_files,
1084 completionOptions);
1085 if (!results) {
1086 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001087 return 1;
1088 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001089 if (I != Repeats-1)
1090 clang_disposeCodeCompleteResults(results);
1091 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001092
Douglas Gregorec6762c2009-12-18 16:20:58 +00001093 if (results) {
1094 unsigned i, n = results->NumResults;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001095 if (!timing_only) {
1096 /* Sort the code-completion results based on the typed text. */
1097 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1098
Douglas Gregor1982c182010-07-12 18:38:41 +00001099 for (i = 0; i != n; ++i)
1100 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001101 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001102 n = clang_codeCompleteGetNumDiagnostics(results);
1103 for (i = 0; i != n; ++i) {
1104 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1105 PrintDiagnostic(diag);
1106 clang_disposeDiagnostic(diag);
1107 }
Douglas Gregorec6762c2009-12-18 16:20:58 +00001108 clang_disposeCodeCompleteResults(results);
1109 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001110 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001111 clang_disposeIndex(CIdx);
1112 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001113
Douglas Gregor735df882009-12-02 09:21:34 +00001114 free_remapped_files(unsaved_files, num_unsaved_files);
1115
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001116 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001117}
1118
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001119typedef struct {
1120 char *filename;
1121 unsigned line;
1122 unsigned column;
1123} CursorSourceLocation;
1124
1125int inspect_cursor_at(int argc, const char **argv) {
1126 CXIndex CIdx;
1127 int errorCode;
1128 struct CXUnsavedFile *unsaved_files = 0;
1129 int num_unsaved_files = 0;
1130 CXTranslationUnit TU;
1131 CXCursor Cursor;
1132 CursorSourceLocation *Locations = 0;
1133 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001134 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001135 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001136
Ted Kremeneke68fff62010-02-17 00:41:32 +00001137 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001138 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1139 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001140
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001141 /* Parse the locations. */
1142 assert(NumLocations > 0 && "Unable to count locations?");
1143 Locations = (CursorSourceLocation *)malloc(
1144 NumLocations * sizeof(CursorSourceLocation));
1145 for (Loc = 0; Loc < NumLocations; ++Loc) {
1146 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001147 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1148 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001149 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001150 return errorCode;
1151 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001152
1153 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001154 &num_unsaved_files))
1155 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001156
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001157 if (getenv("CINDEXTEST_EDITING"))
1158 Repeats = 5;
1159
1160 /* Parse the translation unit. When we're testing clang_getCursor() after
1161 reparsing, don't remap unsaved files until the second parse. */
1162 CIdx = clang_createIndex(1, 1);
1163 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1164 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001165 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001166 unsaved_files,
1167 Repeats > 1? 0 : num_unsaved_files,
1168 getDefaultParsingOptions());
1169
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001170 if (!TU) {
1171 fprintf(stderr, "unable to parse input\n");
1172 return -1;
1173 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001174
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001175 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001176 if (Repeats > 1 &&
1177 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1178 clang_defaultReparseOptions(TU))) {
1179 clang_disposeTranslationUnit(TU);
1180 return 1;
1181 }
1182
1183 for (Loc = 0; Loc < NumLocations; ++Loc) {
1184 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1185 if (!file)
1186 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001187
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001188 Cursor = clang_getCursor(TU,
1189 clang_getLocation(TU, file, Locations[Loc].line,
1190 Locations[Loc].column));
1191 if (I + 1 == Repeats) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001192 PrintCursor(TU, Cursor);
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001193 printf("\n");
1194 free(Locations[Loc].filename);
1195 }
1196 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001197 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001198
Douglas Gregora88084b2010-02-18 18:08:43 +00001199 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001200 clang_disposeTranslationUnit(TU);
1201 clang_disposeIndex(CIdx);
1202 free(Locations);
1203 free_remapped_files(unsaved_files, num_unsaved_files);
1204 return 0;
1205}
1206
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001207int perform_token_annotation(int argc, const char **argv) {
1208 const char *input = argv[1];
1209 char *filename = 0;
1210 unsigned line, second_line;
1211 unsigned column, second_column;
1212 CXIndex CIdx;
1213 CXTranslationUnit TU = 0;
1214 int errorCode;
1215 struct CXUnsavedFile *unsaved_files = 0;
1216 int num_unsaved_files = 0;
1217 CXToken *tokens;
1218 unsigned num_tokens;
1219 CXSourceRange range;
1220 CXSourceLocation startLoc, endLoc;
1221 CXFile file = 0;
1222 CXCursor *cursors = 0;
1223 unsigned i;
1224
1225 input += strlen("-test-annotate-tokens=");
1226 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1227 &second_line, &second_column)))
1228 return errorCode;
1229
1230 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1231 return -1;
1232
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001233 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001234 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1235 argc - num_unsaved_files - 3,
1236 argv + num_unsaved_files + 2,
1237 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001238 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001239 if (!TU) {
1240 fprintf(stderr, "unable to parse input\n");
1241 clang_disposeIndex(CIdx);
1242 free(filename);
1243 free_remapped_files(unsaved_files, num_unsaved_files);
1244 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001245 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001246 errorCode = 0;
1247
1248 file = clang_getFile(TU, filename);
1249 if (!file) {
1250 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1251 errorCode = -1;
1252 goto teardown;
1253 }
1254
1255 startLoc = clang_getLocation(TU, file, line, column);
1256 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001257 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001258 column);
1259 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001260 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001261 }
1262
1263 endLoc = clang_getLocation(TU, file, second_line, second_column);
1264 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001265 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001266 second_line, second_column);
1267 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001268 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001269 }
1270
1271 range = clang_getRange(startLoc, endLoc);
1272 clang_tokenize(TU, range, &tokens, &num_tokens);
1273 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1274 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1275 for (i = 0; i != num_tokens; ++i) {
1276 const char *kind = "<unknown>";
1277 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1278 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1279 unsigned start_line, start_column, end_line, end_column;
1280
1281 switch (clang_getTokenKind(tokens[i])) {
1282 case CXToken_Punctuation: kind = "Punctuation"; break;
1283 case CXToken_Keyword: kind = "Keyword"; break;
1284 case CXToken_Identifier: kind = "Identifier"; break;
1285 case CXToken_Literal: kind = "Literal"; break;
1286 case CXToken_Comment: kind = "Comment"; break;
1287 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00001288 clang_getSpellingLocation(clang_getRangeStart(extent),
1289 0, &start_line, &start_column, 0);
1290 clang_getSpellingLocation(clang_getRangeEnd(extent),
1291 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001292 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1293 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001294 if (!clang_isInvalid(cursors[i].kind)) {
1295 printf(" ");
Douglas Gregordd3e5542011-05-04 00:14:37 +00001296 PrintCursor(TU, cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001297 }
1298 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001299 }
1300 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00001301 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001302
1303 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001304 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001305 clang_disposeTranslationUnit(TU);
1306 clang_disposeIndex(CIdx);
1307 free(filename);
1308 free_remapped_files(unsaved_files, num_unsaved_files);
1309 return errorCode;
1310}
1311
Ted Kremenek0d435192009-11-17 18:13:31 +00001312/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001313/* USR printing. */
1314/******************************************************************************/
1315
1316static int insufficient_usr(const char *kind, const char *usage) {
1317 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1318 return 1;
1319}
1320
1321static unsigned isUSR(const char *s) {
1322 return s[0] == 'c' && s[1] == ':';
1323}
1324
1325static int not_usr(const char *s, const char *arg) {
1326 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1327 return 1;
1328}
1329
1330static void print_usr(CXString usr) {
1331 const char *s = clang_getCString(usr);
1332 printf("%s\n", s);
1333 clang_disposeString(usr);
1334}
1335
1336static void display_usrs() {
1337 fprintf(stderr, "-print-usrs options:\n"
1338 " ObjCCategory <class name> <category name>\n"
1339 " ObjCClass <class name>\n"
1340 " ObjCIvar <ivar name> <class USR>\n"
1341 " ObjCMethod <selector> [0=class method|1=instance method] "
1342 "<class USR>\n"
1343 " ObjCProperty <property name> <class USR>\n"
1344 " ObjCProtocol <protocol name>\n");
1345}
1346
1347int print_usrs(const char **I, const char **E) {
1348 while (I != E) {
1349 const char *kind = *I;
1350 unsigned len = strlen(kind);
1351 switch (len) {
1352 case 8:
1353 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1354 if (I + 2 >= E)
1355 return insufficient_usr(kind, "<ivar name> <class USR>");
1356 if (!isUSR(I[2]))
1357 return not_usr("<class USR>", I[2]);
1358 else {
1359 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001360 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001361 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001362 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1363 }
1364
1365 I += 3;
1366 continue;
1367 }
1368 break;
1369 case 9:
1370 if (memcmp(kind, "ObjCClass", 9) == 0) {
1371 if (I + 1 >= E)
1372 return insufficient_usr(kind, "<class name>");
1373 print_usr(clang_constructUSR_ObjCClass(I[1]));
1374 I += 2;
1375 continue;
1376 }
1377 break;
1378 case 10:
1379 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1380 if (I + 3 >= E)
1381 return insufficient_usr(kind, "<method selector> "
1382 "[0=class method|1=instance method] <class USR>");
1383 if (!isUSR(I[3]))
1384 return not_usr("<class USR>", I[3]);
1385 else {
1386 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001387 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00001388 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001389 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1390 }
1391 I += 4;
1392 continue;
1393 }
1394 break;
1395 case 12:
1396 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1397 if (I + 2 >= E)
1398 return insufficient_usr(kind, "<class name> <category name>");
1399 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1400 I += 3;
1401 continue;
1402 }
1403 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1404 if (I + 1 >= E)
1405 return insufficient_usr(kind, "<protocol name>");
1406 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1407 I += 2;
1408 continue;
1409 }
1410 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1411 if (I + 2 >= E)
1412 return insufficient_usr(kind, "<property name> <class USR>");
1413 if (!isUSR(I[2]))
1414 return not_usr("<class USR>", I[2]);
1415 else {
1416 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001417 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001418 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001419 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1420 }
1421 I += 3;
1422 continue;
1423 }
1424 break;
1425 default:
1426 break;
1427 }
1428 break;
1429 }
1430
1431 if (I != E) {
1432 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1433 display_usrs();
1434 return 1;
1435 }
1436 return 0;
1437}
1438
1439int print_usrs_file(const char *file_name) {
1440 char line[2048];
1441 const char *args[128];
1442 unsigned numChars = 0;
1443
1444 FILE *fp = fopen(file_name, "r");
1445 if (!fp) {
1446 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1447 return 1;
1448 }
1449
1450 /* This code is not really all that safe, but it works fine for testing. */
1451 while (!feof(fp)) {
1452 char c = fgetc(fp);
1453 if (c == '\n') {
1454 unsigned i = 0;
1455 const char *s = 0;
1456
1457 if (numChars == 0)
1458 continue;
1459
1460 line[numChars] = '\0';
1461 numChars = 0;
1462
1463 if (line[0] == '/' && line[1] == '/')
1464 continue;
1465
1466 s = strtok(line, " ");
1467 while (s) {
1468 args[i] = s;
1469 ++i;
1470 s = strtok(0, " ");
1471 }
1472 if (print_usrs(&args[0], &args[i]))
1473 return 1;
1474 }
1475 else
1476 line[numChars++] = c;
1477 }
1478
1479 fclose(fp);
1480 return 0;
1481}
1482
1483/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001484/* Command line processing. */
1485/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001486int write_pch_file(const char *filename, int argc, const char *argv[]) {
1487 CXIndex Idx;
1488 CXTranslationUnit TU;
1489 struct CXUnsavedFile *unsaved_files = 0;
1490 int num_unsaved_files = 0;
1491
1492 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1493
1494 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1495 clang_disposeIndex(Idx);
1496 return -1;
1497 }
1498
1499 TU = clang_parseTranslationUnit(Idx, 0,
1500 argv + num_unsaved_files,
1501 argc - num_unsaved_files,
1502 unsaved_files,
1503 num_unsaved_files,
1504 CXTranslationUnit_Incomplete);
1505 if (!TU) {
1506 fprintf(stderr, "Unable to load translation unit!\n");
1507 free_remapped_files(unsaved_files, num_unsaved_files);
1508 clang_disposeIndex(Idx);
1509 return 1;
1510 }
1511
Douglas Gregor19998442010-08-13 15:35:05 +00001512 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001513 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1514 clang_disposeTranslationUnit(TU);
1515 free_remapped_files(unsaved_files, num_unsaved_files);
1516 clang_disposeIndex(Idx);
1517 return 0;
1518}
1519
1520/******************************************************************************/
1521/* Command line processing. */
1522/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001523
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001524static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001525 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001526 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001527 if (strcmp(s, "-usrs") == 0)
1528 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001529 if (strncmp(s, "-memory-usage", 13) == 0)
1530 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001531 return NULL;
1532}
1533
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001534static void print_usage(void) {
1535 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001536 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001537 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001538 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001539 " c-index-test -test-file-scan <AST file> <source file> "
1540 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001541 " c-index-test -test-load-tu <AST file> <symbol filter> "
1542 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001543 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1544 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001545 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001546 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001547 " c-index-test -test-load-source-memory-usage "
1548 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00001549 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1550 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001551 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001552 " c-index-test -test-load-source-usrs-memory-usage "
1553 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001554 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1555 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001556 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00001557 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001558 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001559 " c-index-test -test-print-typekind {<args>}*\n"
1560 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001561 " c-index-test -print-usr-file <file>\n"
1562 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001563 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001564 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001565 " all - load all symbols, including those from PCH\n"
1566 " local - load all symbols except those in PCH\n"
1567 " category - only load ObjC categories (non-PCH)\n"
1568 " interface - only load ObjC interfaces (non-PCH)\n"
1569 " protocol - only load ObjC protocols (non-PCH)\n"
1570 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001571 " typedef - only load typdefs (non-PCH)\n"
1572 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001573}
1574
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001575/***/
1576
1577int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001578 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001579 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001580 return perform_code_completion(argc, argv, 0);
1581 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1582 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001583 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1584 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001585 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001586 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001587 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001588 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1589 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001590 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001591 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1592 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1593 if (I) {
1594 int trials = atoi(argv[2]);
1595 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1596 NULL);
1597 }
1598 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001599 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001600 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001601
1602 PostVisitTU postVisit = 0;
1603 if (strstr(argv[1], "-memory-usage"))
1604 postVisit = PrintMemoryUsage;
1605
Ted Kremenek7d405622010-01-12 23:34:26 +00001606 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001607 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
1608 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00001609 }
1610 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001611 return perform_file_scan(argv[2], argv[3],
1612 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001613 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1614 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001615 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1616 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1617 PrintInclusionStack);
1618 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1619 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1620 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001621 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1622 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1623 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001624 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1625 return perform_test_load_source(argc - 2, argv + 2, "all",
1626 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001627 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1628 if (argc > 2)
1629 return print_usrs(argv + 2, argv + argc);
1630 else {
1631 display_usrs();
1632 return 1;
1633 }
1634 }
1635 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1636 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001637 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1638 return write_pch_file(argv[2], argc - 3, argv + 3);
1639
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001640 print_usage();
1641 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001642}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001643
1644/***/
1645
1646/* We intentionally run in a separate thread to ensure we at least minimal
1647 * testing of a multithreaded environment (for example, having a reduced stack
1648 * size). */
1649
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001650typedef struct thread_info {
1651 int argc;
1652 const char **argv;
1653 int result;
1654} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00001655void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001656 thread_info *client_data = client_data_v;
1657 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001658}
1659
1660int main(int argc, const char **argv) {
1661 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001662
Douglas Gregor61605982010-10-27 16:00:01 +00001663 if (getenv("CINDEXTEST_NOTHREADS"))
1664 return cindextest_main(argc, argv);
1665
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001666 client_data.argc = argc;
1667 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00001668 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001669 return client_data.result;
1670}