blob: 63031ce178d684f3a4212916868eab8c468b7e1e [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00004#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00005#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00006#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00007#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00008#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009
Ted Kremenek0d435192009-11-17 18:13:31 +000010/******************************************************************************/
11/* Utility functions. */
12/******************************************************************************/
13
John Thompson2e06fc82009-10-27 13:42:56 +000014#ifdef _MSC_VER
15char *basename(const char* path)
16{
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
25
26 return((char*)path);
27}
28#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000029extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000030#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000031
Douglas Gregor45ba9a12010-07-25 17:39:21 +000032/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000033static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
35
36 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000037 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000038 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
Douglas Gregordca8ee82011-05-06 16:33:08 +000040 if (getenv("CINDEXTEST_NESTED_MACROS"))
Chandler Carruthba7537f2011-07-14 09:02:10 +000041 options |= CXTranslationUnit_NestedMacroExpansions;
Douglas Gregor44c181a2010-07-23 00:33:23 +000042
43 return options;
44}
45
Daniel Dunbar51b058c2010-02-14 08:32:24 +000046static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
47 unsigned end_line, unsigned end_column) {
48 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000049 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000050}
51
Ted Kremenek1c6da172009-11-17 19:37:36 +000052static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
53 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000054
Douglas Gregora88084b2010-02-18 18:08:43 +000055 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000056 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000057 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
58 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000059 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000060 return 1;
61}
62
Douglas Gregor4db64a42010-01-23 00:14:00 +000063void free_remapped_files(struct CXUnsavedFile *unsaved_files,
64 int num_unsaved_files) {
65 int i;
66 for (i = 0; i != num_unsaved_files; ++i) {
67 free((char *)unsaved_files[i].Filename);
68 free((char *)unsaved_files[i].Contents);
69 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000070 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000071}
72
73int parse_remapped_files(int argc, const char **argv, int start_arg,
74 struct CXUnsavedFile **unsaved_files,
75 int *num_unsaved_files) {
76 int i;
77 int arg;
78 int prefix_len = strlen("-remap-file=");
79 *unsaved_files = 0;
80 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000081
Douglas Gregor4db64a42010-01-23 00:14:00 +000082 /* Count the number of remapped files. */
83 for (arg = start_arg; arg < argc; ++arg) {
84 if (strncmp(argv[arg], "-remap-file=", prefix_len))
85 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000086
Douglas Gregor4db64a42010-01-23 00:14:00 +000087 ++*num_unsaved_files;
88 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000089
Douglas Gregor4db64a42010-01-23 00:14:00 +000090 if (*num_unsaved_files == 0)
91 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000092
Douglas Gregor4db64a42010-01-23 00:14:00 +000093 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000094 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
95 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000096 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
97 struct CXUnsavedFile *unsaved = *unsaved_files + i;
98 const char *arg_string = argv[arg] + prefix_len;
99 int filename_len;
100 char *filename;
101 char *contents;
102 FILE *to_file;
103 const char *semi = strchr(arg_string, ';');
104 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000105 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000106 "error: -remap-file=from;to argument is missing semicolon\n");
107 free_remapped_files(*unsaved_files, i);
108 *unsaved_files = 0;
109 *num_unsaved_files = 0;
110 return -1;
111 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000112
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000114 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000115 if (!to_file) {
116 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
117 semi + 1);
118 free_remapped_files(*unsaved_files, i);
119 *unsaved_files = 0;
120 *num_unsaved_files = 0;
121 return -1;
122 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000123
Douglas Gregor4db64a42010-01-23 00:14:00 +0000124 /* Determine the length of the file we're remapping to. */
125 fseek(to_file, 0, SEEK_END);
126 unsaved->Length = ftell(to_file);
127 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000128
Douglas Gregor4db64a42010-01-23 00:14:00 +0000129 /* Read the contents of the file we're remapping to. */
130 contents = (char *)malloc(unsaved->Length + 1);
131 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
132 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
133 (feof(to_file) ? "EOF" : "error"), semi + 1);
134 fclose(to_file);
135 free_remapped_files(*unsaved_files, i);
136 *unsaved_files = 0;
137 *num_unsaved_files = 0;
138 return -1;
139 }
140 contents[unsaved->Length] = 0;
141 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000142
Douglas Gregor4db64a42010-01-23 00:14:00 +0000143 /* Close the file. */
144 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000145
Douglas Gregor4db64a42010-01-23 00:14:00 +0000146 /* Copy the file name that we're remapping from. */
147 filename_len = semi - arg_string;
148 filename = (char *)malloc(filename_len + 1);
149 memcpy(filename, arg_string, filename_len);
150 filename[filename_len] = 0;
151 unsaved->Filename = filename;
152 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000153
Douglas Gregor4db64a42010-01-23 00:14:00 +0000154 return 0;
155}
156
Ted Kremenek0d435192009-11-17 18:13:31 +0000157/******************************************************************************/
158/* Pretty-printing. */
159/******************************************************************************/
160
Douglas Gregor358559d2010-10-02 22:49:11 +0000161int want_display_name = 0;
162
Douglas Gregordd3e5542011-05-04 00:14:37 +0000163static void PrintCursor(CXTranslationUnit TU, CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000164 if (clang_isInvalid(Cursor.kind)) {
165 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
166 printf("Invalid Cursor => %s", clang_getCString(ks));
167 clang_disposeString(ks);
168 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000169 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000170 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000171 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000172 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000173 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000174 CXCursor *overridden;
175 unsigned num_overridden;
176
Ted Kremeneke68fff62010-02-17 00:41:32 +0000177 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000178 string = want_display_name? clang_getCursorDisplayName(Cursor)
179 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000180 printf("%s=%s", clang_getCString(ks),
181 clang_getCString(string));
182 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000183 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000184
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000185 Referenced = clang_getCursorReferenced(Cursor);
186 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000187 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
188 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
189 printf("[");
190 for (I = 0; I != N; ++I) {
191 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000192 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000193 if (I)
194 printf(", ");
195
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000196 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000197 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000198 printf("%d:%d", line, column);
199 }
200 printf("]");
201 } else {
202 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000203 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000204 printf(":%d:%d", line, column);
205 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000206 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000207
208 if (clang_isCursorDefinition(Cursor))
209 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000210
211 switch (clang_getCursorAvailability(Cursor)) {
212 case CXAvailability_Available:
213 break;
214
215 case CXAvailability_Deprecated:
216 printf(" (deprecated)");
217 break;
218
219 case CXAvailability_NotAvailable:
220 printf(" (unavailable)");
221 break;
222 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000223
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000224 if (clang_CXXMethod_isStatic(Cursor))
225 printf(" (static)");
226 if (clang_CXXMethod_isVirtual(Cursor))
227 printf(" (virtual)");
228
Ted Kremenek95f33552010-08-26 01:42:22 +0000229 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
230 CXType T =
231 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
232 CXString S = clang_getTypeKindSpelling(T.kind);
233 printf(" [IBOutletCollection=%s]", clang_getCString(S));
234 clang_disposeString(S);
235 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000236
237 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
238 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
239 unsigned isVirtual = clang_isVirtualBase(Cursor);
240 const char *accessStr = 0;
241
242 switch (access) {
243 case CX_CXXInvalidAccessSpecifier:
244 accessStr = "invalid"; break;
245 case CX_CXXPublic:
246 accessStr = "public"; break;
247 case CX_CXXProtected:
248 accessStr = "protected"; break;
249 case CX_CXXPrivate:
250 accessStr = "private"; break;
251 }
252
253 printf(" [access=%s isVirtual=%s]", accessStr,
254 isVirtual ? "true" : "false");
255 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000256
257 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
258 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
259 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
260 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000261 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000262 printf(" [Specialization of %s:%d:%d]",
263 clang_getCString(Name), line, column);
264 clang_disposeString(Name);
265 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000266
267 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
268 if (num_overridden) {
269 unsigned I;
270 printf(" [Overrides ");
271 for (I = 0; I != num_overridden; ++I) {
272 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000273 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000274 if (I)
275 printf(", ");
276 printf("@%d:%d", line, column);
277 }
278 printf("]");
279 clang_disposeOverriddenCursors(overridden);
280 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000281
282 if (Cursor.kind == CXCursor_InclusionDirective) {
283 CXFile File = clang_getIncludedFile(Cursor);
284 CXString Included = clang_getFileName(File);
285 printf(" (%s)", clang_getCString(Included));
286 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000287
288 if (clang_isFileMultipleIncludeGuarded(TU, File))
289 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000290 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000291 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000292}
Steve Naroff89922f82009-08-31 00:59:03 +0000293
Ted Kremeneke68fff62010-02-17 00:41:32 +0000294static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000295 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000296 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000297 CXFile file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000298 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000299 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000300 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000301 clang_disposeString(source);
302 return "<invalid loc>";
303 }
304 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000305 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000306 clang_disposeString(source);
307 return b;
308 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000309}
310
Ted Kremenek0d435192009-11-17 18:13:31 +0000311/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000312/* Callbacks. */
313/******************************************************************************/
314
315typedef void (*PostVisitTU)(CXTranslationUnit);
316
Douglas Gregora88084b2010-02-18 18:08:43 +0000317void PrintDiagnostic(CXDiagnostic Diagnostic) {
318 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000319 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000320 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000321 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000322 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
323 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000324 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000325
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000326 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000327 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000328
Douglas Gregor274f1902010-02-22 23:17:23 +0000329 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
330 fprintf(stderr, "%s\n", clang_getCString(Msg));
331 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000332
Douglas Gregora9b06d42010-11-09 06:24:54 +0000333 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
334 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000335 if (!file)
336 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000337
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000338 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
339 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000340 CXSourceRange range;
341 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
342 CXSourceLocation start = clang_getRangeStart(range);
343 CXSourceLocation end = clang_getRangeEnd(range);
344 unsigned start_line, start_column, end_line, end_column;
345 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000346 clang_getSpellingLocation(start, &start_file, &start_line,
347 &start_column, 0);
348 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000349 if (clang_equalLocations(start, end)) {
350 /* Insertion. */
351 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000352 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000353 clang_getCString(insertion_text), start_line, start_column);
354 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
355 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000356 if (start_file == file && end_file == file) {
357 fprintf(out, "FIX-IT: Remove ");
358 PrintExtent(out, start_line, start_column, end_line, end_column);
359 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000360 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000361 } else {
362 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000363 if (start_file == end_file) {
364 fprintf(out, "FIX-IT: Replace ");
365 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000366 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000367 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000368 break;
369 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000370 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000371 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000372}
373
Douglas Gregora88084b2010-02-18 18:08:43 +0000374void PrintDiagnostics(CXTranslationUnit TU) {
375 int i, n = clang_getNumDiagnostics(TU);
376 for (i = 0; i != n; ++i) {
377 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
378 PrintDiagnostic(Diag);
379 clang_disposeDiagnostic(Diag);
380 }
381}
382
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000383void PrintMemoryUsage(CXTranslationUnit TU) {
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000384 unsigned long total = 0.0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000385 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000386 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000387 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000388 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000389 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000390 unsigned long amount = usage.entries[i].amount;
391 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000392 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000393 ((double) amount)/(1024*1024));
394 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000395 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000396 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000397 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000398}
399
Ted Kremenekce2ae882010-01-26 17:59:48 +0000400/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000401/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000402/******************************************************************************/
403
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000404static const char *FileCheckPrefix = "CHECK";
405
Douglas Gregora7bde202010-01-19 00:34:46 +0000406static void PrintCursorExtent(CXCursor C) {
407 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000408 CXFile begin_file, end_file;
409 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000410
Douglas Gregora9b06d42010-11-09 06:24:54 +0000411 clang_getSpellingLocation(clang_getRangeStart(extent),
412 &begin_file, &begin_line, &begin_column, 0);
413 clang_getSpellingLocation(clang_getRangeEnd(extent),
414 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000415 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000416 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000417
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000418 printf(" Extent=");
419 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000420}
421
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000422/* Data used by all of the visitors. */
423typedef struct {
424 CXTranslationUnit TU;
425 enum CXCursorKind *Filter;
426} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000427
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000428
Ted Kremeneke68fff62010-02-17 00:41:32 +0000429enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000430 CXCursor Parent,
431 CXClientData ClientData) {
432 VisitorData *Data = (VisitorData *)ClientData;
433 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000434 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000435 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000436 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000437 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000438 GetCursorSource(Cursor), line, column);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000439 PrintCursor(Data->TU, Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000440 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000441 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000442 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000443 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000444
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000445 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000446}
Steve Naroff50398192009-08-28 15:28:48 +0000447
Ted Kremeneke68fff62010-02-17 00:41:32 +0000448static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000449 CXCursor Parent,
450 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000451 const char *startBuf, *endBuf;
452 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
453 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000454 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000455
Douglas Gregorb6998662010-01-19 19:34:47 +0000456 if (Cursor.kind != CXCursor_FunctionDecl ||
457 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000458 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000459
460 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
461 &startLine, &startColumn,
462 &endLine, &endColumn);
463 /* Probe the entire body, looking for both decls and refs. */
464 curLine = startLine;
465 curColumn = startColumn;
466
467 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000468 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000469 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000470 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000471
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000472 if (*startBuf == '\n') {
473 startBuf++;
474 curLine++;
475 curColumn = 1;
476 } else if (*startBuf != '\t')
477 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000478
Douglas Gregor98258af2010-01-18 22:46:11 +0000479 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000480 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000481
Douglas Gregor1db19de2010-01-19 21:36:55 +0000482 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000483 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000484 CXSourceLocation RefLoc
485 = clang_getLocation(Data->TU, file, curLine, curColumn);
486 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000487 if (Ref.kind == CXCursor_NoDeclFound) {
488 /* Nothing found here; that's fine. */
489 } else if (Ref.kind != CXCursor_FunctionDecl) {
490 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
491 curLine, curColumn);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000492 PrintCursor(Data->TU, Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000493 printf("\n");
494 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495 }
Ted Kremenek74844072010-02-17 00:41:20 +0000496 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000497 startBuf++;
498 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000499
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000500 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000501}
502
Ted Kremenek7d405622010-01-12 23:34:26 +0000503/******************************************************************************/
504/* USR testing. */
505/******************************************************************************/
506
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000507enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
508 CXClientData ClientData) {
509 VisitorData *Data = (VisitorData *)ClientData;
510 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000511 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000512 const char *cstr = clang_getCString(USR);
513 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000514 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000515 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000516 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000517 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
518
Douglas Gregora7bde202010-01-19 00:34:46 +0000519 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000520 printf("\n");
521 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000522
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000523 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000524 }
525
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000526 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000527}
528
529/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000530/* Inclusion stack testing. */
531/******************************************************************************/
532
533void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
534 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000535
Ted Kremenek16b55a72010-01-26 19:31:51 +0000536 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000537 CXString fname;
538
539 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000540 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000541 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000542
Ted Kremenek16b55a72010-01-26 19:31:51 +0000543 for (i = 0; i < includeStackLen; ++i) {
544 CXFile includingFile;
545 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000546 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
547 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000548 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000549 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000550 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000551 }
552 printf("\n");
553}
554
555void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000556 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000557}
558
559/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000560/* Linkage testing. */
561/******************************************************************************/
562
563static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
564 CXClientData d) {
565 const char *linkage = 0;
566
Douglas Gregordd3e5542011-05-04 00:14:37 +0000567 VisitorData *Data = (VisitorData *)d;
568
Ted Kremenek3bed5272010-03-03 06:37:58 +0000569 if (clang_isInvalid(clang_getCursorKind(cursor)))
570 return CXChildVisit_Recurse;
571
572 switch (clang_getCursorLinkage(cursor)) {
573 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000574 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
575 case CXLinkage_Internal: linkage = "Internal"; break;
576 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
577 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000578 }
579
580 if (linkage) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000581 PrintCursor(Data->TU, cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000582 printf("linkage=%s\n", linkage);
583 }
584
585 return CXChildVisit_Recurse;
586}
587
588/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000589/* Typekind testing. */
590/******************************************************************************/
591
592static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
593 CXClientData d) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000594 VisitorData *Data = (VisitorData *)d;
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000595
596 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
597 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000598 CXString S = clang_getTypeKindSpelling(T.kind);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000599 PrintCursor(Data->TU, cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000600 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000601 if (clang_isConstQualifiedType(T))
602 printf(" const");
603 if (clang_isVolatileQualifiedType(T))
604 printf(" volatile");
605 if (clang_isRestrictQualifiedType(T))
606 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000607 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000608 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000609 {
610 CXType CT = clang_getCanonicalType(T);
611 if (!clang_equalTypes(T, CT)) {
612 CXString CS = clang_getTypeKindSpelling(CT.kind);
613 printf(" [canonical=%s]", clang_getCString(CS));
614 clang_disposeString(CS);
615 }
616 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000617 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000618 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000619 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000620 if (RT.kind != CXType_Invalid) {
621 CXString RS = clang_getTypeKindSpelling(RT.kind);
622 printf(" [result=%s]", clang_getCString(RS));
623 clang_disposeString(RS);
624 }
625 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000626 /* Print if this is a non-POD type. */
627 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000628
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000629 printf("\n");
630 }
631 return CXChildVisit_Recurse;
632}
633
634
635/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000636/* Loading ASTs/source. */
637/******************************************************************************/
638
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000639static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000640 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000641 CXCursorVisitor Visitor,
642 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000643
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000644 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000645 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000646
647 if (Visitor) {
648 enum CXCursorKind K = CXCursor_NotImplemented;
649 enum CXCursorKind *ck = &K;
650 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000651
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000652 /* Perform some simple filtering. */
653 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000654 else if (!strcmp(filter, "all-display") ||
655 !strcmp(filter, "local-display")) {
656 ck = NULL;
657 want_display_name = 1;
658 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000659 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000660 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
661 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
662 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
663 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
664 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
665 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
666 else {
667 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
668 return 1;
669 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000670
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000671 Data.TU = TU;
672 Data.Filter = ck;
673 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000674 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675
Ted Kremenekce2ae882010-01-26 17:59:48 +0000676 if (PV)
677 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000678
Douglas Gregora88084b2010-02-18 18:08:43 +0000679 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000680 clang_disposeTranslationUnit(TU);
681 return 0;
682}
683
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000684int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000685 const char *prefix, CXCursorVisitor Visitor,
686 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000687 CXIndex Idx;
688 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000689 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000690 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000691 !strcmp(filter, "local") ? 1 : 0,
692 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000693
Ted Kremenek020a0952010-02-11 07:41:25 +0000694 if (!CreateTranslationUnit(Idx, file, &TU)) {
695 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000696 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000697 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000698
Ted Kremenek020a0952010-02-11 07:41:25 +0000699 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
700 clang_disposeIndex(Idx);
701 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000702}
703
Ted Kremenekce2ae882010-01-26 17:59:48 +0000704int perform_test_load_source(int argc, const char **argv,
705 const char *filter, CXCursorVisitor Visitor,
706 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +0000707 CXIndex Idx;
708 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000709 struct CXUnsavedFile *unsaved_files = 0;
710 int num_unsaved_files = 0;
711 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000712
Daniel Dunbarada487d2009-12-01 02:03:10 +0000713 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000714 (!strcmp(filter, "local") ||
715 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +0000716 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000717
Ted Kremenek020a0952010-02-11 07:41:25 +0000718 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
719 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000720 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000721 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000722
Douglas Gregordca8ee82011-05-06 16:33:08 +0000723 TU = clang_parseTranslationUnit(Idx, 0,
724 argv + num_unsaved_files,
725 argc - num_unsaved_files,
726 unsaved_files, num_unsaved_files,
727 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +0000728 if (!TU) {
729 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000730 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000731 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000732 return 1;
733 }
734
Ted Kremenekce2ae882010-01-26 17:59:48 +0000735 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000736 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000737 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000738 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000739}
740
Douglas Gregorabc563f2010-07-19 21:46:24 +0000741int perform_test_reparse_source(int argc, const char **argv, int trials,
742 const char *filter, CXCursorVisitor Visitor,
743 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +0000744 CXIndex Idx;
745 CXTranslationUnit TU;
746 struct CXUnsavedFile *unsaved_files = 0;
747 int num_unsaved_files = 0;
748 int result;
749 int trial;
750
751 Idx = clang_createIndex(/* excludeDeclsFromPCH */
752 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +0000753 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +0000754
Douglas Gregorabc563f2010-07-19 21:46:24 +0000755 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
756 clang_disposeIndex(Idx);
757 return -1;
758 }
759
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000760 /* Load the initial translation unit -- we do this without honoring remapped
761 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000762 TU = clang_parseTranslationUnit(Idx, 0,
763 argv + num_unsaved_files,
764 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000765 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000766 if (!TU) {
767 fprintf(stderr, "Unable to load translation unit!\n");
768 free_remapped_files(unsaved_files, num_unsaved_files);
769 clang_disposeIndex(Idx);
770 return 1;
771 }
772
773 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000774 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
775 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000776 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000777 clang_disposeTranslationUnit(TU);
778 free_remapped_files(unsaved_files, num_unsaved_files);
779 clang_disposeIndex(Idx);
780 return -1;
781 }
782 }
783
784 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
785 free_remapped_files(unsaved_files, num_unsaved_files);
786 clang_disposeIndex(Idx);
787 return result;
788}
789
Ted Kremenek0d435192009-11-17 18:13:31 +0000790/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000791/* Logic for testing clang_getCursor(). */
792/******************************************************************************/
793
Douglas Gregordd3e5542011-05-04 00:14:37 +0000794static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +0000795 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000796 unsigned end_line, unsigned end_col,
797 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000798 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000799 if (prefix)
800 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000801 PrintExtent(stdout, start_line, start_col, end_line, end_col);
802 printf(" ");
Douglas Gregordd3e5542011-05-04 00:14:37 +0000803 PrintCursor(TU, cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000804 printf("\n");
805}
806
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000807static int perform_file_scan(const char *ast_file, const char *source_file,
808 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000809 CXIndex Idx;
810 CXTranslationUnit TU;
811 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000812 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000813 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000814 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000815 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000816
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000817 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
818 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000819 fprintf(stderr, "Could not create Index\n");
820 return 1;
821 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000822
Ted Kremenek1c6da172009-11-17 19:37:36 +0000823 if (!CreateTranslationUnit(Idx, ast_file, &TU))
824 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000825
Ted Kremenek1c6da172009-11-17 19:37:36 +0000826 if ((fp = fopen(source_file, "r")) == NULL) {
827 fprintf(stderr, "Could not open '%s'\n", source_file);
828 return 1;
829 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000830
Douglas Gregorb9790342010-01-22 21:44:22 +0000831 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000832 for (;;) {
833 CXCursor cursor;
834 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000835
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000836 if (c == '\n') {
837 ++line;
838 col = 1;
839 } else
840 ++col;
841
842 /* Check the cursor at this position, and dump the previous one if we have
843 * found something new.
844 */
845 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
846 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
847 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +0000848 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000849 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000850 start_line = line;
851 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000852 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000853 if (c == EOF)
854 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000855
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000856 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000857 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000858
Ted Kremenek1c6da172009-11-17 19:37:36 +0000859 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +0000860 clang_disposeTranslationUnit(TU);
861 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000862 return 0;
863}
864
865/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +0000866/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000867/******************************************************************************/
868
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000869/* Parse file:line:column from the input string. Returns 0 on success, non-zero
870 on failure. If successful, the pointer *filename will contain newly-allocated
871 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000872int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000873 unsigned *column, unsigned *second_line,
874 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000875 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000876 const char *last_colon = strrchr(input, ':');
877 unsigned values[4], i;
878 unsigned num_values = (second_line && second_column)? 4 : 2;
879
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000880 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000881 if (!last_colon || last_colon == input) {
882 if (num_values == 4)
883 fprintf(stderr, "could not parse filename:line:column:line:column in "
884 "'%s'\n", input);
885 else
886 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000887 return 1;
888 }
889
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000890 for (i = 0; i != num_values; ++i) {
891 const char *prev_colon;
892
893 /* Parse the next line or column. */
894 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
895 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000896 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000897 (i % 2 ? "column" : "line"), input);
898 return 1;
899 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000900
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000901 if (i + 1 == num_values)
902 break;
903
904 /* Find the previous colon. */
905 prev_colon = last_colon - 1;
906 while (prev_colon != input && *prev_colon != ':')
907 --prev_colon;
908 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000909 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000910 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000911 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000912 }
913
914 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000915 }
916
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000917 *line = values[0];
918 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000919
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000920 if (second_line && second_column) {
921 *second_line = values[2];
922 *second_column = values[3];
923 }
924
Douglas Gregor88d23952009-11-09 18:19:57 +0000925 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000926 *filename = (char*)malloc(last_colon - input + 1);
927 memcpy(*filename, input, last_colon - input);
928 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000929 return 0;
930}
931
932const char *
933clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
934 switch (Kind) {
935 case CXCompletionChunk_Optional: return "Optional";
936 case CXCompletionChunk_TypedText: return "TypedText";
937 case CXCompletionChunk_Text: return "Text";
938 case CXCompletionChunk_Placeholder: return "Placeholder";
939 case CXCompletionChunk_Informative: return "Informative";
940 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
941 case CXCompletionChunk_LeftParen: return "LeftParen";
942 case CXCompletionChunk_RightParen: return "RightParen";
943 case CXCompletionChunk_LeftBracket: return "LeftBracket";
944 case CXCompletionChunk_RightBracket: return "RightBracket";
945 case CXCompletionChunk_LeftBrace: return "LeftBrace";
946 case CXCompletionChunk_RightBrace: return "RightBrace";
947 case CXCompletionChunk_LeftAngle: return "LeftAngle";
948 case CXCompletionChunk_RightAngle: return "RightAngle";
949 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000950 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000951 case CXCompletionChunk_Colon: return "Colon";
952 case CXCompletionChunk_SemiColon: return "SemiColon";
953 case CXCompletionChunk_Equal: return "Equal";
954 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
955 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000956 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000957
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000958 return "Unknown";
959}
960
Douglas Gregor3ac73852009-11-09 16:04:45 +0000961void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000962 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000963
Douglas Gregor3ac73852009-11-09 16:04:45 +0000964 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000965 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000966 CXString text;
967 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000968 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000969 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000970
Douglas Gregor3ac73852009-11-09 16:04:45 +0000971 if (Kind == CXCompletionChunk_Optional) {
972 fprintf(file, "{Optional ");
973 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000974 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000975 file);
976 fprintf(file, "}");
977 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +0000978 }
979
980 if (Kind == CXCompletionChunk_VerticalSpace) {
981 fprintf(file, "{VerticalSpace }");
982 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000983 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000984
Douglas Gregord5a20892009-11-09 17:05:28 +0000985 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000986 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000987 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000988 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000989 cstr ? cstr : "");
990 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000991 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000992
Douglas Gregor3ac73852009-11-09 16:04:45 +0000993}
994
995void print_completion_result(CXCompletionResult *completion_result,
996 CXClientData client_data) {
997 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000998 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
999
1000 fprintf(file, "%s:", clang_getCString(ks));
1001 clang_disposeString(ks);
1002
Douglas Gregor3ac73852009-11-09 16:04:45 +00001003 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001004 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001005 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001006 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1007 case CXAvailability_Available:
1008 break;
1009
1010 case CXAvailability_Deprecated:
1011 fprintf(file, " (deprecated)");
1012 break;
1013
1014 case CXAvailability_NotAvailable:
1015 fprintf(file, " (unavailable)");
1016 break;
1017 }
1018 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001019}
1020
Douglas Gregor3da626b2011-07-07 16:03:39 +00001021void print_completion_contexts(unsigned long long contexts, FILE *file) {
1022 fprintf(file, "Completion contexts:\n");
1023 if (contexts == CXCompletionContext_Unknown) {
1024 fprintf(file, "Unknown\n");
1025 }
1026 if (contexts & CXCompletionContext_AnyType) {
1027 fprintf(file, "Any type\n");
1028 }
1029 if (contexts & CXCompletionContext_AnyValue) {
1030 fprintf(file, "Any value\n");
1031 }
1032 if (contexts & CXCompletionContext_ObjCObjectValue) {
1033 fprintf(file, "Objective-C object value\n");
1034 }
1035 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1036 fprintf(file, "Objective-C selector value\n");
1037 }
1038 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1039 fprintf(file, "C++ class type value\n");
1040 }
1041 if (contexts & CXCompletionContext_DotMemberAccess) {
1042 fprintf(file, "Dot member access\n");
1043 }
1044 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1045 fprintf(file, "Arrow member access\n");
1046 }
1047 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1048 fprintf(file, "Objective-C property access\n");
1049 }
1050 if (contexts & CXCompletionContext_EnumTag) {
1051 fprintf(file, "Enum tag\n");
1052 }
1053 if (contexts & CXCompletionContext_UnionTag) {
1054 fprintf(file, "Union tag\n");
1055 }
1056 if (contexts & CXCompletionContext_StructTag) {
1057 fprintf(file, "Struct tag\n");
1058 }
1059 if (contexts & CXCompletionContext_ClassTag) {
1060 fprintf(file, "Class name\n");
1061 }
1062 if (contexts & CXCompletionContext_Namespace) {
1063 fprintf(file, "Namespace or namespace alias\n");
1064 }
1065 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1066 fprintf(file, "Nested name specifier\n");
1067 }
1068 if (contexts & CXCompletionContext_ObjCInterface) {
1069 fprintf(file, "Objective-C interface\n");
1070 }
1071 if (contexts & CXCompletionContext_ObjCProtocol) {
1072 fprintf(file, "Objective-C protocol\n");
1073 }
1074 if (contexts & CXCompletionContext_ObjCCategory) {
1075 fprintf(file, "Objective-C category\n");
1076 }
1077 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1078 fprintf(file, "Objective-C instance method\n");
1079 }
1080 if (contexts & CXCompletionContext_ObjCClassMessage) {
1081 fprintf(file, "Objective-C class method\n");
1082 }
1083 if (contexts & CXCompletionContext_ObjCSelectorName) {
1084 fprintf(file, "Objective-C selector name\n");
1085 }
1086 if (contexts & CXCompletionContext_MacroName) {
1087 fprintf(file, "Macro name\n");
1088 }
1089 if (contexts & CXCompletionContext_NaturalLanguage) {
1090 fprintf(file, "Natural language\n");
1091 }
1092}
1093
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001094int my_stricmp(const char *s1, const char *s2) {
1095 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001096 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001097 if (c1 < c2)
1098 return -1;
1099 else if (c1 > c2)
1100 return 1;
1101
1102 ++s1;
1103 ++s2;
1104 }
1105
1106 if (*s1)
1107 return 1;
1108 else if (*s2)
1109 return -1;
1110 return 0;
1111}
1112
Douglas Gregor1982c182010-07-12 18:38:41 +00001113int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001114 const char *input = argv[1];
1115 char *filename = 0;
1116 unsigned line;
1117 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001118 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001119 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001120 struct CXUnsavedFile *unsaved_files = 0;
1121 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001122 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001123 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001124 unsigned I, Repeats = 1;
1125 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1126
1127 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1128 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Douglas Gregordf95a132010-08-09 20:45:32 +00001129
Douglas Gregor1982c182010-07-12 18:38:41 +00001130 if (timing_only)
1131 input += strlen("-code-completion-timing=");
1132 else
1133 input += strlen("-code-completion-at=");
1134
Ted Kremeneke68fff62010-02-17 00:41:32 +00001135 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001136 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001137 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001138
Douglas Gregor735df882009-12-02 09:21:34 +00001139 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1140 return -1;
1141
Douglas Gregor32be4a52010-10-11 21:37:58 +00001142 CIdx = clang_createIndex(0, 0);
1143
1144 if (getenv("CINDEXTEST_EDITING"))
1145 Repeats = 5;
1146
1147 TU = clang_parseTranslationUnit(CIdx, 0,
1148 argv + num_unsaved_files + 2,
1149 argc - num_unsaved_files - 2,
1150 0, 0, getDefaultParsingOptions());
1151 if (!TU) {
1152 fprintf(stderr, "Unable to load translation unit!\n");
1153 return 1;
1154 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001155
1156 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1157 fprintf(stderr, "Unable to reparse translation init!\n");
1158 return 1;
1159 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001160
1161 for (I = 0; I != Repeats; ++I) {
1162 results = clang_codeCompleteAt(TU, filename, line, column,
1163 unsaved_files, num_unsaved_files,
1164 completionOptions);
1165 if (!results) {
1166 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001167 return 1;
1168 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001169 if (I != Repeats-1)
1170 clang_disposeCodeCompleteResults(results);
1171 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001172
Douglas Gregorec6762c2009-12-18 16:20:58 +00001173 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001174 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001175 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001176 enum CXCursorKind containerKind;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001177 if (!timing_only) {
1178 /* Sort the code-completion results based on the typed text. */
1179 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1180
Douglas Gregor1982c182010-07-12 18:38:41 +00001181 for (i = 0; i != n; ++i)
1182 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001183 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001184 n = clang_codeCompleteGetNumDiagnostics(results);
1185 for (i = 0; i != n; ++i) {
1186 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1187 PrintDiagnostic(diag);
1188 clang_disposeDiagnostic(diag);
1189 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001190
1191 contexts = clang_codeCompleteGetContexts(results);
1192 print_completion_contexts(contexts, stdout);
1193
Douglas Gregore081a612011-07-21 01:05:26 +00001194 containerKind = clang_codeCompleteGetContainerKind(results, &containerIsIncomplete);
1195
1196 if (containerKind != CXCursor_InvalidCode) {
1197 /* We have found a container */
1198 CXString containerUSR, containerKindSpelling;
1199 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1200 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1201 clang_disposeString(containerKindSpelling);
1202
1203 if (containerIsIncomplete) {
1204 printf("Container is incomplete\n");
1205 }
1206 else {
1207 printf("Container is complete\n");
1208 }
1209
1210 containerUSR = clang_codeCompleteGetContainerUSR(results);
1211 printf("Container USR: %s\n", clang_getCString(containerUSR));
1212 clang_disposeString(containerUSR);
1213 }
1214
Douglas Gregorec6762c2009-12-18 16:20:58 +00001215 clang_disposeCodeCompleteResults(results);
1216 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001217 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001218 clang_disposeIndex(CIdx);
1219 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001220
Douglas Gregor735df882009-12-02 09:21:34 +00001221 free_remapped_files(unsaved_files, num_unsaved_files);
1222
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001223 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001224}
1225
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001226typedef struct {
1227 char *filename;
1228 unsigned line;
1229 unsigned column;
1230} CursorSourceLocation;
1231
1232int inspect_cursor_at(int argc, const char **argv) {
1233 CXIndex CIdx;
1234 int errorCode;
1235 struct CXUnsavedFile *unsaved_files = 0;
1236 int num_unsaved_files = 0;
1237 CXTranslationUnit TU;
1238 CXCursor Cursor;
1239 CursorSourceLocation *Locations = 0;
1240 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001241 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001242 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001243
Ted Kremeneke68fff62010-02-17 00:41:32 +00001244 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001245 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1246 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001247
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001248 /* Parse the locations. */
1249 assert(NumLocations > 0 && "Unable to count locations?");
1250 Locations = (CursorSourceLocation *)malloc(
1251 NumLocations * sizeof(CursorSourceLocation));
1252 for (Loc = 0; Loc < NumLocations; ++Loc) {
1253 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001254 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1255 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001256 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001257 return errorCode;
1258 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001259
1260 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001261 &num_unsaved_files))
1262 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001263
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001264 if (getenv("CINDEXTEST_EDITING"))
1265 Repeats = 5;
1266
1267 /* Parse the translation unit. When we're testing clang_getCursor() after
1268 reparsing, don't remap unsaved files until the second parse. */
1269 CIdx = clang_createIndex(1, 1);
1270 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1271 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001272 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001273 unsaved_files,
1274 Repeats > 1? 0 : num_unsaved_files,
1275 getDefaultParsingOptions());
1276
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001277 if (!TU) {
1278 fprintf(stderr, "unable to parse input\n");
1279 return -1;
1280 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001281
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001282 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001283 if (Repeats > 1 &&
1284 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1285 clang_defaultReparseOptions(TU))) {
1286 clang_disposeTranslationUnit(TU);
1287 return 1;
1288 }
1289
1290 for (Loc = 0; Loc < NumLocations; ++Loc) {
1291 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1292 if (!file)
1293 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001294
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001295 Cursor = clang_getCursor(TU,
1296 clang_getLocation(TU, file, Locations[Loc].line,
1297 Locations[Loc].column));
1298 if (I + 1 == Repeats) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001299 PrintCursor(TU, Cursor);
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001300 printf("\n");
1301 free(Locations[Loc].filename);
1302 }
1303 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001304 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001305
Douglas Gregora88084b2010-02-18 18:08:43 +00001306 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001307 clang_disposeTranslationUnit(TU);
1308 clang_disposeIndex(CIdx);
1309 free(Locations);
1310 free_remapped_files(unsaved_files, num_unsaved_files);
1311 return 0;
1312}
1313
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001314int perform_token_annotation(int argc, const char **argv) {
1315 const char *input = argv[1];
1316 char *filename = 0;
1317 unsigned line, second_line;
1318 unsigned column, second_column;
1319 CXIndex CIdx;
1320 CXTranslationUnit TU = 0;
1321 int errorCode;
1322 struct CXUnsavedFile *unsaved_files = 0;
1323 int num_unsaved_files = 0;
1324 CXToken *tokens;
1325 unsigned num_tokens;
1326 CXSourceRange range;
1327 CXSourceLocation startLoc, endLoc;
1328 CXFile file = 0;
1329 CXCursor *cursors = 0;
1330 unsigned i;
1331
1332 input += strlen("-test-annotate-tokens=");
1333 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1334 &second_line, &second_column)))
1335 return errorCode;
1336
1337 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1338 return -1;
1339
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001340 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00001341 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1342 argv + num_unsaved_files + 2,
1343 argc - num_unsaved_files - 3,
1344 unsaved_files,
1345 num_unsaved_files,
1346 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001347 if (!TU) {
1348 fprintf(stderr, "unable to parse input\n");
1349 clang_disposeIndex(CIdx);
1350 free(filename);
1351 free_remapped_files(unsaved_files, num_unsaved_files);
1352 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001353 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001354 errorCode = 0;
1355
1356 file = clang_getFile(TU, filename);
1357 if (!file) {
1358 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1359 errorCode = -1;
1360 goto teardown;
1361 }
1362
1363 startLoc = clang_getLocation(TU, file, line, column);
1364 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001365 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001366 column);
1367 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001369 }
1370
1371 endLoc = clang_getLocation(TU, file, second_line, second_column);
1372 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001373 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001374 second_line, second_column);
1375 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001376 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001377 }
1378
1379 range = clang_getRange(startLoc, endLoc);
1380 clang_tokenize(TU, range, &tokens, &num_tokens);
1381 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1382 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1383 for (i = 0; i != num_tokens; ++i) {
1384 const char *kind = "<unknown>";
1385 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1386 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1387 unsigned start_line, start_column, end_line, end_column;
1388
1389 switch (clang_getTokenKind(tokens[i])) {
1390 case CXToken_Punctuation: kind = "Punctuation"; break;
1391 case CXToken_Keyword: kind = "Keyword"; break;
1392 case CXToken_Identifier: kind = "Identifier"; break;
1393 case CXToken_Literal: kind = "Literal"; break;
1394 case CXToken_Comment: kind = "Comment"; break;
1395 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00001396 clang_getSpellingLocation(clang_getRangeStart(extent),
1397 0, &start_line, &start_column, 0);
1398 clang_getSpellingLocation(clang_getRangeEnd(extent),
1399 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001400 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1401 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001402 if (!clang_isInvalid(cursors[i].kind)) {
1403 printf(" ");
Douglas Gregordd3e5542011-05-04 00:14:37 +00001404 PrintCursor(TU, cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001405 }
1406 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001407 }
1408 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00001409 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001410
1411 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001412 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001413 clang_disposeTranslationUnit(TU);
1414 clang_disposeIndex(CIdx);
1415 free(filename);
1416 free_remapped_files(unsaved_files, num_unsaved_files);
1417 return errorCode;
1418}
1419
Ted Kremenek0d435192009-11-17 18:13:31 +00001420/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001421/* USR printing. */
1422/******************************************************************************/
1423
1424static int insufficient_usr(const char *kind, const char *usage) {
1425 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1426 return 1;
1427}
1428
1429static unsigned isUSR(const char *s) {
1430 return s[0] == 'c' && s[1] == ':';
1431}
1432
1433static int not_usr(const char *s, const char *arg) {
1434 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1435 return 1;
1436}
1437
1438static void print_usr(CXString usr) {
1439 const char *s = clang_getCString(usr);
1440 printf("%s\n", s);
1441 clang_disposeString(usr);
1442}
1443
1444static void display_usrs() {
1445 fprintf(stderr, "-print-usrs options:\n"
1446 " ObjCCategory <class name> <category name>\n"
1447 " ObjCClass <class name>\n"
1448 " ObjCIvar <ivar name> <class USR>\n"
1449 " ObjCMethod <selector> [0=class method|1=instance method] "
1450 "<class USR>\n"
1451 " ObjCProperty <property name> <class USR>\n"
1452 " ObjCProtocol <protocol name>\n");
1453}
1454
1455int print_usrs(const char **I, const char **E) {
1456 while (I != E) {
1457 const char *kind = *I;
1458 unsigned len = strlen(kind);
1459 switch (len) {
1460 case 8:
1461 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1462 if (I + 2 >= E)
1463 return insufficient_usr(kind, "<ivar name> <class USR>");
1464 if (!isUSR(I[2]))
1465 return not_usr("<class USR>", I[2]);
1466 else {
1467 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001468 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001469 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001470 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1471 }
1472
1473 I += 3;
1474 continue;
1475 }
1476 break;
1477 case 9:
1478 if (memcmp(kind, "ObjCClass", 9) == 0) {
1479 if (I + 1 >= E)
1480 return insufficient_usr(kind, "<class name>");
1481 print_usr(clang_constructUSR_ObjCClass(I[1]));
1482 I += 2;
1483 continue;
1484 }
1485 break;
1486 case 10:
1487 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1488 if (I + 3 >= E)
1489 return insufficient_usr(kind, "<method selector> "
1490 "[0=class method|1=instance method] <class USR>");
1491 if (!isUSR(I[3]))
1492 return not_usr("<class USR>", I[3]);
1493 else {
1494 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001495 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00001496 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001497 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1498 }
1499 I += 4;
1500 continue;
1501 }
1502 break;
1503 case 12:
1504 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1505 if (I + 2 >= E)
1506 return insufficient_usr(kind, "<class name> <category name>");
1507 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1508 I += 3;
1509 continue;
1510 }
1511 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1512 if (I + 1 >= E)
1513 return insufficient_usr(kind, "<protocol name>");
1514 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1515 I += 2;
1516 continue;
1517 }
1518 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1519 if (I + 2 >= E)
1520 return insufficient_usr(kind, "<property name> <class USR>");
1521 if (!isUSR(I[2]))
1522 return not_usr("<class USR>", I[2]);
1523 else {
1524 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00001525 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00001526 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001527 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1528 }
1529 I += 3;
1530 continue;
1531 }
1532 break;
1533 default:
1534 break;
1535 }
1536 break;
1537 }
1538
1539 if (I != E) {
1540 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1541 display_usrs();
1542 return 1;
1543 }
1544 return 0;
1545}
1546
1547int print_usrs_file(const char *file_name) {
1548 char line[2048];
1549 const char *args[128];
1550 unsigned numChars = 0;
1551
1552 FILE *fp = fopen(file_name, "r");
1553 if (!fp) {
1554 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1555 return 1;
1556 }
1557
1558 /* This code is not really all that safe, but it works fine for testing. */
1559 while (!feof(fp)) {
1560 char c = fgetc(fp);
1561 if (c == '\n') {
1562 unsigned i = 0;
1563 const char *s = 0;
1564
1565 if (numChars == 0)
1566 continue;
1567
1568 line[numChars] = '\0';
1569 numChars = 0;
1570
1571 if (line[0] == '/' && line[1] == '/')
1572 continue;
1573
1574 s = strtok(line, " ");
1575 while (s) {
1576 args[i] = s;
1577 ++i;
1578 s = strtok(0, " ");
1579 }
1580 if (print_usrs(&args[0], &args[i]))
1581 return 1;
1582 }
1583 else
1584 line[numChars++] = c;
1585 }
1586
1587 fclose(fp);
1588 return 0;
1589}
1590
1591/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001592/* Command line processing. */
1593/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001594int write_pch_file(const char *filename, int argc, const char *argv[]) {
1595 CXIndex Idx;
1596 CXTranslationUnit TU;
1597 struct CXUnsavedFile *unsaved_files = 0;
1598 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00001599 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001600
1601 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1602
1603 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1604 clang_disposeIndex(Idx);
1605 return -1;
1606 }
1607
1608 TU = clang_parseTranslationUnit(Idx, 0,
1609 argv + num_unsaved_files,
1610 argc - num_unsaved_files,
1611 unsaved_files,
1612 num_unsaved_files,
1613 CXTranslationUnit_Incomplete);
1614 if (!TU) {
1615 fprintf(stderr, "Unable to load translation unit!\n");
1616 free_remapped_files(unsaved_files, num_unsaved_files);
1617 clang_disposeIndex(Idx);
1618 return 1;
1619 }
1620
Douglas Gregor39c411f2011-07-06 16:43:36 +00001621 switch (clang_saveTranslationUnit(TU, filename,
1622 clang_defaultSaveOptions(TU))) {
1623 case CXSaveError_None:
1624 break;
1625
1626 case CXSaveError_TranslationErrors:
1627 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
1628 filename);
1629 result = 2;
1630 break;
1631
1632 case CXSaveError_InvalidTU:
1633 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
1634 filename);
1635 result = 3;
1636 break;
1637
1638 case CXSaveError_Unknown:
1639 default:
1640 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
1641 result = 1;
1642 break;
1643 }
1644
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001645 clang_disposeTranslationUnit(TU);
1646 free_remapped_files(unsaved_files, num_unsaved_files);
1647 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00001648 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001649}
1650
1651/******************************************************************************/
1652/* Command line processing. */
1653/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001654
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001655static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001656 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001657 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001658 if (strcmp(s, "-usrs") == 0)
1659 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001660 if (strncmp(s, "-memory-usage", 13) == 0)
1661 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001662 return NULL;
1663}
1664
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001665static void print_usage(void) {
1666 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001667 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001668 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001669 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001670 " c-index-test -test-file-scan <AST file> <source file> "
1671 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001672 " c-index-test -test-load-tu <AST file> <symbol filter> "
1673 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001674 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1675 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001676 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001677 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001678 " c-index-test -test-load-source-memory-usage "
1679 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00001680 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1681 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001682 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001683 " c-index-test -test-load-source-usrs-memory-usage "
1684 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001685 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1686 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001687 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00001688 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00001689 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001690 " c-index-test -test-print-typekind {<args>}*\n"
1691 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001692 " c-index-test -print-usr-file <file>\n"
1693 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001694 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001695 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001696 " all - load all symbols, including those from PCH\n"
1697 " local - load all symbols except those in PCH\n"
1698 " category - only load ObjC categories (non-PCH)\n"
1699 " interface - only load ObjC interfaces (non-PCH)\n"
1700 " protocol - only load ObjC protocols (non-PCH)\n"
1701 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001702 " typedef - only load typdefs (non-PCH)\n"
1703 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001704}
1705
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001706/***/
1707
1708int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001709 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001710 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001711 return perform_code_completion(argc, argv, 0);
1712 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1713 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001714 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1715 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001716 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001717 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001718 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001719 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1720 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001721 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001722 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1723 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1724 if (I) {
1725 int trials = atoi(argv[2]);
1726 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1727 NULL);
1728 }
1729 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001730 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001731 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001732
1733 PostVisitTU postVisit = 0;
1734 if (strstr(argv[1], "-memory-usage"))
1735 postVisit = PrintMemoryUsage;
1736
Ted Kremenek7d405622010-01-12 23:34:26 +00001737 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00001738 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
1739 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00001740 }
1741 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001742 return perform_file_scan(argv[2], argv[3],
1743 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001744 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1745 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001746 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1747 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1748 PrintInclusionStack);
1749 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1750 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1751 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001752 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1753 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1754 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001755 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1756 return perform_test_load_source(argc - 2, argv + 2, "all",
1757 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001758 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1759 if (argc > 2)
1760 return print_usrs(argv + 2, argv + argc);
1761 else {
1762 display_usrs();
1763 return 1;
1764 }
1765 }
1766 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1767 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001768 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1769 return write_pch_file(argv[2], argc - 3, argv + 3);
1770
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001771 print_usage();
1772 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001773}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001774
1775/***/
1776
1777/* We intentionally run in a separate thread to ensure we at least minimal
1778 * testing of a multithreaded environment (for example, having a reduced stack
1779 * size). */
1780
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001781typedef struct thread_info {
1782 int argc;
1783 const char **argv;
1784 int result;
1785} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00001786void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001787 thread_info *client_data = client_data_v;
1788 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001789}
1790
1791int main(int argc, const char **argv) {
1792 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001793
Douglas Gregor61605982010-10-27 16:00:01 +00001794 if (getenv("CINDEXTEST_NOTHREADS"))
1795 return cindextest_main(argc, argv);
1796
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001797 client_data.argc = argc;
1798 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00001799 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001800 return client_data.result;
1801}