blob: 360f75a66e0d4a173b29d677551393556b821509 [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 Gregor0c8296d2009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00006#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00007#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008
Ted Kremenek0d435192009-11-17 18:13:31 +00009/******************************************************************************/
10/* Utility functions. */
11/******************************************************************************/
12
John Thompson2e06fc82009-10-27 13:42:56 +000013#ifdef _MSC_VER
14char *basename(const char* path)
15{
16 char* base1 = (char*)strrchr(path, '/');
17 char* base2 = (char*)strrchr(path, '\\');
18 if (base1 && base2)
19 return((base1 > base2) ? base1 + 1 : base2 + 1);
20 else if (base1)
21 return(base1 + 1);
22 else if (base2)
23 return(base2 + 1);
24
25 return((char*)path);
26}
27#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000028extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000029#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000030
Ted Kremeneke68fff62010-02-17 00:41:32 +000031static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
Douglas Gregor5352ac02010-01-28 00:27:43 +000032 CXClientData ClientData);
33
Daniel Dunbar51b058c2010-02-14 08:32:24 +000034
35static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
36 unsigned end_line, unsigned end_column) {
37 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000038 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000039}
40
Ted Kremenek1c6da172009-11-17 19:37:36 +000041static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
42 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000043
Daniel Dunbar97150822010-02-16 01:55:10 +000044 *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, stderr);
Ted Kremenek1c6da172009-11-17 19:37:36 +000045 if (!TU) {
46 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
47 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000048 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000049 return 1;
50}
51
Douglas Gregor4db64a42010-01-23 00:14:00 +000052void free_remapped_files(struct CXUnsavedFile *unsaved_files,
53 int num_unsaved_files) {
54 int i;
55 for (i = 0; i != num_unsaved_files; ++i) {
56 free((char *)unsaved_files[i].Filename);
57 free((char *)unsaved_files[i].Contents);
58 }
59}
60
61int parse_remapped_files(int argc, const char **argv, int start_arg,
62 struct CXUnsavedFile **unsaved_files,
63 int *num_unsaved_files) {
64 int i;
65 int arg;
66 int prefix_len = strlen("-remap-file=");
67 *unsaved_files = 0;
68 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000069
Douglas Gregor4db64a42010-01-23 00:14:00 +000070 /* Count the number of remapped files. */
71 for (arg = start_arg; arg < argc; ++arg) {
72 if (strncmp(argv[arg], "-remap-file=", prefix_len))
73 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000074
Douglas Gregor4db64a42010-01-23 00:14:00 +000075 ++*num_unsaved_files;
76 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000077
Douglas Gregor4db64a42010-01-23 00:14:00 +000078 if (*num_unsaved_files == 0)
79 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000080
Douglas Gregor4db64a42010-01-23 00:14:00 +000081 *unsaved_files
Ted Kremeneke68fff62010-02-17 00:41:32 +000082 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
Douglas Gregor4db64a42010-01-23 00:14:00 +000083 *num_unsaved_files);
84 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
85 struct CXUnsavedFile *unsaved = *unsaved_files + i;
86 const char *arg_string = argv[arg] + prefix_len;
87 int filename_len;
88 char *filename;
89 char *contents;
90 FILE *to_file;
91 const char *semi = strchr(arg_string, ';');
92 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000093 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 "error: -remap-file=from;to argument is missing semicolon\n");
95 free_remapped_files(*unsaved_files, i);
96 *unsaved_files = 0;
97 *num_unsaved_files = 0;
98 return -1;
99 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000100
Douglas Gregor4db64a42010-01-23 00:14:00 +0000101 /* Open the file that we're remapping to. */
102 to_file = fopen(semi + 1, "r");
103 if (!to_file) {
104 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
105 semi + 1);
106 free_remapped_files(*unsaved_files, i);
107 *unsaved_files = 0;
108 *num_unsaved_files = 0;
109 return -1;
110 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000111
Douglas Gregor4db64a42010-01-23 00:14:00 +0000112 /* Determine the length of the file we're remapping to. */
113 fseek(to_file, 0, SEEK_END);
114 unsaved->Length = ftell(to_file);
115 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 /* Read the contents of the file we're remapping to. */
118 contents = (char *)malloc(unsaved->Length + 1);
119 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
120 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
121 (feof(to_file) ? "EOF" : "error"), semi + 1);
122 fclose(to_file);
123 free_remapped_files(*unsaved_files, i);
124 *unsaved_files = 0;
125 *num_unsaved_files = 0;
126 return -1;
127 }
128 contents[unsaved->Length] = 0;
129 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000130
Douglas Gregor4db64a42010-01-23 00:14:00 +0000131 /* Close the file. */
132 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000133
Douglas Gregor4db64a42010-01-23 00:14:00 +0000134 /* Copy the file name that we're remapping from. */
135 filename_len = semi - arg_string;
136 filename = (char *)malloc(filename_len + 1);
137 memcpy(filename, arg_string, filename_len);
138 filename[filename_len] = 0;
139 unsaved->Filename = filename;
140 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000141
Douglas Gregor4db64a42010-01-23 00:14:00 +0000142 return 0;
143}
144
Ted Kremenek0d435192009-11-17 18:13:31 +0000145/******************************************************************************/
146/* Pretty-printing. */
147/******************************************************************************/
148
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000149static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000150 if (clang_isInvalid(Cursor.kind)) {
151 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
152 printf("Invalid Cursor => %s", clang_getCString(ks));
153 clang_disposeString(ks);
154 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000155 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000156 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000157 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000158 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000159
160 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000161 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000162 printf("%s=%s", clang_getCString(ks),
163 clang_getCString(string));
164 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000165 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000166
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000167 Referenced = clang_getCursorReferenced(Cursor);
168 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
169 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000170 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000171 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000172 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000173
174 if (clang_isCursorDefinition(Cursor))
175 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000176 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000177}
Steve Naroff89922f82009-08-31 00:59:03 +0000178
Ted Kremeneke68fff62010-02-17 00:41:32 +0000179static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000180 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000181 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000182 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000183 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000184 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000185 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000186 clang_disposeString(source);
187 return "<invalid loc>";
188 }
189 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000190 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000191 clang_disposeString(source);
192 return b;
193 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000194}
195
Ted Kremenek0d435192009-11-17 18:13:31 +0000196/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000197/* Callbacks. */
198/******************************************************************************/
199
200typedef void (*PostVisitTU)(CXTranslationUnit);
201
Ted Kremeneke68fff62010-02-17 00:41:32 +0000202static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000203 CXClientData ClientData) {
204 FILE *out = (FILE *)ClientData;
205 CXFile file;
206 unsigned line, column;
207 CXString text;
208 enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000209
Douglas Gregor5352ac02010-01-28 00:27:43 +0000210 /* Ignore diagnostics that should be ignored. */
211 if (severity == CXDiagnostic_Ignored)
212 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000213
Douglas Gregor5352ac02010-01-28 00:27:43 +0000214 /* Print file:line:column. */
215 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
216 &file, &line, &column, 0);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000217 if (file) {
Douglas Gregora3890ba2010-02-08 23:11:56 +0000218 unsigned i, n;
Douglas Gregor51c6d382010-01-29 00:41:11 +0000219 unsigned printed_any_ranges = 0;
Ted Kremenek74844072010-02-17 00:41:20 +0000220 CXString fname;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000221
Ted Kremenek74844072010-02-17 00:41:20 +0000222 fname = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000223 fprintf(out, "%s:%d:%d:", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000224 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000225
Douglas Gregora3890ba2010-02-08 23:11:56 +0000226 n = clang_getDiagnosticNumRanges(Diagnostic);
227 for (i = 0; i != n; ++i) {
Douglas Gregor51c6d382010-01-29 00:41:11 +0000228 CXFile start_file, end_file;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000229 CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000230
Douglas Gregor51c6d382010-01-29 00:41:11 +0000231 unsigned start_line, start_column, end_line, end_column;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000232 clang_getInstantiationLocation(clang_getRangeStart(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000233 &start_file, &start_line, &start_column,0);
Douglas Gregora3890ba2010-02-08 23:11:56 +0000234 clang_getInstantiationLocation(clang_getRangeEnd(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000235 &end_file, &end_line, &end_column, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000236
Douglas Gregor51c6d382010-01-29 00:41:11 +0000237 if (start_file != end_file || start_file != file)
238 continue;
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000239
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000240 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000241 printed_any_ranges = 1;
242 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000243 if (printed_any_ranges)
244 fprintf(out, ":");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000245
Douglas Gregor51c6d382010-01-29 00:41:11 +0000246 fprintf(out, " ");
247 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000248
Douglas Gregor5352ac02010-01-28 00:27:43 +0000249 /* Print warning/error/etc. */
250 switch (severity) {
251 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
252 case CXDiagnostic_Note: fprintf(out, "note: "); break;
253 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
254 case CXDiagnostic_Error: fprintf(out, "error: "); break;
255 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
256 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000257
Douglas Gregor5352ac02010-01-28 00:27:43 +0000258 text = clang_getDiagnosticSpelling(Diagnostic);
259 if (clang_getCString(text))
260 fprintf(out, "%s\n", clang_getCString(text));
261 else
262 fprintf(out, "<no diagnostic text>\n");
263 clang_disposeString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000264
Douglas Gregor51c6d382010-01-29 00:41:11 +0000265 if (file) {
266 unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
267 for (i = 0; i != num_fixits; ++i) {
268 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
269 case CXFixIt_Insertion: {
270 CXSourceLocation insertion_loc;
271 CXFile insertion_file;
272 unsigned insertion_line, insertion_column;
273 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000274 clang_getInstantiationLocation(insertion_loc, &insertion_file,
Douglas Gregor51c6d382010-01-29 00:41:11 +0000275 &insertion_line, &insertion_column, 0);
276 if (insertion_file == file)
277 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
278 clang_getCString(text), insertion_line, insertion_column);
279 clang_disposeString(text);
280 break;
281 }
282
283 case CXFixIt_Removal: {
284 CXFile start_file, end_file;
285 unsigned start_line, start_column, end_line, end_column;
286 CXSourceRange remove_range
287 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
288 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
289 &start_file, &start_line, &start_column,
290 0);
291 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
292 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000293 if (start_file == file && end_file == file) {
294 fprintf(out, "FIX-IT: Remove ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000295 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000296 fprintf(out, "\n");
297 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000298 break;
299 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000300
Douglas Gregor51c6d382010-01-29 00:41:11 +0000301 case CXFixIt_Replacement: {
302 CXFile start_file, end_file;
303 unsigned start_line, start_column, end_line, end_column;
304 CXSourceRange remove_range;
305 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
306 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
307 &start_file, &start_line, &start_column,
308 0);
309 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
310 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000311 if (start_file == end_file) {
312 fprintf(out, "FIX-IT: Replace ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000313 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000314 fprintf(out, " with \"%s\"\n", clang_getCString(text));
315 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000316 clang_disposeString(text);
317 break;
318 }
319 }
320 }
321 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000322}
323
Ted Kremenekce2ae882010-01-26 17:59:48 +0000324/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000325/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000326/******************************************************************************/
327
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000328static const char *FileCheckPrefix = "CHECK";
329
Douglas Gregora7bde202010-01-19 00:34:46 +0000330static void PrintCursorExtent(CXCursor C) {
331 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000332 CXFile begin_file, end_file;
333 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000334
Douglas Gregor1db19de2010-01-19 21:36:55 +0000335 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000336 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000337 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000338 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000339 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000340 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000341
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000342 printf(" Extent=");
343 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000344}
345
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000346/* Data used by all of the visitors. */
347typedef struct {
348 CXTranslationUnit TU;
349 enum CXCursorKind *Filter;
350} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000351
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000352
Ted Kremeneke68fff62010-02-17 00:41:32 +0000353enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000354 CXCursor Parent,
355 CXClientData ClientData) {
356 VisitorData *Data = (VisitorData *)ClientData;
357 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000358 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000359 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000360 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000361 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000362 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000363 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000364 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000365 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000366 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000367 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000368
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000369 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000370}
Steve Naroff50398192009-08-28 15:28:48 +0000371
Ted Kremeneke68fff62010-02-17 00:41:32 +0000372static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000373 CXCursor Parent,
374 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000375 const char *startBuf, *endBuf;
376 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
377 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000378 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000379
Douglas Gregorb6998662010-01-19 19:34:47 +0000380 if (Cursor.kind != CXCursor_FunctionDecl ||
381 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000382 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000383
384 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
385 &startLine, &startColumn,
386 &endLine, &endColumn);
387 /* Probe the entire body, looking for both decls and refs. */
388 curLine = startLine;
389 curColumn = startColumn;
390
391 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000392 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000393 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000394 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000395
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000396 if (*startBuf == '\n') {
397 startBuf++;
398 curLine++;
399 curColumn = 1;
400 } else if (*startBuf != '\t')
401 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000402
Douglas Gregor98258af2010-01-18 22:46:11 +0000403 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000404 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000405
Douglas Gregor1db19de2010-01-19 21:36:55 +0000406 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000407 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000408 CXSourceLocation RefLoc
409 = clang_getLocation(Data->TU, file, curLine, curColumn);
410 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000411 if (Ref.kind == CXCursor_NoDeclFound) {
412 /* Nothing found here; that's fine. */
413 } else if (Ref.kind != CXCursor_FunctionDecl) {
414 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
415 curLine, curColumn);
416 PrintCursor(Ref);
417 printf("\n");
418 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000419 }
Ted Kremenek74844072010-02-17 00:41:20 +0000420 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000421 startBuf++;
422 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000423
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000424 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000425}
426
Ted Kremenek7d405622010-01-12 23:34:26 +0000427/******************************************************************************/
428/* USR testing. */
429/******************************************************************************/
430
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000431enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
432 CXClientData ClientData) {
433 VisitorData *Data = (VisitorData *)ClientData;
434 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000435 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000436 if (!clang_getCString(USR)) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000437 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000438 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000439 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000440 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C),
441 clang_getCString(USR));
Douglas Gregora7bde202010-01-19 00:34:46 +0000442 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000443 printf("\n");
444 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000445
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000446 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000447 }
448
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000449 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000450}
451
452/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000453/* Inclusion stack testing. */
454/******************************************************************************/
455
456void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
457 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000458
Ted Kremenek16b55a72010-01-26 19:31:51 +0000459 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000460 CXString fname;
461
462 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000463 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000464 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000465
Ted Kremenek16b55a72010-01-26 19:31:51 +0000466 for (i = 0; i < includeStackLen; ++i) {
467 CXFile includingFile;
468 unsigned line, column;
469 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
470 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000471 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000472 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000473 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000474 }
475 printf("\n");
476}
477
478void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000479 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000480}
481
482/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000483/* Loading ASTs/source. */
484/******************************************************************************/
485
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000486static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000487 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000488 CXCursorVisitor Visitor,
489 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000490
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000491 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000493
494 if (Visitor) {
495 enum CXCursorKind K = CXCursor_NotImplemented;
496 enum CXCursorKind *ck = &K;
497 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000498
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000499 /* Perform some simple filtering. */
500 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000501 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000502 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
503 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
504 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
505 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
506 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
507 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
508 else {
509 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
510 return 1;
511 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000512
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000513 Data.TU = TU;
514 Data.Filter = ck;
515 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000516 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517
Ted Kremenekce2ae882010-01-26 17:59:48 +0000518 if (PV)
519 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000520
Ted Kremenek0d435192009-11-17 18:13:31 +0000521 clang_disposeTranslationUnit(TU);
522 return 0;
523}
524
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000525int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000526 const char *prefix, CXCursorVisitor Visitor,
527 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000528 CXIndex Idx;
529 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000530 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000531 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000532 !strcmp(filter, "local") ? 1 : 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000533
Ted Kremenek020a0952010-02-11 07:41:25 +0000534 if (!CreateTranslationUnit(Idx, file, &TU)) {
535 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000536 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000537 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000538
Ted Kremenek020a0952010-02-11 07:41:25 +0000539 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
540 clang_disposeIndex(Idx);
541 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000542}
543
Ted Kremenekce2ae882010-01-26 17:59:48 +0000544int perform_test_load_source(int argc, const char **argv,
545 const char *filter, CXCursorVisitor Visitor,
546 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000547 const char *UseExternalASTs =
548 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000549 CXIndex Idx;
550 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000551 struct CXUnsavedFile *unsaved_files = 0;
552 int num_unsaved_files = 0;
553 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000554
Daniel Dunbarada487d2009-12-01 02:03:10 +0000555 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000556 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000557
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000558 if (UseExternalASTs && strlen(UseExternalASTs))
559 clang_setUseExternalASTGeneration(Idx, 1);
560
Ted Kremenek020a0952010-02-11 07:41:25 +0000561 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
562 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000563 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000564 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000565
Ted Kremeneke68fff62010-02-17 00:41:32 +0000566 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
567 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000568 argv + num_unsaved_files,
569 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000570 unsaved_files,
571 PrintDiagnosticCallback,
572 stderr);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000573 if (!TU) {
574 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000575 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000576 return 1;
577 }
578
Ted Kremenekce2ae882010-01-26 17:59:48 +0000579 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000580 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000581 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000582 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000583}
584
Ted Kremenek0d435192009-11-17 18:13:31 +0000585/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000586/* Logic for testing clang_getCursor(). */
587/******************************************************************************/
588
589static void print_cursor_file_scan(CXCursor cursor,
590 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000591 unsigned end_line, unsigned end_col,
592 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000593 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000594 if (prefix)
595 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000596 PrintExtent(stdout, start_line, start_col, end_line, end_col);
597 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000598 PrintCursor(cursor);
599 printf("\n");
600}
601
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000602static int perform_file_scan(const char *ast_file, const char *source_file,
603 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000604 CXIndex Idx;
605 CXTranslationUnit TU;
606 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000607 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000608 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000609 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000610 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000611
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000612 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000613 fprintf(stderr, "Could not create Index\n");
614 return 1;
615 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000616
Ted Kremenek1c6da172009-11-17 19:37:36 +0000617 if (!CreateTranslationUnit(Idx, ast_file, &TU))
618 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000619
Ted Kremenek1c6da172009-11-17 19:37:36 +0000620 if ((fp = fopen(source_file, "r")) == NULL) {
621 fprintf(stderr, "Could not open '%s'\n", source_file);
622 return 1;
623 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000624
Douglas Gregorb9790342010-01-22 21:44:22 +0000625 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000626 for (;;) {
627 CXCursor cursor;
628 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000629
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000630 if (c == '\n') {
631 ++line;
632 col = 1;
633 } else
634 ++col;
635
636 /* Check the cursor at this position, and dump the previous one if we have
637 * found something new.
638 */
639 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
640 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
641 prevCursor.kind != CXCursor_InvalidFile) {
642 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000643 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000644 start_line = line;
645 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000646 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000647 if (c == EOF)
648 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000649
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000650 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000651 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000652
Ted Kremenek1c6da172009-11-17 19:37:36 +0000653 fclose(fp);
654 return 0;
655}
656
657/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000658/* Logic for testing clang_codeComplete(). */
659/******************************************************************************/
660
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000661/* Parse file:line:column from the input string. Returns 0 on success, non-zero
662 on failure. If successful, the pointer *filename will contain newly-allocated
663 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000664int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000665 unsigned *column, unsigned *second_line,
666 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000667 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000668 const char *last_colon = strrchr(input, ':');
669 unsigned values[4], i;
670 unsigned num_values = (second_line && second_column)? 4 : 2;
671
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000672 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000673 if (!last_colon || last_colon == input) {
674 if (num_values == 4)
675 fprintf(stderr, "could not parse filename:line:column:line:column in "
676 "'%s'\n", input);
677 else
678 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000679 return 1;
680 }
681
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000682 for (i = 0; i != num_values; ++i) {
683 const char *prev_colon;
684
685 /* Parse the next line or column. */
686 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
687 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000688 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000689 (i % 2 ? "column" : "line"), input);
690 return 1;
691 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000692
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000693 if (i + 1 == num_values)
694 break;
695
696 /* Find the previous colon. */
697 prev_colon = last_colon - 1;
698 while (prev_colon != input && *prev_colon != ':')
699 --prev_colon;
700 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000701 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000702 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000703 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000704 }
705
706 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000707 }
708
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000709 *line = values[0];
710 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000711
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000712 if (second_line && second_column) {
713 *second_line = values[2];
714 *second_column = values[3];
715 }
716
Douglas Gregor88d23952009-11-09 18:19:57 +0000717 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000718 *filename = (char*)malloc(last_colon - input + 1);
719 memcpy(*filename, input, last_colon - input);
720 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000721 return 0;
722}
723
724const char *
725clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
726 switch (Kind) {
727 case CXCompletionChunk_Optional: return "Optional";
728 case CXCompletionChunk_TypedText: return "TypedText";
729 case CXCompletionChunk_Text: return "Text";
730 case CXCompletionChunk_Placeholder: return "Placeholder";
731 case CXCompletionChunk_Informative: return "Informative";
732 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
733 case CXCompletionChunk_LeftParen: return "LeftParen";
734 case CXCompletionChunk_RightParen: return "RightParen";
735 case CXCompletionChunk_LeftBracket: return "LeftBracket";
736 case CXCompletionChunk_RightBracket: return "RightBracket";
737 case CXCompletionChunk_LeftBrace: return "LeftBrace";
738 case CXCompletionChunk_RightBrace: return "RightBrace";
739 case CXCompletionChunk_LeftAngle: return "LeftAngle";
740 case CXCompletionChunk_RightAngle: return "RightAngle";
741 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000742 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000743 case CXCompletionChunk_Colon: return "Colon";
744 case CXCompletionChunk_SemiColon: return "SemiColon";
745 case CXCompletionChunk_Equal: return "Equal";
746 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
747 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000748 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000749
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000750 return "Unknown";
751}
752
Douglas Gregor3ac73852009-11-09 16:04:45 +0000753void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000754 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000755
Douglas Gregor3ac73852009-11-09 16:04:45 +0000756 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000757 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000758 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000759 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000760 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000761
Douglas Gregor3ac73852009-11-09 16:04:45 +0000762 if (Kind == CXCompletionChunk_Optional) {
763 fprintf(file, "{Optional ");
764 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000765 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000766 file);
767 fprintf(file, "}");
768 continue;
769 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000770
Douglas Gregord5a20892009-11-09 17:05:28 +0000771 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000772 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000773 clang_getCompletionChunkKindSpelling(Kind),
774 text? text : "");
775 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000776}
777
778void print_completion_result(CXCompletionResult *completion_result,
779 CXClientData client_data) {
780 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000781 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
782
783 fprintf(file, "%s:", clang_getCString(ks));
784 clang_disposeString(ks);
785
Douglas Gregor3ac73852009-11-09 16:04:45 +0000786 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000787 fprintf(file, "\n");
788}
789
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000790int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000791 const char *input = argv[1];
792 char *filename = 0;
793 unsigned line;
794 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000795 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000796 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000797 struct CXUnsavedFile *unsaved_files = 0;
798 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000799 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000800
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000801 input += strlen("-code-completion-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000802 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000803 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000804 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000805
Douglas Gregor735df882009-12-02 09:21:34 +0000806 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
807 return -1;
808
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000809 CIdx = clang_createIndex(0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000810 results = clang_codeComplete(CIdx,
811 argv[argc - 1], argc - num_unsaved_files - 3,
812 argv + num_unsaved_files + 2,
Douglas Gregorec6762c2009-12-18 16:20:58 +0000813 num_unsaved_files, unsaved_files,
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000814 filename, line, column,
815 PrintDiagnosticCallback, stderr);
816
Douglas Gregorec6762c2009-12-18 16:20:58 +0000817 if (results) {
818 unsigned i, n = results->NumResults;
819 for (i = 0; i != n; ++i)
820 print_completion_result(results->Results + i, stdout);
821 clang_disposeCodeCompleteResults(results);
822 }
823
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000824 clang_disposeIndex(CIdx);
825 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000826
Douglas Gregor735df882009-12-02 09:21:34 +0000827 free_remapped_files(unsaved_files, num_unsaved_files);
828
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000829 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000830}
831
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000832typedef struct {
833 char *filename;
834 unsigned line;
835 unsigned column;
836} CursorSourceLocation;
837
838int inspect_cursor_at(int argc, const char **argv) {
839 CXIndex CIdx;
840 int errorCode;
841 struct CXUnsavedFile *unsaved_files = 0;
842 int num_unsaved_files = 0;
843 CXTranslationUnit TU;
844 CXCursor Cursor;
845 CursorSourceLocation *Locations = 0;
846 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000847
Ted Kremeneke68fff62010-02-17 00:41:32 +0000848 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000849 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
850 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000851
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000852 /* Parse the locations. */
853 assert(NumLocations > 0 && "Unable to count locations?");
854 Locations = (CursorSourceLocation *)malloc(
855 NumLocations * sizeof(CursorSourceLocation));
856 for (Loc = 0; Loc < NumLocations; ++Loc) {
857 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000858 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
859 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000860 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000861 return errorCode;
862 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000863
864 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000865 &num_unsaved_files))
866 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000867
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000868 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000869 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
870 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000871 argv + num_unsaved_files + 1 + NumLocations,
872 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000873 unsaved_files,
874 PrintDiagnosticCallback,
875 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000876 if (!TU) {
877 fprintf(stderr, "unable to parse input\n");
878 return -1;
879 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000880
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000881 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000882 CXFile file = clang_getFile(TU, Locations[Loc].filename);
883 if (!file)
884 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000885
886 Cursor = clang_getCursor(TU,
887 clang_getLocation(TU, file, Locations[Loc].line,
888 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000889 PrintCursor(Cursor);
890 printf("\n");
891 free(Locations[Loc].filename);
892 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000893
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000894 clang_disposeTranslationUnit(TU);
895 clang_disposeIndex(CIdx);
896 free(Locations);
897 free_remapped_files(unsaved_files, num_unsaved_files);
898 return 0;
899}
900
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000901int perform_token_annotation(int argc, const char **argv) {
902 const char *input = argv[1];
903 char *filename = 0;
904 unsigned line, second_line;
905 unsigned column, second_column;
906 CXIndex CIdx;
907 CXTranslationUnit TU = 0;
908 int errorCode;
909 struct CXUnsavedFile *unsaved_files = 0;
910 int num_unsaved_files = 0;
911 CXToken *tokens;
912 unsigned num_tokens;
913 CXSourceRange range;
914 CXSourceLocation startLoc, endLoc;
915 CXFile file = 0;
916 CXCursor *cursors = 0;
917 unsigned i;
918
919 input += strlen("-test-annotate-tokens=");
920 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
921 &second_line, &second_column)))
922 return errorCode;
923
924 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
925 return -1;
926
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000927 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000928 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
929 argc - num_unsaved_files - 3,
930 argv + num_unsaved_files + 2,
931 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000932 unsaved_files,
933 PrintDiagnosticCallback,
934 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000935 if (!TU) {
936 fprintf(stderr, "unable to parse input\n");
937 clang_disposeIndex(CIdx);
938 free(filename);
939 free_remapped_files(unsaved_files, num_unsaved_files);
940 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000941 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000942 errorCode = 0;
943
944 file = clang_getFile(TU, filename);
945 if (!file) {
946 fprintf(stderr, "file %s is not in this translation unit\n", filename);
947 errorCode = -1;
948 goto teardown;
949 }
950
951 startLoc = clang_getLocation(TU, file, line, column);
952 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000953 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000954 column);
955 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000956 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000957 }
958
959 endLoc = clang_getLocation(TU, file, second_line, second_column);
960 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000961 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000962 second_line, second_column);
963 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000964 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000965 }
966
967 range = clang_getRange(startLoc, endLoc);
968 clang_tokenize(TU, range, &tokens, &num_tokens);
969 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
970 clang_annotateTokens(TU, tokens, num_tokens, cursors);
971 for (i = 0; i != num_tokens; ++i) {
972 const char *kind = "<unknown>";
973 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
974 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
975 unsigned start_line, start_column, end_line, end_column;
976
977 switch (clang_getTokenKind(tokens[i])) {
978 case CXToken_Punctuation: kind = "Punctuation"; break;
979 case CXToken_Keyword: kind = "Keyword"; break;
980 case CXToken_Identifier: kind = "Identifier"; break;
981 case CXToken_Literal: kind = "Literal"; break;
982 case CXToken_Comment: kind = "Comment"; break;
983 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000984 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000985 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000986 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000987 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000988 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
989 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000990 if (!clang_isInvalid(cursors[i].kind)) {
991 printf(" ");
992 PrintCursor(cursors[i]);
993 }
994 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000995 }
996 free(cursors);
997
998 teardown:
999 clang_disposeTranslationUnit(TU);
1000 clang_disposeIndex(CIdx);
1001 free(filename);
1002 free_remapped_files(unsaved_files, num_unsaved_files);
1003 return errorCode;
1004}
1005
Ted Kremenek0d435192009-11-17 18:13:31 +00001006/******************************************************************************/
1007/* Command line processing. */
1008/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001009
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001010static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001011 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001012 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001013 if (strcmp(s, "-usrs") == 0)
1014 return USRVisitor;
1015 return NULL;
1016}
1017
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001018static void print_usage(void) {
1019 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001020 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001021 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001022 " c-index-test -test-file-scan <AST file> <source file> "
1023 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001024 " c-index-test -test-load-tu <AST file> <symbol filter> "
1025 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001026 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1027 "[FileCheck prefix]\n"
1028 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001029 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001030 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001031 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1032 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1033 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001034 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001035 " all - load all symbols, including those from PCH\n"
1036 " local - load all symbols except those in PCH\n"
1037 " category - only load ObjC categories (non-PCH)\n"
1038 " interface - only load ObjC interfaces (non-PCH)\n"
1039 " protocol - only load ObjC protocols (non-PCH)\n"
1040 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001041 " typedef - only load typdefs (non-PCH)\n"
1042 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001043}
1044
1045int main(int argc, const char **argv) {
1046 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1047 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001048 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1049 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001050 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001051 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001052 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001053 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1054 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001055 }
1056 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001057 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001058 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001059 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001060 }
1061 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001062 return perform_file_scan(argv[2], argv[3],
1063 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001064 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1065 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001066 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1067 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1068 PrintInclusionStack);
1069 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1070 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1071 PrintInclusionStack);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001072
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001073 print_usage();
1074 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001075}