blob: 543e444dbfcd0b947e175a93d8d3ed27e4682baa [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) {
37 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000038 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000039}
40
Ted Kremenek1c6da172009-11-17 19:37:36 +000041static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
42 CXTranslationUnit *TU) {
43
Douglas Gregor5352ac02010-01-28 00:27:43 +000044 *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, 0);
Ted Kremenek1c6da172009-11-17 19:37:36 +000045 if (!TU) {
46 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
47 return 0;
48 }
49 return 1;
50}
51
Douglas Gregor4db64a42010-01-23 00:14:00 +000052void free_remapped_files(struct CXUnsavedFile *unsaved_files,
53 int num_unsaved_files) {
54 int i;
55 for (i = 0; i != num_unsaved_files; ++i) {
56 free((char *)unsaved_files[i].Filename);
57 free((char *)unsaved_files[i].Contents);
58 }
59}
60
61int parse_remapped_files(int argc, const char **argv, int start_arg,
62 struct CXUnsavedFile **unsaved_files,
63 int *num_unsaved_files) {
64 int i;
65 int arg;
66 int prefix_len = strlen("-remap-file=");
67 *unsaved_files = 0;
68 *num_unsaved_files = 0;
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 Kremenek0d435192009-11-17 18:13:31 +0000145/******************************************************************************/
146/* Pretty-printing. */
147/******************************************************************************/
148
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000149static void PrintCursor(CXCursor Cursor) {
Steve Naroff77128dd2009-09-15 20:25:34 +0000150 if (clang_isInvalid(Cursor.kind))
Ted Kremenek1c6da172009-11-17 19:37:36 +0000151 printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind));
Steve Naroff699a07d2009-09-25 21:32:34 +0000152 else {
Steve Naroffef0cef62009-11-09 17:45:52 +0000153 CXString string;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000154 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000155 unsigned line, column;
Steve Naroffef0cef62009-11-09 17:45:52 +0000156 string = clang_getCursorSpelling(Cursor);
Steve Naroffff9e18c2009-09-24 20:03:06 +0000157 printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind),
Steve Naroffef0cef62009-11-09 17:45:52 +0000158 clang_getCString(string));
159 clang_disposeString(string);
Douglas Gregorc5d1e932010-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 Gregor46766dc2010-01-26 19:19:08 +0000164 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000165 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000166 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000167
168 if (clang_isCursorDefinition(Cursor))
169 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000170 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000171}
Steve Naroff89922f82009-08-31 00:59:03 +0000172
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000173static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000174 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
175 const char *source;
176 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000177 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000178 source = clang_getFileName(file);
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000179 if (!source)
180 return "<invalid loc>";
181 return basename(source);
182}
183
Ted Kremenek0d435192009-11-17 18:13:31 +0000184/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000185/* Callbacks. */
186/******************************************************************************/
187
188typedef void (*PostVisitTU)(CXTranslationUnit);
189
Douglas Gregor5352ac02010-01-28 00:27:43 +0000190static void PrintDiagnosticCallback(CXDiagnostic Diagnostic,
191 CXClientData ClientData) {
192 FILE *out = (FILE *)ClientData;
193 CXFile file;
194 unsigned line, column;
195 CXString text;
196 enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic);
197
198 /* Ignore diagnostics that should be ignored. */
199 if (severity == CXDiagnostic_Ignored)
200 return;
201
202 /* Print file:line:column. */
203 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
204 &file, &line, &column, 0);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000205 if (file) {
Douglas Gregora3890ba2010-02-08 23:11:56 +0000206 unsigned i, n;
Douglas Gregor51c6d382010-01-29 00:41:11 +0000207 unsigned printed_any_ranges = 0;
208
209 fprintf(out, "%s:%d:%d:", clang_getFileName(file), line, column);
210
Douglas Gregora3890ba2010-02-08 23:11:56 +0000211 n = clang_getDiagnosticNumRanges(Diagnostic);
212 for (i = 0; i != n; ++i) {
Douglas Gregor51c6d382010-01-29 00:41:11 +0000213 CXFile start_file, end_file;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000214 CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i);
215
Douglas Gregor51c6d382010-01-29 00:41:11 +0000216 unsigned start_line, start_column, end_line, end_column;
Douglas Gregora3890ba2010-02-08 23:11:56 +0000217 clang_getInstantiationLocation(clang_getRangeStart(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000218 &start_file, &start_line, &start_column,0);
Douglas Gregora3890ba2010-02-08 23:11:56 +0000219 clang_getInstantiationLocation(clang_getRangeEnd(range),
Douglas Gregor51c6d382010-01-29 00:41:11 +0000220 &end_file, &end_line, &end_column, 0);
221
222 if (start_file != end_file || start_file != file)
223 continue;
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000224
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000225 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000226 printed_any_ranges = 1;
227 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000228 if (printed_any_ranges)
229 fprintf(out, ":");
230
231 fprintf(out, " ");
232 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000233
234 /* Print warning/error/etc. */
235 switch (severity) {
236 case CXDiagnostic_Ignored: assert(0 && "impossible"); break;
237 case CXDiagnostic_Note: fprintf(out, "note: "); break;
238 case CXDiagnostic_Warning: fprintf(out, "warning: "); break;
239 case CXDiagnostic_Error: fprintf(out, "error: "); break;
240 case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break;
241 }
242
243 text = clang_getDiagnosticSpelling(Diagnostic);
244 if (clang_getCString(text))
245 fprintf(out, "%s\n", clang_getCString(text));
246 else
247 fprintf(out, "<no diagnostic text>\n");
248 clang_disposeString(text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000249
250 if (file) {
251 unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
252 for (i = 0; i != num_fixits; ++i) {
253 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
254 case CXFixIt_Insertion: {
255 CXSourceLocation insertion_loc;
256 CXFile insertion_file;
257 unsigned insertion_line, insertion_column;
258 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
259 clang_getInstantiationLocation(insertion_loc, &insertion_file,
260 &insertion_line, &insertion_column, 0);
261 if (insertion_file == file)
262 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
263 clang_getCString(text), insertion_line, insertion_column);
264 clang_disposeString(text);
265 break;
266 }
267
268 case CXFixIt_Removal: {
269 CXFile start_file, end_file;
270 unsigned start_line, start_column, end_line, end_column;
271 CXSourceRange remove_range
272 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
273 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
274 &start_file, &start_line, &start_column,
275 0);
276 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
277 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000278 if (start_file == file && end_file == file) {
279 fprintf(out, "FIX-IT: Remove ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000280 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000281 fprintf(out, "\n");
282 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000283 break;
284 }
285
286 case CXFixIt_Replacement: {
287 CXFile start_file, end_file;
288 unsigned start_line, start_column, end_line, end_column;
289 CXSourceRange remove_range;
290 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
291 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
292 &start_file, &start_line, &start_column,
293 0);
294 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
295 &end_file, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000296 if (start_file == end_file) {
297 fprintf(out, "FIX-IT: Replace ");
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000298 PrintExtent(out, start_line, start_column, end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000299 fprintf(out, " with \"%s\"\n", clang_getCString(text));
300 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000301 clang_disposeString(text);
302 break;
303 }
304 }
305 }
306 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000307}
308
Ted Kremenekce2ae882010-01-26 17:59:48 +0000309/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000310/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000311/******************************************************************************/
312
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000313static const char *FileCheckPrefix = "CHECK";
314
Douglas Gregora7bde202010-01-19 00:34:46 +0000315static void PrintCursorExtent(CXCursor C) {
316 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000317 CXFile begin_file, end_file;
318 unsigned begin_line, begin_column, end_line, end_column;
319
320 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000321 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000322 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000323 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000324 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000325 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000326
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000327 printf(" Extent=");
328 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000329}
330
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000331/* Data used by all of the visitors. */
332typedef struct {
333 CXTranslationUnit TU;
334 enum CXCursorKind *Filter;
335} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000336
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000337
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000338enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
339 CXCursor Parent,
340 CXClientData ClientData) {
341 VisitorData *Data = (VisitorData *)ClientData;
342 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000343 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000345 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000346 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000347 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000348 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000349 PrintCursorExtent(Cursor);
Ted Kremenek70ee5422010-01-16 01:44:12 +0000350 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000351 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000352 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000353
354 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000355}
Steve Naroff50398192009-08-28 15:28:48 +0000356
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000357static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
358 CXCursor Parent,
359 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000360 const char *startBuf, *endBuf;
361 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
362 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000363 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000364
Douglas Gregorb6998662010-01-19 19:34:47 +0000365 if (Cursor.kind != CXCursor_FunctionDecl ||
366 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000367 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000368
369 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
370 &startLine, &startColumn,
371 &endLine, &endColumn);
372 /* Probe the entire body, looking for both decls and refs. */
373 curLine = startLine;
374 curColumn = startColumn;
375
376 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000377 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000378 CXFile file;
Douglas Gregor98258af2010-01-18 22:46:11 +0000379 const char *source = 0;
380
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000381 if (*startBuf == '\n') {
382 startBuf++;
383 curLine++;
384 curColumn = 1;
385 } else if (*startBuf != '\t')
386 curColumn++;
387
Douglas Gregor98258af2010-01-18 22:46:11 +0000388 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000389 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000390 source = clang_getFileName(file);
Douglas Gregor98258af2010-01-18 22:46:11 +0000391 if (source) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000392 CXSourceLocation RefLoc
393 = clang_getLocation(Data->TU, file, curLine, curColumn);
394 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000395 if (Ref.kind == CXCursor_NoDeclFound) {
396 /* Nothing found here; that's fine. */
397 } else if (Ref.kind != CXCursor_FunctionDecl) {
398 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
399 curLine, curColumn);
400 PrintCursor(Ref);
401 printf("\n");
402 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000403 }
404 startBuf++;
405 }
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000406
407 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000408}
409
Ted Kremenek7d405622010-01-12 23:34:26 +0000410/******************************************************************************/
411/* USR testing. */
412/******************************************************************************/
413
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000414enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
415 CXClientData ClientData) {
416 VisitorData *Data = (VisitorData *)ClientData;
417 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000418 CXString USR = clang_getCursorUSR(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000419 if (!USR.Spelling) {
420 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000421 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000422 }
423 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling);
Douglas Gregora7bde202010-01-19 00:34:46 +0000424 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000425 printf("\n");
426 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000427
428 return CXChildVisit_Recurse;
429 }
430
431 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000432}
433
434/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000435/* Inclusion stack testing. */
436/******************************************************************************/
437
438void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
439 unsigned includeStackLen, CXClientData data) {
440
441 unsigned i;
442 printf("file: %s\nincluded by:\n", clang_getFileName(includedFile));
443 for (i = 0; i < includeStackLen; ++i) {
444 CXFile includingFile;
445 unsigned line, column;
446 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
447 &column, 0);
448 printf(" %s:%d:%d\n", clang_getFileName(includingFile), line, column);
449 }
450 printf("\n");
451}
452
453void PrintInclusionStack(CXTranslationUnit TU) {
454 clang_getInclusions(TU, InclusionVisitor, NULL);
455}
456
457/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000458/* Loading ASTs/source. */
459/******************************************************************************/
460
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000461static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000462 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000463 CXCursorVisitor Visitor,
464 PostVisitTU PV) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000465
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000466 if (prefix)
467 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000468
469 if (Visitor) {
470 enum CXCursorKind K = CXCursor_NotImplemented;
471 enum CXCursorKind *ck = &K;
472 VisitorData Data;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000473
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000474 /* Perform some simple filtering. */
475 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000476 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000477 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
478 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
479 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
480 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
481 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
482 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
483 else {
484 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
485 return 1;
486 }
487
488 Data.TU = TU;
489 Data.Filter = ck;
490 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000491 }
Ted Kremenekce2ae882010-01-26 17:59:48 +0000492
493 if (PV)
494 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000495
Ted Kremenek0d435192009-11-17 18:13:31 +0000496 clang_disposeTranslationUnit(TU);
497 return 0;
498}
499
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000500int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000501 const char *prefix, CXCursorVisitor Visitor,
502 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000503 CXIndex Idx;
504 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000505 int result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000506 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000507 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000508
Ted Kremenek020a0952010-02-11 07:41:25 +0000509 if (!CreateTranslationUnit(Idx, file, &TU)) {
510 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000511 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000512 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000513
Ted Kremenek020a0952010-02-11 07:41:25 +0000514 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
515 clang_disposeIndex(Idx);
516 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000517}
518
Ted Kremenekce2ae882010-01-26 17:59:48 +0000519int perform_test_load_source(int argc, const char **argv,
520 const char *filter, CXCursorVisitor Visitor,
521 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000522 const char *UseExternalASTs =
523 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000524 CXIndex Idx;
525 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000526 struct CXUnsavedFile *unsaved_files = 0;
527 int num_unsaved_files = 0;
528 int result;
529
Daniel Dunbarada487d2009-12-01 02:03:10 +0000530 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000531 !strcmp(filter, "local") ? 1 : 0);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000532
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000533 if (UseExternalASTs && strlen(UseExternalASTs))
534 clang_setUseExternalASTGeneration(Idx, 1);
535
Ted Kremenek020a0952010-02-11 07:41:25 +0000536 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
537 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000538 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000539 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000540
541 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
542 argc - num_unsaved_files,
543 argv + num_unsaved_files,
544 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000545 unsaved_files,
546 PrintDiagnosticCallback,
547 stderr);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000548 if (!TU) {
549 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000550 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000551 return 1;
552 }
553
Ted Kremenekce2ae882010-01-26 17:59:48 +0000554 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000555 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000556 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000557 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000558}
559
Ted Kremenek0d435192009-11-17 18:13:31 +0000560/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000561/* Logic for testing clang_getCursor(). */
562/******************************************************************************/
563
564static void print_cursor_file_scan(CXCursor cursor,
565 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000566 unsigned end_line, unsigned end_col,
567 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000568 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000569 if (prefix)
570 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000571 PrintExtent(stdout, start_line, start_col, end_line, end_col);
572 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000573 PrintCursor(cursor);
574 printf("\n");
575}
576
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000577static int perform_file_scan(const char *ast_file, const char *source_file,
578 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000579 CXIndex Idx;
580 CXTranslationUnit TU;
581 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000582 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000583 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000584 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000585 unsigned start_line = 1, start_col = 1;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000586
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000587 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000588 fprintf(stderr, "Could not create Index\n");
589 return 1;
590 }
591
592 if (!CreateTranslationUnit(Idx, ast_file, &TU))
593 return 1;
594
595 if ((fp = fopen(source_file, "r")) == NULL) {
596 fprintf(stderr, "Could not open '%s'\n", source_file);
597 return 1;
598 }
599
Douglas Gregorb9790342010-01-22 21:44:22 +0000600 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000601 for (;;) {
602 CXCursor cursor;
603 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000604
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000605 if (c == '\n') {
606 ++line;
607 col = 1;
608 } else
609 ++col;
610
611 /* Check the cursor at this position, and dump the previous one if we have
612 * found something new.
613 */
614 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
615 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
616 prevCursor.kind != CXCursor_InvalidFile) {
617 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000618 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000619 start_line = line;
620 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000621 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000622 if (c == EOF)
623 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000624
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000625 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000626 }
627
Ted Kremenek1c6da172009-11-17 19:37:36 +0000628 fclose(fp);
629 return 0;
630}
631
632/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000633/* Logic for testing clang_codeComplete(). */
634/******************************************************************************/
635
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000636/* Parse file:line:column from the input string. Returns 0 on success, non-zero
637 on failure. If successful, the pointer *filename will contain newly-allocated
638 memory (that will be owned by the caller) to store the file name. */
639int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000640 unsigned *column, unsigned *second_line,
641 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000642 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000643 const char *last_colon = strrchr(input, ':');
644 unsigned values[4], i;
645 unsigned num_values = (second_line && second_column)? 4 : 2;
646
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000647 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000648 if (!last_colon || last_colon == input) {
649 if (num_values == 4)
650 fprintf(stderr, "could not parse filename:line:column:line:column in "
651 "'%s'\n", input);
652 else
653 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000654 return 1;
655 }
656
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000657 for (i = 0; i != num_values; ++i) {
658 const char *prev_colon;
659
660 /* Parse the next line or column. */
661 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
662 if (*endptr != 0 && *endptr != ':') {
663 fprintf(stderr, "could not parse %s in '%s'\n",
664 (i % 2 ? "column" : "line"), input);
665 return 1;
666 }
667
668 if (i + 1 == num_values)
669 break;
670
671 /* Find the previous colon. */
672 prev_colon = last_colon - 1;
673 while (prev_colon != input && *prev_colon != ':')
674 --prev_colon;
675 if (prev_colon == input) {
676 fprintf(stderr, "could not parse %s in '%s'\n",
677 (i % 2 == 0? "column" : "line"), input);
678 return 1;
679 }
680
681 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000682 }
683
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000684 *line = values[0];
685 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000686
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000687 if (second_line && second_column) {
688 *second_line = values[2];
689 *second_column = values[3];
690 }
691
Douglas Gregor88d23952009-11-09 18:19:57 +0000692 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000693 *filename = (char*)malloc(last_colon - input + 1);
694 memcpy(*filename, input, last_colon - input);
695 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000696 return 0;
697}
698
699const char *
700clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
701 switch (Kind) {
702 case CXCompletionChunk_Optional: return "Optional";
703 case CXCompletionChunk_TypedText: return "TypedText";
704 case CXCompletionChunk_Text: return "Text";
705 case CXCompletionChunk_Placeholder: return "Placeholder";
706 case CXCompletionChunk_Informative: return "Informative";
707 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
708 case CXCompletionChunk_LeftParen: return "LeftParen";
709 case CXCompletionChunk_RightParen: return "RightParen";
710 case CXCompletionChunk_LeftBracket: return "LeftBracket";
711 case CXCompletionChunk_RightBracket: return "RightBracket";
712 case CXCompletionChunk_LeftBrace: return "LeftBrace";
713 case CXCompletionChunk_RightBrace: return "RightBrace";
714 case CXCompletionChunk_LeftAngle: return "LeftAngle";
715 case CXCompletionChunk_RightAngle: return "RightAngle";
716 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000717 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000718 case CXCompletionChunk_Colon: return "Colon";
719 case CXCompletionChunk_SemiColon: return "SemiColon";
720 case CXCompletionChunk_Equal: return "Equal";
721 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
722 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000723 }
724
725 return "Unknown";
726}
727
Douglas Gregor3ac73852009-11-09 16:04:45 +0000728void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000729 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000730
731 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000732 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000733 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000734 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000735 = clang_getCompletionChunkKind(completion_string, I);
736
737 if (Kind == CXCompletionChunk_Optional) {
738 fprintf(file, "{Optional ");
739 print_completion_string(
740 clang_getCompletionChunkCompletionString(completion_string, I),
741 file);
742 fprintf(file, "}");
743 continue;
744 }
745
Douglas Gregord5a20892009-11-09 17:05:28 +0000746 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000747 fprintf(file, "{%s %s}",
748 clang_getCompletionChunkKindSpelling(Kind),
749 text? text : "");
750 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000751}
752
753void print_completion_result(CXCompletionResult *completion_result,
754 CXClientData client_data) {
755 FILE *file = (FILE *)client_data;
756 fprintf(file, "%s:",
757 clang_getCursorKindSpelling(completion_result->CursorKind));
758 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000759 fprintf(file, "\n");
760}
761
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000762int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000763 const char *input = argv[1];
764 char *filename = 0;
765 unsigned line;
766 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000767 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000768 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000769 struct CXUnsavedFile *unsaved_files = 0;
770 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000771 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000772
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000773 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000774 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
775 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000776 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000777
Douglas Gregor735df882009-12-02 09:21:34 +0000778 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
779 return -1;
780
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000781 CIdx = clang_createIndex(0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000782 results = clang_codeComplete(CIdx,
783 argv[argc - 1], argc - num_unsaved_files - 3,
784 argv + num_unsaved_files + 2,
785 num_unsaved_files, unsaved_files,
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000786 filename, line, column,
787 PrintDiagnosticCallback, stderr);
788
Douglas Gregorec6762c2009-12-18 16:20:58 +0000789 if (results) {
790 unsigned i, n = results->NumResults;
791 for (i = 0; i != n; ++i)
792 print_completion_result(results->Results + i, stdout);
793 clang_disposeCodeCompleteResults(results);
794 }
795
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000796 clang_disposeIndex(CIdx);
797 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000798
Douglas Gregor735df882009-12-02 09:21:34 +0000799 free_remapped_files(unsaved_files, num_unsaved_files);
800
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000801 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000802}
803
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000804typedef struct {
805 char *filename;
806 unsigned line;
807 unsigned column;
808} CursorSourceLocation;
809
810int inspect_cursor_at(int argc, const char **argv) {
811 CXIndex CIdx;
812 int errorCode;
813 struct CXUnsavedFile *unsaved_files = 0;
814 int num_unsaved_files = 0;
815 CXTranslationUnit TU;
816 CXCursor Cursor;
817 CursorSourceLocation *Locations = 0;
818 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000819
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000820 /* Count the number of locations. */
821 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
822 ++NumLocations;
823
824 /* Parse the locations. */
825 assert(NumLocations > 0 && "Unable to count locations?");
826 Locations = (CursorSourceLocation *)malloc(
827 NumLocations * sizeof(CursorSourceLocation));
828 for (Loc = 0; Loc < NumLocations; ++Loc) {
829 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
830 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
831 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000832 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000833 return errorCode;
834 }
835
836 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
837 &num_unsaved_files))
838 return -1;
839
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000840 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000841 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
842 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000843 argv + num_unsaved_files + 1 + NumLocations,
844 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000845 unsaved_files,
846 PrintDiagnosticCallback,
847 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000848 if (!TU) {
849 fprintf(stderr, "unable to parse input\n");
850 return -1;
851 }
852
853 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000854 CXFile file = clang_getFile(TU, Locations[Loc].filename);
855 if (!file)
856 continue;
857
858 Cursor = clang_getCursor(TU,
859 clang_getLocation(TU, file, Locations[Loc].line,
860 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000861 PrintCursor(Cursor);
862 printf("\n");
863 free(Locations[Loc].filename);
864 }
865
866 clang_disposeTranslationUnit(TU);
867 clang_disposeIndex(CIdx);
868 free(Locations);
869 free_remapped_files(unsaved_files, num_unsaved_files);
870 return 0;
871}
872
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000873int perform_token_annotation(int argc, const char **argv) {
874 const char *input = argv[1];
875 char *filename = 0;
876 unsigned line, second_line;
877 unsigned column, second_column;
878 CXIndex CIdx;
879 CXTranslationUnit TU = 0;
880 int errorCode;
881 struct CXUnsavedFile *unsaved_files = 0;
882 int num_unsaved_files = 0;
883 CXToken *tokens;
884 unsigned num_tokens;
885 CXSourceRange range;
886 CXSourceLocation startLoc, endLoc;
887 CXFile file = 0;
888 CXCursor *cursors = 0;
889 unsigned i;
890
891 input += strlen("-test-annotate-tokens=");
892 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
893 &second_line, &second_column)))
894 return errorCode;
895
896 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
897 return -1;
898
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000899 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000900 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
901 argc - num_unsaved_files - 3,
902 argv + num_unsaved_files + 2,
903 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000904 unsaved_files,
905 PrintDiagnosticCallback,
906 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000907 if (!TU) {
908 fprintf(stderr, "unable to parse input\n");
909 clang_disposeIndex(CIdx);
910 free(filename);
911 free_remapped_files(unsaved_files, num_unsaved_files);
912 return -1;
913 }
914 errorCode = 0;
915
916 file = clang_getFile(TU, filename);
917 if (!file) {
918 fprintf(stderr, "file %s is not in this translation unit\n", filename);
919 errorCode = -1;
920 goto teardown;
921 }
922
923 startLoc = clang_getLocation(TU, file, line, column);
924 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
925 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
926 column);
927 errorCode = -1;
928 goto teardown;
929 }
930
931 endLoc = clang_getLocation(TU, file, second_line, second_column);
932 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
933 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
934 second_line, second_column);
935 errorCode = -1;
936 goto teardown;
937 }
938
939 range = clang_getRange(startLoc, endLoc);
940 clang_tokenize(TU, range, &tokens, &num_tokens);
941 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
942 clang_annotateTokens(TU, tokens, num_tokens, cursors);
943 for (i = 0; i != num_tokens; ++i) {
944 const char *kind = "<unknown>";
945 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
946 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
947 unsigned start_line, start_column, end_line, end_column;
948
949 switch (clang_getTokenKind(tokens[i])) {
950 case CXToken_Punctuation: kind = "Punctuation"; break;
951 case CXToken_Keyword: kind = "Keyword"; break;
952 case CXToken_Identifier: kind = "Identifier"; break;
953 case CXToken_Literal: kind = "Literal"; break;
954 case CXToken_Comment: kind = "Comment"; break;
955 }
956 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000957 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000958 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000959 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000960 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
961 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000962 if (!clang_isInvalid(cursors[i].kind)) {
963 printf(" ");
964 PrintCursor(cursors[i]);
965 }
966 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000967 }
968 free(cursors);
969
970 teardown:
971 clang_disposeTranslationUnit(TU);
972 clang_disposeIndex(CIdx);
973 free(filename);
974 free_remapped_files(unsaved_files, num_unsaved_files);
975 return errorCode;
976}
977
Ted Kremenek0d435192009-11-17 18:13:31 +0000978/******************************************************************************/
979/* Command line processing. */
980/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000981
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000982static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000983 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000984 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000985 if (strcmp(s, "-usrs") == 0)
986 return USRVisitor;
987 return NULL;
988}
989
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000990static void print_usage(void) {
991 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000992 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000993 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000994 " c-index-test -test-file-scan <AST file> <source file> "
995 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000996 " c-index-test -test-load-tu <AST file> <symbol filter> "
997 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000998 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
999 "[FileCheck prefix]\n"
1000 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001001 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001002 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001003 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1004 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1005 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001006 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001007 " all - load all symbols, including those from PCH\n"
1008 " local - load all symbols except those in PCH\n"
1009 " category - only load ObjC categories (non-PCH)\n"
1010 " interface - only load ObjC interfaces (non-PCH)\n"
1011 " protocol - only load ObjC protocols (non-PCH)\n"
1012 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001013 " typedef - only load typdefs (non-PCH)\n"
1014 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001015}
1016
1017int main(int argc, const char **argv) {
1018 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1019 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001020 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1021 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001022 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001023 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001024 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001025 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1026 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001027 }
1028 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001029 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001030 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001031 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001032 }
1033 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001034 return perform_file_scan(argv[2], argv[3],
1035 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001036 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1037 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001038 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1039 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1040 PrintInclusionStack);
1041 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1042 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1043 PrintInclusionStack);
1044
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001045 print_usage();
1046 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001047}