blob: 640375aaeace828998232b9938d59bc4c2e6622b [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) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000758 CXString text;
759 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000760 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000761 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000762
Douglas Gregor3ac73852009-11-09 16:04:45 +0000763 if (Kind == CXCompletionChunk_Optional) {
764 fprintf(file, "{Optional ");
765 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000766 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000767 file);
768 fprintf(file, "}");
769 continue;
770 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000771
Douglas Gregord5a20892009-11-09 17:05:28 +0000772 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000773 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000774 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000775 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000776 cstr ? cstr : "");
777 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000778 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000779
Douglas Gregor3ac73852009-11-09 16:04:45 +0000780}
781
782void print_completion_result(CXCompletionResult *completion_result,
783 CXClientData client_data) {
784 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000785 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
786
787 fprintf(file, "%s:", clang_getCString(ks));
788 clang_disposeString(ks);
789
Douglas Gregor3ac73852009-11-09 16:04:45 +0000790 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000791 fprintf(file, "\n");
792}
793
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000794int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000795 const char *input = argv[1];
796 char *filename = 0;
797 unsigned line;
798 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000799 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000800 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000801 struct CXUnsavedFile *unsaved_files = 0;
802 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000803 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000804
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000805 input += strlen("-code-completion-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000806 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000807 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000808 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000809
Douglas Gregor735df882009-12-02 09:21:34 +0000810 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
811 return -1;
812
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000813 CIdx = clang_createIndex(0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000814 results = clang_codeComplete(CIdx,
815 argv[argc - 1], argc - num_unsaved_files - 3,
816 argv + num_unsaved_files + 2,
Douglas Gregorec6762c2009-12-18 16:20:58 +0000817 num_unsaved_files, unsaved_files,
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000818 filename, line, column,
819 PrintDiagnosticCallback, stderr);
820
Douglas Gregorec6762c2009-12-18 16:20:58 +0000821 if (results) {
822 unsigned i, n = results->NumResults;
823 for (i = 0; i != n; ++i)
824 print_completion_result(results->Results + i, stdout);
825 clang_disposeCodeCompleteResults(results);
826 }
827
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000828 clang_disposeIndex(CIdx);
829 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000830
Douglas Gregor735df882009-12-02 09:21:34 +0000831 free_remapped_files(unsaved_files, num_unsaved_files);
832
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000833 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000834}
835
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000836typedef struct {
837 char *filename;
838 unsigned line;
839 unsigned column;
840} CursorSourceLocation;
841
842int inspect_cursor_at(int argc, const char **argv) {
843 CXIndex CIdx;
844 int errorCode;
845 struct CXUnsavedFile *unsaved_files = 0;
846 int num_unsaved_files = 0;
847 CXTranslationUnit TU;
848 CXCursor Cursor;
849 CursorSourceLocation *Locations = 0;
850 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000851
Ted Kremeneke68fff62010-02-17 00:41:32 +0000852 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000853 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
854 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000855
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000856 /* Parse the locations. */
857 assert(NumLocations > 0 && "Unable to count locations?");
858 Locations = (CursorSourceLocation *)malloc(
859 NumLocations * sizeof(CursorSourceLocation));
860 for (Loc = 0; Loc < NumLocations; ++Loc) {
861 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000862 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
863 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000864 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000865 return errorCode;
866 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000867
868 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000869 &num_unsaved_files))
870 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000871
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000872 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000873 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
874 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000875 argv + num_unsaved_files + 1 + NumLocations,
876 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000877 unsaved_files,
878 PrintDiagnosticCallback,
879 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000880 if (!TU) {
881 fprintf(stderr, "unable to parse input\n");
882 return -1;
883 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000884
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000885 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000886 CXFile file = clang_getFile(TU, Locations[Loc].filename);
887 if (!file)
888 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000889
890 Cursor = clang_getCursor(TU,
891 clang_getLocation(TU, file, Locations[Loc].line,
892 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000893 PrintCursor(Cursor);
894 printf("\n");
895 free(Locations[Loc].filename);
896 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000897
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000898 clang_disposeTranslationUnit(TU);
899 clang_disposeIndex(CIdx);
900 free(Locations);
901 free_remapped_files(unsaved_files, num_unsaved_files);
902 return 0;
903}
904
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000905int perform_token_annotation(int argc, const char **argv) {
906 const char *input = argv[1];
907 char *filename = 0;
908 unsigned line, second_line;
909 unsigned column, second_column;
910 CXIndex CIdx;
911 CXTranslationUnit TU = 0;
912 int errorCode;
913 struct CXUnsavedFile *unsaved_files = 0;
914 int num_unsaved_files = 0;
915 CXToken *tokens;
916 unsigned num_tokens;
917 CXSourceRange range;
918 CXSourceLocation startLoc, endLoc;
919 CXFile file = 0;
920 CXCursor *cursors = 0;
921 unsigned i;
922
923 input += strlen("-test-annotate-tokens=");
924 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
925 &second_line, &second_column)))
926 return errorCode;
927
928 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
929 return -1;
930
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000931 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000932 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
933 argc - num_unsaved_files - 3,
934 argv + num_unsaved_files + 2,
935 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000936 unsaved_files,
937 PrintDiagnosticCallback,
938 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000939 if (!TU) {
940 fprintf(stderr, "unable to parse input\n");
941 clang_disposeIndex(CIdx);
942 free(filename);
943 free_remapped_files(unsaved_files, num_unsaved_files);
944 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000945 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000946 errorCode = 0;
947
948 file = clang_getFile(TU, filename);
949 if (!file) {
950 fprintf(stderr, "file %s is not in this translation unit\n", filename);
951 errorCode = -1;
952 goto teardown;
953 }
954
955 startLoc = clang_getLocation(TU, file, line, column);
956 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000957 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000958 column);
959 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000960 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000961 }
962
963 endLoc = clang_getLocation(TU, file, second_line, second_column);
964 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000965 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000966 second_line, second_column);
967 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000968 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000969 }
970
971 range = clang_getRange(startLoc, endLoc);
972 clang_tokenize(TU, range, &tokens, &num_tokens);
973 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
974 clang_annotateTokens(TU, tokens, num_tokens, cursors);
975 for (i = 0; i != num_tokens; ++i) {
976 const char *kind = "<unknown>";
977 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
978 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
979 unsigned start_line, start_column, end_line, end_column;
980
981 switch (clang_getTokenKind(tokens[i])) {
982 case CXToken_Punctuation: kind = "Punctuation"; break;
983 case CXToken_Keyword: kind = "Keyword"; break;
984 case CXToken_Identifier: kind = "Identifier"; break;
985 case CXToken_Literal: kind = "Literal"; break;
986 case CXToken_Comment: kind = "Comment"; break;
987 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000988 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000989 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000990 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000991 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000992 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
993 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000994 if (!clang_isInvalid(cursors[i].kind)) {
995 printf(" ");
996 PrintCursor(cursors[i]);
997 }
998 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000999 }
1000 free(cursors);
1001
1002 teardown:
1003 clang_disposeTranslationUnit(TU);
1004 clang_disposeIndex(CIdx);
1005 free(filename);
1006 free_remapped_files(unsaved_files, num_unsaved_files);
1007 return errorCode;
1008}
1009
Ted Kremenek0d435192009-11-17 18:13:31 +00001010/******************************************************************************/
1011/* Command line processing. */
1012/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001013
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001014static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001015 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001016 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001017 if (strcmp(s, "-usrs") == 0)
1018 return USRVisitor;
1019 return NULL;
1020}
1021
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001022static void print_usage(void) {
1023 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001024 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001025 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001026 " c-index-test -test-file-scan <AST file> <source file> "
1027 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001028 " c-index-test -test-load-tu <AST file> <symbol filter> "
1029 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001030 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1031 "[FileCheck prefix]\n"
1032 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001033 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001034 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001035 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1036 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1037 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001038 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001039 " all - load all symbols, including those from PCH\n"
1040 " local - load all symbols except those in PCH\n"
1041 " category - only load ObjC categories (non-PCH)\n"
1042 " interface - only load ObjC interfaces (non-PCH)\n"
1043 " protocol - only load ObjC protocols (non-PCH)\n"
1044 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001045 " typedef - only load typdefs (non-PCH)\n"
1046 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001047}
1048
1049int main(int argc, const char **argv) {
1050 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1051 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001052 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1053 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001054 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001055 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001056 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001057 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1058 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001059 }
1060 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001061 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001062 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001063 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001064 }
1065 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001066 return perform_file_scan(argv[2], argv[3],
1067 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001068 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1069 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001070 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1071 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1072 PrintInclusionStack);
1073 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1074 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1075 PrintInclusionStack);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001076
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001077 print_usage();
1078 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001079}