blob: b7025fc396586be90f395b2d81d17d7cbc39febd [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
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000161static void PrintCursor(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);
280 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000281 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000282}
Steve Naroff89922f82009-08-31 00:59:03 +0000283
Ted Kremeneke68fff62010-02-17 00:41:32 +0000284static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000285 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000286 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000287 CXFile file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000288 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000289 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000290 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000291 clang_disposeString(source);
292 return "<invalid loc>";
293 }
294 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000295 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000296 clang_disposeString(source);
297 return b;
298 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000299}
300
Ted Kremenek0d435192009-11-17 18:13:31 +0000301/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000302/* Callbacks. */
303/******************************************************************************/
304
305typedef void (*PostVisitTU)(CXTranslationUnit);
306
Douglas Gregora88084b2010-02-18 18:08:43 +0000307void PrintDiagnostic(CXDiagnostic Diagnostic) {
308 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000309 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000310 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000311 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000312 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
313 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000314 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000315
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000316 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000317 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000318
Douglas Gregor274f1902010-02-22 23:17:23 +0000319 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
320 fprintf(stderr, "%s\n", clang_getCString(Msg));
321 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000322
Douglas Gregora9b06d42010-11-09 06:24:54 +0000323 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
324 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000325 if (!file)
326 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000327
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000328 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
329 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000330 CXSourceRange range;
331 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
332 CXSourceLocation start = clang_getRangeStart(range);
333 CXSourceLocation end = clang_getRangeEnd(range);
334 unsigned start_line, start_column, end_line, end_column;
335 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000336 clang_getSpellingLocation(start, &start_file, &start_line,
337 &start_column, 0);
338 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000339 if (clang_equalLocations(start, end)) {
340 /* Insertion. */
341 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000342 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000343 clang_getCString(insertion_text), start_line, start_column);
344 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
345 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000346 if (start_file == file && end_file == file) {
347 fprintf(out, "FIX-IT: Remove ");
348 PrintExtent(out, start_line, start_column, end_line, end_column);
349 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000350 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000351 } else {
352 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000353 if (start_file == end_file) {
354 fprintf(out, "FIX-IT: Replace ");
355 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000356 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000357 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000358 break;
359 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000360 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000361 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000362}
363
Douglas Gregora88084b2010-02-18 18:08:43 +0000364void PrintDiagnostics(CXTranslationUnit TU) {
365 int i, n = clang_getNumDiagnostics(TU);
366 for (i = 0; i != n; ++i) {
367 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
368 PrintDiagnostic(Diag);
369 clang_disposeDiagnostic(Diag);
370 }
371}
372
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000373void PrintMemoryUsage(CXTranslationUnit TU) {
374 CXTUMemoryUsage usage = clang_getCXTUMemoryUsage(TU);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000375 unsigned long total = 0.0;
Francois Pichet3c683362011-04-18 23:33:22 +0000376 unsigned i, n;
377
378 fprintf(stderr, "Memory usage:\n");
379 for (i = 0, n = usage.numEntries; i != n; ++i) {
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000380 const char *name = clang_getTUMemoryUsageName(usage.entries[i].kind);
381 unsigned long amount = usage.entries[i].amount;
382 total += amount;
383 fprintf(stderr, " %s : %ld bytes (%lf MBytes)\n", name, amount,
384 ((double) amount)/(1024*1024));
385 }
386 fprintf(stderr, " TOTAL = %ld bytes (%lf MBytes)\n", total,
387 ((double) total)/(1024*1024));
388 clang_disposeCXTUMemoryUsage(usage);
389}
390
Ted Kremenekce2ae882010-01-26 17:59:48 +0000391/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000392/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000393/******************************************************************************/
394
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000395static const char *FileCheckPrefix = "CHECK";
396
Douglas Gregora7bde202010-01-19 00:34:46 +0000397static void PrintCursorExtent(CXCursor C) {
398 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000399 CXFile begin_file, end_file;
400 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000401
Douglas Gregora9b06d42010-11-09 06:24:54 +0000402 clang_getSpellingLocation(clang_getRangeStart(extent),
403 &begin_file, &begin_line, &begin_column, 0);
404 clang_getSpellingLocation(clang_getRangeEnd(extent),
405 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000406 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000407 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000408
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000409 printf(" Extent=");
410 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000411}
412
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000413/* Data used by all of the visitors. */
414typedef struct {
415 CXTranslationUnit TU;
416 enum CXCursorKind *Filter;
417} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000418
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000419
Ted Kremeneke68fff62010-02-17 00:41:32 +0000420enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000421 CXCursor Parent,
422 CXClientData ClientData) {
423 VisitorData *Data = (VisitorData *)ClientData;
424 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000425 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000426 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000427 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000428 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000429 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000430 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000431 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000432 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000433 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000434 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000435
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000436 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000437}
Steve Naroff50398192009-08-28 15:28:48 +0000438
Ted Kremeneke68fff62010-02-17 00:41:32 +0000439static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000440 CXCursor Parent,
441 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000442 const char *startBuf, *endBuf;
443 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
444 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000445 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000446
Douglas Gregorb6998662010-01-19 19:34:47 +0000447 if (Cursor.kind != CXCursor_FunctionDecl ||
448 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000449 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000450
451 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
452 &startLine, &startColumn,
453 &endLine, &endColumn);
454 /* Probe the entire body, looking for both decls and refs. */
455 curLine = startLine;
456 curColumn = startColumn;
457
458 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000459 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000460 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000461 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000462
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000463 if (*startBuf == '\n') {
464 startBuf++;
465 curLine++;
466 curColumn = 1;
467 } else if (*startBuf != '\t')
468 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000469
Douglas Gregor98258af2010-01-18 22:46:11 +0000470 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000471 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000472
Douglas Gregor1db19de2010-01-19 21:36:55 +0000473 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000474 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000475 CXSourceLocation RefLoc
476 = clang_getLocation(Data->TU, file, curLine, curColumn);
477 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000478 if (Ref.kind == CXCursor_NoDeclFound) {
479 /* Nothing found here; that's fine. */
480 } else if (Ref.kind != CXCursor_FunctionDecl) {
481 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
482 curLine, curColumn);
483 PrintCursor(Ref);
484 printf("\n");
485 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000486 }
Ted Kremenek74844072010-02-17 00:41:20 +0000487 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000488 startBuf++;
489 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000490
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000491 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000492}
493
Ted Kremenek7d405622010-01-12 23:34:26 +0000494/******************************************************************************/
495/* USR testing. */
496/******************************************************************************/
497
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000498enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
499 CXClientData ClientData) {
500 VisitorData *Data = (VisitorData *)ClientData;
501 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000502 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000503 const char *cstr = clang_getCString(USR);
504 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000505 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000506 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000507 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000508 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
509
Douglas Gregora7bde202010-01-19 00:34:46 +0000510 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000511 printf("\n");
512 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000513
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000514 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515 }
516
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000517 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000518}
519
520/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000521/* Inclusion stack testing. */
522/******************************************************************************/
523
524void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
525 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000526
Ted Kremenek16b55a72010-01-26 19:31:51 +0000527 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000528 CXString fname;
529
530 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000531 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000532 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000533
Ted Kremenek16b55a72010-01-26 19:31:51 +0000534 for (i = 0; i < includeStackLen; ++i) {
535 CXFile includingFile;
536 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000537 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
538 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000539 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000540 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000541 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000542 }
543 printf("\n");
544}
545
546void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000547 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000548}
549
550/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000551/* Linkage testing. */
552/******************************************************************************/
553
554static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
555 CXClientData d) {
556 const char *linkage = 0;
557
558 if (clang_isInvalid(clang_getCursorKind(cursor)))
559 return CXChildVisit_Recurse;
560
561 switch (clang_getCursorLinkage(cursor)) {
562 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000563 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
564 case CXLinkage_Internal: linkage = "Internal"; break;
565 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
566 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000567 }
568
569 if (linkage) {
570 PrintCursor(cursor);
571 printf("linkage=%s\n", linkage);
572 }
573
574 return CXChildVisit_Recurse;
575}
576
577/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000578/* Typekind testing. */
579/******************************************************************************/
580
581static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
582 CXClientData d) {
583
584 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
585 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000586 CXString S = clang_getTypeKindSpelling(T.kind);
587 PrintCursor(cursor);
588 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000589 if (clang_isConstQualifiedType(T))
590 printf(" const");
591 if (clang_isVolatileQualifiedType(T))
592 printf(" volatile");
593 if (clang_isRestrictQualifiedType(T))
594 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000595 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000596 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000597 {
598 CXType CT = clang_getCanonicalType(T);
599 if (!clang_equalTypes(T, CT)) {
600 CXString CS = clang_getTypeKindSpelling(CT.kind);
601 printf(" [canonical=%s]", clang_getCString(CS));
602 clang_disposeString(CS);
603 }
604 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000605 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000606 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000607 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000608 if (RT.kind != CXType_Invalid) {
609 CXString RS = clang_getTypeKindSpelling(RT.kind);
610 printf(" [result=%s]", clang_getCString(RS));
611 clang_disposeString(RS);
612 }
613 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000614 /* Print if this is a non-POD type. */
615 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000616
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000617 printf("\n");
618 }
619 return CXChildVisit_Recurse;
620}
621
622
623/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000624/* Loading ASTs/source. */
625/******************************************************************************/
626
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000627static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000628 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000629 CXCursorVisitor Visitor,
630 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000631
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000632 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000633 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000634
635 if (Visitor) {
636 enum CXCursorKind K = CXCursor_NotImplemented;
637 enum CXCursorKind *ck = &K;
638 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000639
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000640 /* Perform some simple filtering. */
641 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000642 else if (!strcmp(filter, "all-display") ||
643 !strcmp(filter, "local-display")) {
644 ck = NULL;
645 want_display_name = 1;
646 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000647 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000648 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
649 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
650 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
651 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
652 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
653 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
654 else {
655 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
656 return 1;
657 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000658
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000659 Data.TU = TU;
660 Data.Filter = ck;
661 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000662 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000663
Ted Kremenekce2ae882010-01-26 17:59:48 +0000664 if (PV)
665 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000666
Douglas Gregora88084b2010-02-18 18:08:43 +0000667 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000668 clang_disposeTranslationUnit(TU);
669 return 0;
670}
671
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000672int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000673 const char *prefix, CXCursorVisitor Visitor,
674 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000675 CXIndex Idx;
676 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000677 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000678 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000679 !strcmp(filter, "local") ? 1 : 0,
680 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000681
Ted Kremenek020a0952010-02-11 07:41:25 +0000682 if (!CreateTranslationUnit(Idx, file, &TU)) {
683 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000684 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000685 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000686
Ted Kremenek020a0952010-02-11 07:41:25 +0000687 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
688 clang_disposeIndex(Idx);
689 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000690}
691
Ted Kremenekce2ae882010-01-26 17:59:48 +0000692int perform_test_load_source(int argc, const char **argv,
693 const char *filter, CXCursorVisitor Visitor,
694 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000695 CXIndex Idx;
696 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000697 struct CXUnsavedFile *unsaved_files = 0;
698 int num_unsaved_files = 0;
699 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000700
Daniel Dunbarada487d2009-12-01 02:03:10 +0000701 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000702 (!strcmp(filter, "local") ||
703 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000704 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000705
Ted Kremenek020a0952010-02-11 07:41:25 +0000706 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
707 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000708 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000709 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000710
Ted Kremeneke68fff62010-02-17 00:41:32 +0000711 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
712 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000713 argv + num_unsaved_files,
714 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000715 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000716 if (!TU) {
717 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000718 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000719 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000720 return 1;
721 }
722
Ted Kremenekce2ae882010-01-26 17:59:48 +0000723 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000724 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000725 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000726 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000727}
728
Douglas Gregorabc563f2010-07-19 21:46:24 +0000729int perform_test_reparse_source(int argc, const char **argv, int trials,
730 const char *filter, CXCursorVisitor Visitor,
731 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000732 CXIndex Idx;
733 CXTranslationUnit TU;
734 struct CXUnsavedFile *unsaved_files = 0;
735 int num_unsaved_files = 0;
736 int result;
737 int trial;
738
739 Idx = clang_createIndex(/* excludeDeclsFromPCH */
740 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000741 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000742
Douglas Gregorabc563f2010-07-19 21:46:24 +0000743 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
744 clang_disposeIndex(Idx);
745 return -1;
746 }
747
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000748 /* Load the initial translation unit -- we do this without honoring remapped
749 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000750 TU = clang_parseTranslationUnit(Idx, 0,
751 argv + num_unsaved_files,
752 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000753 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000754 if (!TU) {
755 fprintf(stderr, "Unable to load translation unit!\n");
756 free_remapped_files(unsaved_files, num_unsaved_files);
757 clang_disposeIndex(Idx);
758 return 1;
759 }
760
761 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000762 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
763 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000764 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000765 clang_disposeTranslationUnit(TU);
766 free_remapped_files(unsaved_files, num_unsaved_files);
767 clang_disposeIndex(Idx);
768 return -1;
769 }
770 }
771
772 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
773 free_remapped_files(unsaved_files, num_unsaved_files);
774 clang_disposeIndex(Idx);
775 return result;
776}
777
Ted Kremenek0d435192009-11-17 18:13:31 +0000778/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000779/* Logic for testing clang_getCursor(). */
780/******************************************************************************/
781
782static void print_cursor_file_scan(CXCursor cursor,
783 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000784 unsigned end_line, unsigned end_col,
785 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000786 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000787 if (prefix)
788 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000789 PrintExtent(stdout, start_line, start_col, end_line, end_col);
790 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000791 PrintCursor(cursor);
792 printf("\n");
793}
794
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000795static int perform_file_scan(const char *ast_file, const char *source_file,
796 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000797 CXIndex Idx;
798 CXTranslationUnit TU;
799 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000800 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000801 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000802 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000803 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000804
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000805 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
806 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000807 fprintf(stderr, "Could not create Index\n");
808 return 1;
809 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000810
Ted Kremenek1c6da172009-11-17 19:37:36 +0000811 if (!CreateTranslationUnit(Idx, ast_file, &TU))
812 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000813
Ted Kremenek1c6da172009-11-17 19:37:36 +0000814 if ((fp = fopen(source_file, "r")) == NULL) {
815 fprintf(stderr, "Could not open '%s'\n", source_file);
816 return 1;
817 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000818
Douglas Gregorb9790342010-01-22 21:44:22 +0000819 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000820 for (;;) {
821 CXCursor cursor;
822 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000823
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000824 if (c == '\n') {
825 ++line;
826 col = 1;
827 } else
828 ++col;
829
830 /* Check the cursor at this position, and dump the previous one if we have
831 * found something new.
832 */
833 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
834 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
835 prevCursor.kind != CXCursor_InvalidFile) {
836 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000837 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000838 start_line = line;
839 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000840 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000841 if (c == EOF)
842 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000843
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000844 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000845 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000846
Ted Kremenek1c6da172009-11-17 19:37:36 +0000847 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000848 clang_disposeTranslationUnit(TU);
849 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000850 return 0;
851}
852
853/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000854/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000855/******************************************************************************/
856
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000857/* Parse file:line:column from the input string. Returns 0 on success, non-zero
858 on failure. If successful, the pointer *filename will contain newly-allocated
859 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000860int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000861 unsigned *column, unsigned *second_line,
862 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000863 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000864 const char *last_colon = strrchr(input, ':');
865 unsigned values[4], i;
866 unsigned num_values = (second_line && second_column)? 4 : 2;
867
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000868 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000869 if (!last_colon || last_colon == input) {
870 if (num_values == 4)
871 fprintf(stderr, "could not parse filename:line:column:line:column in "
872 "'%s'\n", input);
873 else
874 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000875 return 1;
876 }
877
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000878 for (i = 0; i != num_values; ++i) {
879 const char *prev_colon;
880
881 /* Parse the next line or column. */
882 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
883 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000884 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000885 (i % 2 ? "column" : "line"), input);
886 return 1;
887 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000888
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000889 if (i + 1 == num_values)
890 break;
891
892 /* Find the previous colon. */
893 prev_colon = last_colon - 1;
894 while (prev_colon != input && *prev_colon != ':')
895 --prev_colon;
896 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000897 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000898 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000899 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000900 }
901
902 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000903 }
904
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000905 *line = values[0];
906 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000907
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000908 if (second_line && second_column) {
909 *second_line = values[2];
910 *second_column = values[3];
911 }
912
Douglas Gregor88d23952009-11-09 18:19:57 +0000913 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000914 *filename = (char*)malloc(last_colon - input + 1);
915 memcpy(*filename, input, last_colon - input);
916 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000917 return 0;
918}
919
920const char *
921clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
922 switch (Kind) {
923 case CXCompletionChunk_Optional: return "Optional";
924 case CXCompletionChunk_TypedText: return "TypedText";
925 case CXCompletionChunk_Text: return "Text";
926 case CXCompletionChunk_Placeholder: return "Placeholder";
927 case CXCompletionChunk_Informative: return "Informative";
928 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
929 case CXCompletionChunk_LeftParen: return "LeftParen";
930 case CXCompletionChunk_RightParen: return "RightParen";
931 case CXCompletionChunk_LeftBracket: return "LeftBracket";
932 case CXCompletionChunk_RightBracket: return "RightBracket";
933 case CXCompletionChunk_LeftBrace: return "LeftBrace";
934 case CXCompletionChunk_RightBrace: return "RightBrace";
935 case CXCompletionChunk_LeftAngle: return "LeftAngle";
936 case CXCompletionChunk_RightAngle: return "RightAngle";
937 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000938 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000939 case CXCompletionChunk_Colon: return "Colon";
940 case CXCompletionChunk_SemiColon: return "SemiColon";
941 case CXCompletionChunk_Equal: return "Equal";
942 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
943 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000944 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000945
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000946 return "Unknown";
947}
948
Douglas Gregor3ac73852009-11-09 16:04:45 +0000949void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000950 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000951
Douglas Gregor3ac73852009-11-09 16:04:45 +0000952 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000953 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000954 CXString text;
955 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000956 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000957 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000958
Douglas Gregor3ac73852009-11-09 16:04:45 +0000959 if (Kind == CXCompletionChunk_Optional) {
960 fprintf(file, "{Optional ");
961 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000963 file);
964 fprintf(file, "}");
965 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +0000966 }
967
968 if (Kind == CXCompletionChunk_VerticalSpace) {
969 fprintf(file, "{VerticalSpace }");
970 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000971 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972
Douglas Gregord5a20892009-11-09 17:05:28 +0000973 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000974 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000975 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000976 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000977 cstr ? cstr : "");
978 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000979 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000980
Douglas Gregor3ac73852009-11-09 16:04:45 +0000981}
982
983void print_completion_result(CXCompletionResult *completion_result,
984 CXClientData client_data) {
985 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000986 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
987
988 fprintf(file, "%s:", clang_getCString(ks));
989 clang_disposeString(ks);
990
Douglas Gregor3ac73852009-11-09 16:04:45 +0000991 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000992 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000993 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000994 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
995 case CXAvailability_Available:
996 break;
997
998 case CXAvailability_Deprecated:
999 fprintf(file, " (deprecated)");
1000 break;
1001
1002 case CXAvailability_NotAvailable:
1003 fprintf(file, " (unavailable)");
1004 break;
1005 }
1006 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001007}
1008
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001009int my_stricmp(const char *s1, const char *s2) {
1010 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001011 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001012 if (c1 < c2)
1013 return -1;
1014 else if (c1 > c2)
1015 return 1;
1016
1017 ++s1;
1018 ++s2;
1019 }
1020
1021 if (*s1)
1022 return 1;
1023 else if (*s2)
1024 return -1;
1025 return 0;
1026}
1027
Douglas Gregor1982c182010-07-12 18:38:41 +00001028int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001029 const char *input = argv[1];
1030 char *filename = 0;
1031 unsigned line;
1032 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001033 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001034 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001035 struct CXUnsavedFile *unsaved_files = 0;
1036 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001037 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001038 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001039 unsigned I, Repeats = 1;
1040 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1041
1042 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1043 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001044
Douglas Gregor1982c182010-07-12 18:38:41 +00001045 if (timing_only)
1046 input += strlen("-code-completion-timing=");
1047 else
1048 input += strlen("-code-completion-at=");
1049
Ted Kremeneke68fff62010-02-17 00:41:32 +00001050 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001051 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001052 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001053
Douglas Gregor735df882009-12-02 09:21:34 +00001054 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1055 return -1;
1056
Douglas Gregor32be4a52010-10-11 21:37:58 +00001057 CIdx = clang_createIndex(0, 0);
1058
1059 if (getenv("CINDEXTEST_EDITING"))
1060 Repeats = 5;
1061
1062 TU = clang_parseTranslationUnit(CIdx, 0,
1063 argv + num_unsaved_files + 2,
1064 argc - num_unsaved_files - 2,
1065 0, 0, getDefaultParsingOptions());
1066 if (!TU) {
1067 fprintf(stderr, "Unable to load translation unit!\n");
1068 return 1;
1069 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001070
1071 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1072 fprintf(stderr, "Unable to reparse translation init!\n");
1073 return 1;
1074 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001075
1076 for (I = 0; I != Repeats; ++I) {
1077 results = clang_codeCompleteAt(TU, filename, line, column,
1078 unsaved_files, num_unsaved_files,
1079 completionOptions);
1080 if (!results) {
1081 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001082 return 1;
1083 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001084 if (I != Repeats-1)
1085 clang_disposeCodeCompleteResults(results);
1086 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001087
Douglas Gregorec6762c2009-12-18 16:20:58 +00001088 if (results) {
1089 unsigned i, n = results->NumResults;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001090 if (!timing_only) {
1091 /* Sort the code-completion results based on the typed text. */
1092 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1093
Douglas Gregor1982c182010-07-12 18:38:41 +00001094 for (i = 0; i != n; ++i)
1095 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001096 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001097 n = clang_codeCompleteGetNumDiagnostics(results);
1098 for (i = 0; i != n; ++i) {
1099 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1100 PrintDiagnostic(diag);
1101 clang_disposeDiagnostic(diag);
1102 }
Douglas Gregorec6762c2009-12-18 16:20:58 +00001103 clang_disposeCodeCompleteResults(results);
1104 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001105 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001106 clang_disposeIndex(CIdx);
1107 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001108
Douglas Gregor735df882009-12-02 09:21:34 +00001109 free_remapped_files(unsaved_files, num_unsaved_files);
1110
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001111 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001112}
1113
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001114typedef struct {
1115 char *filename;
1116 unsigned line;
1117 unsigned column;
1118} CursorSourceLocation;
1119
1120int inspect_cursor_at(int argc, const char **argv) {
1121 CXIndex CIdx;
1122 int errorCode;
1123 struct CXUnsavedFile *unsaved_files = 0;
1124 int num_unsaved_files = 0;
1125 CXTranslationUnit TU;
1126 CXCursor Cursor;
1127 CursorSourceLocation *Locations = 0;
1128 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001129 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001130 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001131
Ted Kremeneke68fff62010-02-17 00:41:32 +00001132 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001133 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1134 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001135
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001136 /* Parse the locations. */
1137 assert(NumLocations > 0 && "Unable to count locations?");
1138 Locations = (CursorSourceLocation *)malloc(
1139 NumLocations * sizeof(CursorSourceLocation));
1140 for (Loc = 0; Loc < NumLocations; ++Loc) {
1141 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001142 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1143 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001144 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001145 return errorCode;
1146 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001147
1148 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001149 &num_unsaved_files))
1150 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001151
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001152 if (getenv("CINDEXTEST_EDITING"))
1153 Repeats = 5;
1154
1155 /* Parse the translation unit. When we're testing clang_getCursor() after
1156 reparsing, don't remap unsaved files until the second parse. */
1157 CIdx = clang_createIndex(1, 1);
1158 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1159 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001160 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001161 unsaved_files,
1162 Repeats > 1? 0 : num_unsaved_files,
1163 getDefaultParsingOptions());
1164
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001165 if (!TU) {
1166 fprintf(stderr, "unable to parse input\n");
1167 return -1;
1168 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001169
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001170 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001171 if (Repeats > 1 &&
1172 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1173 clang_defaultReparseOptions(TU))) {
1174 clang_disposeTranslationUnit(TU);
1175 return 1;
1176 }
1177
1178 for (Loc = 0; Loc < NumLocations; ++Loc) {
1179 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1180 if (!file)
1181 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001182
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001183 Cursor = clang_getCursor(TU,
1184 clang_getLocation(TU, file, Locations[Loc].line,
1185 Locations[Loc].column));
1186 if (I + 1 == Repeats) {
1187 PrintCursor(Cursor);
1188 printf("\n");
1189 free(Locations[Loc].filename);
1190 }
1191 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001192 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001193
Douglas Gregora88084b2010-02-18 18:08:43 +00001194 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001195 clang_disposeTranslationUnit(TU);
1196 clang_disposeIndex(CIdx);
1197 free(Locations);
1198 free_remapped_files(unsaved_files, num_unsaved_files);
1199 return 0;
1200}
1201
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001202int perform_token_annotation(int argc, const char **argv) {
1203 const char *input = argv[1];
1204 char *filename = 0;
1205 unsigned line, second_line;
1206 unsigned column, second_column;
1207 CXIndex CIdx;
1208 CXTranslationUnit TU = 0;
1209 int errorCode;
1210 struct CXUnsavedFile *unsaved_files = 0;
1211 int num_unsaved_files = 0;
1212 CXToken *tokens;
1213 unsigned num_tokens;
1214 CXSourceRange range;
1215 CXSourceLocation startLoc, endLoc;
1216 CXFile file = 0;
1217 CXCursor *cursors = 0;
1218 unsigned i;
1219
1220 input += strlen("-test-annotate-tokens=");
1221 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1222 &second_line, &second_column)))
1223 return errorCode;
1224
1225 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1226 return -1;
1227
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001228 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001229 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1230 argc - num_unsaved_files - 3,
1231 argv + num_unsaved_files + 2,
1232 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001233 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001234 if (!TU) {
1235 fprintf(stderr, "unable to parse input\n");
1236 clang_disposeIndex(CIdx);
1237 free(filename);
1238 free_remapped_files(unsaved_files, num_unsaved_files);
1239 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001240 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001241 errorCode = 0;
1242
1243 file = clang_getFile(TU, filename);
1244 if (!file) {
1245 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1246 errorCode = -1;
1247 goto teardown;
1248 }
1249
1250 startLoc = clang_getLocation(TU, file, line, column);
1251 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001252 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001253 column);
1254 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001255 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001256 }
1257
1258 endLoc = clang_getLocation(TU, file, second_line, second_column);
1259 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001260 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001261 second_line, second_column);
1262 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001263 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001264 }
1265
1266 range = clang_getRange(startLoc, endLoc);
1267 clang_tokenize(TU, range, &tokens, &num_tokens);
1268 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1269 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1270 for (i = 0; i != num_tokens; ++i) {
1271 const char *kind = "<unknown>";
1272 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1273 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1274 unsigned start_line, start_column, end_line, end_column;
1275
1276 switch (clang_getTokenKind(tokens[i])) {
1277 case CXToken_Punctuation: kind = "Punctuation"; break;
1278 case CXToken_Keyword: kind = "Keyword"; break;
1279 case CXToken_Identifier: kind = "Identifier"; break;
1280 case CXToken_Literal: kind = "Literal"; break;
1281 case CXToken_Comment: kind = "Comment"; break;
1282 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00001283 clang_getSpellingLocation(clang_getRangeStart(extent),
1284 0, &start_line, &start_column, 0);
1285 clang_getSpellingLocation(clang_getRangeEnd(extent),
1286 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001287 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1288 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001289 if (!clang_isInvalid(cursors[i].kind)) {
1290 printf(" ");
1291 PrintCursor(cursors[i]);
1292 }
1293 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001294 }
1295 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00001296 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001297
1298 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001299 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001300 clang_disposeTranslationUnit(TU);
1301 clang_disposeIndex(CIdx);
1302 free(filename);
1303 free_remapped_files(unsaved_files, num_unsaved_files);
1304 return errorCode;
1305}
1306
Ted Kremenek0d435192009-11-17 18:13:31 +00001307/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001308/* USR printing. */
1309/******************************************************************************/
1310
1311static int insufficient_usr(const char *kind, const char *usage) {
1312 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1313 return 1;
1314}
1315
1316static unsigned isUSR(const char *s) {
1317 return s[0] == 'c' && s[1] == ':';
1318}
1319
1320static int not_usr(const char *s, const char *arg) {
1321 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1322 return 1;
1323}
1324
1325static void print_usr(CXString usr) {
1326 const char *s = clang_getCString(usr);
1327 printf("%s\n", s);
1328 clang_disposeString(usr);
1329}
1330
1331static void display_usrs() {
1332 fprintf(stderr, "-print-usrs options:\n"
1333 " ObjCCategory <class name> <category name>\n"
1334 " ObjCClass <class name>\n"
1335 " ObjCIvar <ivar name> <class USR>\n"
1336 " ObjCMethod <selector> [0=class method|1=instance method] "
1337 "<class USR>\n"
1338 " ObjCProperty <property name> <class USR>\n"
1339 " ObjCProtocol <protocol name>\n");
1340}
1341
1342int print_usrs(const char **I, const char **E) {
1343 while (I != E) {
1344 const char *kind = *I;
1345 unsigned len = strlen(kind);
1346 switch (len) {
1347 case 8:
1348 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1349 if (I + 2 >= E)
1350 return insufficient_usr(kind, "<ivar name> <class USR>");
1351 if (!isUSR(I[2]))
1352 return not_usr("<class USR>", I[2]);
1353 else {
1354 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001355 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001356 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001357 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1358 }
1359
1360 I += 3;
1361 continue;
1362 }
1363 break;
1364 case 9:
1365 if (memcmp(kind, "ObjCClass", 9) == 0) {
1366 if (I + 1 >= E)
1367 return insufficient_usr(kind, "<class name>");
1368 print_usr(clang_constructUSR_ObjCClass(I[1]));
1369 I += 2;
1370 continue;
1371 }
1372 break;
1373 case 10:
1374 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1375 if (I + 3 >= E)
1376 return insufficient_usr(kind, "<method selector> "
1377 "[0=class method|1=instance method] <class USR>");
1378 if (!isUSR(I[3]))
1379 return not_usr("<class USR>", I[3]);
1380 else {
1381 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001382 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00001383 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001384 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1385 }
1386 I += 4;
1387 continue;
1388 }
1389 break;
1390 case 12:
1391 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1392 if (I + 2 >= E)
1393 return insufficient_usr(kind, "<class name> <category name>");
1394 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1395 I += 3;
1396 continue;
1397 }
1398 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1399 if (I + 1 >= E)
1400 return insufficient_usr(kind, "<protocol name>");
1401 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1402 I += 2;
1403 continue;
1404 }
1405 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1406 if (I + 2 >= E)
1407 return insufficient_usr(kind, "<property name> <class USR>");
1408 if (!isUSR(I[2]))
1409 return not_usr("<class USR>", I[2]);
1410 else {
1411 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001412 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001413 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001414 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1415 }
1416 I += 3;
1417 continue;
1418 }
1419 break;
1420 default:
1421 break;
1422 }
1423 break;
1424 }
1425
1426 if (I != E) {
1427 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1428 display_usrs();
1429 return 1;
1430 }
1431 return 0;
1432}
1433
1434int print_usrs_file(const char *file_name) {
1435 char line[2048];
1436 const char *args[128];
1437 unsigned numChars = 0;
1438
1439 FILE *fp = fopen(file_name, "r");
1440 if (!fp) {
1441 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1442 return 1;
1443 }
1444
1445 /* This code is not really all that safe, but it works fine for testing. */
1446 while (!feof(fp)) {
1447 char c = fgetc(fp);
1448 if (c == '\n') {
1449 unsigned i = 0;
1450 const char *s = 0;
1451
1452 if (numChars == 0)
1453 continue;
1454
1455 line[numChars] = '\0';
1456 numChars = 0;
1457
1458 if (line[0] == '/' && line[1] == '/')
1459 continue;
1460
1461 s = strtok(line, " ");
1462 while (s) {
1463 args[i] = s;
1464 ++i;
1465 s = strtok(0, " ");
1466 }
1467 if (print_usrs(&args[0], &args[i]))
1468 return 1;
1469 }
1470 else
1471 line[numChars++] = c;
1472 }
1473
1474 fclose(fp);
1475 return 0;
1476}
1477
1478/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001479/* Command line processing. */
1480/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001481int write_pch_file(const char *filename, int argc, const char *argv[]) {
1482 CXIndex Idx;
1483 CXTranslationUnit TU;
1484 struct CXUnsavedFile *unsaved_files = 0;
1485 int num_unsaved_files = 0;
1486
1487 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1488
1489 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1490 clang_disposeIndex(Idx);
1491 return -1;
1492 }
1493
1494 TU = clang_parseTranslationUnit(Idx, 0,
1495 argv + num_unsaved_files,
1496 argc - num_unsaved_files,
1497 unsaved_files,
1498 num_unsaved_files,
1499 CXTranslationUnit_Incomplete);
1500 if (!TU) {
1501 fprintf(stderr, "Unable to load translation unit!\n");
1502 free_remapped_files(unsaved_files, num_unsaved_files);
1503 clang_disposeIndex(Idx);
1504 return 1;
1505 }
1506
Douglas Gregor19998442010-08-13 15:35:05 +00001507 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001508 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1509 clang_disposeTranslationUnit(TU);
1510 free_remapped_files(unsaved_files, num_unsaved_files);
1511 clang_disposeIndex(Idx);
1512 return 0;
1513}
1514
1515/******************************************************************************/
1516/* Command line processing. */
1517/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001518
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001519static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001520 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001521 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001522 if (strcmp(s, "-usrs") == 0)
1523 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001524 if (strncmp(s, "-memory-usage", 13) == 0)
1525 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001526 return NULL;
1527}
1528
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001529static void print_usage(void) {
1530 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001531 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001532 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001533 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001534 " c-index-test -test-file-scan <AST file> <source file> "
1535 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001536 " c-index-test -test-load-tu <AST file> <symbol filter> "
1537 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001538 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1539 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001540 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001541 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001542 " c-index-test -test-load-source-memory-usage "
1543 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00001544 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1545 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001546 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001547 " c-index-test -test-load-source-usrs-memory-usage "
1548 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001549 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1550 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001551 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001552 " c-index-test -test-print-linkage-source {<args>}*\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00001553 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001554 " c-index-test -test-print-typekind {<args>}*\n"
1555 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001556 " c-index-test -print-usr-file <file>\n"
1557 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001558 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001559 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001560 " all - load all symbols, including those from PCH\n"
1561 " local - load all symbols except those in PCH\n"
1562 " category - only load ObjC categories (non-PCH)\n"
1563 " interface - only load ObjC interfaces (non-PCH)\n"
1564 " protocol - only load ObjC protocols (non-PCH)\n"
1565 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001566 " typedef - only load typdefs (non-PCH)\n"
1567 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001568}
1569
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001570/***/
1571
1572int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001573 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001574 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001575 return perform_code_completion(argc, argv, 0);
1576 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1577 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001578 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1579 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001580 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001581 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001582 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001583 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1584 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001585 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001586 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1587 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1588 if (I) {
1589 int trials = atoi(argv[2]);
1590 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1591 NULL);
1592 }
1593 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001594 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001595 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001596
1597 PostVisitTU postVisit = 0;
1598 if (strstr(argv[1], "-memory-usage"))
1599 postVisit = PrintMemoryUsage;
1600
Ted Kremenek7d405622010-01-12 23:34:26 +00001601 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001602 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
1603 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00001604 }
1605 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001606 return perform_file_scan(argv[2], argv[3],
1607 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001608 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1609 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001610 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1611 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1612 PrintInclusionStack);
1613 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1614 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1615 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001616 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1617 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1618 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001619 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1620 return perform_test_load_source(argc - 2, argv + 2, "all",
1621 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001622 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1623 if (argc > 2)
1624 return print_usrs(argv + 2, argv + argc);
1625 else {
1626 display_usrs();
1627 return 1;
1628 }
1629 }
1630 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1631 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001632 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1633 return write_pch_file(argv[2], argc - 3, argv + 3);
1634
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001635 print_usage();
1636 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001637}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001638
1639/***/
1640
1641/* We intentionally run in a separate thread to ensure we at least minimal
1642 * testing of a multithreaded environment (for example, having a reduced stack
1643 * size). */
1644
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001645typedef struct thread_info {
1646 int argc;
1647 const char **argv;
1648 int result;
1649} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00001650void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001651 thread_info *client_data = client_data_v;
1652 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001653}
1654
1655int main(int argc, const char **argv) {
1656 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001657
Douglas Gregor61605982010-10-27 16:00:01 +00001658 if (getenv("CINDEXTEST_NOTHREADS"))
1659 return cindextest_main(argc, argv);
1660
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001661 client_data.argc = argc;
1662 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00001663 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001664 return client_data.result;
1665}