blob: 334c7891f7d3a70b4606ac25b0b9e49e41e6a2a8 [file] [log] [blame]
Steve Naroff69b10fd2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroffa1c72842009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor9eb77012009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff1054e602009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +00006#include <string.h>
Douglas Gregor082c3e62010-01-15 19:40:17 +00007#include <assert.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +00008
Ted Kremenek1cd27d52009-11-17 18:13:31 +00009/******************************************************************************/
10/* Utility functions. */
11/******************************************************************************/
12
John Thompsonde258b52009-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 Naroffa7753c42009-09-24 20:03:06 +000028extern char *basename(const char *);
John Thompsonde258b52009-10-27 13:42:56 +000029#endif
Steve Naroffa7753c42009-09-24 20:03:06 +000030
Douglas Gregor4f9c3762010-01-28 00:27:43 +000031static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
32 CXClientData ClientData);
33
Daniel Dunbar98c07e02010-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 Dunbar02968e52010-02-14 10:02:57 +000038 end_line, end_column);
Daniel Dunbar98c07e02010-02-14 08:32:24 +000039}
40
Ted Kremenek2df52dc2009-11-17 19:37:36 +000041static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
42 CXTranslationUnit *TU) {
43
Daniel Dunbarce79bc9c2010-02-16 01:55:10 +000044 *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, stderr);
Ted Kremenek2df52dc2009-11-17 19:37:36 +000045 if (!TU) {
46 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
47 return 0;
48 }
49 return 1;
50}
51
Douglas Gregoraa98ed92010-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;
69
70 /* 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;
74
75 ++*num_unsaved_files;
76 }
77
78 if (*num_unsaved_files == 0)
79 return 0;
80
81 *unsaved_files
82 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
83 *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) {
93 fprintf(stderr,
94 "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 }
100
101 /* 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 }
111
112 /* 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);
116
117 /* 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;
130
131 /* Close the file. */
132 fclose(to_file);
133
134 /* 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 }
141
142 return 0;
143}
144
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000145/******************************************************************************/
146/* Pretty-printing. */
147/******************************************************************************/
148
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000149static void PrintCursor(CXCursor Cursor) {
Steve Naroff54f22fb2009-09-15 20:25:34 +0000150 if (clang_isInvalid(Cursor.kind))
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000151 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff63f475a2009-09-25 21:32:34 +0000152 else {
Steve Naroff8675d5c2009-11-09 17:45:52 +0000153 CXString string;
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000154 CXCursor Referenced;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000155 unsigned line, column;
Steve Naroff8675d5c2009-11-09 17:45:52 +0000156 string = clang_getCursorSpelling(Cursor);
Steve Naroffa7753c42009-09-24 20:03:06 +0000157 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroff8675d5c2009-11-09 17:45:52 +0000158 clang_getCString(string));
159 clang_disposeString(string);
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000160
161 Referenced = clang_getCursorReferenced(Cursor);
162 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
163 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000164 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000165 printf(":%d:%d", line, column);
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000166 }
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000167
168 if (clang_isCursorDefinition(Cursor))
169 printf(" (Definition)");
Steve Naroff63f475a2009-09-25 21:32:34 +0000170 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000171}
Steve Naroff1054e602009-08-31 00:59:03 +0000172
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000173static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor4f46e782010-01-19 21:36:55 +0000174 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekc560b682010-02-17 00:41:20 +0000175 CXString source;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000176 CXFile file;
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000177 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000178 source = clang_getFileName(file);
Ted Kremenekc560b682010-02-17 00:41:20 +0000179 if (!source.Spelling) {
180 clang_disposeString(source);
181 return "<invalid loc>";
182 }
183 else {
184 const char *b = basename(source.Spelling);
185 clang_disposeString(source);
186 return b;
187 }
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000188}
189
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000190/******************************************************************************/
Ted Kremenekb478ff42010-01-26 17:59:48 +0000191/* Callbacks. */
192/******************************************************************************/
193
194typedef void (*PostVisitTU)(CXTranslationUnit);
195
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000196static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
197 CXClientData ClientData) {
198 FILE *out = (FILE *)ClientData;
199 CXFile file;
200 unsigned line, column;
201 CXString text;
202 enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
203
204 /* Ignore diagnostics that should be ignored. */
205 if (severity == CXDiagnostic_Ignored)
206 return;
207
208 /* Print file:line:column. */
209 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
210 &file, &line, &column, 0);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000211 if (file) {
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000212 unsigned i, n;
Douglas Gregor60b11f62010-01-29 00:41:11 +0000213 unsigned printed_any_ranges = 0;
Ted Kremenekc560b682010-02-17 00:41:20 +0000214 CXString fname;
Douglas Gregor60b11f62010-01-29 00:41:11 +0000215
Ted Kremenekc560b682010-02-17 00:41:20 +0000216 fname = clang_getFileName(file);
217 fprintf(out, "%s:%d:%d:", fname.Spelling, line, column);
218 clang_disposeString(fname);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000219
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000220 n = clang_getDiagnosticNumRanges(Diagnostic);
221 for (i = 0; i != n; ++i) {
Douglas Gregor60b11f62010-01-29 00:41:11 +0000222 CXFile start_file, end_file;
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000223 CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
224
Douglas Gregor60b11f62010-01-29 00:41:11 +0000225 unsigned start_line, start_column, end_line, end_column;
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000226 clang_getInstantiationLocation(clang_getRangeStart(range),
Douglas Gregor60b11f62010-01-29 00:41:11 +0000227 &start_file, &start_line, &start_column,0);
Douglas Gregor4b8fd6d2010-02-08 23:11:56 +0000228 clang_getInstantiationLocation(clang_getRangeEnd(range),
Douglas Gregor60b11f62010-01-29 00:41:11 +0000229 &end_file, &end_line, &end_column, 0);
230
231 if (start_file != end_file || start_file != file)
232 continue;
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000233
Daniel Dunbar6092d502010-02-14 08:32:51 +0000234 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000235 printed_any_ranges = 1;
236 }
Douglas Gregor60b11f62010-01-29 00:41:11 +0000237 if (printed_any_ranges)
238 fprintf(out, ":");
239
240 fprintf(out, " ");
241 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000242
243 /* Print warning/error/etc. */
244 switch (severity) {
245 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
246 case CXDiagnostic_Note: fprintf(out, "note: "); break;
247 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
248 case CXDiagnostic_Error: fprintf(out, "error: "); break;
249 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
250 }
251
252 text = clang_getDiagnosticSpelling(Diagnostic);
253 if (clang_getCString(text))
254 fprintf(out, "%s\n", clang_getCString(text));
255 else
256 fprintf(out, "<no diagnostic text>\n");
257 clang_disposeString(text);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000258
259 if (file) {
260 unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
261 for (i = 0; i != num_fixits; ++i) {
262 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
263 case CXFixIt_Insertion: {
264 CXSourceLocation insertion_loc;
265 CXFile insertion_file;
266 unsigned insertion_line, insertion_column;
267 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
268 clang_getInstantiationLocation(insertion_loc, &insertion_file,
269 &insertion_line, &insertion_column, 0);
270 if (insertion_file == file)
271 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
272 clang_getCString(text), insertion_line, insertion_column);
273 clang_disposeString(text);
274 break;
275 }
276
277 case CXFixIt_Removal: {
278 CXFile start_file, end_file;
279 unsigned start_line, start_column, end_line, end_column;
280 CXSourceRange remove_range
281 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
282 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
283 &start_file, &start_line, &start_column,
284 0);
285 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
286 &end_file, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000287 if (start_file == file && end_file == file) {
288 fprintf(out, "FIX-IT: Remove ");
Daniel Dunbar6092d502010-02-14 08:32:51 +0000289 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000290 fprintf(out, "\n");
291 }
Douglas Gregor60b11f62010-01-29 00:41:11 +0000292 break;
293 }
294
295 case CXFixIt_Replacement: {
296 CXFile start_file, end_file;
297 unsigned start_line, start_column, end_line, end_column;
298 CXSourceRange remove_range;
299 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
300 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
301 &start_file, &start_line, &start_column,
302 0);
303 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
304 &end_file, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000305 if (start_file == end_file) {
306 fprintf(out, "FIX-IT: Replace ");
Daniel Dunbar6092d502010-02-14 08:32:51 +0000307 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000308 fprintf(out, " with \"%s\"\n", clang_getCString(text));
309 }
Douglas Gregor60b11f62010-01-29 00:41:11 +0000310 clang_disposeString(text);
311 break;
312 }
313 }
314 }
315 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000316}
317
Ted Kremenekb478ff42010-01-26 17:59:48 +0000318/******************************************************************************/
Douglas Gregor720d0052010-01-20 21:32:04 +0000319/* Logic for testing traversal. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000320/******************************************************************************/
321
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000322static const char *FileCheckPrefix = "CHECK";
323
Douglas Gregor33c34ac2010-01-19 00:34:46 +0000324static void PrintCursorExtent(CXCursor C) {
325 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000326 CXFile begin_file, end_file;
327 unsigned begin_line, begin_column, end_line, end_column;
328
329 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000330 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000331 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000332 &end_file, &end_line, &end_column, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000333 if (!begin_file || !end_file)
Ted Kremenek9cec0002010-01-16 01:44:12 +0000334 return;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000335
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000336 printf(" Extent=");
337 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000338}
339
Douglas Gregor720d0052010-01-20 21:32:04 +0000340/* Data used by all of the visitors. */
341typedef struct {
342 CXTranslationUnit TU;
343 enum CXCursorKind *Filter;
344} VisitorData;
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000345
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000346
Douglas Gregor720d0052010-01-20 21:32:04 +0000347enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
348 CXCursor Parent,
349 CXClientData ClientData) {
350 VisitorData *Data = (VisitorData *)ClientData;
351 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor66a58812010-01-18 22:46:11 +0000352 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000353 unsigned line, column;
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000354 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000355 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor4f46e782010-01-19 21:36:55 +0000356 GetCursorSource(Cursor), line, column);
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000357 PrintCursor(Cursor);
Douglas Gregor33c34ac2010-01-19 00:34:46 +0000358 PrintCursorExtent(Cursor);
Ted Kremenek9cec0002010-01-16 01:44:12 +0000359 printf("\n");
Douglas Gregor720d0052010-01-20 21:32:04 +0000360 return CXChildVisit_Recurse;
Steve Naroff772c1a42009-08-31 14:26:51 +0000361 }
Douglas Gregor720d0052010-01-20 21:32:04 +0000362
363 return CXChildVisit_Continue;
Steve Naroff1054e602009-08-31 00:59:03 +0000364}
Steve Naroffa1c72842009-08-28 15:28:48 +0000365
Douglas Gregor720d0052010-01-20 21:32:04 +0000366static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
367 CXCursor Parent,
368 CXClientData ClientData) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000369 const char *startBuf, *endBuf;
370 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
371 CXCursor Ref;
Douglas Gregor720d0052010-01-20 21:32:04 +0000372 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000373
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000374 if (Cursor.kind != CXCursor_FunctionDecl ||
375 !clang_isCursorDefinition(Cursor))
Douglas Gregor720d0052010-01-20 21:32:04 +0000376 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000377
378 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
379 &startLine, &startColumn,
380 &endLine, &endColumn);
381 /* Probe the entire body, looking for both decls and refs. */
382 curLine = startLine;
383 curColumn = startColumn;
384
385 while (startBuf < endBuf) {
Douglas Gregor66a58812010-01-18 22:46:11 +0000386 CXSourceLocation Loc;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000387 CXFile file;
Ted Kremenekc560b682010-02-17 00:41:20 +0000388 CXString source;
Douglas Gregor66a58812010-01-18 22:46:11 +0000389
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000390 if (*startBuf == '\n') {
391 startBuf++;
392 curLine++;
393 curColumn = 1;
394 } else if (*startBuf != '\t')
395 curColumn++;
396
Douglas Gregor66a58812010-01-18 22:46:11 +0000397 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000398 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +0000399
Douglas Gregor4f46e782010-01-19 21:36:55 +0000400 source = clang_getFileName(file);
Ted Kremenekc560b682010-02-17 00:41:20 +0000401 if (source.Spelling) {
Douglas Gregor816fd362010-01-22 21:44:22 +0000402 CXSourceLocation RefLoc
403 = clang_getLocation(Data->TU, file, curLine, curColumn);
404 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor66a58812010-01-18 22:46:11 +0000405 if (Ref.kind == CXCursor_NoDeclFound) {
406 /* Nothing found here; that's fine. */
407 } else if (Ref.kind != CXCursor_FunctionDecl) {
408 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
409 curLine, curColumn);
410 PrintCursor(Ref);
411 printf("\n");
412 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000413 }
Ted Kremenekc560b682010-02-17 00:41:20 +0000414 clang_disposeString(source);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000415 startBuf++;
416 }
Douglas Gregor720d0052010-01-20 21:32:04 +0000417
418 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000419}
420
Ted Kremenek58a6a8e2010-01-12 23:34:26 +0000421/******************************************************************************/
422/* USR testing. */
423/******************************************************************************/
424
Douglas Gregor720d0052010-01-20 21:32:04 +0000425enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
426 CXClientData ClientData) {
427 VisitorData *Data = (VisitorData *)ClientData;
428 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenek473c7a72010-01-18 20:23:29 +0000429 CXString USR = clang_getCursorUSR(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +0000430 if (!USR.Spelling) {
431 clang_disposeString(USR);
Douglas Gregor720d0052010-01-20 21:32:04 +0000432 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +0000433 }
434 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregor33c34ac2010-01-19 00:34:46 +0000435 PrintCursorExtent(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +0000436 printf("\n");
437 clang_disposeString(USR);
Douglas Gregor720d0052010-01-20 21:32:04 +0000438
439 return CXChildVisit_Recurse;
440 }
441
442 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +0000443}
444
445/******************************************************************************/
Ted Kremenek0b86e3a2010-01-26 19:31:51 +0000446/* Inclusion stack testing. */
447/******************************************************************************/
448
449void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
450 unsigned includeStackLen, CXClientData data) {
451
452 unsigned i;
Ted Kremenekc560b682010-02-17 00:41:20 +0000453 CXString fname;
454
455 fname = clang_getFileName(includedFile);
456 printf("file: %s\nincluded by:\n", fname.Spelling);
457 clang_disposeString(fname);
458
Ted Kremenek0b86e3a2010-01-26 19:31:51 +0000459 for (i = 0; i < includeStackLen; ++i) {
460 CXFile includingFile;
461 unsigned line, column;
462 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
463 &column, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +0000464 fname = clang_getFileName(includingFile);
465 printf(" %s:%d:%d\n", fname.Spelling, line, column);
466 clang_disposeString(fname);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +0000467 }
468 printf("\n");
469}
470
471void PrintInclusionStack(CXTranslationUnit TU) {
472 clang_getInclusions(TU, InclusionVisitor, NULL);
473}
474
475/******************************************************************************/
Ted Kremenek58a6a8e2010-01-12 23:34:26 +0000476/* Loading ASTs/source. */
477/******************************************************************************/
478
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000479static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek73eccd22010-01-12 18:53:15 +0000480 const char *filter, const char *prefix,
Ted Kremenekb478ff42010-01-26 17:59:48 +0000481 CXCursorVisitor Visitor,
482 PostVisitTU PV) {
Douglas Gregor720d0052010-01-20 21:32:04 +0000483
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000484 if (prefix)
485 FileCheckPrefix = prefix;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +0000486
487 if (Visitor) {
488 enum CXCursorKind K = CXCursor_NotImplemented;
489 enum CXCursorKind *ck = &K;
490 VisitorData Data;
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000491
Ted Kremeneka97a5cd2010-01-26 17:55:33 +0000492 /* Perform some simple filtering. */
493 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbard64ce7b2010-02-10 20:42:40 +0000494 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +0000495 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
496 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
497 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
498 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
499 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
500 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
501 else {
502 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
503 return 1;
504 }
505
506 Data.TU = TU;
507 Data.Filter = ck;
508 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000509 }
Ted Kremenekb478ff42010-01-26 17:59:48 +0000510
511 if (PV)
512 PV(TU);
Ted Kremeneka97a5cd2010-01-26 17:55:33 +0000513
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000514 clang_disposeTranslationUnit(TU);
515 return 0;
516}
517
Ted Kremeneka44d99c2010-01-05 23:18:49 +0000518int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekb478ff42010-01-26 17:59:48 +0000519 const char *prefix, CXCursorVisitor Visitor,
520 PostVisitTU PV) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000521 CXIndex Idx;
522 CXTranslationUnit TU;
Ted Kremenek50228be2010-02-11 07:41:25 +0000523 int result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000524 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregorba965fb2010-01-28 00:56:43 +0000525 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000526
Ted Kremenek50228be2010-02-11 07:41:25 +0000527 if (!CreateTranslationUnit(Idx, file, &TU)) {
528 clang_disposeIndex(Idx);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000529 return 1;
Ted Kremenek50228be2010-02-11 07:41:25 +0000530 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000531
Ted Kremenek50228be2010-02-11 07:41:25 +0000532 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
533 clang_disposeIndex(Idx);
534 return result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +0000535}
536
Ted Kremenekb478ff42010-01-26 17:59:48 +0000537int perform_test_load_source(int argc, const char **argv,
538 const char *filter, CXCursorVisitor Visitor,
539 PostVisitTU PV) {
Daniel Dunbar11089662009-12-03 01:54:28 +0000540 const char *UseExternalASTs =
541 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbar3e535d72009-12-01 02:03:10 +0000542 CXIndex Idx;
543 CXTranslationUnit TU;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000544 struct CXUnsavedFile *unsaved_files = 0;
545 int num_unsaved_files = 0;
546 int result;
547
Daniel Dunbar3e535d72009-12-01 02:03:10 +0000548 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregorba965fb2010-01-28 00:56:43 +0000549 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbar3e535d72009-12-01 02:03:10 +0000550
Daniel Dunbar11089662009-12-03 01:54:28 +0000551 if (UseExternalASTs && strlen(UseExternalASTs))
552 clang_setUseExternalASTGeneration(Idx, 1);
553
Ted Kremenek50228be2010-02-11 07:41:25 +0000554 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
555 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000556 return -1;
Ted Kremenek50228be2010-02-11 07:41:25 +0000557 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000558
559 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
560 argc - num_unsaved_files,
561 argv + num_unsaved_files,
562 num_unsaved_files,
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000563 unsaved_files,
564 PrintDiagnosticCallback,
565 stderr);
Daniel Dunbar3e535d72009-12-01 02:03:10 +0000566 if (!TU) {
567 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek50228be2010-02-11 07:41:25 +0000568 clang_disposeIndex(Idx);
Daniel Dunbar3e535d72009-12-01 02:03:10 +0000569 return 1;
570 }
571
Ted Kremenekb478ff42010-01-26 17:59:48 +0000572 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000573 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +0000574 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000575 return result;
Daniel Dunbar3e535d72009-12-01 02:03:10 +0000576}
577
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000578/******************************************************************************/
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000579/* Logic for testing clang_getCursor(). */
580/******************************************************************************/
581
582static void print_cursor_file_scan(CXCursor cursor,
583 unsigned start_line, unsigned start_col,
Ted Kremenek0469b7e2009-11-18 02:02:52 +0000584 unsigned end_line, unsigned end_col,
585 const char *prefix) {
Ted Kremenekb58514e2010-01-07 01:17:12 +0000586 printf("// %s: ", FileCheckPrefix);
Ted Kremenek0469b7e2009-11-18 02:02:52 +0000587 if (prefix)
588 printf("-%s", prefix);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000589 PrintExtent(stdout, start_line, start_col, end_line, end_col);
590 printf(" ");
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000591 PrintCursor(cursor);
592 printf("\n");
593}
594
Ted Kremenek0469b7e2009-11-18 02:02:52 +0000595static int perform_file_scan(const char *ast_file, const char *source_file,
596 const char *prefix) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000597 CXIndex Idx;
598 CXTranslationUnit TU;
599 FILE *fp;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000600 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregor816fd362010-01-22 21:44:22 +0000601 CXFile file;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000602 unsigned line = 1, col = 1;
Daniel Dunbar6092d502010-02-14 08:32:51 +0000603 unsigned start_line = 1, start_col = 1;
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000604
Douglas Gregorba965fb2010-01-28 00:56:43 +0000605 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000606 fprintf(stderr, "Could not create Index\n");
607 return 1;
608 }
609
610 if (!CreateTranslationUnit(Idx, ast_file, &TU))
611 return 1;
612
613 if ((fp = fopen(source_file, "r")) == NULL) {
614 fprintf(stderr, "Could not open '%s'\n", source_file);
615 return 1;
616 }
617
Douglas Gregor816fd362010-01-22 21:44:22 +0000618 file = clang_getFile(TU, source_file);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000619 for (;;) {
620 CXCursor cursor;
621 int c = fgetc(fp);
Benjamin Kramer10d083172009-11-17 20:51:40 +0000622
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000623 if (c == '\n') {
624 ++line;
625 col = 1;
626 } else
627 ++col;
628
629 /* Check the cursor at this position, and dump the previous one if we have
630 * found something new.
631 */
632 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
633 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
634 prevCursor.kind != CXCursor_InvalidFile) {
635 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbar02968e52010-02-14 10:02:57 +0000636 line, col, prefix);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000637 start_line = line;
638 start_col = col;
Benjamin Kramer10d083172009-11-17 20:51:40 +0000639 }
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000640 if (c == EOF)
641 break;
Benjamin Kramer10d083172009-11-17 20:51:40 +0000642
Daniel Dunbareb27e7d2010-02-14 08:32:32 +0000643 prevCursor = cursor;
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000644 }
645
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000646 fclose(fp);
647 return 0;
648}
649
650/******************************************************************************/
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000651/* Logic for testing clang_codeComplete(). */
652/******************************************************************************/
653
Douglas Gregor9eb77012009-11-07 00:00:49 +0000654/* Parse file:line:column from the input string. Returns 0 on success, non-zero
655 on failure. If successful, the pointer *filename will contain newly-allocated
656 memory (that will be owned by the caller) to store the file name. */
657int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000658 unsigned *column, unsigned *second_line,
659 unsigned *second_column) {
Douglas Gregorf96ea292009-11-09 18:19:57 +0000660 /* Find the second colon. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000661 const char *last_colon = strrchr(input, ':');
662 unsigned values[4], i;
663 unsigned num_values = (second_line && second_column)? 4 : 2;
664
Douglas Gregor9eb77012009-11-07 00:00:49 +0000665 char *endptr = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000666 if (!last_colon || last_colon == input) {
667 if (num_values == 4)
668 fprintf(stderr, "could not parse filename:line:column:line:column in "
669 "'%s'\n", input);
670 else
671 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000672 return 1;
673 }
674
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000675 for (i = 0; i != num_values; ++i) {
676 const char *prev_colon;
677
678 /* Parse the next line or column. */
679 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
680 if (*endptr != 0 && *endptr != ':') {
681 fprintf(stderr, "could not parse %s in '%s'\n",
682 (i % 2 ? "column" : "line"), input);
683 return 1;
684 }
685
686 if (i + 1 == num_values)
687 break;
688
689 /* Find the previous colon. */
690 prev_colon = last_colon - 1;
691 while (prev_colon != input && *prev_colon != ':')
692 --prev_colon;
693 if (prev_colon == input) {
694 fprintf(stderr, "could not parse %s in '%s'\n",
695 (i % 2 == 0? "column" : "line"), input);
696 return 1;
697 }
698
699 last_colon = prev_colon;
Douglas Gregorf96ea292009-11-09 18:19:57 +0000700 }
701
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000702 *line = values[0];
703 *column = values[1];
Douglas Gregor9eb77012009-11-07 00:00:49 +0000704
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000705 if (second_line && second_column) {
706 *second_line = values[2];
707 *second_column = values[3];
708 }
709
Douglas Gregorf96ea292009-11-09 18:19:57 +0000710 /* Copy the file name. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000711 *filename = (char*)malloc(last_colon - input + 1);
712 memcpy(*filename, input, last_colon - input);
713 (*filename)[last_colon - input] = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000714 return 0;
715}
716
717const char *
718clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
719 switch (Kind) {
720 case CXCompletionChunk_Optional: return "Optional";
721 case CXCompletionChunk_TypedText: return "TypedText";
722 case CXCompletionChunk_Text: return "Text";
723 case CXCompletionChunk_Placeholder: return "Placeholder";
724 case CXCompletionChunk_Informative: return "Informative";
725 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
726 case CXCompletionChunk_LeftParen: return "LeftParen";
727 case CXCompletionChunk_RightParen: return "RightParen";
728 case CXCompletionChunk_LeftBracket: return "LeftBracket";
729 case CXCompletionChunk_RightBracket: return "RightBracket";
730 case CXCompletionChunk_LeftBrace: return "LeftBrace";
731 case CXCompletionChunk_RightBrace: return "RightBrace";
732 case CXCompletionChunk_LeftAngle: return "LeftAngle";
733 case CXCompletionChunk_RightAngle: return "RightAngle";
734 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorb3fa9192009-12-18 18:53:37 +0000735 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor504a6ae2010-01-10 23:08:15 +0000736 case CXCompletionChunk_Colon: return "Colon";
737 case CXCompletionChunk_SemiColon: return "SemiColon";
738 case CXCompletionChunk_Equal: return "Equal";
739 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
740 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor9eb77012009-11-07 00:00:49 +0000741 }
742
743 return "Unknown";
744}
745
Douglas Gregor8b14f8f2009-11-09 16:04:45 +0000746void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbar4ba3b292009-11-07 18:34:24 +0000747 int I, N;
Douglas Gregor8b14f8f2009-11-09 16:04:45 +0000748
749 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000750 for (I = 0; I != N; ++I) {
Douglas Gregorf81f5282009-11-09 17:05:28 +0000751 const char *text = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000752 enum CXCompletionChunkKind Kind
Douglas Gregor8b14f8f2009-11-09 16:04:45 +0000753 = clang_getCompletionChunkKind(completion_string, I);
754
755 if (Kind == CXCompletionChunk_Optional) {
756 fprintf(file, "{Optional ");
757 print_completion_string(
758 clang_getCompletionChunkCompletionString(completion_string, I),
759 file);
760 fprintf(file, "}");
761 continue;
762 }
763
Douglas Gregorf81f5282009-11-09 17:05:28 +0000764 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000765 fprintf(file, "{%s %s}",
766 clang_getCompletionChunkKindSpelling(Kind),
767 text? text : "");
768 }
Douglas Gregor8b14f8f2009-11-09 16:04:45 +0000769}
770
771void print_completion_result(CXCompletionResult *completion_result,
772 CXClientData client_data) {
773 FILE *file = (FILE *)client_data;
774 fprintf(file, "%s:",
775 clang_getCursorKindSpelling(completion_result->CursorKind));
776 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor9eb77012009-11-07 00:00:49 +0000777 fprintf(file, "\n");
778}
779
Ted Kremenekef3339b2009-11-17 18:09:14 +0000780int perform_code_completion(int argc, const char **argv) {
Douglas Gregor9eb77012009-11-07 00:00:49 +0000781 const char *input = argv[1];
782 char *filename = 0;
783 unsigned line;
784 unsigned column;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +0000785 CXIndex CIdx;
Ted Kremenekef3339b2009-11-17 18:09:14 +0000786 int errorCode;
Douglas Gregor9485bf92009-12-02 09:21:34 +0000787 struct CXUnsavedFile *unsaved_files = 0;
788 int num_unsaved_files = 0;
Douglas Gregorf72b6ac2009-12-18 16:20:58 +0000789 CXCodeCompleteResults *results = 0;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +0000790
Douglas Gregor9eb77012009-11-07 00:00:49 +0000791 input += strlen("-code-completion-at=");
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000792 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
793 0, 0)))
Ted Kremenekef3339b2009-11-17 18:09:14 +0000794 return errorCode;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000795
Douglas Gregor9485bf92009-12-02 09:21:34 +0000796 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
797 return -1;
798
Douglas Gregorba965fb2010-01-28 00:56:43 +0000799 CIdx = clang_createIndex(0);
Douglas Gregorf72b6ac2009-12-18 16:20:58 +0000800 results = clang_codeComplete(CIdx,
801 argv[argc - 1], argc - num_unsaved_files - 3,
802 argv + num_unsaved_files + 2,
803 num_unsaved_files, unsaved_files,
Douglas Gregorba965fb2010-01-28 00:56:43 +0000804 filename, line, column,
805 PrintDiagnosticCallback, stderr);
806
Douglas Gregorf72b6ac2009-12-18 16:20:58 +0000807 if (results) {
808 unsigned i, n = results->NumResults;
809 for (i = 0; i != n; ++i)
810 print_completion_result(results->Results + i, stdout);
811 clang_disposeCodeCompleteResults(results);
812 }
813
Douglas Gregor9eb77012009-11-07 00:00:49 +0000814 clang_disposeIndex(CIdx);
815 free(filename);
Ted Kremenekef3339b2009-11-17 18:09:14 +0000816
Douglas Gregor9485bf92009-12-02 09:21:34 +0000817 free_remapped_files(unsaved_files, num_unsaved_files);
818
Ted Kremenekef3339b2009-11-17 18:09:14 +0000819 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +0000820}
821
Douglas Gregor082c3e62010-01-15 19:40:17 +0000822typedef struct {
823 char *filename;
824 unsigned line;
825 unsigned column;
826} CursorSourceLocation;
827
828int inspect_cursor_at(int argc, const char **argv) {
829 CXIndex CIdx;
830 int errorCode;
831 struct CXUnsavedFile *unsaved_files = 0;
832 int num_unsaved_files = 0;
833 CXTranslationUnit TU;
834 CXCursor Cursor;
835 CursorSourceLocation *Locations = 0;
836 unsigned NumLocations = 0, Loc;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000837
Douglas Gregor082c3e62010-01-15 19:40:17 +0000838 /* Count the number of locations. */
839 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
840 ++NumLocations;
841
842 /* Parse the locations. */
843 assert(NumLocations > 0 && "Unable to count locations?");
844 Locations = (CursorSourceLocation *)malloc(
845 NumLocations * sizeof(CursorSourceLocation));
846 for (Loc = 0; Loc < NumLocations; ++Loc) {
847 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
848 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
849 &Locations[Loc].line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000850 &Locations[Loc].column, 0, 0)))
Douglas Gregor082c3e62010-01-15 19:40:17 +0000851 return errorCode;
852 }
853
854 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
855 &num_unsaved_files))
856 return -1;
857
Douglas Gregorba965fb2010-01-28 00:56:43 +0000858 CIdx = clang_createIndex(0);
Douglas Gregor082c3e62010-01-15 19:40:17 +0000859 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
860 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000861 argv + num_unsaved_files + 1 + NumLocations,
862 num_unsaved_files,
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000863 unsaved_files,
864 PrintDiagnosticCallback,
865 stderr);
Douglas Gregor082c3e62010-01-15 19:40:17 +0000866 if (!TU) {
867 fprintf(stderr, "unable to parse input\n");
868 return -1;
869 }
870
871 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregor816fd362010-01-22 21:44:22 +0000872 CXFile file = clang_getFile(TU, Locations[Loc].filename);
873 if (!file)
874 continue;
875
876 Cursor = clang_getCursor(TU,
877 clang_getLocation(TU, file, Locations[Loc].line,
878 Locations[Loc].column));
Douglas Gregor082c3e62010-01-15 19:40:17 +0000879 PrintCursor(Cursor);
880 printf("\n");
881 free(Locations[Loc].filename);
882 }
883
884 clang_disposeTranslationUnit(TU);
885 clang_disposeIndex(CIdx);
886 free(Locations);
887 free_remapped_files(unsaved_files, num_unsaved_files);
888 return 0;
889}
890
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000891int perform_token_annotation(int argc, const char **argv) {
892 const char *input = argv[1];
893 char *filename = 0;
894 unsigned line, second_line;
895 unsigned column, second_column;
896 CXIndex CIdx;
897 CXTranslationUnit TU = 0;
898 int errorCode;
899 struct CXUnsavedFile *unsaved_files = 0;
900 int num_unsaved_files = 0;
901 CXToken *tokens;
902 unsigned num_tokens;
903 CXSourceRange range;
904 CXSourceLocation startLoc, endLoc;
905 CXFile file = 0;
906 CXCursor *cursors = 0;
907 unsigned i;
908
909 input += strlen("-test-annotate-tokens=");
910 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
911 &second_line, &second_column)))
912 return errorCode;
913
914 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
915 return -1;
916
Douglas Gregorba965fb2010-01-28 00:56:43 +0000917 CIdx = clang_createIndex(0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000918 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
919 argc - num_unsaved_files - 3,
920 argv + num_unsaved_files + 2,
921 num_unsaved_files,
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000922 unsaved_files,
923 PrintDiagnosticCallback,
924 stderr);
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000925 if (!TU) {
926 fprintf(stderr, "unable to parse input\n");
927 clang_disposeIndex(CIdx);
928 free(filename);
929 free_remapped_files(unsaved_files, num_unsaved_files);
930 return -1;
931 }
932 errorCode = 0;
933
934 file = clang_getFile(TU, filename);
935 if (!file) {
936 fprintf(stderr, "file %s is not in this translation unit\n", filename);
937 errorCode = -1;
938 goto teardown;
939 }
940
941 startLoc = clang_getLocation(TU, file, line, column);
942 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
943 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
944 column);
945 errorCode = -1;
946 goto teardown;
947 }
948
949 endLoc = clang_getLocation(TU, file, second_line, second_column);
950 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
951 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
952 second_line, second_column);
953 errorCode = -1;
954 goto teardown;
955 }
956
957 range = clang_getRange(startLoc, endLoc);
958 clang_tokenize(TU, range, &tokens, &num_tokens);
959 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
960 clang_annotateTokens(TU, tokens, num_tokens, cursors);
961 for (i = 0; i != num_tokens; ++i) {
962 const char *kind = "<unknown>";
963 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
964 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
965 unsigned start_line, start_column, end_line, end_column;
966
967 switch (clang_getTokenKind(tokens[i])) {
968 case CXToken_Punctuation: kind = "Punctuation"; break;
969 case CXToken_Keyword: kind = "Keyword"; break;
970 case CXToken_Identifier: kind = "Identifier"; break;
971 case CXToken_Literal: kind = "Literal"; break;
972 case CXToken_Comment: kind = "Comment"; break;
973 }
974 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000975 0, &start_line, &start_column, 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000976 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor9bd6db52010-01-26 19:19:08 +0000977 0, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000978 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
979 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor61656112010-01-26 18:31:56 +0000980 if (!clang_isInvalid(cursors[i].kind)) {
981 printf(" ");
982 PrintCursor(cursors[i]);
983 }
984 printf("\n");
Douglas Gregor27b4fa92010-01-26 17:06:03 +0000985 }
986 free(cursors);
987
988 teardown:
989 clang_disposeTranslationUnit(TU);
990 clang_disposeIndex(CIdx);
991 free(filename);
992 free_remapped_files(unsaved_files, num_unsaved_files);
993 return errorCode;
994}
995
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000996/******************************************************************************/
997/* Command line processing. */
998/******************************************************************************/
Ted Kremenekef3339b2009-11-17 18:09:14 +0000999
Douglas Gregor720d0052010-01-20 21:32:04 +00001000static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001001 if (s[0] == '\0')
Douglas Gregor720d0052010-01-20 21:32:04 +00001002 return FilteredPrintingVisitor;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001003 if (strcmp(s, "-usrs") == 0)
1004 return USRVisitor;
1005 return NULL;
1006}
1007
Ted Kremenekef3339b2009-11-17 18:09:14 +00001008static void print_usage(void) {
1009 fprintf(stderr,
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001010 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor082c3e62010-01-15 19:40:17 +00001011 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001012 " c-index-test -test-file-scan <AST file> <source file> "
1013 "[FileCheck prefix]\n"
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001014 " c-index-test -test-load-tu <AST file> <symbol filter> "
1015 "[FileCheck prefix]\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001016 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1017 "[FileCheck prefix]\n"
1018 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001019 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregor082c3e62010-01-15 19:40:17 +00001020 fprintf(stderr,
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001021 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1022 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1023 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001024 " <symbol filter> values:\n%s",
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001025 " all - load all symbols, including those from PCH\n"
1026 " local - load all symbols except those in PCH\n"
1027 " category - only load ObjC categories (non-PCH)\n"
1028 " interface - only load ObjC interfaces (non-PCH)\n"
1029 " protocol - only load ObjC protocols (non-PCH)\n"
1030 " function - only load functions (non-PCH)\n"
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001031 " typedef - only load typdefs (non-PCH)\n"
1032 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekef3339b2009-11-17 18:09:14 +00001033}
1034
1035int main(int argc, const char **argv) {
1036 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1037 return perform_code_completion(argc, argv);
Douglas Gregor082c3e62010-01-15 19:40:17 +00001038 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1039 return inspect_cursor_at(argc, argv);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001040 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00001041 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001042 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00001043 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1044 NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001045 }
1046 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00001047 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001048 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00001049 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001050 }
1051 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001052 return perform_file_scan(argv[2], argv[3],
1053 argc >= 5 ? argv[4] : 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001054 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1055 return perform_token_annotation(argc, argv);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001056 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1057 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1058 PrintInclusionStack);
1059 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1060 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1061 PrintInclusionStack);
1062
Ted Kremenekef3339b2009-11-17 18:09:14 +00001063 print_usage();
1064 return 1;
Steve Naroffa1c72842009-08-28 15:28:48 +00001065}