blob: b983d3a4de750ca3ec67f1747bd5eb77ed41c309 [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
Daniel Dunbar51b058c2010-02-14 08:32:24 +000031static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
32 unsigned end_line, unsigned end_column) {
33 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000034 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000035}
36
Ted Kremenek1c6da172009-11-17 19:37:36 +000037static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
38 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000039
Douglas Gregora88084b2010-02-18 18:08:43 +000040 *TU = clang_createTranslationUnit(Idx, file);
Ted Kremenek1c6da172009-11-17 19:37:36 +000041 if (!TU) {
42 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
43 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000044 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000045 return 1;
46}
47
Douglas Gregor4db64a42010-01-23 00:14:00 +000048void free_remapped_files(struct CXUnsavedFile *unsaved_files,
49 int num_unsaved_files) {
50 int i;
51 for (i = 0; i != num_unsaved_files; ++i) {
52 free((char *)unsaved_files[i].Filename);
53 free((char *)unsaved_files[i].Contents);
54 }
55}
56
57int parse_remapped_files(int argc, const char **argv, int start_arg,
58 struct CXUnsavedFile **unsaved_files,
59 int *num_unsaved_files) {
60 int i;
61 int arg;
62 int prefix_len = strlen("-remap-file=");
63 *unsaved_files = 0;
64 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000065
Douglas Gregor4db64a42010-01-23 00:14:00 +000066 /* Count the number of remapped files. */
67 for (arg = start_arg; arg < argc; ++arg) {
68 if (strncmp(argv[arg], "-remap-file=", prefix_len))
69 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000070
Douglas Gregor4db64a42010-01-23 00:14:00 +000071 ++*num_unsaved_files;
72 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000073
Douglas Gregor4db64a42010-01-23 00:14:00 +000074 if (*num_unsaved_files == 0)
75 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000076
Douglas Gregor4db64a42010-01-23 00:14:00 +000077 *unsaved_files
Ted Kremeneke68fff62010-02-17 00:41:32 +000078 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
Douglas Gregor4db64a42010-01-23 00:14:00 +000079 *num_unsaved_files);
80 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
81 struct CXUnsavedFile *unsaved = *unsaved_files + i;
82 const char *arg_string = argv[arg] + prefix_len;
83 int filename_len;
84 char *filename;
85 char *contents;
86 FILE *to_file;
87 const char *semi = strchr(arg_string, ';');
88 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000089 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +000090 "error: -remap-file=from;to argument is missing semicolon\n");
91 free_remapped_files(*unsaved_files, i);
92 *unsaved_files = 0;
93 *num_unsaved_files = 0;
94 return -1;
95 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000096
Douglas Gregor4db64a42010-01-23 00:14:00 +000097 /* Open the file that we're remapping to. */
98 to_file = fopen(semi + 1, "r");
99 if (!to_file) {
100 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
101 semi + 1);
102 free_remapped_files(*unsaved_files, i);
103 *unsaved_files = 0;
104 *num_unsaved_files = 0;
105 return -1;
106 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000107
Douglas Gregor4db64a42010-01-23 00:14:00 +0000108 /* Determine the length of the file we're remapping to. */
109 fseek(to_file, 0, SEEK_END);
110 unsaved->Length = ftell(to_file);
111 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000112
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113 /* Read the contents of the file we're remapping to. */
114 contents = (char *)malloc(unsaved->Length + 1);
115 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
116 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
117 (feof(to_file) ? "EOF" : "error"), semi + 1);
118 fclose(to_file);
119 free_remapped_files(*unsaved_files, i);
120 *unsaved_files = 0;
121 *num_unsaved_files = 0;
122 return -1;
123 }
124 contents[unsaved->Length] = 0;
125 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 /* Close the file. */
128 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000129
Douglas Gregor4db64a42010-01-23 00:14:00 +0000130 /* Copy the file name that we're remapping from. */
131 filename_len = semi - arg_string;
132 filename = (char *)malloc(filename_len + 1);
133 memcpy(filename, arg_string, filename_len);
134 filename[filename_len] = 0;
135 unsaved->Filename = filename;
136 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000137
Douglas Gregor4db64a42010-01-23 00:14:00 +0000138 return 0;
139}
140
Ted Kremenek0d435192009-11-17 18:13:31 +0000141/******************************************************************************/
142/* Pretty-printing. */
143/******************************************************************************/
144
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000145static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000146 if (clang_isInvalid(Cursor.kind)) {
147 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
148 printf("Invalid Cursor => %s", clang_getCString(ks));
149 clang_disposeString(ks);
150 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000151 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000152 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000153 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000154 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000155
156 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000157 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000158 printf("%s=%s", clang_getCString(ks),
159 clang_getCString(string));
160 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000161 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000162
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000163 Referenced = clang_getCursorReferenced(Cursor);
164 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
165 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000166 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000167 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000168 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000169
170 if (clang_isCursorDefinition(Cursor))
171 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000172 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000173}
Steve Naroff89922f82009-08-31 00:59:03 +0000174
Ted Kremeneke68fff62010-02-17 00:41:32 +0000175static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000176 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000177 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000178 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000179 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000180 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000181 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000182 clang_disposeString(source);
183 return "<invalid loc>";
184 }
185 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000186 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000187 clang_disposeString(source);
188 return b;
189 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000190}
191
Ted Kremenek0d435192009-11-17 18:13:31 +0000192/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000193/* Callbacks. */
194/******************************************************************************/
195
196typedef void (*PostVisitTU)(CXTranslationUnit);
197
Douglas Gregora88084b2010-02-18 18:08:43 +0000198void PrintDiagnostic(CXDiagnostic Diagnostic) {
199 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000200 CXFile file;
201 unsigned line, column;
202 CXString text;
203 enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000204
Douglas Gregor5352ac02010-01-28 00:27:43 +0000205 /* Ignore diagnostics that should be ignored. */
206 if (severity == CXDiagnostic_Ignored)
207 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000208
Douglas Gregor5352ac02010-01-28 00:27:43 +0000209 /* Print file:line:column. */
210 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
211 &file, &line, &column, 0);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000212 if (file) {
Douglas Gregora3890ba2010-02-08 23:11:56 +0000213 unsigned i, n;
Douglas Gregor51c6d382010-01-29 00:41:11 +0000214 unsigned printed_any_ranges = 0;
Ted Kremenek74844072010-02-17 00:41:20 +0000215 CXString fname;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000216
Ted Kremenek74844072010-02-17 00:41:20 +0000217 fname = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000218 fprintf(out, "%s:%d:%d:", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000219 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000220
Douglas Gregora3890ba2010-02-08 23:11:56 +0000221 n = clang_getDiagnosticNumRanges(Diagnostic);
222 for (i = 0; i != n; ++i) {
Douglas Gregor51c6d382010-01-29 00:41:11 +0000223 CXFile start_file, end_file;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000224 CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000225
Douglas Gregor51c6d382010-01-29 00:41:11 +0000226 unsigned start_line, start_column, end_line, end_column;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000227 clang_getInstantiationLocation(clang_getRangeStart(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000228 &start_file, &start_line, &start_column,0);
Douglas Gregora3890ba2010-02-08 23:11:56 +0000229 clang_getInstantiationLocation(clang_getRangeEnd(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000230 &end_file, &end_line, &end_column, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000231
Douglas Gregor51c6d382010-01-29 00:41:11 +0000232 if (start_file != end_file || start_file != file)
233 continue;
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000234
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000235 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000236 printed_any_ranges = 1;
237 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000238 if (printed_any_ranges)
239 fprintf(out, ":");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000240
Douglas Gregor51c6d382010-01-29 00:41:11 +0000241 fprintf(out, " ");
242 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000243
Douglas Gregor5352ac02010-01-28 00:27:43 +0000244 /* Print warning/error/etc. */
245 switch (severity) {
246 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
247 case CXDiagnostic_Note: fprintf(out, "note: "); break;
248 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
249 case CXDiagnostic_Error: fprintf(out, "error: "); break;
250 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
251 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000252
Douglas Gregor5352ac02010-01-28 00:27:43 +0000253 text = clang_getDiagnosticSpelling(Diagnostic);
254 if (clang_getCString(text))
255 fprintf(out, "%s\n", clang_getCString(text));
256 else
257 fprintf(out, "<no diagnostic text>\n");
258 clang_disposeString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000259
Douglas Gregor51c6d382010-01-29 00:41:11 +0000260 if (file) {
261 unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
262 for (i = 0; i != num_fixits; ++i) {
263 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
264 case CXFixIt_Insertion: {
265 CXSourceLocation insertion_loc;
266 CXFile insertion_file;
267 unsigned insertion_line, insertion_column;
268 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000269 clang_getInstantiationLocation(insertion_loc, &insertion_file,
Douglas Gregor51c6d382010-01-29 00:41:11 +0000270 &insertion_line, &insertion_column, 0);
271 if (insertion_file == file)
272 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
273 clang_getCString(text), insertion_line, insertion_column);
274 clang_disposeString(text);
275 break;
276 }
277
278 case CXFixIt_Removal: {
279 CXFile start_file, end_file;
280 unsigned start_line, start_column, end_line, end_column;
281 CXSourceRange remove_range
282 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
283 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
284 &start_file, &start_line, &start_column,
285 0);
286 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
287 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000288 if (start_file == file && end_file == file) {
289 fprintf(out, "FIX-IT: Remove ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000290 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000291 fprintf(out, "\n");
292 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000293 break;
294 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000295
Douglas Gregor51c6d382010-01-29 00:41:11 +0000296 case CXFixIt_Replacement: {
297 CXFile start_file, end_file;
298 unsigned start_line, start_column, end_line, end_column;
299 CXSourceRange remove_range;
300 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
301 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
302 &start_file, &start_line, &start_column,
303 0);
304 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
305 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000306 if (start_file == end_file) {
307 fprintf(out, "FIX-IT: Replace ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000308 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000309 fprintf(out, " with \"%s\"\n", clang_getCString(text));
310 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000311 clang_disposeString(text);
312 break;
313 }
314 }
315 }
316 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000317}
318
Douglas Gregora88084b2010-02-18 18:08:43 +0000319void PrintDiagnostics(CXTranslationUnit TU) {
320 int i, n = clang_getNumDiagnostics(TU);
321 for (i = 0; i != n; ++i) {
322 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
323 PrintDiagnostic(Diag);
324 clang_disposeDiagnostic(Diag);
325 }
326}
327
Ted Kremenekce2ae882010-01-26 17:59:48 +0000328/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000329/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000330/******************************************************************************/
331
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000332static const char *FileCheckPrefix = "CHECK";
333
Douglas Gregora7bde202010-01-19 00:34:46 +0000334static void PrintCursorExtent(CXCursor C) {
335 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000336 CXFile begin_file, end_file;
337 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000338
Douglas Gregor1db19de2010-01-19 21:36:55 +0000339 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000340 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000341 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000342 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000343 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000344 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000346 printf(" Extent=");
347 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000348}
349
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000350/* Data used by all of the visitors. */
351typedef struct {
352 CXTranslationUnit TU;
353 enum CXCursorKind *Filter;
354} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000355
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000356
Ted Kremeneke68fff62010-02-17 00:41:32 +0000357enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000358 CXCursor Parent,
359 CXClientData ClientData) {
360 VisitorData *Data = (VisitorData *)ClientData;
361 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000362 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000363 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000364 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000365 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000366 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000367 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000368 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000369 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000370 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000371 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000372
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000373 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000374}
Steve Naroff50398192009-08-28 15:28:48 +0000375
Ted Kremeneke68fff62010-02-17 00:41:32 +0000376static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000377 CXCursor Parent,
378 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000379 const char *startBuf, *endBuf;
380 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
381 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000382 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000383
Douglas Gregorb6998662010-01-19 19:34:47 +0000384 if (Cursor.kind != CXCursor_FunctionDecl ||
385 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000386 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000387
388 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
389 &startLine, &startColumn,
390 &endLine, &endColumn);
391 /* Probe the entire body, looking for both decls and refs. */
392 curLine = startLine;
393 curColumn = startColumn;
394
395 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000396 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000397 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000398 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000399
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000400 if (*startBuf == '\n') {
401 startBuf++;
402 curLine++;
403 curColumn = 1;
404 } else if (*startBuf != '\t')
405 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000406
Douglas Gregor98258af2010-01-18 22:46:11 +0000407 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000408 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000409
Douglas Gregor1db19de2010-01-19 21:36:55 +0000410 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000411 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000412 CXSourceLocation RefLoc
413 = clang_getLocation(Data->TU, file, curLine, curColumn);
414 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000415 if (Ref.kind == CXCursor_NoDeclFound) {
416 /* Nothing found here; that's fine. */
417 } else if (Ref.kind != CXCursor_FunctionDecl) {
418 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
419 curLine, curColumn);
420 PrintCursor(Ref);
421 printf("\n");
422 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000423 }
Ted Kremenek74844072010-02-17 00:41:20 +0000424 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000425 startBuf++;
426 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000427
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000428 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000429}
430
Ted Kremenek7d405622010-01-12 23:34:26 +0000431/******************************************************************************/
432/* USR testing. */
433/******************************************************************************/
434
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000435enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
436 CXClientData ClientData) {
437 VisitorData *Data = (VisitorData *)ClientData;
438 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000439 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000440 if (!clang_getCString(USR)) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000441 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000442 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000443 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000444 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C),
445 clang_getCString(USR));
Douglas Gregora7bde202010-01-19 00:34:46 +0000446 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000447 printf("\n");
448 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000449
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000450 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000451 }
452
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000453 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000454}
455
456/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000457/* Inclusion stack testing. */
458/******************************************************************************/
459
460void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
461 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000462
Ted Kremenek16b55a72010-01-26 19:31:51 +0000463 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000464 CXString fname;
465
466 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000467 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000468 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000469
Ted Kremenek16b55a72010-01-26 19:31:51 +0000470 for (i = 0; i < includeStackLen; ++i) {
471 CXFile includingFile;
472 unsigned line, column;
473 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
474 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000475 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000476 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000477 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000478 }
479 printf("\n");
480}
481
482void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000483 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000484}
485
486/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000487/* Loading ASTs/source. */
488/******************************************************************************/
489
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000490static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000491 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000492 CXCursorVisitor Visitor,
493 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000494
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000495 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000496 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000497
498 if (Visitor) {
499 enum CXCursorKind K = CXCursor_NotImplemented;
500 enum CXCursorKind *ck = &K;
501 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000502
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000503 /* Perform some simple filtering. */
504 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000505 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000506 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
507 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
508 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
509 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
510 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
511 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
512 else {
513 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
514 return 1;
515 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000516
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000517 Data.TU = TU;
518 Data.Filter = ck;
519 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000520 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000521
Ted Kremenekce2ae882010-01-26 17:59:48 +0000522 if (PV)
523 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000524
Douglas Gregora88084b2010-02-18 18:08:43 +0000525 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000526 clang_disposeTranslationUnit(TU);
527 return 0;
528}
529
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000530int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000531 const char *prefix, CXCursorVisitor Visitor,
532 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000533 CXIndex Idx;
534 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000535 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000536 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000537 !strcmp(filter, "local") ? 1 : 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000538
Ted Kremenek020a0952010-02-11 07:41:25 +0000539 if (!CreateTranslationUnit(Idx, file, &TU)) {
540 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000541 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000542 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000543
Ted Kremenek020a0952010-02-11 07:41:25 +0000544 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
545 clang_disposeIndex(Idx);
546 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000547}
548
Ted Kremenekce2ae882010-01-26 17:59:48 +0000549int perform_test_load_source(int argc, const char **argv,
550 const char *filter, CXCursorVisitor Visitor,
551 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000552 const char *UseExternalASTs =
553 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000554 CXIndex Idx;
555 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000556 struct CXUnsavedFile *unsaved_files = 0;
557 int num_unsaved_files = 0;
558 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000559
Daniel Dunbarada487d2009-12-01 02:03:10 +0000560 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000561 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000562
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000563 if (UseExternalASTs && strlen(UseExternalASTs))
564 clang_setUseExternalASTGeneration(Idx, 1);
565
Ted Kremenek020a0952010-02-11 07:41:25 +0000566 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
567 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000568 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000569 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000570
Ted Kremeneke68fff62010-02-17 00:41:32 +0000571 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
572 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000573 argv + num_unsaved_files,
574 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000575 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000576 if (!TU) {
577 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000578 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000579 return 1;
580 }
581
Ted Kremenekce2ae882010-01-26 17:59:48 +0000582 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000583 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000584 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000585 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000586}
587
Ted Kremenek0d435192009-11-17 18:13:31 +0000588/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000589/* Logic for testing clang_getCursor(). */
590/******************************************************************************/
591
592static void print_cursor_file_scan(CXCursor cursor,
593 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000594 unsigned end_line, unsigned end_col,
595 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000596 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000597 if (prefix)
598 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000599 PrintExtent(stdout, start_line, start_col, end_line, end_col);
600 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000601 PrintCursor(cursor);
602 printf("\n");
603}
604
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000605static int perform_file_scan(const char *ast_file, const char *source_file,
606 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000607 CXIndex Idx;
608 CXTranslationUnit TU;
609 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000610 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000611 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000612 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000613 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000614
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000615 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000616 fprintf(stderr, "Could not create Index\n");
617 return 1;
618 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000619
Ted Kremenek1c6da172009-11-17 19:37:36 +0000620 if (!CreateTranslationUnit(Idx, ast_file, &TU))
621 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000622
Ted Kremenek1c6da172009-11-17 19:37:36 +0000623 if ((fp = fopen(source_file, "r")) == NULL) {
624 fprintf(stderr, "Could not open '%s'\n", source_file);
625 return 1;
626 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000627
Douglas Gregorb9790342010-01-22 21:44:22 +0000628 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000629 for (;;) {
630 CXCursor cursor;
631 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000632
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000633 if (c == '\n') {
634 ++line;
635 col = 1;
636 } else
637 ++col;
638
639 /* Check the cursor at this position, and dump the previous one if we have
640 * found something new.
641 */
642 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
643 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
644 prevCursor.kind != CXCursor_InvalidFile) {
645 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000646 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000647 start_line = line;
648 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000649 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000650 if (c == EOF)
651 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000652
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000653 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000654 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000655
Ted Kremenek1c6da172009-11-17 19:37:36 +0000656 fclose(fp);
657 return 0;
658}
659
660/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000661/* Logic for testing clang_codeComplete(). */
662/******************************************************************************/
663
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000664/* Parse file:line:column from the input string. Returns 0 on success, non-zero
665 on failure. If successful, the pointer *filename will contain newly-allocated
666 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000667int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000668 unsigned *column, unsigned *second_line,
669 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000670 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000671 const char *last_colon = strrchr(input, ':');
672 unsigned values[4], i;
673 unsigned num_values = (second_line && second_column)? 4 : 2;
674
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000675 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000676 if (!last_colon || last_colon == input) {
677 if (num_values == 4)
678 fprintf(stderr, "could not parse filename:line:column:line:column in "
679 "'%s'\n", input);
680 else
681 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000682 return 1;
683 }
684
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000685 for (i = 0; i != num_values; ++i) {
686 const char *prev_colon;
687
688 /* Parse the next line or column. */
689 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
690 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000691 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000692 (i % 2 ? "column" : "line"), input);
693 return 1;
694 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000695
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000696 if (i + 1 == num_values)
697 break;
698
699 /* Find the previous colon. */
700 prev_colon = last_colon - 1;
701 while (prev_colon != input && *prev_colon != ':')
702 --prev_colon;
703 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000704 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000705 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000706 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000707 }
708
709 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000710 }
711
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000712 *line = values[0];
713 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000714
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000715 if (second_line && second_column) {
716 *second_line = values[2];
717 *second_column = values[3];
718 }
719
Douglas Gregor88d23952009-11-09 18:19:57 +0000720 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000721 *filename = (char*)malloc(last_colon - input + 1);
722 memcpy(*filename, input, last_colon - input);
723 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000724 return 0;
725}
726
727const char *
728clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
729 switch (Kind) {
730 case CXCompletionChunk_Optional: return "Optional";
731 case CXCompletionChunk_TypedText: return "TypedText";
732 case CXCompletionChunk_Text: return "Text";
733 case CXCompletionChunk_Placeholder: return "Placeholder";
734 case CXCompletionChunk_Informative: return "Informative";
735 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
736 case CXCompletionChunk_LeftParen: return "LeftParen";
737 case CXCompletionChunk_RightParen: return "RightParen";
738 case CXCompletionChunk_LeftBracket: return "LeftBracket";
739 case CXCompletionChunk_RightBracket: return "RightBracket";
740 case CXCompletionChunk_LeftBrace: return "LeftBrace";
741 case CXCompletionChunk_RightBrace: return "RightBrace";
742 case CXCompletionChunk_LeftAngle: return "LeftAngle";
743 case CXCompletionChunk_RightAngle: return "RightAngle";
744 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000745 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000746 case CXCompletionChunk_Colon: return "Colon";
747 case CXCompletionChunk_SemiColon: return "SemiColon";
748 case CXCompletionChunk_Equal: return "Equal";
749 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
750 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000751 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000752
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000753 return "Unknown";
754}
755
Douglas Gregor3ac73852009-11-09 16:04:45 +0000756void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000757 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000758
Douglas Gregor3ac73852009-11-09 16:04:45 +0000759 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000760 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000761 CXString text;
762 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000763 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000764 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000765
Douglas Gregor3ac73852009-11-09 16:04:45 +0000766 if (Kind == CXCompletionChunk_Optional) {
767 fprintf(file, "{Optional ");
768 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000769 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000770 file);
771 fprintf(file, "}");
772 continue;
773 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000774
Douglas Gregord5a20892009-11-09 17:05:28 +0000775 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000776 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000777 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000778 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000779 cstr ? cstr : "");
780 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000781 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000782
Douglas Gregor3ac73852009-11-09 16:04:45 +0000783}
784
785void print_completion_result(CXCompletionResult *completion_result,
786 CXClientData client_data) {
787 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000788 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
789
790 fprintf(file, "%s:", clang_getCString(ks));
791 clang_disposeString(ks);
792
Douglas Gregor3ac73852009-11-09 16:04:45 +0000793 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000794 fprintf(file, "\n");
795}
796
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000797int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000798 const char *input = argv[1];
799 char *filename = 0;
800 unsigned line;
801 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000802 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000803 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000804 struct CXUnsavedFile *unsaved_files = 0;
805 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000806 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000807
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000808 input += strlen("-code-completion-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000809 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000810 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000811 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000812
Douglas Gregor735df882009-12-02 09:21:34 +0000813 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
814 return -1;
815
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000816 CIdx = clang_createIndex(0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000817 results = clang_codeComplete(CIdx,
818 argv[argc - 1], argc - num_unsaved_files - 3,
819 argv + num_unsaved_files + 2,
Douglas Gregorec6762c2009-12-18 16:20:58 +0000820 num_unsaved_files, unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000821 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000822
Douglas Gregorec6762c2009-12-18 16:20:58 +0000823 if (results) {
824 unsigned i, n = results->NumResults;
825 for (i = 0; i != n; ++i)
826 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000827 n = clang_codeCompleteGetNumDiagnostics(results);
828 for (i = 0; i != n; ++i) {
829 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
830 PrintDiagnostic(diag);
831 clang_disposeDiagnostic(diag);
832 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000833 clang_disposeCodeCompleteResults(results);
834 }
835
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000836 clang_disposeIndex(CIdx);
837 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000838
Douglas Gregor735df882009-12-02 09:21:34 +0000839 free_remapped_files(unsaved_files, num_unsaved_files);
840
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000841 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000842}
843
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000844typedef struct {
845 char *filename;
846 unsigned line;
847 unsigned column;
848} CursorSourceLocation;
849
850int inspect_cursor_at(int argc, const char **argv) {
851 CXIndex CIdx;
852 int errorCode;
853 struct CXUnsavedFile *unsaved_files = 0;
854 int num_unsaved_files = 0;
855 CXTranslationUnit TU;
856 CXCursor Cursor;
857 CursorSourceLocation *Locations = 0;
858 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000859
Ted Kremeneke68fff62010-02-17 00:41:32 +0000860 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000861 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
862 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000863
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000864 /* Parse the locations. */
865 assert(NumLocations > 0 && "Unable to count locations?");
866 Locations = (CursorSourceLocation *)malloc(
867 NumLocations * sizeof(CursorSourceLocation));
868 for (Loc = 0; Loc < NumLocations; ++Loc) {
869 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000870 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
871 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000872 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000873 return errorCode;
874 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875
876 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000877 &num_unsaved_files))
878 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000879
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000880 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000881 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
882 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000883 argv + num_unsaved_files + 1 + NumLocations,
884 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000885 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000886 if (!TU) {
887 fprintf(stderr, "unable to parse input\n");
888 return -1;
889 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000890
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000891 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000892 CXFile file = clang_getFile(TU, Locations[Loc].filename);
893 if (!file)
894 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000895
896 Cursor = clang_getCursor(TU,
897 clang_getLocation(TU, file, Locations[Loc].line,
898 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000899 PrintCursor(Cursor);
900 printf("\n");
901 free(Locations[Loc].filename);
902 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000903
Douglas Gregora88084b2010-02-18 18:08:43 +0000904 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000905 clang_disposeTranslationUnit(TU);
906 clang_disposeIndex(CIdx);
907 free(Locations);
908 free_remapped_files(unsaved_files, num_unsaved_files);
909 return 0;
910}
911
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000912int perform_token_annotation(int argc, const char **argv) {
913 const char *input = argv[1];
914 char *filename = 0;
915 unsigned line, second_line;
916 unsigned column, second_column;
917 CXIndex CIdx;
918 CXTranslationUnit TU = 0;
919 int errorCode;
920 struct CXUnsavedFile *unsaved_files = 0;
921 int num_unsaved_files = 0;
922 CXToken *tokens;
923 unsigned num_tokens;
924 CXSourceRange range;
925 CXSourceLocation startLoc, endLoc;
926 CXFile file = 0;
927 CXCursor *cursors = 0;
928 unsigned i;
929
930 input += strlen("-test-annotate-tokens=");
931 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
932 &second_line, &second_column)))
933 return errorCode;
934
935 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
936 return -1;
937
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000938 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000939 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
940 argc - num_unsaved_files - 3,
941 argv + num_unsaved_files + 2,
942 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000943 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000944 if (!TU) {
945 fprintf(stderr, "unable to parse input\n");
946 clang_disposeIndex(CIdx);
947 free(filename);
948 free_remapped_files(unsaved_files, num_unsaved_files);
949 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000950 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 errorCode = 0;
952
953 file = clang_getFile(TU, filename);
954 if (!file) {
955 fprintf(stderr, "file %s is not in this translation unit\n", filename);
956 errorCode = -1;
957 goto teardown;
958 }
959
960 startLoc = clang_getLocation(TU, file, line, column);
961 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000963 column);
964 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000965 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000966 }
967
968 endLoc = clang_getLocation(TU, file, second_line, second_column);
969 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000970 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000971 second_line, second_column);
972 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000973 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000974 }
975
976 range = clang_getRange(startLoc, endLoc);
977 clang_tokenize(TU, range, &tokens, &num_tokens);
978 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
979 clang_annotateTokens(TU, tokens, num_tokens, cursors);
980 for (i = 0; i != num_tokens; ++i) {
981 const char *kind = "<unknown>";
982 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
983 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
984 unsigned start_line, start_column, end_line, end_column;
985
986 switch (clang_getTokenKind(tokens[i])) {
987 case CXToken_Punctuation: kind = "Punctuation"; break;
988 case CXToken_Keyword: kind = "Keyword"; break;
989 case CXToken_Identifier: kind = "Identifier"; break;
990 case CXToken_Literal: kind = "Literal"; break;
991 case CXToken_Comment: kind = "Comment"; break;
992 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000993 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000994 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000995 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000996 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000997 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
998 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000999 if (!clang_isInvalid(cursors[i].kind)) {
1000 printf(" ");
1001 PrintCursor(cursors[i]);
1002 }
1003 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001004 }
1005 free(cursors);
1006
1007 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001008 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001009 clang_disposeTranslationUnit(TU);
1010 clang_disposeIndex(CIdx);
1011 free(filename);
1012 free_remapped_files(unsaved_files, num_unsaved_files);
1013 return errorCode;
1014}
1015
Ted Kremenek0d435192009-11-17 18:13:31 +00001016/******************************************************************************/
1017/* Command line processing. */
1018/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001019
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001020static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001021 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001022 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001023 if (strcmp(s, "-usrs") == 0)
1024 return USRVisitor;
1025 return NULL;
1026}
1027
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001028static void print_usage(void) {
1029 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001030 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001031 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001032 " c-index-test -test-file-scan <AST file> <source file> "
1033 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001034 " c-index-test -test-load-tu <AST file> <symbol filter> "
1035 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001036 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1037 "[FileCheck prefix]\n"
1038 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001039 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001040 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001041 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1042 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1043 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001044 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001045 " all - load all symbols, including those from PCH\n"
1046 " local - load all symbols except those in PCH\n"
1047 " category - only load ObjC categories (non-PCH)\n"
1048 " interface - only load ObjC interfaces (non-PCH)\n"
1049 " protocol - only load ObjC protocols (non-PCH)\n"
1050 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001051 " typedef - only load typdefs (non-PCH)\n"
1052 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001053}
1054
1055int main(int argc, const char **argv) {
1056 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1057 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001058 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1059 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001060 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001061 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001062 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001063 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1064 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001065 }
1066 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001067 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001068 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001069 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001070 }
1071 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001072 return perform_file_scan(argv[2], argv[3],
1073 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001074 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1075 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001076 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1077 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1078 PrintInclusionStack);
1079 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1080 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1081 PrintInclusionStack);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001082
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001083 print_usage();
1084 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001085}