blob: ca4b1e3169cd81c36b4a98ac5afabb2c709cae05 [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. */
112 to_file = fopen(semi + 1, "r");
113 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 Gregor1f60d9e2010-09-13 22:52:57 +0000195 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
196 printf("%d:%d", line, column);
197 }
198 printf("]");
199 } else {
200 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
201 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
202 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);
254 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
255 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]);
266 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
267 if (I)
268 printf(", ");
269 printf("@%d:%d", line, column);
270 }
271 printf("]");
272 clang_disposeOverriddenCursors(overridden);
273 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000274 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000275}
Steve Naroff89922f82009-08-31 00:59:03 +0000276
Ted Kremeneke68fff62010-02-17 00:41:32 +0000277static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000278 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000279 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000280 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000281 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000282 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000283 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000284 clang_disposeString(source);
285 return "<invalid loc>";
286 }
287 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000288 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000289 clang_disposeString(source);
290 return b;
291 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000292}
293
Ted Kremenek0d435192009-11-17 18:13:31 +0000294/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000295/* Callbacks. */
296/******************************************************************************/
297
298typedef void (*PostVisitTU)(CXTranslationUnit);
299
Douglas Gregora88084b2010-02-18 18:08:43 +0000300void PrintDiagnostic(CXDiagnostic Diagnostic) {
301 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000302 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000303 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000304 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
305 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
306 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000307
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000308 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000309 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000310
Douglas Gregor274f1902010-02-22 23:17:23 +0000311 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
312 fprintf(stderr, "%s\n", clang_getCString(Msg));
313 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000314
Douglas Gregor5352ac02010-01-28 00:27:43 +0000315 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000316 &file, 0, 0, 0);
317 if (!file)
318 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000319
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000320 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
321 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000322 CXSourceRange range;
323 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
324 CXSourceLocation start = clang_getRangeStart(range);
325 CXSourceLocation end = clang_getRangeEnd(range);
326 unsigned start_line, start_column, end_line, end_column;
327 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000328 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000329 &start_column, 0);
330 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
331 if (clang_equalLocations(start, end)) {
332 /* Insertion. */
333 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000334 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000335 clang_getCString(insertion_text), start_line, start_column);
336 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
337 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000338 if (start_file == file && end_file == file) {
339 fprintf(out, "FIX-IT: Remove ");
340 PrintExtent(out, start_line, start_column, end_line, end_column);
341 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000342 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000343 } else {
344 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000345 if (start_file == end_file) {
346 fprintf(out, "FIX-IT: Replace ");
347 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000348 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000349 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000350 break;
351 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000352 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000353 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000354}
355
Douglas Gregora88084b2010-02-18 18:08:43 +0000356void PrintDiagnostics(CXTranslationUnit TU) {
357 int i, n = clang_getNumDiagnostics(TU);
358 for (i = 0; i != n; ++i) {
359 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
360 PrintDiagnostic(Diag);
361 clang_disposeDiagnostic(Diag);
362 }
363}
364
Ted Kremenekce2ae882010-01-26 17:59:48 +0000365/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000366/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000367/******************************************************************************/
368
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000369static const char *FileCheckPrefix = "CHECK";
370
Douglas Gregora7bde202010-01-19 00:34:46 +0000371static void PrintCursorExtent(CXCursor C) {
372 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000373 CXFile begin_file, end_file;
374 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000375
Douglas Gregor1db19de2010-01-19 21:36:55 +0000376 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000377 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000378 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000379 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000380 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000381 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000382
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000383 printf(" Extent=");
384 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000385}
386
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000387/* Data used by all of the visitors. */
388typedef struct {
389 CXTranslationUnit TU;
390 enum CXCursorKind *Filter;
391} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000392
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000393
Ted Kremeneke68fff62010-02-17 00:41:32 +0000394enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000395 CXCursor Parent,
396 CXClientData ClientData) {
397 VisitorData *Data = (VisitorData *)ClientData;
398 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000399 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000400 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000401 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000402 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000403 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000404 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000405 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000406 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000407 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000408 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000409
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000410 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000411}
Steve Naroff50398192009-08-28 15:28:48 +0000412
Ted Kremeneke68fff62010-02-17 00:41:32 +0000413static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000414 CXCursor Parent,
415 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000416 const char *startBuf, *endBuf;
417 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
418 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000419 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000420
Douglas Gregorb6998662010-01-19 19:34:47 +0000421 if (Cursor.kind != CXCursor_FunctionDecl ||
422 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000423 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000424
425 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
426 &startLine, &startColumn,
427 &endLine, &endColumn);
428 /* Probe the entire body, looking for both decls and refs. */
429 curLine = startLine;
430 curColumn = startColumn;
431
432 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000433 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000434 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000435 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000436
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000437 if (*startBuf == '\n') {
438 startBuf++;
439 curLine++;
440 curColumn = 1;
441 } else if (*startBuf != '\t')
442 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000443
Douglas Gregor98258af2010-01-18 22:46:11 +0000444 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000445 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000446
Douglas Gregor1db19de2010-01-19 21:36:55 +0000447 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000448 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000449 CXSourceLocation RefLoc
450 = clang_getLocation(Data->TU, file, curLine, curColumn);
451 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000452 if (Ref.kind == CXCursor_NoDeclFound) {
453 /* Nothing found here; that's fine. */
454 } else if (Ref.kind != CXCursor_FunctionDecl) {
455 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
456 curLine, curColumn);
457 PrintCursor(Ref);
458 printf("\n");
459 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000460 }
Ted Kremenek74844072010-02-17 00:41:20 +0000461 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000462 startBuf++;
463 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000464
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000465 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000466}
467
Ted Kremenek7d405622010-01-12 23:34:26 +0000468/******************************************************************************/
469/* USR testing. */
470/******************************************************************************/
471
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000472enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
473 CXClientData ClientData) {
474 VisitorData *Data = (VisitorData *)ClientData;
475 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000476 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000477 const char *cstr = clang_getCString(USR);
478 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000479 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000480 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000481 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000482 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
483
Douglas Gregora7bde202010-01-19 00:34:46 +0000484 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000485 printf("\n");
486 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000487
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000488 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000489 }
490
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000491 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000492}
493
494/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000495/* Inclusion stack testing. */
496/******************************************************************************/
497
498void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
499 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000500
Ted Kremenek16b55a72010-01-26 19:31:51 +0000501 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000502 CXString fname;
503
504 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000505 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000506 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000507
Ted Kremenek16b55a72010-01-26 19:31:51 +0000508 for (i = 0; i < includeStackLen; ++i) {
509 CXFile includingFile;
510 unsigned line, column;
511 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
512 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000513 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000514 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000515 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000516 }
517 printf("\n");
518}
519
520void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000521 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000522}
523
524/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000525/* Linkage testing. */
526/******************************************************************************/
527
528static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
529 CXClientData d) {
530 const char *linkage = 0;
531
532 if (clang_isInvalid(clang_getCursorKind(cursor)))
533 return CXChildVisit_Recurse;
534
535 switch (clang_getCursorLinkage(cursor)) {
536 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000537 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
538 case CXLinkage_Internal: linkage = "Internal"; break;
539 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
540 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000541 }
542
543 if (linkage) {
544 PrintCursor(cursor);
545 printf("linkage=%s\n", linkage);
546 }
547
548 return CXChildVisit_Recurse;
549}
550
551/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000552/* Typekind testing. */
553/******************************************************************************/
554
555static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
556 CXClientData d) {
557
558 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
559 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000560 CXString S = clang_getTypeKindSpelling(T.kind);
561 PrintCursor(cursor);
562 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000563 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000564 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000565 {
566 CXType CT = clang_getCanonicalType(T);
567 if (!clang_equalTypes(T, CT)) {
568 CXString CS = clang_getTypeKindSpelling(CT.kind);
569 printf(" [canonical=%s]", clang_getCString(CS));
570 clang_disposeString(CS);
571 }
572 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000573 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000574 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000575 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000576 if (RT.kind != CXType_Invalid) {
577 CXString RS = clang_getTypeKindSpelling(RT.kind);
578 printf(" [result=%s]", clang_getCString(RS));
579 clang_disposeString(RS);
580 }
581 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000582 /* Print if this is a non-POD type. */
583 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000584
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000585 printf("\n");
586 }
587 return CXChildVisit_Recurse;
588}
589
590
591/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000592/* Loading ASTs/source. */
593/******************************************************************************/
594
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000595static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000596 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000597 CXCursorVisitor Visitor,
598 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000599
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000600 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000601 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000602
603 if (Visitor) {
604 enum CXCursorKind K = CXCursor_NotImplemented;
605 enum CXCursorKind *ck = &K;
606 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000607
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000608 /* Perform some simple filtering. */
609 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +0000610 else if (!strcmp(filter, "all-display") ||
611 !strcmp(filter, "local-display")) {
612 ck = NULL;
613 want_display_name = 1;
614 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000615 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000616 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
617 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
618 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
619 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
620 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
621 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
622 else {
623 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
624 return 1;
625 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000626
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000627 Data.TU = TU;
628 Data.Filter = ck;
629 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000630 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000631
Ted Kremenekce2ae882010-01-26 17:59:48 +0000632 if (PV)
633 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000634
Douglas Gregora88084b2010-02-18 18:08:43 +0000635 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000636 clang_disposeTranslationUnit(TU);
637 return 0;
638}
639
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000640int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000641 const char *prefix, CXCursorVisitor Visitor,
642 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000643 CXIndex Idx;
644 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000645 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000646 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000647 !strcmp(filter, "local") ? 1 : 0,
648 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000649
Ted Kremenek020a0952010-02-11 07:41:25 +0000650 if (!CreateTranslationUnit(Idx, file, &TU)) {
651 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000652 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000653 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000654
Ted Kremenek020a0952010-02-11 07:41:25 +0000655 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
656 clang_disposeIndex(Idx);
657 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000658}
659
Ted Kremenekce2ae882010-01-26 17:59:48 +0000660int perform_test_load_source(int argc, const char **argv,
661 const char *filter, CXCursorVisitor Visitor,
662 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000663 const char *UseExternalASTs =
664 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000665 CXIndex Idx;
666 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000667 struct CXUnsavedFile *unsaved_files = 0;
668 int num_unsaved_files = 0;
669 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000670
Daniel Dunbarada487d2009-12-01 02:03:10 +0000671 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +0000672 (!strcmp(filter, "local") ||
673 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000674 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000675
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000676 if (UseExternalASTs && strlen(UseExternalASTs))
677 clang_setUseExternalASTGeneration(Idx, 1);
678
Ted Kremenek020a0952010-02-11 07:41:25 +0000679 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
680 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000681 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000682 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000683
Ted Kremeneke68fff62010-02-17 00:41:32 +0000684 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
685 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000686 argv + num_unsaved_files,
687 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000688 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000689 if (!TU) {
690 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000691 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000692 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000693 return 1;
694 }
695
Ted Kremenekce2ae882010-01-26 17:59:48 +0000696 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000697 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000698 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000699 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000700}
701
Douglas Gregorabc563f2010-07-19 21:46:24 +0000702int perform_test_reparse_source(int argc, const char **argv, int trials,
703 const char *filter, CXCursorVisitor Visitor,
704 PostVisitTU PV) {
705 const char *UseExternalASTs =
706 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
707 CXIndex Idx;
708 CXTranslationUnit TU;
709 struct CXUnsavedFile *unsaved_files = 0;
710 int num_unsaved_files = 0;
711 int result;
712 int trial;
713
714 Idx = clang_createIndex(/* excludeDeclsFromPCH */
715 !strcmp(filter, "local") ? 1 : 0,
716 /* displayDiagnosics=*/1);
717
718 if (UseExternalASTs && strlen(UseExternalASTs))
719 clang_setUseExternalASTGeneration(Idx, 1);
720
721 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
722 clang_disposeIndex(Idx);
723 return -1;
724 }
725
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000726 /* Load the initial translation unit -- we do this without honoring remapped
727 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000728 TU = clang_parseTranslationUnit(Idx, 0,
729 argv + num_unsaved_files,
730 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000731 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000732 if (!TU) {
733 fprintf(stderr, "Unable to load translation unit!\n");
734 free_remapped_files(unsaved_files, num_unsaved_files);
735 clang_disposeIndex(Idx);
736 return 1;
737 }
738
739 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000740 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
741 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000742 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000743 clang_disposeTranslationUnit(TU);
744 free_remapped_files(unsaved_files, num_unsaved_files);
745 clang_disposeIndex(Idx);
746 return -1;
747 }
748 }
749
750 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
751 free_remapped_files(unsaved_files, num_unsaved_files);
752 clang_disposeIndex(Idx);
753 return result;
754}
755
Ted Kremenek0d435192009-11-17 18:13:31 +0000756/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000757/* Logic for testing clang_getCursor(). */
758/******************************************************************************/
759
760static void print_cursor_file_scan(CXCursor cursor,
761 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000762 unsigned end_line, unsigned end_col,
763 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000764 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000765 if (prefix)
766 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000767 PrintExtent(stdout, start_line, start_col, end_line, end_col);
768 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000769 PrintCursor(cursor);
770 printf("\n");
771}
772
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000773static int perform_file_scan(const char *ast_file, const char *source_file,
774 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000775 CXIndex Idx;
776 CXTranslationUnit TU;
777 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000778 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000779 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000780 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000781 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000782
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000783 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
784 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000785 fprintf(stderr, "Could not create Index\n");
786 return 1;
787 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000788
Ted Kremenek1c6da172009-11-17 19:37:36 +0000789 if (!CreateTranslationUnit(Idx, ast_file, &TU))
790 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000791
Ted Kremenek1c6da172009-11-17 19:37:36 +0000792 if ((fp = fopen(source_file, "r")) == NULL) {
793 fprintf(stderr, "Could not open '%s'\n", source_file);
794 return 1;
795 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000796
Douglas Gregorb9790342010-01-22 21:44:22 +0000797 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000798 for (;;) {
799 CXCursor cursor;
800 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000801
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000802 if (c == '\n') {
803 ++line;
804 col = 1;
805 } else
806 ++col;
807
808 /* Check the cursor at this position, and dump the previous one if we have
809 * found something new.
810 */
811 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
812 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
813 prevCursor.kind != CXCursor_InvalidFile) {
814 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000815 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000816 start_line = line;
817 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000818 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000819 if (c == EOF)
820 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000821
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000822 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000823 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000824
Ted Kremenek1c6da172009-11-17 19:37:36 +0000825 fclose(fp);
826 return 0;
827}
828
829/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000830/* Logic for testing clang_codeComplete(). */
831/******************************************************************************/
832
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000833/* Parse file:line:column from the input string. Returns 0 on success, non-zero
834 on failure. If successful, the pointer *filename will contain newly-allocated
835 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000836int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000837 unsigned *column, unsigned *second_line,
838 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000839 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000840 const char *last_colon = strrchr(input, ':');
841 unsigned values[4], i;
842 unsigned num_values = (second_line && second_column)? 4 : 2;
843
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000844 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000845 if (!last_colon || last_colon == input) {
846 if (num_values == 4)
847 fprintf(stderr, "could not parse filename:line:column:line:column in "
848 "'%s'\n", input);
849 else
850 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000851 return 1;
852 }
853
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000854 for (i = 0; i != num_values; ++i) {
855 const char *prev_colon;
856
857 /* Parse the next line or column. */
858 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
859 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000860 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000861 (i % 2 ? "column" : "line"), input);
862 return 1;
863 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000864
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000865 if (i + 1 == num_values)
866 break;
867
868 /* Find the previous colon. */
869 prev_colon = last_colon - 1;
870 while (prev_colon != input && *prev_colon != ':')
871 --prev_colon;
872 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000873 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000874 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000876 }
877
878 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000879 }
880
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000881 *line = values[0];
882 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000884 if (second_line && second_column) {
885 *second_line = values[2];
886 *second_column = values[3];
887 }
888
Douglas Gregor88d23952009-11-09 18:19:57 +0000889 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000890 *filename = (char*)malloc(last_colon - input + 1);
891 memcpy(*filename, input, last_colon - input);
892 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000893 return 0;
894}
895
896const char *
897clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
898 switch (Kind) {
899 case CXCompletionChunk_Optional: return "Optional";
900 case CXCompletionChunk_TypedText: return "TypedText";
901 case CXCompletionChunk_Text: return "Text";
902 case CXCompletionChunk_Placeholder: return "Placeholder";
903 case CXCompletionChunk_Informative: return "Informative";
904 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
905 case CXCompletionChunk_LeftParen: return "LeftParen";
906 case CXCompletionChunk_RightParen: return "RightParen";
907 case CXCompletionChunk_LeftBracket: return "LeftBracket";
908 case CXCompletionChunk_RightBracket: return "RightBracket";
909 case CXCompletionChunk_LeftBrace: return "LeftBrace";
910 case CXCompletionChunk_RightBrace: return "RightBrace";
911 case CXCompletionChunk_LeftAngle: return "LeftAngle";
912 case CXCompletionChunk_RightAngle: return "RightAngle";
913 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000914 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000915 case CXCompletionChunk_Colon: return "Colon";
916 case CXCompletionChunk_SemiColon: return "SemiColon";
917 case CXCompletionChunk_Equal: return "Equal";
918 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
919 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000920 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000921
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000922 return "Unknown";
923}
924
Douglas Gregor3ac73852009-11-09 16:04:45 +0000925void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000926 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000927
Douglas Gregor3ac73852009-11-09 16:04:45 +0000928 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000929 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000930 CXString text;
931 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000932 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000933 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000934
Douglas Gregor3ac73852009-11-09 16:04:45 +0000935 if (Kind == CXCompletionChunk_Optional) {
936 fprintf(file, "{Optional ");
937 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000938 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000939 file);
940 fprintf(file, "}");
941 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +0000942 }
943
944 if (Kind == CXCompletionChunk_VerticalSpace) {
945 fprintf(file, "{VerticalSpace }");
946 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000947 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000948
Douglas Gregord5a20892009-11-09 17:05:28 +0000949 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000950 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000951 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000952 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000953 cstr ? cstr : "");
954 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000955 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000956
Douglas Gregor3ac73852009-11-09 16:04:45 +0000957}
958
959void print_completion_result(CXCompletionResult *completion_result,
960 CXClientData client_data) {
961 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
963
964 fprintf(file, "%s:", clang_getCString(ks));
965 clang_disposeString(ks);
966
Douglas Gregor3ac73852009-11-09 16:04:45 +0000967 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000968 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000969 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000970 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
971 case CXAvailability_Available:
972 break;
973
974 case CXAvailability_Deprecated:
975 fprintf(file, " (deprecated)");
976 break;
977
978 case CXAvailability_NotAvailable:
979 fprintf(file, " (unavailable)");
980 break;
981 }
982 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000983}
984
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000985int my_stricmp(const char *s1, const char *s2) {
986 while (*s1 && *s2) {
987 int c1 = tolower(*s1), c2 = tolower(*s2);
988 if (c1 < c2)
989 return -1;
990 else if (c1 > c2)
991 return 1;
992
993 ++s1;
994 ++s2;
995 }
996
997 if (*s1)
998 return 1;
999 else if (*s2)
1000 return -1;
1001 return 0;
1002}
1003
Douglas Gregor1982c182010-07-12 18:38:41 +00001004int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001005 const char *input = argv[1];
1006 char *filename = 0;
1007 unsigned line;
1008 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001009 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001010 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001011 struct CXUnsavedFile *unsaved_files = 0;
1012 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001013 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001014 CXTranslationUnit TU = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +00001015
Douglas Gregor1982c182010-07-12 18:38:41 +00001016 if (timing_only)
1017 input += strlen("-code-completion-timing=");
1018 else
1019 input += strlen("-code-completion-at=");
1020
Ted Kremeneke68fff62010-02-17 00:41:32 +00001021 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001022 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001023 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001024
Douglas Gregor735df882009-12-02 09:21:34 +00001025 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1026 return -1;
1027
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001028 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00001029 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +00001030 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +00001031 TU = clang_parseTranslationUnit(CIdx, 0,
1032 argv + num_unsaved_files + 2,
1033 argc - num_unsaved_files - 2,
Daniel Dunbar43f331d2010-08-19 23:44:04 +00001034 0, 0, getDefaultParsingOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001035 if (!TU) {
1036 fprintf(stderr, "Unable to load translation unit!\n");
1037 return 1;
1038 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001039 for (I = 0; I != Repeats; ++I) {
1040 results = clang_codeCompleteAt(TU, filename, line, column,
1041 unsaved_files, num_unsaved_files,
1042 clang_defaultCodeCompleteOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001043 if (!results) {
1044 fprintf(stderr, "Unable to perform code completion!\n");
1045 return 1;
1046 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001047 if (I != Repeats-1)
1048 clang_disposeCodeCompleteResults(results);
1049 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00001050 } else
1051 results = clang_codeComplete(CIdx,
1052 argv[argc - 1], argc - num_unsaved_files - 3,
1053 argv + num_unsaved_files + 2,
1054 num_unsaved_files, unsaved_files,
1055 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001056
Douglas Gregorec6762c2009-12-18 16:20:58 +00001057 if (results) {
1058 unsigned i, n = results->NumResults;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001059 if (!timing_only) {
1060 /* Sort the code-completion results based on the typed text. */
1061 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1062
Douglas Gregor1982c182010-07-12 18:38:41 +00001063 for (i = 0; i != n; ++i)
1064 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001065 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001066 n = clang_codeCompleteGetNumDiagnostics(results);
1067 for (i = 0; i != n; ++i) {
1068 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1069 PrintDiagnostic(diag);
1070 clang_disposeDiagnostic(diag);
1071 }
Douglas Gregorec6762c2009-12-18 16:20:58 +00001072 clang_disposeCodeCompleteResults(results);
1073 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001074 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001075 clang_disposeIndex(CIdx);
1076 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001077
Douglas Gregor735df882009-12-02 09:21:34 +00001078 free_remapped_files(unsaved_files, num_unsaved_files);
1079
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001080 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001081}
1082
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001083typedef struct {
1084 char *filename;
1085 unsigned line;
1086 unsigned column;
1087} CursorSourceLocation;
1088
1089int inspect_cursor_at(int argc, const char **argv) {
1090 CXIndex CIdx;
1091 int errorCode;
1092 struct CXUnsavedFile *unsaved_files = 0;
1093 int num_unsaved_files = 0;
1094 CXTranslationUnit TU;
1095 CXCursor Cursor;
1096 CursorSourceLocation *Locations = 0;
1097 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001098
Ted Kremeneke68fff62010-02-17 00:41:32 +00001099 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001100 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1101 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001102
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001103 /* Parse the locations. */
1104 assert(NumLocations > 0 && "Unable to count locations?");
1105 Locations = (CursorSourceLocation *)malloc(
1106 NumLocations * sizeof(CursorSourceLocation));
1107 for (Loc = 0; Loc < NumLocations; ++Loc) {
1108 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001109 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1110 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001111 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001112 return errorCode;
1113 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001114
1115 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001116 &num_unsaved_files))
1117 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001118
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001119 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001120 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1121 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001122 argv + num_unsaved_files + 1 + NumLocations,
1123 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001124 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001125 if (!TU) {
1126 fprintf(stderr, "unable to parse input\n");
1127 return -1;
1128 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001129
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001130 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001131 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1132 if (!file)
1133 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001134
1135 Cursor = clang_getCursor(TU,
1136 clang_getLocation(TU, file, Locations[Loc].line,
1137 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001138 PrintCursor(Cursor);
1139 printf("\n");
1140 free(Locations[Loc].filename);
1141 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001142
Douglas Gregora88084b2010-02-18 18:08:43 +00001143 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001144 clang_disposeTranslationUnit(TU);
1145 clang_disposeIndex(CIdx);
1146 free(Locations);
1147 free_remapped_files(unsaved_files, num_unsaved_files);
1148 return 0;
1149}
1150
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001151int perform_token_annotation(int argc, const char **argv) {
1152 const char *input = argv[1];
1153 char *filename = 0;
1154 unsigned line, second_line;
1155 unsigned column, second_column;
1156 CXIndex CIdx;
1157 CXTranslationUnit TU = 0;
1158 int errorCode;
1159 struct CXUnsavedFile *unsaved_files = 0;
1160 int num_unsaved_files = 0;
1161 CXToken *tokens;
1162 unsigned num_tokens;
1163 CXSourceRange range;
1164 CXSourceLocation startLoc, endLoc;
1165 CXFile file = 0;
1166 CXCursor *cursors = 0;
1167 unsigned i;
1168
1169 input += strlen("-test-annotate-tokens=");
1170 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1171 &second_line, &second_column)))
1172 return errorCode;
1173
1174 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1175 return -1;
1176
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001177 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001178 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1179 argc - num_unsaved_files - 3,
1180 argv + num_unsaved_files + 2,
1181 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001182 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001183 if (!TU) {
1184 fprintf(stderr, "unable to parse input\n");
1185 clang_disposeIndex(CIdx);
1186 free(filename);
1187 free_remapped_files(unsaved_files, num_unsaved_files);
1188 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001189 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001190 errorCode = 0;
1191
1192 file = clang_getFile(TU, filename);
1193 if (!file) {
1194 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1195 errorCode = -1;
1196 goto teardown;
1197 }
1198
1199 startLoc = clang_getLocation(TU, file, line, column);
1200 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001201 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001202 column);
1203 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001204 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001205 }
1206
1207 endLoc = clang_getLocation(TU, file, second_line, second_column);
1208 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001209 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001210 second_line, second_column);
1211 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001212 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001213 }
1214
1215 range = clang_getRange(startLoc, endLoc);
1216 clang_tokenize(TU, range, &tokens, &num_tokens);
1217 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1218 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1219 for (i = 0; i != num_tokens; ++i) {
1220 const char *kind = "<unknown>";
1221 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1222 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1223 unsigned start_line, start_column, end_line, end_column;
1224
1225 switch (clang_getTokenKind(tokens[i])) {
1226 case CXToken_Punctuation: kind = "Punctuation"; break;
1227 case CXToken_Keyword: kind = "Keyword"; break;
1228 case CXToken_Identifier: kind = "Identifier"; break;
1229 case CXToken_Literal: kind = "Literal"; break;
1230 case CXToken_Comment: kind = "Comment"; break;
1231 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001232 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001233 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001234 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001235 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001236 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1237 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001238 if (!clang_isInvalid(cursors[i].kind)) {
1239 printf(" ");
1240 PrintCursor(cursors[i]);
1241 }
1242 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001243 }
1244 free(cursors);
1245
1246 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001247 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001248 clang_disposeTranslationUnit(TU);
1249 clang_disposeIndex(CIdx);
1250 free(filename);
1251 free_remapped_files(unsaved_files, num_unsaved_files);
1252 return errorCode;
1253}
1254
Ted Kremenek0d435192009-11-17 18:13:31 +00001255/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001256/* USR printing. */
1257/******************************************************************************/
1258
1259static int insufficient_usr(const char *kind, const char *usage) {
1260 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1261 return 1;
1262}
1263
1264static unsigned isUSR(const char *s) {
1265 return s[0] == 'c' && s[1] == ':';
1266}
1267
1268static int not_usr(const char *s, const char *arg) {
1269 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1270 return 1;
1271}
1272
1273static void print_usr(CXString usr) {
1274 const char *s = clang_getCString(usr);
1275 printf("%s\n", s);
1276 clang_disposeString(usr);
1277}
1278
1279static void display_usrs() {
1280 fprintf(stderr, "-print-usrs options:\n"
1281 " ObjCCategory <class name> <category name>\n"
1282 " ObjCClass <class name>\n"
1283 " ObjCIvar <ivar name> <class USR>\n"
1284 " ObjCMethod <selector> [0=class method|1=instance method] "
1285 "<class USR>\n"
1286 " ObjCProperty <property name> <class USR>\n"
1287 " ObjCProtocol <protocol name>\n");
1288}
1289
1290int print_usrs(const char **I, const char **E) {
1291 while (I != E) {
1292 const char *kind = *I;
1293 unsigned len = strlen(kind);
1294 switch (len) {
1295 case 8:
1296 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1297 if (I + 2 >= E)
1298 return insufficient_usr(kind, "<ivar name> <class USR>");
1299 if (!isUSR(I[2]))
1300 return not_usr("<class USR>", I[2]);
1301 else {
1302 CXString x;
1303 x.Spelling = I[2];
1304 x.MustFreeString = 0;
1305 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1306 }
1307
1308 I += 3;
1309 continue;
1310 }
1311 break;
1312 case 9:
1313 if (memcmp(kind, "ObjCClass", 9) == 0) {
1314 if (I + 1 >= E)
1315 return insufficient_usr(kind, "<class name>");
1316 print_usr(clang_constructUSR_ObjCClass(I[1]));
1317 I += 2;
1318 continue;
1319 }
1320 break;
1321 case 10:
1322 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1323 if (I + 3 >= E)
1324 return insufficient_usr(kind, "<method selector> "
1325 "[0=class method|1=instance method] <class USR>");
1326 if (!isUSR(I[3]))
1327 return not_usr("<class USR>", I[3]);
1328 else {
1329 CXString x;
1330 x.Spelling = I[3];
1331 x.MustFreeString = 0;
1332 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1333 }
1334 I += 4;
1335 continue;
1336 }
1337 break;
1338 case 12:
1339 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1340 if (I + 2 >= E)
1341 return insufficient_usr(kind, "<class name> <category name>");
1342 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1343 I += 3;
1344 continue;
1345 }
1346 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1347 if (I + 1 >= E)
1348 return insufficient_usr(kind, "<protocol name>");
1349 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1350 I += 2;
1351 continue;
1352 }
1353 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1354 if (I + 2 >= E)
1355 return insufficient_usr(kind, "<property name> <class USR>");
1356 if (!isUSR(I[2]))
1357 return not_usr("<class USR>", I[2]);
1358 else {
1359 CXString x;
1360 x.Spelling = I[2];
1361 x.MustFreeString = 0;
1362 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1363 }
1364 I += 3;
1365 continue;
1366 }
1367 break;
1368 default:
1369 break;
1370 }
1371 break;
1372 }
1373
1374 if (I != E) {
1375 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1376 display_usrs();
1377 return 1;
1378 }
1379 return 0;
1380}
1381
1382int print_usrs_file(const char *file_name) {
1383 char line[2048];
1384 const char *args[128];
1385 unsigned numChars = 0;
1386
1387 FILE *fp = fopen(file_name, "r");
1388 if (!fp) {
1389 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1390 return 1;
1391 }
1392
1393 /* This code is not really all that safe, but it works fine for testing. */
1394 while (!feof(fp)) {
1395 char c = fgetc(fp);
1396 if (c == '\n') {
1397 unsigned i = 0;
1398 const char *s = 0;
1399
1400 if (numChars == 0)
1401 continue;
1402
1403 line[numChars] = '\0';
1404 numChars = 0;
1405
1406 if (line[0] == '/' && line[1] == '/')
1407 continue;
1408
1409 s = strtok(line, " ");
1410 while (s) {
1411 args[i] = s;
1412 ++i;
1413 s = strtok(0, " ");
1414 }
1415 if (print_usrs(&args[0], &args[i]))
1416 return 1;
1417 }
1418 else
1419 line[numChars++] = c;
1420 }
1421
1422 fclose(fp);
1423 return 0;
1424}
1425
1426/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001427/* Command line processing. */
1428/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001429int write_pch_file(const char *filename, int argc, const char *argv[]) {
1430 CXIndex Idx;
1431 CXTranslationUnit TU;
1432 struct CXUnsavedFile *unsaved_files = 0;
1433 int num_unsaved_files = 0;
1434
1435 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1436
1437 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1438 clang_disposeIndex(Idx);
1439 return -1;
1440 }
1441
1442 TU = clang_parseTranslationUnit(Idx, 0,
1443 argv + num_unsaved_files,
1444 argc - num_unsaved_files,
1445 unsaved_files,
1446 num_unsaved_files,
1447 CXTranslationUnit_Incomplete);
1448 if (!TU) {
1449 fprintf(stderr, "Unable to load translation unit!\n");
1450 free_remapped_files(unsaved_files, num_unsaved_files);
1451 clang_disposeIndex(Idx);
1452 return 1;
1453 }
1454
Douglas Gregor19998442010-08-13 15:35:05 +00001455 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001456 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1457 clang_disposeTranslationUnit(TU);
1458 free_remapped_files(unsaved_files, num_unsaved_files);
1459 clang_disposeIndex(Idx);
1460 return 0;
1461}
1462
1463/******************************************************************************/
1464/* Command line processing. */
1465/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001466
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001467static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001468 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001469 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001470 if (strcmp(s, "-usrs") == 0)
1471 return USRVisitor;
1472 return NULL;
1473}
1474
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001475static void print_usage(void) {
1476 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001477 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001478 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001479 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001480 " c-index-test -test-file-scan <AST file> <source file> "
1481 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001482 " c-index-test -test-load-tu <AST file> <symbol filter> "
1483 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001484 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1485 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001486 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001487 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001488 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1489 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001490 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001491 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1492 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001493 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001494 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001495 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001496 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1497 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001498 " c-index-test -print-usr-file <file>\n"
1499 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001500 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001501 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001502 " all - load all symbols, including those from PCH\n"
1503 " local - load all symbols except those in PCH\n"
1504 " category - only load ObjC categories (non-PCH)\n"
1505 " interface - only load ObjC interfaces (non-PCH)\n"
1506 " protocol - only load ObjC protocols (non-PCH)\n"
1507 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001508 " typedef - only load typdefs (non-PCH)\n"
1509 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001510}
1511
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001512/***/
1513
1514int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001515 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001516 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001517 return perform_code_completion(argc, argv, 0);
1518 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1519 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001520 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1521 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001522 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001523 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001524 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001525 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1526 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001527 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001528 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1529 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1530 if (I) {
1531 int trials = atoi(argv[2]);
1532 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1533 NULL);
1534 }
1535 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001536 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001537 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001538 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001539 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001540 }
1541 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001542 return perform_file_scan(argv[2], argv[3],
1543 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001544 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1545 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001546 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1547 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1548 PrintInclusionStack);
1549 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1550 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1551 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001552 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1553 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1554 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001555 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1556 return perform_test_load_source(argc - 2, argv + 2, "all",
1557 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001558 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1559 if (argc > 2)
1560 return print_usrs(argv + 2, argv + argc);
1561 else {
1562 display_usrs();
1563 return 1;
1564 }
1565 }
1566 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1567 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001568 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1569 return write_pch_file(argv[2], argc - 3, argv + 3);
1570
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001571 print_usage();
1572 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001573}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001574
1575/***/
1576
1577/* We intentionally run in a separate thread to ensure we at least minimal
1578 * testing of a multithreaded environment (for example, having a reduced stack
1579 * size). */
1580
1581#include "llvm/Config/config.h"
1582#ifdef HAVE_PTHREAD_H
1583
1584#include <pthread.h>
1585
1586typedef struct thread_info {
1587 int argc;
1588 const char **argv;
1589 int result;
1590} thread_info;
1591void *thread_runner(void *client_data_v) {
1592 thread_info *client_data = client_data_v;
1593 client_data->result = cindextest_main(client_data->argc, client_data->argv);
1594 return 0;
1595}
1596
1597int main(int argc, const char **argv) {
1598 thread_info client_data;
1599 pthread_t thread;
1600 int res;
1601
1602 client_data.argc = argc;
1603 client_data.argv = argv;
1604 res = pthread_create(&thread, 0, thread_runner, &client_data);
1605 if (res != 0) {
1606 perror("thread creation failed");
1607 return 1;
1608 }
1609
1610 res = pthread_join(thread, 0);
1611 if (res != 0) {
1612 perror("thread join failed");
1613 return 1;
1614 }
1615
1616 return client_data.result;
1617}
1618
1619#else
1620
1621int main(int argc, const char **argv) {
1622 return cindextest_main(argc, argv);
1623}
1624
1625#endif