blob: 1619616bb77922a387a7f734c46f3b6ea9cf0e4f [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
Douglas Gregor5352ac02010-01-28 00:27:43 +000031static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
32 CXClientData ClientData);
33
Daniel Dunbar51b058c2010-02-14 08:32:24 +000034
35static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
36 unsigned end_line, unsigned end_column) {
Daniel Dunbar8f0bf812010-02-14 08:32:51 +000037 /* FIXME: Remove this + 1. */
Daniel Dunbar51b058c2010-02-14 08:32:24 +000038 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbar8f0bf812010-02-14 08:32:51 +000039 end_line, end_column + 1);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000040}
41
Ted Kremenek1c6da172009-11-17 19:37:36 +000042static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
43 CXTranslationUnit *TU) {
44
Douglas Gregor5352ac02010-01-28 00:27:43 +000045 *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, 0);
Ted Kremenek1c6da172009-11-17 19:37:36 +000046 if (!TU) {
47 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
48 return 0;
49 }
50 return 1;
51}
52
Douglas Gregor4db64a42010-01-23 00:14:00 +000053void free_remapped_files(struct CXUnsavedFile *unsaved_files,
54 int num_unsaved_files) {
55 int i;
56 for (i = 0; i != num_unsaved_files; ++i) {
57 free((char *)unsaved_files[i].Filename);
58 free((char *)unsaved_files[i].Contents);
59 }
60}
61
62int parse_remapped_files(int argc, const char **argv, int start_arg,
63 struct CXUnsavedFile **unsaved_files,
64 int *num_unsaved_files) {
65 int i;
66 int arg;
67 int prefix_len = strlen("-remap-file=");
68 *unsaved_files = 0;
69 *num_unsaved_files = 0;
70
71 /* Count the number of remapped files. */
72 for (arg = start_arg; arg < argc; ++arg) {
73 if (strncmp(argv[arg], "-remap-file=", prefix_len))
74 break;
75
76 ++*num_unsaved_files;
77 }
78
79 if (*num_unsaved_files == 0)
80 return 0;
81
82 *unsaved_files
83 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
84 *num_unsaved_files);
85 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
86 struct CXUnsavedFile *unsaved = *unsaved_files + i;
87 const char *arg_string = argv[arg] + prefix_len;
88 int filename_len;
89 char *filename;
90 char *contents;
91 FILE *to_file;
92 const char *semi = strchr(arg_string, ';');
93 if (!semi) {
94 fprintf(stderr,
95 "error: -remap-file=from;to argument is missing semicolon\n");
96 free_remapped_files(*unsaved_files, i);
97 *unsaved_files = 0;
98 *num_unsaved_files = 0;
99 return -1;
100 }
101
102 /* Open the file that we're remapping to. */
103 to_file = fopen(semi + 1, "r");
104 if (!to_file) {
105 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
106 semi + 1);
107 free_remapped_files(*unsaved_files, i);
108 *unsaved_files = 0;
109 *num_unsaved_files = 0;
110 return -1;
111 }
112
113 /* Determine the length of the file we're remapping to. */
114 fseek(to_file, 0, SEEK_END);
115 unsaved->Length = ftell(to_file);
116 fseek(to_file, 0, SEEK_SET);
117
118 /* Read the contents of the file we're remapping to. */
119 contents = (char *)malloc(unsaved->Length + 1);
120 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
121 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
122 (feof(to_file) ? "EOF" : "error"), semi + 1);
123 fclose(to_file);
124 free_remapped_files(*unsaved_files, i);
125 *unsaved_files = 0;
126 *num_unsaved_files = 0;
127 return -1;
128 }
129 contents[unsaved->Length] = 0;
130 unsaved->Contents = contents;
131
132 /* Close the file. */
133 fclose(to_file);
134
135 /* Copy the file name that we're remapping from. */
136 filename_len = semi - arg_string;
137 filename = (char *)malloc(filename_len + 1);
138 memcpy(filename, arg_string, filename_len);
139 filename[filename_len] = 0;
140 unsaved->Filename = filename;
141 }
142
143 return 0;
144}
145
Ted Kremenek0d435192009-11-17 18:13:31 +0000146/******************************************************************************/
147/* Pretty-printing. */
148/******************************************************************************/
149
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000150static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000151 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +0000152 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +0000153 else {
Steve Naroffef0cef62009-11-09 17:45:52 +0000154 CXString string;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000155 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000156 unsigned line, column;
Steve Naroffef0cef62009-11-09 17:45:52 +0000157 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000158 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +0000159 clang_getCString(string));
160 clang_disposeString(string);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000161
162 Referenced = clang_getCursorReferenced(Cursor);
163 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
164 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000165 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000166 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000167 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000168
169 if (clang_isCursorDefinition(Cursor))
170 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000171 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000172}
Steve Naroff89922f82009-08-31 00:59:03 +0000173
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000174static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000175 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
176 const char *source;
177 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000178 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000179 source = clang_getFileName(file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000180 if (!source)
181 return "<invalid loc>";
182 return basename(source);
183}
184
Ted Kremenek0d435192009-11-17 18:13:31 +0000185/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000186/* Callbacks. */
187/******************************************************************************/
188
189typedef void (*PostVisitTU)(CXTranslationUnit);
190
Douglas Gregor5352ac02010-01-28 00:27:43 +0000191static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
192 CXClientData ClientData) {
193 FILE *out = (FILE *)ClientData;
194 CXFile file;
195 unsigned line, column;
196 CXString text;
197 enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
198
199 /* Ignore diagnostics that should be ignored. */
200 if (severity == CXDiagnostic_Ignored)
201 return;
202
203 /* Print file:line:column. */
204 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
205 &file, &line, &column, 0);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000206 if (file) {
Douglas Gregora3890ba2010-02-08 23:11:56 +0000207 unsigned i, n;
Douglas Gregor51c6d382010-01-29 00:41:11 +0000208 unsigned printed_any_ranges = 0;
209
210 fprintf(out, "%s:%d:%d:", clang_getFileName(file), line, column);
211
Douglas Gregora3890ba2010-02-08 23:11:56 +0000212 n = clang_getDiagnosticNumRanges(Diagnostic);
213 for (i = 0; i != n; ++i) {
Douglas Gregor51c6d382010-01-29 00:41:11 +0000214 CXFile start_file, end_file;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000215 CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
216
Douglas Gregor51c6d382010-01-29 00:41:11 +0000217 unsigned start_line, start_column, end_line, end_column;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000218 clang_getInstantiationLocation(clang_getRangeStart(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000219 &start_file, &start_line, &start_column,0);
Douglas Gregora3890ba2010-02-08 23:11:56 +0000220 clang_getInstantiationLocation(clang_getRangeEnd(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000221 &end_file, &end_line, &end_column, 0);
222
223 if (start_file != end_file || start_file != file)
224 continue;
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000225
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000226 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000227 printed_any_ranges = 1;
228 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000229 if (printed_any_ranges)
230 fprintf(out, ":");
231
232 fprintf(out, " ");
233 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000234
235 /* Print warning/error/etc. */
236 switch (severity) {
237 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
238 case CXDiagnostic_Note: fprintf(out, "note: "); break;
239 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
240 case CXDiagnostic_Error: fprintf(out, "error: "); break;
241 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
242 }
243
244 text = clang_getDiagnosticSpelling(Diagnostic);
245 if (clang_getCString(text))
246 fprintf(out, "%s\n", clang_getCString(text));
247 else
248 fprintf(out, "<no diagnostic text>\n");
249 clang_disposeString(text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000250
251 if (file) {
252 unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
253 for (i = 0; i != num_fixits; ++i) {
254 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
255 case CXFixIt_Insertion: {
256 CXSourceLocation insertion_loc;
257 CXFile insertion_file;
258 unsigned insertion_line, insertion_column;
259 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
260 clang_getInstantiationLocation(insertion_loc, &insertion_file,
261 &insertion_line, &insertion_column, 0);
262 if (insertion_file == file)
263 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
264 clang_getCString(text), insertion_line, insertion_column);
265 clang_disposeString(text);
266 break;
267 }
268
269 case CXFixIt_Removal: {
270 CXFile start_file, end_file;
271 unsigned start_line, start_column, end_line, end_column;
272 CXSourceRange remove_range
273 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
274 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
275 &start_file, &start_line, &start_column,
276 0);
277 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
278 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000279 if (start_file == file && end_file == file) {
280 fprintf(out, "FIX-IT: Remove ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000281 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000282 fprintf(out, "\n");
283 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000284 break;
285 }
286
287 case CXFixIt_Replacement: {
288 CXFile start_file, end_file;
289 unsigned start_line, start_column, end_line, end_column;
290 CXSourceRange remove_range;
291 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
292 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
293 &start_file, &start_line, &start_column,
294 0);
295 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
296 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000297 if (start_file == end_file) {
298 fprintf(out, "FIX-IT: Replace ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000299 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000300 fprintf(out, " with \"%s\"\n", clang_getCString(text));
301 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000302 clang_disposeString(text);
303 break;
304 }
305 }
306 }
307 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000308}
309
Ted Kremenekce2ae882010-01-26 17:59:48 +0000310/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000311/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000312/******************************************************************************/
313
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000314static const char *FileCheckPrefix = "CHECK";
315
Douglas Gregora7bde202010-01-19 00:34:46 +0000316static void PrintCursorExtent(CXCursor C) {
317 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000318 CXFile begin_file, end_file;
319 unsigned begin_line, begin_column, end_line, end_column;
320
321 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000322 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000323 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000324 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000325 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000326 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000327
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000328 printf(" Extent=");
329 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000330}
331
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000332/* Data used by all of the visitors. */
333typedef struct {
334 CXTranslationUnit TU;
335 enum CXCursorKind *Filter;
336} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000337
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000338
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000339enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
340 CXCursor Parent,
341 CXClientData ClientData) {
342 VisitorData *Data = (VisitorData *)ClientData;
343 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000344 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000346 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000347 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000348 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000349 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000350 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000351 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000352 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000353 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000354
355 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000356}
Steve Naroff50398192009-08-28 15:28:48 +0000357
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000358static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
359 CXCursor Parent,
360 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000361 const char *startBuf, *endBuf;
362 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
363 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000364 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000365
Douglas Gregorb6998662010-01-19 19:34:47 +0000366 if (Cursor.kind != CXCursor_FunctionDecl ||
367 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000368 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000369
370 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
371 &startLine, &startColumn,
372 &endLine, &endColumn);
373 /* Probe the entire body, looking for both decls and refs. */
374 curLine = startLine;
375 curColumn = startColumn;
376
377 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000378 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000379 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000380 const char *source = 0;
381
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000382 if (*startBuf == '\n') {
383 startBuf++;
384 curLine++;
385 curColumn = 1;
386 } else if (*startBuf != '\t')
387 curColumn++;
388
Douglas Gregor98258af2010-01-18 22:46:11 +0000389 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000390 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000391 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000392 if (source) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000393 CXSourceLocation RefLoc
394 = clang_getLocation(Data->TU, file, curLine, curColumn);
395 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000396 if (Ref.kind == CXCursor_NoDeclFound) {
397 /* Nothing found here; that's fine. */
398 } else if (Ref.kind != CXCursor_FunctionDecl) {
399 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
400 curLine, curColumn);
401 PrintCursor(Ref);
402 printf("\n");
403 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000404 }
405 startBuf++;
406 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000407
408 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000409}
410
Ted Kremenek7d405622010-01-12 23:34:26 +0000411/******************************************************************************/
412/* USR testing. */
413/******************************************************************************/
414
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000415enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
416 CXClientData ClientData) {
417 VisitorData *Data = (VisitorData *)ClientData;
418 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000419 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000420 if (!USR.Spelling) {
421 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000422 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000423 }
424 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000425 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000426 printf("\n");
427 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000428
429 return CXChildVisit_Recurse;
430 }
431
432 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000433}
434
435/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000436/* Inclusion stack testing. */
437/******************************************************************************/
438
439void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
440 unsigned includeStackLen, CXClientData data) {
441
442 unsigned i;
443 printf("file: %s\nincluded by:\n", clang_getFileName(includedFile));
444 for (i = 0; i < includeStackLen; ++i) {
445 CXFile includingFile;
446 unsigned line, column;
447 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
448 &column, 0);
449 printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column);
450 }
451 printf("\n");
452}
453
454void PrintInclusionStack(CXTranslationUnit TU) {
455 clang_getInclusions(TU, InclusionVisitor, NULL);
456}
457
458/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000459/* Loading ASTs/source. */
460/******************************************************************************/
461
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000462static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000463 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000464 CXCursorVisitor Visitor,
465 PostVisitTU PV) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000466
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000467 if (prefix)
468 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000469
470 if (Visitor) {
471 enum CXCursorKind K = CXCursor_NotImplemented;
472 enum CXCursorKind *ck = &K;
473 VisitorData Data;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000474
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000475 /* Perform some simple filtering. */
476 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000477 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000478 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
479 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
480 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
481 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
482 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
483 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
484 else {
485 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
486 return 1;
487 }
488
489 Data.TU = TU;
490 Data.Filter = ck;
491 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000492 }
Ted Kremenekce2ae882010-01-26 17:59:48 +0000493
494 if (PV)
495 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000496
Ted Kremenek0d435192009-11-17 18:13:31 +0000497 clang_disposeTranslationUnit(TU);
498 return 0;
499}
500
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000501int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000502 const char *prefix, CXCursorVisitor Visitor,
503 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000504 CXIndex Idx;
505 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000506 int result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000507 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000508 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000509
Ted Kremenek020a0952010-02-11 07:41:25 +0000510 if (!CreateTranslationUnit(Idx, file, &TU)) {
511 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000512 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000513 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000514
Ted Kremenek020a0952010-02-11 07:41:25 +0000515 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
516 clang_disposeIndex(Idx);
517 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000518}
519
Ted Kremenekce2ae882010-01-26 17:59:48 +0000520int perform_test_load_source(int argc, const char **argv,
521 const char *filter, CXCursorVisitor Visitor,
522 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000523 const char *UseExternalASTs =
524 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000525 CXIndex Idx;
526 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000527 struct CXUnsavedFile *unsaved_files = 0;
528 int num_unsaved_files = 0;
529 int result;
530
Daniel Dunbarada487d2009-12-01 02:03:10 +0000531 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000532 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000533
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000534 if (UseExternalASTs && strlen(UseExternalASTs))
535 clang_setUseExternalASTGeneration(Idx, 1);
536
Ted Kremenek020a0952010-02-11 07:41:25 +0000537 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
538 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000539 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000540 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000541
542 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
543 argc - num_unsaved_files,
544 argv + num_unsaved_files,
545 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000546 unsaved_files,
547 PrintDiagnosticCallback,
548 stderr);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000549 if (!TU) {
550 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000551 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000552 return 1;
553 }
554
Ted Kremenekce2ae882010-01-26 17:59:48 +0000555 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000556 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000557 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000558 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000559}
560
Ted Kremenek0d435192009-11-17 18:13:31 +0000561/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000562/* Logic for testing clang_getCursor(). */
563/******************************************************************************/
564
565static void print_cursor_file_scan(CXCursor cursor,
566 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000567 unsigned end_line, unsigned end_col,
568 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000569 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000570 if (prefix)
571 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000572 PrintExtent(stdout, start_line, start_col, end_line, end_col);
573 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000574 PrintCursor(cursor);
575 printf("\n");
576}
577
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000578static int perform_file_scan(const char *ast_file, const char *source_file,
579 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000580 CXIndex Idx;
581 CXTranslationUnit TU;
582 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000583 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000584 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000585 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000586 unsigned start_line = 1, start_col = 1;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000587
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000588 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000589 fprintf(stderr, "Could not create Index\n");
590 return 1;
591 }
592
593 if (!CreateTranslationUnit(Idx, ast_file, &TU))
594 return 1;
595
596 if ((fp = fopen(source_file, "r")) == NULL) {
597 fprintf(stderr, "Could not open '%s'\n", source_file);
598 return 1;
599 }
600
Douglas Gregorb9790342010-01-22 21:44:22 +0000601 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000602 for (;;) {
603 CXCursor cursor;
604 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000605
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000606 if (c == '\n') {
607 ++line;
608 col = 1;
609 } else
610 ++col;
611
612 /* Check the cursor at this position, and dump the previous one if we have
613 * found something new.
614 */
615 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
616 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
617 prevCursor.kind != CXCursor_InvalidFile) {
618 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000619 line, col - 1, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000620 start_line = line;
621 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000622 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000623 if (c == EOF)
624 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000625
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000626 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000627 }
628
Ted Kremenek1c6da172009-11-17 19:37:36 +0000629 fclose(fp);
630 return 0;
631}
632
633/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000634/* Logic for testing clang_codeComplete(). */
635/******************************************************************************/
636
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000637/* Parse file:line:column from the input string. Returns 0 on success, non-zero
638 on failure. If successful, the pointer *filename will contain newly-allocated
639 memory (that will be owned by the caller) to store the file name. */
640int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000641 unsigned *column, unsigned *second_line,
642 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000643 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000644 const char *last_colon = strrchr(input, ':');
645 unsigned values[4], i;
646 unsigned num_values = (second_line && second_column)? 4 : 2;
647
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000648 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000649 if (!last_colon || last_colon == input) {
650 if (num_values == 4)
651 fprintf(stderr, "could not parse filename:line:column:line:column in "
652 "'%s'\n", input);
653 else
654 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000655 return 1;
656 }
657
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000658 for (i = 0; i != num_values; ++i) {
659 const char *prev_colon;
660
661 /* Parse the next line or column. */
662 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
663 if (*endptr != 0 && *endptr != ':') {
664 fprintf(stderr, "could not parse %s in '%s'\n",
665 (i % 2 ? "column" : "line"), input);
666 return 1;
667 }
668
669 if (i + 1 == num_values)
670 break;
671
672 /* Find the previous colon. */
673 prev_colon = last_colon - 1;
674 while (prev_colon != input && *prev_colon != ':')
675 --prev_colon;
676 if (prev_colon == input) {
677 fprintf(stderr, "could not parse %s in '%s'\n",
678 (i % 2 == 0? "column" : "line"), input);
679 return 1;
680 }
681
682 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000683 }
684
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000685 *line = values[0];
686 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000687
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000688 if (second_line && second_column) {
689 *second_line = values[2];
690 *second_column = values[3];
691 }
692
Douglas Gregor88d23952009-11-09 18:19:57 +0000693 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000694 *filename = (char*)malloc(last_colon - input + 1);
695 memcpy(*filename, input, last_colon - input);
696 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000697 return 0;
698}
699
700const char *
701clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
702 switch (Kind) {
703 case CXCompletionChunk_Optional: return "Optional";
704 case CXCompletionChunk_TypedText: return "TypedText";
705 case CXCompletionChunk_Text: return "Text";
706 case CXCompletionChunk_Placeholder: return "Placeholder";
707 case CXCompletionChunk_Informative: return "Informative";
708 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
709 case CXCompletionChunk_LeftParen: return "LeftParen";
710 case CXCompletionChunk_RightParen: return "RightParen";
711 case CXCompletionChunk_LeftBracket: return "LeftBracket";
712 case CXCompletionChunk_RightBracket: return "RightBracket";
713 case CXCompletionChunk_LeftBrace: return "LeftBrace";
714 case CXCompletionChunk_RightBrace: return "RightBrace";
715 case CXCompletionChunk_LeftAngle: return "LeftAngle";
716 case CXCompletionChunk_RightAngle: return "RightAngle";
717 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000718 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000719 case CXCompletionChunk_Colon: return "Colon";
720 case CXCompletionChunk_SemiColon: return "SemiColon";
721 case CXCompletionChunk_Equal: return "Equal";
722 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
723 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000724 }
725
726 return "Unknown";
727}
728
Douglas Gregor3ac73852009-11-09 16:04:45 +0000729void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000730 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000731
732 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000733 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000734 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000735 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000736 = clang_getCompletionChunkKind(completion_string, I);
737
738 if (Kind == CXCompletionChunk_Optional) {
739 fprintf(file, "{Optional ");
740 print_completion_string(
741 clang_getCompletionChunkCompletionString(completion_string, I),
742 file);
743 fprintf(file, "}");
744 continue;
745 }
746
Douglas Gregord5a20892009-11-09 17:05:28 +0000747 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000748 fprintf(file, "{%s %s}",
749 clang_getCompletionChunkKindSpelling(Kind),
750 text? text : "");
751 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000752}
753
754void print_completion_result(CXCompletionResult *completion_result,
755 CXClientData client_data) {
756 FILE *file = (FILE *)client_data;
757 fprintf(file, "%s:",
758 clang_getCursorKindSpelling(completion_result->CursorKind));
759 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000760 fprintf(file, "\n");
761}
762
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000763int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000764 const char *input = argv[1];
765 char *filename = 0;
766 unsigned line;
767 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000768 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000769 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000770 struct CXUnsavedFile *unsaved_files = 0;
771 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000772 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000773
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000774 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000775 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
776 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000777 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000778
Douglas Gregor735df882009-12-02 09:21:34 +0000779 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
780 return -1;
781
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000782 CIdx = clang_createIndex(0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000783 results = clang_codeComplete(CIdx,
784 argv[argc - 1], argc - num_unsaved_files - 3,
785 argv + num_unsaved_files + 2,
786 num_unsaved_files, unsaved_files,
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000787 filename, line, column,
788 PrintDiagnosticCallback, stderr);
789
Douglas Gregorec6762c2009-12-18 16:20:58 +0000790 if (results) {
791 unsigned i, n = results->NumResults;
792 for (i = 0; i != n; ++i)
793 print_completion_result(results->Results + i, stdout);
794 clang_disposeCodeCompleteResults(results);
795 }
796
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000797 clang_disposeIndex(CIdx);
798 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000799
Douglas Gregor735df882009-12-02 09:21:34 +0000800 free_remapped_files(unsaved_files, num_unsaved_files);
801
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000802 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000803}
804
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000805typedef struct {
806 char *filename;
807 unsigned line;
808 unsigned column;
809} CursorSourceLocation;
810
811int inspect_cursor_at(int argc, const char **argv) {
812 CXIndex CIdx;
813 int errorCode;
814 struct CXUnsavedFile *unsaved_files = 0;
815 int num_unsaved_files = 0;
816 CXTranslationUnit TU;
817 CXCursor Cursor;
818 CursorSourceLocation *Locations = 0;
819 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000820
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000821 /* Count the number of locations. */
822 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
823 ++NumLocations;
824
825 /* Parse the locations. */
826 assert(NumLocations > 0 && "Unable to count locations?");
827 Locations = (CursorSourceLocation *)malloc(
828 NumLocations * sizeof(CursorSourceLocation));
829 for (Loc = 0; Loc < NumLocations; ++Loc) {
830 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
831 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
832 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000833 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000834 return errorCode;
835 }
836
837 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
838 &num_unsaved_files))
839 return -1;
840
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000841 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000842 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
843 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000844 argv + num_unsaved_files + 1 + NumLocations,
845 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000846 unsaved_files,
847 PrintDiagnosticCallback,
848 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000849 if (!TU) {
850 fprintf(stderr, "unable to parse input\n");
851 return -1;
852 }
853
854 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000855 CXFile file = clang_getFile(TU, Locations[Loc].filename);
856 if (!file)
857 continue;
858
859 Cursor = clang_getCursor(TU,
860 clang_getLocation(TU, file, Locations[Loc].line,
861 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000862 PrintCursor(Cursor);
863 printf("\n");
864 free(Locations[Loc].filename);
865 }
866
867 clang_disposeTranslationUnit(TU);
868 clang_disposeIndex(CIdx);
869 free(Locations);
870 free_remapped_files(unsaved_files, num_unsaved_files);
871 return 0;
872}
873
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000874int perform_token_annotation(int argc, const char **argv) {
875 const char *input = argv[1];
876 char *filename = 0;
877 unsigned line, second_line;
878 unsigned column, second_column;
879 CXIndex CIdx;
880 CXTranslationUnit TU = 0;
881 int errorCode;
882 struct CXUnsavedFile *unsaved_files = 0;
883 int num_unsaved_files = 0;
884 CXToken *tokens;
885 unsigned num_tokens;
886 CXSourceRange range;
887 CXSourceLocation startLoc, endLoc;
888 CXFile file = 0;
889 CXCursor *cursors = 0;
890 unsigned i;
891
892 input += strlen("-test-annotate-tokens=");
893 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
894 &second_line, &second_column)))
895 return errorCode;
896
897 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
898 return -1;
899
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000900 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000901 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
902 argc - num_unsaved_files - 3,
903 argv + num_unsaved_files + 2,
904 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000905 unsaved_files,
906 PrintDiagnosticCallback,
907 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000908 if (!TU) {
909 fprintf(stderr, "unable to parse input\n");
910 clang_disposeIndex(CIdx);
911 free(filename);
912 free_remapped_files(unsaved_files, num_unsaved_files);
913 return -1;
914 }
915 errorCode = 0;
916
917 file = clang_getFile(TU, filename);
918 if (!file) {
919 fprintf(stderr, "file %s is not in this translation unit\n", filename);
920 errorCode = -1;
921 goto teardown;
922 }
923
924 startLoc = clang_getLocation(TU, file, line, column);
925 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
926 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
927 column);
928 errorCode = -1;
929 goto teardown;
930 }
931
932 endLoc = clang_getLocation(TU, file, second_line, second_column);
933 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
934 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
935 second_line, second_column);
936 errorCode = -1;
937 goto teardown;
938 }
939
940 range = clang_getRange(startLoc, endLoc);
941 clang_tokenize(TU, range, &tokens, &num_tokens);
942 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
943 clang_annotateTokens(TU, tokens, num_tokens, cursors);
944 for (i = 0; i != num_tokens; ++i) {
945 const char *kind = "<unknown>";
946 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
947 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
948 unsigned start_line, start_column, end_line, end_column;
949
950 switch (clang_getTokenKind(tokens[i])) {
951 case CXToken_Punctuation: kind = "Punctuation"; break;
952 case CXToken_Keyword: kind = "Keyword"; break;
953 case CXToken_Identifier: kind = "Identifier"; break;
954 case CXToken_Literal: kind = "Literal"; break;
955 case CXToken_Comment: kind = "Comment"; break;
956 }
957 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000958 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000959 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000960 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000961 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
962 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000963 if (!clang_isInvalid(cursors[i].kind)) {
964 printf(" ");
965 PrintCursor(cursors[i]);
966 }
967 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000968 }
969 free(cursors);
970
971 teardown:
972 clang_disposeTranslationUnit(TU);
973 clang_disposeIndex(CIdx);
974 free(filename);
975 free_remapped_files(unsaved_files, num_unsaved_files);
976 return errorCode;
977}
978
Ted Kremenek0d435192009-11-17 18:13:31 +0000979/******************************************************************************/
980/* Command line processing. */
981/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000982
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000983static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000984 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000985 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000986 if (strcmp(s, "-usrs") == 0)
987 return USRVisitor;
988 return NULL;
989}
990
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000991static void print_usage(void) {
992 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000993 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000994 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000995 " c-index-test -test-file-scan <AST file> <source file> "
996 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000997 " c-index-test -test-load-tu <AST file> <symbol filter> "
998 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000999 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1000 "[FileCheck prefix]\n"
1001 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001002 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001003 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001004 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1005 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1006 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001007 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001008 " all - load all symbols, including those from PCH\n"
1009 " local - load all symbols except those in PCH\n"
1010 " category - only load ObjC categories (non-PCH)\n"
1011 " interface - only load ObjC interfaces (non-PCH)\n"
1012 " protocol - only load ObjC protocols (non-PCH)\n"
1013 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001014 " typedef - only load typdefs (non-PCH)\n"
1015 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001016}
1017
1018int main(int argc, const char **argv) {
1019 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1020 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001021 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1022 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001023 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001024 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001025 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001026 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1027 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001028 }
1029 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001030 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001031 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001032 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001033 }
1034 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001035 return perform_file_scan(argv[2], argv[3],
1036 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001037 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1038 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001039 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1040 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1041 PrintInclusionStack);
1042 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1043 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1044 PrintInclusionStack);
1045
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001046 print_usage();
1047 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001048}