blob: 34bb99024654037baacbe83f5a7d03501b07a987 [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 Gregor274f1902010-02-22 23:17:23 +0000201 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000202 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
203 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
204 unsigned i, num_fixits;
Douglas Gregor274f1902010-02-22 23:17:23 +0000205
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000206 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000207 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000208
Douglas Gregor274f1902010-02-22 23:17:23 +0000209 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
210 fprintf(stderr, "%s\n", clang_getCString(Msg));
211 clang_disposeString(Msg);
212
Douglas Gregor5352ac02010-01-28 00:27:43 +0000213 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000214 &file, 0, 0, 0);
215 if (!file)
216 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000217
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000218 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
219 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000220 CXSourceRange range;
221 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
222 CXSourceLocation start = clang_getRangeStart(range);
223 CXSourceLocation end = clang_getRangeEnd(range);
224 unsigned start_line, start_column, end_line, end_column;
225 CXFile start_file, end_file;
226 clang_getInstantiationLocation(start, &start_file, &start_line,
227 &start_column, 0);
228 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
229 if (clang_equalLocations(start, end)) {
230 /* Insertion. */
231 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000232 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000233 clang_getCString(insertion_text), start_line, start_column);
234 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
235 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000236 if (start_file == file && end_file == file) {
237 fprintf(out, "FIX-IT: Remove ");
238 PrintExtent(out, start_line, start_column, end_line, end_column);
239 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000240 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000241 } else {
242 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000243 if (start_file == end_file) {
244 fprintf(out, "FIX-IT: Replace ");
245 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000246 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000247 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000248 break;
249 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000250 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000251 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000252}
253
Douglas Gregora88084b2010-02-18 18:08:43 +0000254void PrintDiagnostics(CXTranslationUnit TU) {
255 int i, n = clang_getNumDiagnostics(TU);
256 for (i = 0; i != n; ++i) {
257 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
258 PrintDiagnostic(Diag);
259 clang_disposeDiagnostic(Diag);
260 }
261}
262
Ted Kremenekce2ae882010-01-26 17:59:48 +0000263/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000264/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000265/******************************************************************************/
266
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000267static const char *FileCheckPrefix = "CHECK";
268
Douglas Gregora7bde202010-01-19 00:34:46 +0000269static void PrintCursorExtent(CXCursor C) {
270 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000271 CXFile begin_file, end_file;
272 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000273
Douglas Gregor1db19de2010-01-19 21:36:55 +0000274 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000275 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000276 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000277 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000278 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000279 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000280
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000281 printf(" Extent=");
282 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000283}
284
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000285/* Data used by all of the visitors. */
286typedef struct {
287 CXTranslationUnit TU;
288 enum CXCursorKind *Filter;
289} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000290
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000291
Ted Kremeneke68fff62010-02-17 00:41:32 +0000292enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000293 CXCursor Parent,
294 CXClientData ClientData) {
295 VisitorData *Data = (VisitorData *)ClientData;
296 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000297 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000298 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000299 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000300 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000301 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000302 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000303 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000304 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000305 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000306 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000307
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000308 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000309}
Steve Naroff50398192009-08-28 15:28:48 +0000310
Ted Kremeneke68fff62010-02-17 00:41:32 +0000311static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000312 CXCursor Parent,
313 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000314 const char *startBuf, *endBuf;
315 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
316 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000317 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000318
Douglas Gregorb6998662010-01-19 19:34:47 +0000319 if (Cursor.kind != CXCursor_FunctionDecl ||
320 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000321 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000322
323 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
324 &startLine, &startColumn,
325 &endLine, &endColumn);
326 /* Probe the entire body, looking for both decls and refs. */
327 curLine = startLine;
328 curColumn = startColumn;
329
330 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000331 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000332 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000333 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000334
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000335 if (*startBuf == '\n') {
336 startBuf++;
337 curLine++;
338 curColumn = 1;
339 } else if (*startBuf != '\t')
340 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000341
Douglas Gregor98258af2010-01-18 22:46:11 +0000342 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000343 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000344
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000346 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000347 CXSourceLocation RefLoc
348 = clang_getLocation(Data->TU, file, curLine, curColumn);
349 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000350 if (Ref.kind == CXCursor_NoDeclFound) {
351 /* Nothing found here; that's fine. */
352 } else if (Ref.kind != CXCursor_FunctionDecl) {
353 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
354 curLine, curColumn);
355 PrintCursor(Ref);
356 printf("\n");
357 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000358 }
Ted Kremenek74844072010-02-17 00:41:20 +0000359 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000360 startBuf++;
361 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000362
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000363 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000364}
365
Ted Kremenek7d405622010-01-12 23:34:26 +0000366/******************************************************************************/
367/* USR testing. */
368/******************************************************************************/
369
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000370enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
371 CXClientData ClientData) {
372 VisitorData *Data = (VisitorData *)ClientData;
373 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000374 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000375 if (!clang_getCString(USR)) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000376 clang_disposeString(USR);
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000377 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000378 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000379 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C),
380 clang_getCString(USR));
Douglas Gregora7bde202010-01-19 00:34:46 +0000381 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000382 printf("\n");
383 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000384
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000385 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000386 }
387
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000388 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000389}
390
391/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000392/* Inclusion stack testing. */
393/******************************************************************************/
394
395void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
396 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000397
Ted Kremenek16b55a72010-01-26 19:31:51 +0000398 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000399 CXString fname;
400
401 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000402 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000403 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000404
Ted Kremenek16b55a72010-01-26 19:31:51 +0000405 for (i = 0; i < includeStackLen; ++i) {
406 CXFile includingFile;
407 unsigned line, column;
408 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
409 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000410 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000411 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000412 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000413 }
414 printf("\n");
415}
416
417void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000418 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000419}
420
421/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000422/* Loading ASTs/source. */
423/******************************************************************************/
424
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000425static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000426 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000427 CXCursorVisitor Visitor,
428 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000429
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000430 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000431 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000432
433 if (Visitor) {
434 enum CXCursorKind K = CXCursor_NotImplemented;
435 enum CXCursorKind *ck = &K;
436 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000437
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000438 /* Perform some simple filtering. */
439 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000440 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000441 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
442 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
443 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
444 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
445 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
446 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
447 else {
448 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
449 return 1;
450 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000451
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000452 Data.TU = TU;
453 Data.Filter = ck;
454 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000455 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000456
Ted Kremenekce2ae882010-01-26 17:59:48 +0000457 if (PV)
458 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000459
Douglas Gregora88084b2010-02-18 18:08:43 +0000460 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000461 clang_disposeTranslationUnit(TU);
462 return 0;
463}
464
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000465int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000466 const char *prefix, CXCursorVisitor Visitor,
467 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000468 CXIndex Idx;
469 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000470 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000471 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000472 !strcmp(filter, "local") ? 1 : 0,
473 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000474
Ted Kremenek020a0952010-02-11 07:41:25 +0000475 if (!CreateTranslationUnit(Idx, file, &TU)) {
476 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000477 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000478 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000479
Ted Kremenek020a0952010-02-11 07:41:25 +0000480 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
481 clang_disposeIndex(Idx);
482 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000483}
484
Ted Kremenekce2ae882010-01-26 17:59:48 +0000485int perform_test_load_source(int argc, const char **argv,
486 const char *filter, CXCursorVisitor Visitor,
487 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000488 const char *UseExternalASTs =
489 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000490 CXIndex Idx;
491 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000492 struct CXUnsavedFile *unsaved_files = 0;
493 int num_unsaved_files = 0;
494 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000495
Daniel Dunbarada487d2009-12-01 02:03:10 +0000496 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000497 !strcmp(filter, "local") ? 1 : 0,
498 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000499
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000500 if (UseExternalASTs && strlen(UseExternalASTs))
501 clang_setUseExternalASTGeneration(Idx, 1);
502
Ted Kremenek020a0952010-02-11 07:41:25 +0000503 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
504 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000505 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000506 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000507
Ted Kremeneke68fff62010-02-17 00:41:32 +0000508 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
509 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000510 argv + num_unsaved_files,
511 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000512 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000513 if (!TU) {
514 fprintf(stderr, "Unable to load translation unit!\n");
Ted Kremenek020a0952010-02-11 07:41:25 +0000515 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000516 return 1;
517 }
518
Ted Kremenekce2ae882010-01-26 17:59:48 +0000519 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000520 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000521 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000522 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000523}
524
Ted Kremenek0d435192009-11-17 18:13:31 +0000525/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000526/* Logic for testing clang_getCursor(). */
527/******************************************************************************/
528
529static void print_cursor_file_scan(CXCursor cursor,
530 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000531 unsigned end_line, unsigned end_col,
532 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000533 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000534 if (prefix)
535 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000536 PrintExtent(stdout, start_line, start_col, end_line, end_col);
537 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000538 PrintCursor(cursor);
539 printf("\n");
540}
541
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000542static int perform_file_scan(const char *ast_file, const char *source_file,
543 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000544 CXIndex Idx;
545 CXTranslationUnit TU;
546 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000547 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000548 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000549 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000550 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000551
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000552 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
553 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000554 fprintf(stderr, "Could not create Index\n");
555 return 1;
556 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000557
Ted Kremenek1c6da172009-11-17 19:37:36 +0000558 if (!CreateTranslationUnit(Idx, ast_file, &TU))
559 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000560
Ted Kremenek1c6da172009-11-17 19:37:36 +0000561 if ((fp = fopen(source_file, "r")) == NULL) {
562 fprintf(stderr, "Could not open '%s'\n", source_file);
563 return 1;
564 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000565
Douglas Gregorb9790342010-01-22 21:44:22 +0000566 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000567 for (;;) {
568 CXCursor cursor;
569 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000570
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000571 if (c == '\n') {
572 ++line;
573 col = 1;
574 } else
575 ++col;
576
577 /* Check the cursor at this position, and dump the previous one if we have
578 * found something new.
579 */
580 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
581 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
582 prevCursor.kind != CXCursor_InvalidFile) {
583 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000584 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000585 start_line = line;
586 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000587 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000588 if (c == EOF)
589 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000590
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000591 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000592 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000593
Ted Kremenek1c6da172009-11-17 19:37:36 +0000594 fclose(fp);
595 return 0;
596}
597
598/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000599/* Logic for testing clang_codeComplete(). */
600/******************************************************************************/
601
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000602/* Parse file:line:column from the input string. Returns 0 on success, non-zero
603 on failure. If successful, the pointer *filename will contain newly-allocated
604 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000605int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000606 unsigned *column, unsigned *second_line,
607 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000608 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000609 const char *last_colon = strrchr(input, ':');
610 unsigned values[4], i;
611 unsigned num_values = (second_line && second_column)? 4 : 2;
612
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000613 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000614 if (!last_colon || last_colon == input) {
615 if (num_values == 4)
616 fprintf(stderr, "could not parse filename:line:column:line:column in "
617 "'%s'\n", input);
618 else
619 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000620 return 1;
621 }
622
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000623 for (i = 0; i != num_values; ++i) {
624 const char *prev_colon;
625
626 /* Parse the next line or column. */
627 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
628 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000629 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000630 (i % 2 ? "column" : "line"), input);
631 return 1;
632 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000633
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000634 if (i + 1 == num_values)
635 break;
636
637 /* Find the previous colon. */
638 prev_colon = last_colon - 1;
639 while (prev_colon != input && *prev_colon != ':')
640 --prev_colon;
641 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000642 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000643 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000644 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000645 }
646
647 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000648 }
649
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000650 *line = values[0];
651 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000652
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000653 if (second_line && second_column) {
654 *second_line = values[2];
655 *second_column = values[3];
656 }
657
Douglas Gregor88d23952009-11-09 18:19:57 +0000658 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000659 *filename = (char*)malloc(last_colon - input + 1);
660 memcpy(*filename, input, last_colon - input);
661 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000662 return 0;
663}
664
665const char *
666clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
667 switch (Kind) {
668 case CXCompletionChunk_Optional: return "Optional";
669 case CXCompletionChunk_TypedText: return "TypedText";
670 case CXCompletionChunk_Text: return "Text";
671 case CXCompletionChunk_Placeholder: return "Placeholder";
672 case CXCompletionChunk_Informative: return "Informative";
673 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
674 case CXCompletionChunk_LeftParen: return "LeftParen";
675 case CXCompletionChunk_RightParen: return "RightParen";
676 case CXCompletionChunk_LeftBracket: return "LeftBracket";
677 case CXCompletionChunk_RightBracket: return "RightBracket";
678 case CXCompletionChunk_LeftBrace: return "LeftBrace";
679 case CXCompletionChunk_RightBrace: return "RightBrace";
680 case CXCompletionChunk_LeftAngle: return "LeftAngle";
681 case CXCompletionChunk_RightAngle: return "RightAngle";
682 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000683 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000684 case CXCompletionChunk_Colon: return "Colon";
685 case CXCompletionChunk_SemiColon: return "SemiColon";
686 case CXCompletionChunk_Equal: return "Equal";
687 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
688 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000689 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000690
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000691 return "Unknown";
692}
693
Douglas Gregor3ac73852009-11-09 16:04:45 +0000694void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000695 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000696
Douglas Gregor3ac73852009-11-09 16:04:45 +0000697 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000698 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000699 CXString text;
700 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000701 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000702 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000703
Douglas Gregor3ac73852009-11-09 16:04:45 +0000704 if (Kind == CXCompletionChunk_Optional) {
705 fprintf(file, "{Optional ");
706 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000708 file);
709 fprintf(file, "}");
710 continue;
711 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000712
Douglas Gregord5a20892009-11-09 17:05:28 +0000713 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000714 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000715 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000716 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000717 cstr ? cstr : "");
718 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000719 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000720
Douglas Gregor3ac73852009-11-09 16:04:45 +0000721}
722
723void print_completion_result(CXCompletionResult *completion_result,
724 CXClientData client_data) {
725 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000726 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
727
728 fprintf(file, "%s:", clang_getCString(ks));
729 clang_disposeString(ks);
730
Douglas Gregor3ac73852009-11-09 16:04:45 +0000731 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000732 fprintf(file, "\n");
733}
734
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000735int perform_code_completion(int argc, const char **argv) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000736 const char *input = argv[1];
737 char *filename = 0;
738 unsigned line;
739 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000740 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000741 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000742 struct CXUnsavedFile *unsaved_files = 0;
743 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000744 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000745
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000746 input += strlen("-code-completion-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000747 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000748 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000749 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000750
Douglas Gregor735df882009-12-02 09:21:34 +0000751 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
752 return -1;
753
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000754 CIdx = clang_createIndex(0, 1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000755 results = clang_codeComplete(CIdx,
756 argv[argc - 1], argc - num_unsaved_files - 3,
757 argv + num_unsaved_files + 2,
Douglas Gregorec6762c2009-12-18 16:20:58 +0000758 num_unsaved_files, unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000759 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000760
Douglas Gregorec6762c2009-12-18 16:20:58 +0000761 if (results) {
762 unsigned i, n = results->NumResults;
763 for (i = 0; i != n; ++i)
764 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000765 n = clang_codeCompleteGetNumDiagnostics(results);
766 for (i = 0; i != n; ++i) {
767 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
768 PrintDiagnostic(diag);
769 clang_disposeDiagnostic(diag);
770 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000771 clang_disposeCodeCompleteResults(results);
772 }
773
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000774 clang_disposeIndex(CIdx);
775 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000776
Douglas Gregor735df882009-12-02 09:21:34 +0000777 free_remapped_files(unsaved_files, num_unsaved_files);
778
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000779 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000780}
781
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000782typedef struct {
783 char *filename;
784 unsigned line;
785 unsigned column;
786} CursorSourceLocation;
787
788int inspect_cursor_at(int argc, const char **argv) {
789 CXIndex CIdx;
790 int errorCode;
791 struct CXUnsavedFile *unsaved_files = 0;
792 int num_unsaved_files = 0;
793 CXTranslationUnit TU;
794 CXCursor Cursor;
795 CursorSourceLocation *Locations = 0;
796 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000797
Ted Kremeneke68fff62010-02-17 00:41:32 +0000798 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000799 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
800 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000801
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000802 /* Parse the locations. */
803 assert(NumLocations > 0 && "Unable to count locations?");
804 Locations = (CursorSourceLocation *)malloc(
805 NumLocations * sizeof(CursorSourceLocation));
806 for (Loc = 0; Loc < NumLocations; ++Loc) {
807 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000808 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
809 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000810 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000811 return errorCode;
812 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000813
814 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000815 &num_unsaved_files))
816 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000817
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000818 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000819 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
820 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000821 argv + num_unsaved_files + 1 + NumLocations,
822 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000823 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000824 if (!TU) {
825 fprintf(stderr, "unable to parse input\n");
826 return -1;
827 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000828
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000829 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000830 CXFile file = clang_getFile(TU, Locations[Loc].filename);
831 if (!file)
832 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000833
834 Cursor = clang_getCursor(TU,
835 clang_getLocation(TU, file, Locations[Loc].line,
836 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000837 PrintCursor(Cursor);
838 printf("\n");
839 free(Locations[Loc].filename);
840 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000841
Douglas Gregora88084b2010-02-18 18:08:43 +0000842 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000843 clang_disposeTranslationUnit(TU);
844 clang_disposeIndex(CIdx);
845 free(Locations);
846 free_remapped_files(unsaved_files, num_unsaved_files);
847 return 0;
848}
849
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000850int perform_token_annotation(int argc, const char **argv) {
851 const char *input = argv[1];
852 char *filename = 0;
853 unsigned line, second_line;
854 unsigned column, second_column;
855 CXIndex CIdx;
856 CXTranslationUnit TU = 0;
857 int errorCode;
858 struct CXUnsavedFile *unsaved_files = 0;
859 int num_unsaved_files = 0;
860 CXToken *tokens;
861 unsigned num_tokens;
862 CXSourceRange range;
863 CXSourceLocation startLoc, endLoc;
864 CXFile file = 0;
865 CXCursor *cursors = 0;
866 unsigned i;
867
868 input += strlen("-test-annotate-tokens=");
869 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
870 &second_line, &second_column)))
871 return errorCode;
872
873 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
874 return -1;
875
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000876 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000877 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
878 argc - num_unsaved_files - 3,
879 argv + num_unsaved_files + 2,
880 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000881 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000882 if (!TU) {
883 fprintf(stderr, "unable to parse input\n");
884 clang_disposeIndex(CIdx);
885 free(filename);
886 free_remapped_files(unsaved_files, num_unsaved_files);
887 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000888 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000889 errorCode = 0;
890
891 file = clang_getFile(TU, filename);
892 if (!file) {
893 fprintf(stderr, "file %s is not in this translation unit\n", filename);
894 errorCode = -1;
895 goto teardown;
896 }
897
898 startLoc = clang_getLocation(TU, file, line, column);
899 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000900 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000901 column);
902 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000903 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000904 }
905
906 endLoc = clang_getLocation(TU, file, second_line, second_column);
907 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000908 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000909 second_line, second_column);
910 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000911 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000912 }
913
914 range = clang_getRange(startLoc, endLoc);
915 clang_tokenize(TU, range, &tokens, &num_tokens);
916 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
917 clang_annotateTokens(TU, tokens, num_tokens, cursors);
918 for (i = 0; i != num_tokens; ++i) {
919 const char *kind = "<unknown>";
920 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
921 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
922 unsigned start_line, start_column, end_line, end_column;
923
924 switch (clang_getTokenKind(tokens[i])) {
925 case CXToken_Punctuation: kind = "Punctuation"; break;
926 case CXToken_Keyword: kind = "Keyword"; break;
927 case CXToken_Identifier: kind = "Identifier"; break;
928 case CXToken_Literal: kind = "Literal"; break;
929 case CXToken_Comment: kind = "Comment"; break;
930 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000931 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000932 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000933 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000934 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000935 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
936 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +0000937 if (!clang_isInvalid(cursors[i].kind)) {
938 printf(" ");
939 PrintCursor(cursors[i]);
940 }
941 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000942 }
943 free(cursors);
944
945 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +0000946 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000947 clang_disposeTranslationUnit(TU);
948 clang_disposeIndex(CIdx);
949 free(filename);
950 free_remapped_files(unsaved_files, num_unsaved_files);
951 return errorCode;
952}
953
Ted Kremenek0d435192009-11-17 18:13:31 +0000954/******************************************************************************/
955/* Command line processing. */
956/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000957
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000958static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +0000959 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000960 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +0000961 if (strcmp(s, "-usrs") == 0)
962 return USRVisitor;
963 return NULL;
964}
965
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000966static void print_usage(void) {
967 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +0000968 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000969 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000970 " c-index-test -test-file-scan <AST file> <source file> "
971 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000972 " c-index-test -test-load-tu <AST file> <symbol filter> "
973 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000974 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
975 "[FileCheck prefix]\n"
976 " c-index-test -test-load-source <symbol filter> {<args>}*\n"
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000977 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000978 fprintf(stderr,
Ted Kremenek16b55a72010-01-26 19:31:51 +0000979 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
980 " c-index-test -test-inclusion-stack-source {<args>}*\n"
981 " c-index-test -test-inclusion-stack-tu <AST file>\n\n"
Ted Kremenek7d405622010-01-12 23:34:26 +0000982 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +0000983 " all - load all symbols, including those from PCH\n"
984 " local - load all symbols except those in PCH\n"
985 " category - only load ObjC categories (non-PCH)\n"
986 " interface - only load ObjC interfaces (non-PCH)\n"
987 " protocol - only load ObjC protocols (non-PCH)\n"
988 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000989 " typedef - only load typdefs (non-PCH)\n"
990 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000991}
992
993int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000994 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000995 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
996 return perform_code_completion(argc, argv);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000997 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
998 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +0000999 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001000 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001001 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001002 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1003 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001004 }
1005 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001006 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001007 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001008 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001009 }
1010 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001011 return perform_file_scan(argv[2], argv[3],
1012 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001013 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1014 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001015 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1016 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1017 PrintInclusionStack);
1018 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1019 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1020 PrintInclusionStack);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001021
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001022 print_usage();
1023 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001024}