blob: 4f56efe1570c7c292b36319ca2a8b5cf01f697eb [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,
38 end_line, end_column);
39}
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
225 PrintExtent(out, start_line, start_column, end_line, end_column+1);
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 ");
280 PrintExtent(out, start_line, start_column, end_line, end_column+1);
281 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 ");
298 PrintExtent(out, start_line, start_column, end_line, end_column+1);
299 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;
582 unsigned line;
583 CXCursor prevCursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000584 CXFile file;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000585 unsigned printed;
586 unsigned start_line, start_col, last_line, last_col;
587 size_t i;
588
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000589 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000590 fprintf(stderr, "Could not create Index\n");
591 return 1;
592 }
593
594 if (!CreateTranslationUnit(Idx, ast_file, &TU))
595 return 1;
596
597 if ((fp = fopen(source_file, "r")) == NULL) {
598 fprintf(stderr, "Could not open '%s'\n", source_file);
599 return 1;
600 }
601
602 line = 0;
603 prevCursor = clang_getNullCursor();
604 printed = 0;
605 start_line = last_line = 1;
606 start_col = last_col = 1;
607
Douglas Gregorb9790342010-01-22 21:44:22 +0000608 file = clang_getFile(TU, source_file);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000609 while (!feof(fp)) {
Benjamin Kramera9933b92009-11-17 20:51:40 +0000610 size_t len = 0;
611 int c;
612
613 while ((c = fgetc(fp)) != EOF) {
614 len++;
615 if (c == '\n')
616 break;
617 }
618
Ted Kremenek1c6da172009-11-17 19:37:36 +0000619 ++line;
620
621 for (i = 0; i < len ; ++i) {
622 CXCursor cursor;
Douglas Gregorb9790342010-01-22 21:44:22 +0000623 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, i+1));
Ted Kremenek1c6da172009-11-17 19:37:36 +0000624
625 if (!clang_equalCursors(cursor, prevCursor) &&
626 prevCursor.kind != CXCursor_InvalidFile) {
627 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000628 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000629 printed = 1;
630 start_line = line;
631 start_col = (unsigned) i+1;
632 }
633 else {
634 printed = 0;
635 }
636
637 prevCursor = cursor;
638 last_line = line;
639 last_col = (unsigned) i+1;
640 }
641 }
642
643 if (!printed && prevCursor.kind != CXCursor_InvalidFile) {
644 print_cursor_file_scan(prevCursor, start_line, start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000645 last_line, last_col, prefix);
Ted Kremenek1c6da172009-11-17 19:37:36 +0000646 }
647
648 fclose(fp);
649 return 0;
650}
651
652/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000653/* Logic for testing clang_codeComplete(). */
654/******************************************************************************/
655
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000656/* Parse file:line:column from the input string. Returns 0 on success, non-zero
657 on failure. If successful, the pointer *filename will contain newly-allocated
658 memory (that will be owned by the caller) to store the file name. */
659int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000660 unsigned *column, unsigned *second_line,
661 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000662 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000663 const char *last_colon = strrchr(input, ':');
664 unsigned values[4], i;
665 unsigned num_values = (second_line && second_column)? 4 : 2;
666
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000667 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000668 if (!last_colon || last_colon == input) {
669 if (num_values == 4)
670 fprintf(stderr, "could not parse filename:line:column:line:column in "
671 "'%s'\n", input);
672 else
673 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000674 return 1;
675 }
676
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000677 for (i = 0; i != num_values; ++i) {
678 const char *prev_colon;
679
680 /* Parse the next line or column. */
681 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
682 if (*endptr != 0 && *endptr != ':') {
683 fprintf(stderr, "could not parse %s in '%s'\n",
684 (i % 2 ? "column" : "line"), input);
685 return 1;
686 }
687
688 if (i + 1 == num_values)
689 break;
690
691 /* Find the previous colon. */
692 prev_colon = last_colon - 1;
693 while (prev_colon != input && *prev_colon != ':')
694 --prev_colon;
695 if (prev_colon == input) {
696 fprintf(stderr, "could not parse %s in '%s'\n",
697 (i % 2 == 0? "column" : "line"), input);
698 return 1;
699 }
700
701 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000702 }
703
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000704 *line = values[0];
705 *column = values[1];
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000706
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000707 if (second_line && second_column) {
708 *second_line = values[2];
709 *second_column = values[3];
710 }
711
Douglas Gregor88d23952009-11-09 18:19:57 +0000712 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000713 *filename = (char*)malloc(last_colon - input + 1);
714 memcpy(*filename, input, last_colon - input);
715 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000716 return 0;
717}
718
719const char *
720clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
721 switch (Kind) {
722 case CXCompletionChunk_Optional: return "Optional";
723 case CXCompletionChunk_TypedText: return "TypedText";
724 case CXCompletionChunk_Text: return "Text";
725 case CXCompletionChunk_Placeholder: return "Placeholder";
726 case CXCompletionChunk_Informative: return "Informative";
727 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
728 case CXCompletionChunk_LeftParen: return "LeftParen";
729 case CXCompletionChunk_RightParen: return "RightParen";
730 case CXCompletionChunk_LeftBracket: return "LeftBracket";
731 case CXCompletionChunk_RightBracket: return "RightBracket";
732 case CXCompletionChunk_LeftBrace: return "LeftBrace";
733 case CXCompletionChunk_RightBrace: return "RightBrace";
734 case CXCompletionChunk_LeftAngle: return "LeftAngle";
735 case CXCompletionChunk_RightAngle: return "RightAngle";
736 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000737 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000738 case CXCompletionChunk_Colon: return "Colon";
739 case CXCompletionChunk_SemiColon: return "SemiColon";
740 case CXCompletionChunk_Equal: return "Equal";
741 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
742 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000743 }
744
745 return "Unknown";
746}
747
Douglas Gregor3ac73852009-11-09 16:04:45 +0000748void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000749 int I, N;
Douglas Gregor3ac73852009-11-09 16:04:45 +0000750
751 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000752 for (I = 0; I != N; ++I) {
Douglas Gregord5a20892009-11-09 17:05:28 +0000753 const char *text = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000754 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000755 = clang_getCompletionChunkKind(completion_string, I);
756
757 if (Kind == CXCompletionChunk_Optional) {
758 fprintf(file, "{Optional ");
759 print_completion_string(
760 clang_getCompletionChunkCompletionString(completion_string, I),
761 file);
762 fprintf(file, "}");
763 continue;
764 }
765
Douglas Gregord5a20892009-11-09 17:05:28 +0000766 text = clang_getCompletionChunkText(completion_string, I);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000767 fprintf(file, "{%s %s}",
768 clang_getCompletionChunkKindSpelling(Kind),
769 text? text : "");
770 }
Douglas Gregor3ac73852009-11-09 16:04:45 +0000771}
772
773void print_completion_result(CXCompletionResult *completion_result,
774 CXClientData client_data) {
775 FILE *file = (FILE *)client_data;
776 fprintf(file, "%s:",
777 clang_getCursorKindSpelling(completion_result->CursorKind));
778 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000779 fprintf(file, "\n");
780}
781
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000782int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000783 const char *input = argv[1];
784 char *filename = 0;
785 unsigned line;
786 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000787 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000788 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000789 struct CXUnsavedFile *unsaved_files = 0;
790 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000791 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000792
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000793 input += strlen("-code-completion-at=");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000794 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
795 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000796 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000797
Douglas Gregor735df882009-12-02 09:21:34 +0000798 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
799 return -1;
800
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000801 CIdx = clang_createIndex(0);
Douglas Gregorec6762c2009-12-18 16:20:58 +0000802 results = clang_codeComplete(CIdx,
803 argv[argc - 1], argc - num_unsaved_files - 3,
804 argv + num_unsaved_files + 2,
805 num_unsaved_files, unsaved_files,
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000806 filename, line, column,
807 PrintDiagnosticCallback, stderr);
808
Douglas Gregorec6762c2009-12-18 16:20:58 +0000809 if (results) {
810 unsigned i, n = results->NumResults;
811 for (i = 0; i != n; ++i)
812 print_completion_result(results->Results + i, stdout);
813 clang_disposeCodeCompleteResults(results);
814 }
815
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000816 clang_disposeIndex(CIdx);
817 free(filename);
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000818
Douglas Gregor735df882009-12-02 09:21:34 +0000819 free_remapped_files(unsaved_files, num_unsaved_files);
820
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000821 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000822}
823
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000824typedef struct {
825 char *filename;
826 unsigned line;
827 unsigned column;
828} CursorSourceLocation;
829
830int inspect_cursor_at(int argc, const char **argv) {
831 CXIndex CIdx;
832 int errorCode;
833 struct CXUnsavedFile *unsaved_files = 0;
834 int num_unsaved_files = 0;
835 CXTranslationUnit TU;
836 CXCursor Cursor;
837 CursorSourceLocation *Locations = 0;
838 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000839
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000840 /* Count the number of locations. */
841 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
842 ++NumLocations;
843
844 /* Parse the locations. */
845 assert(NumLocations > 0 && "Unable to count locations?");
846 Locations = (CursorSourceLocation *)malloc(
847 NumLocations * sizeof(CursorSourceLocation));
848 for (Loc = 0; Loc < NumLocations; ++Loc) {
849 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
850 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
851 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000852 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000853 return errorCode;
854 }
855
856 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
857 &num_unsaved_files))
858 return -1;
859
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000860 CIdx = clang_createIndex(0);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000861 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
862 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000863 argv + num_unsaved_files + 1 + NumLocations,
864 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000865 unsaved_files,
866 PrintDiagnosticCallback,
867 stderr);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000868 if (!TU) {
869 fprintf(stderr, "unable to parse input\n");
870 return -1;
871 }
872
873 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000874 CXFile file = clang_getFile(TU, Locations[Loc].filename);
875 if (!file)
876 continue;
877
878 Cursor = clang_getCursor(TU,
879 clang_getLocation(TU, file, Locations[Loc].line,
880 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000881 PrintCursor(Cursor);
882 printf("\n");
883 free(Locations[Loc].filename);
884 }
885
886 clang_disposeTranslationUnit(TU);
887 clang_disposeIndex(CIdx);
888 free(Locations);
889 free_remapped_files(unsaved_files, num_unsaved_files);
890 return 0;
891}
892
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000893int perform_token_annotation(int argc, const char **argv) {
894 const char *input = argv[1];
895 char *filename = 0;
896 unsigned line, second_line;
897 unsigned column, second_column;
898 CXIndex CIdx;
899 CXTranslationUnit TU = 0;
900 int errorCode;
901 struct CXUnsavedFile *unsaved_files = 0;
902 int num_unsaved_files = 0;
903 CXToken *tokens;
904 unsigned num_tokens;
905 CXSourceRange range;
906 CXSourceLocation startLoc, endLoc;
907 CXFile file = 0;
908 CXCursor *cursors = 0;
909 unsigned i;
910
911 input += strlen("-test-annotate-tokens=");
912 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
913 &second_line, &second_column)))
914 return errorCode;
915
916 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
917 return -1;
918
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000919 CIdx = clang_createIndex(0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000920 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
921 argc - num_unsaved_files - 3,
922 argv + num_unsaved_files + 2,
923 num_unsaved_files,
Douglas Gregor5352ac02010-01-28 00:27:43 +0000924 unsaved_files,
925 PrintDiagnosticCallback,
926 stderr);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000927 if (!TU) {
928 fprintf(stderr, "unable to parse input\n");
929 clang_disposeIndex(CIdx);
930 free(filename);
931 free_remapped_files(unsaved_files, num_unsaved_files);
932 return -1;
933 }
934 errorCode = 0;
935
936 file = clang_getFile(TU, filename);
937 if (!file) {
938 fprintf(stderr, "file %s is not in this translation unit\n", filename);
939 errorCode = -1;
940 goto teardown;
941 }
942
943 startLoc = clang_getLocation(TU, file, line, column);
944 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
945 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
946 column);
947 errorCode = -1;
948 goto teardown;
949 }
950
951 endLoc = clang_getLocation(TU, file, second_line, second_column);
952 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
953 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
954 second_line, second_column);
955 errorCode = -1;
956 goto teardown;
957 }
958
959 range = clang_getRange(startLoc, endLoc);
960 clang_tokenize(TU, range, &tokens, &num_tokens);
961 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
962 clang_annotateTokens(TU, tokens, num_tokens, cursors);
963 for (i = 0; i != num_tokens; ++i) {
964 const char *kind = "<unknown>";
965 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
966 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
967 unsigned start_line, start_column, end_line, end_column;
968
969 switch (clang_getTokenKind(tokens[i])) {
970 case CXToken_Punctuation: kind = "Punctuation"; break;
971 case CXToken_Keyword: kind = "Keyword"; break;
972 case CXToken_Identifier: kind = "Identifier"; break;
973 case CXToken_Literal: kind = "Literal"; break;
974 case CXToken_Comment: kind = "Comment"; break;
975 }
976 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000977 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000978 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000979 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000980 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
981 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000982 if (!clang_isInvalid(cursors[i].kind)) {
983 printf(" ");
984 PrintCursor(cursors[i]);
985 }
986 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000987 }
988 free(cursors);
989
990 teardown:
991 clang_disposeTranslationUnit(TU);
992 clang_disposeIndex(CIdx);
993 free(filename);
994 free_remapped_files(unsaved_files, num_unsaved_files);
995 return errorCode;
996}
997
Ted Kremenek0d435192009-11-17 18:13:31 +0000998/******************************************************************************/
999/* Command line processing. */
1000/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001001
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001002static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001003 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001004 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001005 if (strcmp(s, "-usrs") == 0)
1006 return USRVisitor;
1007 return NULL;
1008}
1009
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001010static void print_usage(void) {
1011 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001012 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001013 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001014 " c-index-test -test-file-scan <AST file> <source file> "
1015 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001016 " c-index-test -test-load-tu <AST file> <symbol filter> "
1017 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001018 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1019 "[FileCheck prefix]\n"
1020 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001021 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001022 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +00001023 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1024 " c-index-test -test-inclusion-stack-source {<args>}*\n"
1025 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001026 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001027 " all - load all symbols, including those from PCH\n"
1028 " local - load all symbols except those in PCH\n"
1029 " category - only load ObjC categories (non-PCH)\n"
1030 " interface - only load ObjC interfaces (non-PCH)\n"
1031 " protocol - only load ObjC protocols (non-PCH)\n"
1032 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001033 " typedef - only load typdefs (non-PCH)\n"
1034 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001035}
1036
1037int main(int argc, const char **argv) {
1038 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
1039 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001040 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1041 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001042 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001043 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001044 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001045 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1046 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001047 }
1048 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001049 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001050 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001051 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001052 }
1053 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001054 return perform_file_scan(argv[2], argv[3],
1055 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001056 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1057 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001058 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1059 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1060 PrintInclusionStack);
1061 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1062 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1063 PrintInclusionStack);
1064
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001065 print_usage();
1066 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001067}