blob: c5f8431c348e8d08e4f11bd93eafd10a85c622b1 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Douglas Gregor0c8296d2009-11-07 00:00:49 +00004#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00005#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00006#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00007#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008
Ted Kremenek0d435192009-11-17 18:13:31 +00009/******************************************************************************/
10/* Utility functions. */
11/******************************************************************************/
12
John Thompson2e06fc82009-10-27 13:42:56 +000013#ifdef _MSC_VER
14char *basename(const char* path)
15{
16 char* base1 = (char*)strrchr(path, '/');
17 char* base2 = (char*)strrchr(path, '\\');
18 if (base1 && base2)
19 return((base1 > base2) ? base1 + 1 : base2 + 1);
20 else if (base1)
21 return(base1 + 1);
22 else if (base2)
23 return(base2 + 1);
24
25 return((char*)path);
26}
27#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000028extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000029#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000030
Daniel Dunbar51b058c2010-02-14 08:32:24 +000031static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
32 unsigned end_line, unsigned end_column) {
33 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000034 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000035}
36
Ted Kremenek1c6da172009-11-17 19:37:36 +000037static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
38 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000039
Douglas Gregora88084b2010-02-18 18:08:43 +000040 *TU = clang_createTranslationUnit(Idx, file);
Ted Kremenek1c6da172009-11-17 19:37:36 +000041 if (!TU) {
42 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
43 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000044 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000045 return 1;
46}
47
Douglas Gregor4db64a42010-01-23 00:14:00 +000048void free_remapped_files(struct CXUnsavedFile *unsaved_files,
49 int num_unsaved_files) {
50 int i;
51 for (i = 0; i != num_unsaved_files; ++i) {
52 free((char *)unsaved_files[i].Filename);
53 free((char *)unsaved_files[i].Contents);
54 }
55}
56
57int parse_remapped_files(int argc, const char **argv, int start_arg,
58 struct CXUnsavedFile **unsaved_files,
59 int *num_unsaved_files) {
60 int i;
61 int arg;
62 int prefix_len = strlen("-remap-file=");
63 *unsaved_files = 0;
64 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000065
Douglas Gregor4db64a42010-01-23 00:14:00 +000066 /* Count the number of remapped files. */
67 for (arg = start_arg; arg < argc; ++arg) {
68 if (strncmp(argv[arg], "-remap-file=", prefix_len))
69 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000070
Douglas Gregor4db64a42010-01-23 00:14:00 +000071 ++*num_unsaved_files;
72 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000073
Douglas Gregor4db64a42010-01-23 00:14:00 +000074 if (*num_unsaved_files == 0)
75 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000076
Douglas Gregor4db64a42010-01-23 00:14:00 +000077 *unsaved_files
Ted Kremeneke68fff62010-02-17 00:41:32 +000078 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
Douglas Gregor4db64a42010-01-23 00:14:00 +000079 *num_unsaved_files);
80 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
81 struct CXUnsavedFile *unsaved = *unsaved_files + i;
82 const char *arg_string = argv[arg] + prefix_len;
83 int filename_len;
84 char *filename;
85 char *contents;
86 FILE *to_file;
87 const char *semi = strchr(arg_string, ';');
88 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000089 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +000090 "error: -remap-file=from;to argument is missing semicolon\n");
91 free_remapped_files(*unsaved_files, i);
92 *unsaved_files = 0;
93 *num_unsaved_files = 0;
94 return -1;
95 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000096
Douglas Gregor4db64a42010-01-23 00:14:00 +000097 /* Open the file that we're remapping to. */
98 to_file = fopen(semi + 1, "r");
99 if (!to_file) {
100 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
101 semi + 1);
102 free_remapped_files(*unsaved_files, i);
103 *unsaved_files = 0;
104 *num_unsaved_files = 0;
105 return -1;
106 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000107
Douglas Gregor4db64a42010-01-23 00:14:00 +0000108 /* Determine the length of the file we're remapping to. */
109 fseek(to_file, 0, SEEK_END);
110 unsaved->Length = ftell(to_file);
111 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000112
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113 /* Read the contents of the file we're remapping to. */
114 contents = (char *)malloc(unsaved->Length + 1);
115 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
116 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
117 (feof(to_file) ? "EOF" : "error"), semi + 1);
118 fclose(to_file);
119 free_remapped_files(*unsaved_files, i);
120 *unsaved_files = 0;
121 *num_unsaved_files = 0;
122 return -1;
123 }
124 contents[unsaved->Length] = 0;
125 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 /* Close the file. */
128 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000129
Douglas Gregor4db64a42010-01-23 00:14:00 +0000130 /* Copy the file name that we're remapping from. */
131 filename_len = semi - arg_string;
132 filename = (char *)malloc(filename_len + 1);
133 memcpy(filename, arg_string, filename_len);
134 filename[filename_len] = 0;
135 unsaved->Filename = filename;
136 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000137
Douglas Gregor4db64a42010-01-23 00:14:00 +0000138 return 0;
139}
140
Ted Kremenek0d435192009-11-17 18:13:31 +0000141/******************************************************************************/
142/* Pretty-printing. */
143/******************************************************************************/
144
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000145static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000146 if (clang_isInvalid(Cursor.kind)) {
147 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
148 printf("Invalid Cursor => %s", clang_getCString(ks));
149 clang_disposeString(ks);
150 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000151 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000152 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000153 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000154 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000155
156 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000157 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000158 printf("%s=%s", clang_getCString(ks),
159 clang_getCString(string));
160 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000161 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000162
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000163 Referenced = clang_getCursorReferenced(Cursor);
164 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
165 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000166 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000167 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000168 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000169
170 if (clang_isCursorDefinition(Cursor))
171 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000172 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000173}
Steve Naroff89922f82009-08-31 00:59:03 +0000174
Ted Kremeneke68fff62010-02-17 00:41:32 +0000175static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000176 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000177 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000178 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000179 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000180 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000181 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000182 clang_disposeString(source);
183 return "<invalid loc>";
184 }
185 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000186 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000187 clang_disposeString(source);
188 return b;
189 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000190}
191
Ted Kremenek0d435192009-11-17 18:13:31 +0000192/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000193/* Callbacks. */
194/******************************************************************************/
195
196typedef void (*PostVisitTU)(CXTranslationUnit);
197
Douglas Gregora88084b2010-02-18 18:08:43 +0000198void PrintDiagnostic(CXDiagnostic Diagnostic) {
199 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000200 CXFile file;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000201 CXString text;
Douglas Gregor4c589232010-02-18 19:08:21 +0000202 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
203 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
204 unsigned i, num_fixits;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000205
Douglas Gregor4c589232010-02-18 19:08:21 +0000206 clang_displayDiagnostic(Diagnostic, out, display_opts);
207 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000208 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000209
Douglas Gregor5352ac02010-01-28 00:27:43 +0000210 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor4c589232010-02-18 19:08:21 +0000211 &file, 0, 0, 0);
212 if (!file)
213 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000214
Douglas Gregor4c589232010-02-18 19:08:21 +0000215 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
216 for (i = 0; i != num_fixits; ++i) {
217 switch (clang_getDiagnosticFixItKind(Diagnostic, i)) {
218 case CXFixIt_Insertion: {
219 CXSourceLocation insertion_loc;
220 CXFile insertion_file;
221 unsigned insertion_line, insertion_column;
222 text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc);
223 clang_getInstantiationLocation(insertion_loc, &insertion_file,
224 &insertion_line, &insertion_column, 0);
225 if (insertion_file == file)
226 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
227 clang_getCString(text), insertion_line, insertion_column);
228 clang_disposeString(text);
229 break;
Douglas Gregor51c6d382010-01-29 00:41:11 +0000230 }
Douglas Gregor4c589232010-02-18 19:08:21 +0000231
232 case CXFixIt_Removal: {
233 CXFile start_file, end_file;
234 unsigned start_line, start_column, end_line, end_column;
235 CXSourceRange remove_range
236 = clang_getDiagnosticFixItRemoval(Diagnostic, i);
237 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
238 &start_file, &start_line, &start_column,
239 0);
240 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
241 &end_file, &end_line, &end_column, 0);
242 if (start_file == file && end_file == file) {
243 fprintf(out, "FIX-IT: Remove ");
244 PrintExtent(out, start_line, start_column, end_line, end_column);
245 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000246 }
Douglas Gregor4c589232010-02-18 19:08:21 +0000247 break;
248 }
249
250 case CXFixIt_Replacement: {
251 CXFile start_file, end_file;
252 unsigned start_line, start_column, end_line, end_column;
253 CXSourceRange remove_range;
254 text = clang_getDiagnosticFixItReplacement(Diagnostic, i,&remove_range);
255 clang_getInstantiationLocation(clang_getRangeStart(remove_range),
256 &start_file, &start_line, &start_column,
257 0);
258 clang_getInstantiationLocation(clang_getRangeEnd(remove_range),
259 &end_file, &end_line, &end_column, 0);
260 if (start_file == end_file) {
261 fprintf(out, "FIX-IT: Replace ");
262 PrintExtent(out, start_line, start_column, end_line, end_column);
263 fprintf(out, " with \"%s\"\n", clang_getCString(text));
Douglas Gregor51c6d382010-01-29 00:41:11 +0000264 }
Douglas Gregor4c589232010-02-18 19:08:21 +0000265 clang_disposeString(text);
266 break;
267 }
Douglas Gregor51c6d382010-01-29 00:41:11 +0000268 }
269 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000270}
271
Douglas Gregora88084b2010-02-18 18:08:43 +0000272void PrintDiagnostics(CXTranslationUnit TU) {
273 int i, n = clang_getNumDiagnostics(TU);
274 for (i = 0; i != n; ++i) {
275 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
276 PrintDiagnostic(Diag);
277 clang_disposeDiagnostic(Diag);
278 }
279}
280
Ted Kremenekce2ae882010-01-26 17:59:48 +0000281/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000282/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000283/******************************************************************************/
284
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000285static const char *FileCheckPrefix = "CHECK";
286
Douglas Gregora7bde202010-01-19 00:34:46 +0000287static void PrintCursorExtent(CXCursor C) {
288 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000289 CXFile begin_file, end_file;
290 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000291
Douglas Gregor1db19de2010-01-19 21:36:55 +0000292 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000293 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000294 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000295 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000296 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000297 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000298
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000299 printf(" Extent=");
300 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000301}
302
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000303/* Data used by all of the visitors. */
304typedef struct {
305 CXTranslationUnit TU;
306 enum CXCursorKind *Filter;
307} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000308
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000309
Ted Kremeneke68fff62010-02-17 00:41:32 +0000310enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000311 CXCursor Parent,
312 CXClientData ClientData) {
313 VisitorData *Data = (VisitorData *)ClientData;
314 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000315 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000316 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000317 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000318 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000319 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000320 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000321 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000322 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000323 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000324 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000325
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000326 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000327}
Steve Naroff50398192009-08-28 15:28:48 +0000328
Ted Kremeneke68fff62010-02-17 00:41:32 +0000329static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000330 CXCursor Parent,
331 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000332 const char *startBuf, *endBuf;
333 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
334 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000335 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000336
Douglas Gregorb6998662010-01-19 19:34:47 +0000337 if (Cursor.kind != CXCursor_FunctionDecl ||
338 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000339 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000340
341 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
342 &startLine, &startColumn,
343 &endLine, &endColumn);
344 /* Probe the entire body, looking for both decls and refs. */
345 curLine = startLine;
346 curColumn = startColumn;
347
348 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000349 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000350 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000351 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000352
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000353 if (*startBuf == '\n') {
354 startBuf++;
355 curLine++;
356 curColumn = 1;
357 } else if (*startBuf != '\t')
358 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000359
Douglas Gregor98258af2010-01-18 22:46:11 +0000360 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000361 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000362
Douglas Gregor1db19de2010-01-19 21:36:55 +0000363 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000364 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000365 CXSourceLocation RefLoc
366 = clang_getLocation(Data->TU, file, curLine, curColumn);
367 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000368 if (Ref.kind == CXCursor_NoDeclFound) {
369 /* Nothing found here; that's fine. */
370 } else if (Ref.kind != CXCursor_FunctionDecl) {
371 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
372 curLine, curColumn);
373 PrintCursor(Ref);
374 printf("\n");
375 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000376 }
Ted Kremenek74844072010-02-17 00:41:20 +0000377 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000378 startBuf++;
379 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000380
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000381 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000382}
383
Ted Kremenek7d405622010-01-12 23:34:26 +0000384/******************************************************************************/
385/* USR testing. */
386/******************************************************************************/
387
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000388enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
389 CXClientData ClientData) {
390 VisitorData *Data = (VisitorData *)ClientData;
391 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000392 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000393 if (!clang_getCString(USR)) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000394 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000395 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000396 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000397 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C),
398 clang_getCString(USR));
Douglas Gregora7bde202010-01-19 00:34:46 +0000399 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000400 printf("\n");
401 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000402
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000403 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000404 }
405
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000406 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000407}
408
409/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000410/* Inclusion stack testing. */
411/******************************************************************************/
412
413void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
414 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000415
Ted Kremenek16b55a72010-01-26 19:31:51 +0000416 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000417 CXString fname;
418
419 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000420 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000421 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000422
Ted Kremenek16b55a72010-01-26 19:31:51 +0000423 for (i = 0; i < includeStackLen; ++i) {
424 CXFile includingFile;
425 unsigned line, column;
426 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
427 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000428 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000429 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000430 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000431 }
432 printf("\n");
433}
434
435void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000436 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000437}
438
439/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000440/* Loading ASTs/source. */
441/******************************************************************************/
442
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000443static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000444 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000445 CXCursorVisitor Visitor,
446 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000447
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000448 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000449 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000450
451 if (Visitor) {
452 enum CXCursorKind K = CXCursor_NotImplemented;
453 enum CXCursorKind *ck = &K;
454 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000455
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000456 /* Perform some simple filtering. */
457 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000458 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000459 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
460 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
461 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
462 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
463 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
464 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
465 else {
466 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
467 return 1;
468 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000469
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000470 Data.TU = TU;
471 Data.Filter = ck;
472 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000473 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000474
Ted Kremenekce2ae882010-01-26 17:59:48 +0000475 if (PV)
476 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000477
Douglas Gregora88084b2010-02-18 18:08:43 +0000478 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000479 clang_disposeTranslationUnit(TU);
480 return 0;
481}
482
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000483int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000484 const char *prefix, CXCursorVisitor Visitor,
485 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000486 CXIndex Idx;
487 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000488 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000489 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregorb2710712010-02-18 20:11:31 +0000490 !strcmp(filter, "local") ? 1 : 0,
491 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000492
Ted Kremenek020a0952010-02-11 07:41:25 +0000493 if (!CreateTranslationUnit(Idx, file, &TU)) {
494 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000495 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000496 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000497
Ted Kremenek020a0952010-02-11 07:41:25 +0000498 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
499 clang_disposeIndex(Idx);
500 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000501}
502
Ted Kremenekce2ae882010-01-26 17:59:48 +0000503int perform_test_load_source(int argc, const char **argv,
504 const char *filter, CXCursorVisitor Visitor,
505 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000506 const char *UseExternalASTs =
507 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000508 CXIndex Idx;
509 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000510 struct CXUnsavedFile *unsaved_files = 0;
511 int num_unsaved_files = 0;
512 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000513
Daniel Dunbarada487d2009-12-01 02:03:10 +0000514 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregorb2710712010-02-18 20:11:31 +0000515 !strcmp(filter, "local") ? 1 : 0,
516 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000517
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000518 if (UseExternalASTs && strlen(UseExternalASTs))
519 clang_setUseExternalASTGeneration(Idx, 1);
520
Ted Kremenek020a0952010-02-11 07:41:25 +0000521 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
522 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000523 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000524 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000525
Ted Kremeneke68fff62010-02-17 00:41:32 +0000526 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
527 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000528 argv + num_unsaved_files,
529 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000530 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000531 if (!TU) {
532 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000533 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000534 return 1;
535 }
536
Ted Kremenekce2ae882010-01-26 17:59:48 +0000537 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000538 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000539 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000540 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000541}
542
Ted Kremenek0d435192009-11-17 18:13:31 +0000543/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000544/* Logic for testing clang_getCursor(). */
545/******************************************************************************/
546
547static void print_cursor_file_scan(CXCursor cursor,
548 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000549 unsigned end_line, unsigned end_col,
550 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000551 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000552 if (prefix)
553 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000554 PrintExtent(stdout, start_line, start_col, end_line, end_col);
555 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000556 PrintCursor(cursor);
557 printf("\n");
558}
559
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000560static int perform_file_scan(const char *ast_file, const char *source_file,
561 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000562 CXIndex Idx;
563 CXTranslationUnit TU;
564 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000565 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000566 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000567 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000568 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000569
Douglas Gregorb2710712010-02-18 20:11:31 +0000570 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
571 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000572 fprintf(stderr, "Could not create Index\n");
573 return 1;
574 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000575
Ted Kremenek1c6da172009-11-17 19:37:36 +0000576 if (!CreateTranslationUnit(Idx, ast_file, &TU))
577 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578
Ted Kremenek1c6da172009-11-17 19:37:36 +0000579 if ((fp = fopen(source_file, "r")) == NULL) {
580 fprintf(stderr, "Could not open '%s'\n", source_file);
581 return 1;
582 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000583
Douglas Gregorb9790342010-01-22 21:44:22 +0000584 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000585 for (;;) {
586 CXCursor cursor;
587 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000588
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000589 if (c == '\n') {
590 ++line;
591 col = 1;
592 } else
593 ++col;
594
595 /* Check the cursor at this position, and dump the previous one if we have
596 * found something new.
597 */
598 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
599 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
600 prevCursor.kind != CXCursor_InvalidFile) {
601 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000602 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000603 start_line = line;
604 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000605 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000606 if (c == EOF)
607 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000608
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000609 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000610 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000611
Ted Kremenek1c6da172009-11-17 19:37:36 +0000612 fclose(fp);
613 return 0;
614}
615
616/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000617/* Logic for testing clang_codeComplete(). */
618/******************************************************************************/
619
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000620/* Parse file:line:column from the input string. Returns 0 on success, non-zero
621 on failure. If successful, the pointer *filename will contain newly-allocated
622 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000623int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000624 unsigned *column, unsigned *second_line,
625 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000626 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000627 const char *last_colon = strrchr(input, ':');
628 unsigned values[4], i;
629 unsigned num_values = (second_line && second_column)? 4 : 2;
630
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000631 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000632 if (!last_colon || last_colon == input) {
633 if (num_values == 4)
634 fprintf(stderr, "could not parse filename:line:column:line:column in "
635 "'%s'\n", input);
636 else
637 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000638 return 1;
639 }
640
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000641 for (i = 0; i != num_values; ++i) {
642 const char *prev_colon;
643
644 /* Parse the next line or column. */
645 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
646 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000647 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000648 (i % 2 ? "column" : "line"), input);
649 return 1;
650 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000651
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000652 if (i + 1 == num_values)
653 break;
654
655 /* Find the previous colon. */
656 prev_colon = last_colon - 1;
657 while (prev_colon != input && *prev_colon != ':')
658 --prev_colon;
659 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000660 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000661 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000662 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000663 }
664
665 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000666 }
667
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000668 *line = values[0];
669 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000670
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000671 if (second_line && second_column) {
672 *second_line = values[2];
673 *second_column = values[3];
674 }
675
Douglas Gregor88d23952009-11-09 18:19:57 +0000676 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000677 *filename = (char*)malloc(last_colon - input + 1);
678 memcpy(*filename, input, last_colon - input);
679 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000680 return 0;
681}
682
683const char *
684clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
685 switch (Kind) {
686 case CXCompletionChunk_Optional: return "Optional";
687 case CXCompletionChunk_TypedText: return "TypedText";
688 case CXCompletionChunk_Text: return "Text";
689 case CXCompletionChunk_Placeholder: return "Placeholder";
690 case CXCompletionChunk_Informative: return "Informative";
691 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
692 case CXCompletionChunk_LeftParen: return "LeftParen";
693 case CXCompletionChunk_RightParen: return "RightParen";
694 case CXCompletionChunk_LeftBracket: return "LeftBracket";
695 case CXCompletionChunk_RightBracket: return "RightBracket";
696 case CXCompletionChunk_LeftBrace: return "LeftBrace";
697 case CXCompletionChunk_RightBrace: return "RightBrace";
698 case CXCompletionChunk_LeftAngle: return "LeftAngle";
699 case CXCompletionChunk_RightAngle: return "RightAngle";
700 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000701 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000702 case CXCompletionChunk_Colon: return "Colon";
703 case CXCompletionChunk_SemiColon: return "SemiColon";
704 case CXCompletionChunk_Equal: return "Equal";
705 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
706 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000707 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000708
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000709 return "Unknown";
710}
711
Douglas Gregor3ac73852009-11-09 16:04:45 +0000712void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000713 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000714
Douglas Gregor3ac73852009-11-09 16:04:45 +0000715 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000716 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000717 CXString text;
718 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000719 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000720 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000721
Douglas Gregor3ac73852009-11-09 16:04:45 +0000722 if (Kind == CXCompletionChunk_Optional) {
723 fprintf(file, "{Optional ");
724 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000725 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000726 file);
727 fprintf(file, "}");
728 continue;
729 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000730
Douglas Gregord5a20892009-11-09 17:05:28 +0000731 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000732 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000733 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000734 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000735 cstr ? cstr : "");
736 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000737 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000738
Douglas Gregor3ac73852009-11-09 16:04:45 +0000739}
740
741void print_completion_result(CXCompletionResult *completion_result,
742 CXClientData client_data) {
743 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000744 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
745
746 fprintf(file, "%s:", clang_getCString(ks));
747 clang_disposeString(ks);
748
Douglas Gregor3ac73852009-11-09 16:04:45 +0000749 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000750 fprintf(file, "\n");
751}
752
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000753int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000754 const char *input = argv[1];
755 char *filename = 0;
756 unsigned line;
757 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000758 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000759 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000760 struct CXUnsavedFile *unsaved_files = 0;
761 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000762 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000763
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000764 input += strlen("-code-completion-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000765 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000766 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000767 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000768
Douglas Gregor735df882009-12-02 09:21:34 +0000769 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
770 return -1;
771
Douglas Gregorb2710712010-02-18 20:11:31 +0000772 CIdx = clang_createIndex(0, 1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000773 results = clang_codeComplete(CIdx,
774 argv[argc - 1], argc - num_unsaved_files - 3,
775 argv + num_unsaved_files + 2,
Douglas Gregorec6762c2009-12-18 16:20:58 +0000776 num_unsaved_files, unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000777 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000778
Douglas Gregorec6762c2009-12-18 16:20:58 +0000779 if (results) {
780 unsigned i, n = results->NumResults;
781 for (i = 0; i != n; ++i)
782 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000783 n = clang_codeCompleteGetNumDiagnostics(results);
784 for (i = 0; i != n; ++i) {
785 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
786 PrintDiagnostic(diag);
787 clang_disposeDiagnostic(diag);
788 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000789 clang_disposeCodeCompleteResults(results);
790 }
791
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000792 clang_disposeIndex(CIdx);
793 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000794
Douglas Gregor735df882009-12-02 09:21:34 +0000795 free_remapped_files(unsaved_files, num_unsaved_files);
796
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000797 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000798}
799
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000800typedef struct {
801 char *filename;
802 unsigned line;
803 unsigned column;
804} CursorSourceLocation;
805
806int inspect_cursor_at(int argc, const char **argv) {
807 CXIndex CIdx;
808 int errorCode;
809 struct CXUnsavedFile *unsaved_files = 0;
810 int num_unsaved_files = 0;
811 CXTranslationUnit TU;
812 CXCursor Cursor;
813 CursorSourceLocation *Locations = 0;
814 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000815
Ted Kremeneke68fff62010-02-17 00:41:32 +0000816 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000817 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
818 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000819
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000820 /* Parse the locations. */
821 assert(NumLocations > 0 && "Unable to count locations?");
822 Locations = (CursorSourceLocation *)malloc(
823 NumLocations * sizeof(CursorSourceLocation));
824 for (Loc = 0; Loc < NumLocations; ++Loc) {
825 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000826 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
827 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000828 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000829 return errorCode;
830 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000831
832 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000833 &num_unsaved_files))
834 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000835
Douglas Gregorb2710712010-02-18 20:11:31 +0000836 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000837 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
838 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000839 argv + num_unsaved_files + 1 + NumLocations,
840 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000841 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000842 if (!TU) {
843 fprintf(stderr, "unable to parse input\n");
844 return -1;
845 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000846
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000847 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000848 CXFile file = clang_getFile(TU, Locations[Loc].filename);
849 if (!file)
850 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000851
852 Cursor = clang_getCursor(TU,
853 clang_getLocation(TU, file, Locations[Loc].line,
854 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000855 PrintCursor(Cursor);
856 printf("\n");
857 free(Locations[Loc].filename);
858 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000859
Douglas Gregora88084b2010-02-18 18:08:43 +0000860 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000861 clang_disposeTranslationUnit(TU);
862 clang_disposeIndex(CIdx);
863 free(Locations);
864 free_remapped_files(unsaved_files, num_unsaved_files);
865 return 0;
866}
867
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000868int perform_token_annotation(int argc, const char **argv) {
869 const char *input = argv[1];
870 char *filename = 0;
871 unsigned line, second_line;
872 unsigned column, second_column;
873 CXIndex CIdx;
874 CXTranslationUnit TU = 0;
875 int errorCode;
876 struct CXUnsavedFile *unsaved_files = 0;
877 int num_unsaved_files = 0;
878 CXToken *tokens;
879 unsigned num_tokens;
880 CXSourceRange range;
881 CXSourceLocation startLoc, endLoc;
882 CXFile file = 0;
883 CXCursor *cursors = 0;
884 unsigned i;
885
886 input += strlen("-test-annotate-tokens=");
887 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
888 &second_line, &second_column)))
889 return errorCode;
890
891 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
892 return -1;
893
Douglas Gregorb2710712010-02-18 20:11:31 +0000894 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000895 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
896 argc - num_unsaved_files - 3,
897 argv + num_unsaved_files + 2,
898 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000899 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000900 if (!TU) {
901 fprintf(stderr, "unable to parse input\n");
902 clang_disposeIndex(CIdx);
903 free(filename);
904 free_remapped_files(unsaved_files, num_unsaved_files);
905 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000906 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000907 errorCode = 0;
908
909 file = clang_getFile(TU, filename);
910 if (!file) {
911 fprintf(stderr, "file %s is not in this translation unit\n", filename);
912 errorCode = -1;
913 goto teardown;
914 }
915
916 startLoc = clang_getLocation(TU, file, line, column);
917 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000918 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000919 column);
920 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000921 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000922 }
923
924 endLoc = clang_getLocation(TU, file, second_line, second_column);
925 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000926 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000927 second_line, second_column);
928 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000929 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000930 }
931
932 range = clang_getRange(startLoc, endLoc);
933 clang_tokenize(TU, range, &tokens, &num_tokens);
934 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
935 clang_annotateTokens(TU, tokens, num_tokens, cursors);
936 for (i = 0; i != num_tokens; ++i) {
937 const char *kind = "<unknown>";
938 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
939 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
940 unsigned start_line, start_column, end_line, end_column;
941
942 switch (clang_getTokenKind(tokens[i])) {
943 case CXToken_Punctuation: kind = "Punctuation"; break;
944 case CXToken_Keyword: kind = "Keyword"; break;
945 case CXToken_Identifier: kind = "Identifier"; break;
946 case CXToken_Literal: kind = "Literal"; break;
947 case CXToken_Comment: kind = "Comment"; break;
948 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000949 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000950 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000951 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000952 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000953 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
954 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000955 if (!clang_isInvalid(cursors[i].kind)) {
956 printf(" ");
957 PrintCursor(cursors[i]);
958 }
959 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000960 }
961 free(cursors);
962
963 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +0000964 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000965 clang_disposeTranslationUnit(TU);
966 clang_disposeIndex(CIdx);
967 free(filename);
968 free_remapped_files(unsaved_files, num_unsaved_files);
969 return errorCode;
970}
971
Ted Kremenek0d435192009-11-17 18:13:31 +0000972/******************************************************************************/
973/* Command line processing. */
974/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000975
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000976static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000977 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000978 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000979 if (strcmp(s, "-usrs") == 0)
980 return USRVisitor;
981 return NULL;
982}
983
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000984static void print_usage(void) {
985 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000986 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000987 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000988 " c-index-test -test-file-scan <AST file> <source file> "
989 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000990 " c-index-test -test-load-tu <AST file> <symbol filter> "
991 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000992 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
993 "[FileCheck prefix]\n"
994 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000995 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000996 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +0000997 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
998 " c-index-test -test-inclusion-stack-source {<args>}*\n"
999 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001000 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001001 " all - load all symbols, including those from PCH\n"
1002 " local - load all symbols except those in PCH\n"
1003 " category - only load ObjC categories (non-PCH)\n"
1004 " interface - only load ObjC interfaces (non-PCH)\n"
1005 " protocol - only load ObjC protocols (non-PCH)\n"
1006 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001007 " typedef - only load typdefs (non-PCH)\n"
1008 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001009}
1010
1011int main(int argc, const char **argv) {
Douglas Gregorb9c8a242010-02-18 20:22:25 +00001012 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001013 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1014 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001015 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1016 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001017 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001018 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001019 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001020 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1021 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001022 }
1023 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001024 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001025 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001026 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001027 }
1028 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001029 return perform_file_scan(argv[2], argv[3],
1030 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001031 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1032 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001033 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1034 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1035 PrintInclusionStack);
1036 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1037 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1038 PrintInclusionStack);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001039
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001040 print_usage();
1041 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001042}