blob: c822a135c21e1c2a05768facfa21de99719fa7f3 [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 Gregor45ba9a12010-07-25 17:39:21 +000031/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000032static unsigned getDefaultParsingOptions() {
33 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
34
35 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000036 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000037 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
38 options |= CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000039
40 return options;
41}
42
Daniel Dunbar51b058c2010-02-14 08:32:24 +000043static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
44 unsigned end_line, unsigned end_column) {
45 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000046 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000047}
48
Ted Kremenek1c6da172009-11-17 19:37:36 +000049static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
50 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000051
Douglas Gregora88084b2010-02-18 18:08:43 +000052 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000053 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000054 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
55 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000056 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000057 return 1;
58}
59
Douglas Gregor4db64a42010-01-23 00:14:00 +000060void free_remapped_files(struct CXUnsavedFile *unsaved_files,
61 int num_unsaved_files) {
62 int i;
63 for (i = 0; i != num_unsaved_files; ++i) {
64 free((char *)unsaved_files[i].Filename);
65 free((char *)unsaved_files[i].Contents);
66 }
67}
68
69int parse_remapped_files(int argc, const char **argv, int start_arg,
70 struct CXUnsavedFile **unsaved_files,
71 int *num_unsaved_files) {
72 int i;
73 int arg;
74 int prefix_len = strlen("-remap-file=");
75 *unsaved_files = 0;
76 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000077
Douglas Gregor4db64a42010-01-23 00:14:00 +000078 /* Count the number of remapped files. */
79 for (arg = start_arg; arg < argc; ++arg) {
80 if (strncmp(argv[arg], "-remap-file=", prefix_len))
81 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000082
Douglas Gregor4db64a42010-01-23 00:14:00 +000083 ++*num_unsaved_files;
84 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000085
Douglas Gregor4db64a42010-01-23 00:14:00 +000086 if (*num_unsaved_files == 0)
87 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000088
Douglas Gregor4db64a42010-01-23 00:14:00 +000089 *unsaved_files
Ted Kremeneke68fff62010-02-17 00:41:32 +000090 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 *num_unsaved_files);
92 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
93 struct CXUnsavedFile *unsaved = *unsaved_files + i;
94 const char *arg_string = argv[arg] + prefix_len;
95 int filename_len;
96 char *filename;
97 char *contents;
98 FILE *to_file;
99 const char *semi = strchr(arg_string, ';');
100 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000101 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000102 "error: -remap-file=from;to argument is missing semicolon\n");
103 free_remapped_files(*unsaved_files, i);
104 *unsaved_files = 0;
105 *num_unsaved_files = 0;
106 return -1;
107 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000108
Douglas Gregor4db64a42010-01-23 00:14:00 +0000109 /* Open the file that we're remapping to. */
110 to_file = fopen(semi + 1, "r");
111 if (!to_file) {
112 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
113 semi + 1);
114 free_remapped_files(*unsaved_files, i);
115 *unsaved_files = 0;
116 *num_unsaved_files = 0;
117 return -1;
118 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000119
Douglas Gregor4db64a42010-01-23 00:14:00 +0000120 /* Determine the length of the file we're remapping to. */
121 fseek(to_file, 0, SEEK_END);
122 unsaved->Length = ftell(to_file);
123 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000124
Douglas Gregor4db64a42010-01-23 00:14:00 +0000125 /* Read the contents of the file we're remapping to. */
126 contents = (char *)malloc(unsaved->Length + 1);
127 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
128 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
129 (feof(to_file) ? "EOF" : "error"), semi + 1);
130 fclose(to_file);
131 free_remapped_files(*unsaved_files, i);
132 *unsaved_files = 0;
133 *num_unsaved_files = 0;
134 return -1;
135 }
136 contents[unsaved->Length] = 0;
137 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000138
Douglas Gregor4db64a42010-01-23 00:14:00 +0000139 /* Close the file. */
140 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000141
Douglas Gregor4db64a42010-01-23 00:14:00 +0000142 /* Copy the file name that we're remapping from. */
143 filename_len = semi - arg_string;
144 filename = (char *)malloc(filename_len + 1);
145 memcpy(filename, arg_string, filename_len);
146 filename[filename_len] = 0;
147 unsaved->Filename = filename;
148 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 return 0;
151}
152
Ted Kremenek0d435192009-11-17 18:13:31 +0000153/******************************************************************************/
154/* Pretty-printing. */
155/******************************************************************************/
156
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000157static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000158 if (clang_isInvalid(Cursor.kind)) {
159 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
160 printf("Invalid Cursor => %s", clang_getCString(ks));
161 clang_disposeString(ks);
162 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000163 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000164 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000165 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000166 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000167
168 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000169 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000170 printf("%s=%s", clang_getCString(ks),
171 clang_getCString(string));
172 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000173 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000174
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000175 Referenced = clang_getCursorReferenced(Cursor);
176 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
177 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000178 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000179 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000180 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000181
182 if (clang_isCursorDefinition(Cursor))
183 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000184 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000185}
Steve Naroff89922f82009-08-31 00:59:03 +0000186
Ted Kremeneke68fff62010-02-17 00:41:32 +0000187static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000188 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000189 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000190 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000191 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000192 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000193 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000194 clang_disposeString(source);
195 return "<invalid loc>";
196 }
197 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000198 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000199 clang_disposeString(source);
200 return b;
201 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000202}
203
Ted Kremenek0d435192009-11-17 18:13:31 +0000204/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000205/* Callbacks. */
206/******************************************************************************/
207
208typedef void (*PostVisitTU)(CXTranslationUnit);
209
Douglas Gregora88084b2010-02-18 18:08:43 +0000210void PrintDiagnostic(CXDiagnostic Diagnostic) {
211 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000212 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000213 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000214 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
215 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
216 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000217
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000218 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000219 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000220
Douglas Gregor274f1902010-02-22 23:17:23 +0000221 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
222 fprintf(stderr, "%s\n", clang_getCString(Msg));
223 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000224
Douglas Gregor5352ac02010-01-28 00:27:43 +0000225 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000226 &file, 0, 0, 0);
227 if (!file)
228 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000229
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000230 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
231 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000232 CXSourceRange range;
233 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
234 CXSourceLocation start = clang_getRangeStart(range);
235 CXSourceLocation end = clang_getRangeEnd(range);
236 unsigned start_line, start_column, end_line, end_column;
237 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000238 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000239 &start_column, 0);
240 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
241 if (clang_equalLocations(start, end)) {
242 /* Insertion. */
243 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000244 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000245 clang_getCString(insertion_text), start_line, start_column);
246 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
247 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000248 if (start_file == file && end_file == file) {
249 fprintf(out, "FIX-IT: Remove ");
250 PrintExtent(out, start_line, start_column, end_line, end_column);
251 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000252 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000253 } else {
254 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000255 if (start_file == end_file) {
256 fprintf(out, "FIX-IT: Replace ");
257 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000258 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000259 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000260 break;
261 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000262 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000263 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000264}
265
Douglas Gregora88084b2010-02-18 18:08:43 +0000266void PrintDiagnostics(CXTranslationUnit TU) {
267 int i, n = clang_getNumDiagnostics(TU);
268 for (i = 0; i != n; ++i) {
269 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
270 PrintDiagnostic(Diag);
271 clang_disposeDiagnostic(Diag);
272 }
273}
274
Ted Kremenekce2ae882010-01-26 17:59:48 +0000275/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000276/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000277/******************************************************************************/
278
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000279static const char *FileCheckPrefix = "CHECK";
280
Douglas Gregora7bde202010-01-19 00:34:46 +0000281static void PrintCursorExtent(CXCursor C) {
282 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000283 CXFile begin_file, end_file;
284 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000285
Douglas Gregor1db19de2010-01-19 21:36:55 +0000286 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000287 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000288 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000289 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000290 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000291 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000292
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000293 printf(" Extent=");
294 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000295}
296
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000297/* Data used by all of the visitors. */
298typedef struct {
299 CXTranslationUnit TU;
300 enum CXCursorKind *Filter;
301} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000302
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000303
Ted Kremeneke68fff62010-02-17 00:41:32 +0000304enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000305 CXCursor Parent,
306 CXClientData ClientData) {
307 VisitorData *Data = (VisitorData *)ClientData;
308 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000309 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000310 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000311 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000312 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000313 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000314 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000315 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000316 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000317 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000318 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000319
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000320 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000321}
Steve Naroff50398192009-08-28 15:28:48 +0000322
Ted Kremeneke68fff62010-02-17 00:41:32 +0000323static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000324 CXCursor Parent,
325 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000326 const char *startBuf, *endBuf;
327 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
328 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000329 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000330
Douglas Gregorb6998662010-01-19 19:34:47 +0000331 if (Cursor.kind != CXCursor_FunctionDecl ||
332 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000333 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000334
335 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
336 &startLine, &startColumn,
337 &endLine, &endColumn);
338 /* Probe the entire body, looking for both decls and refs. */
339 curLine = startLine;
340 curColumn = startColumn;
341
342 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000343 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000345 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000346
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000347 if (*startBuf == '\n') {
348 startBuf++;
349 curLine++;
350 curColumn = 1;
351 } else if (*startBuf != '\t')
352 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000353
Douglas Gregor98258af2010-01-18 22:46:11 +0000354 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000355 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000356
Douglas Gregor1db19de2010-01-19 21:36:55 +0000357 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000358 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000359 CXSourceLocation RefLoc
360 = clang_getLocation(Data->TU, file, curLine, curColumn);
361 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000362 if (Ref.kind == CXCursor_NoDeclFound) {
363 /* Nothing found here; that's fine. */
364 } else if (Ref.kind != CXCursor_FunctionDecl) {
365 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
366 curLine, curColumn);
367 PrintCursor(Ref);
368 printf("\n");
369 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000370 }
Ted Kremenek74844072010-02-17 00:41:20 +0000371 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000372 startBuf++;
373 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000374
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000375 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000376}
377
Ted Kremenek7d405622010-01-12 23:34:26 +0000378/******************************************************************************/
379/* USR testing. */
380/******************************************************************************/
381
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000382enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
383 CXClientData ClientData) {
384 VisitorData *Data = (VisitorData *)ClientData;
385 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000386 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000387 const char *cstr = clang_getCString(USR);
388 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000389 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000390 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000391 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000392 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
393
Douglas Gregora7bde202010-01-19 00:34:46 +0000394 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000395 printf("\n");
396 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000397
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000398 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000399 }
400
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000401 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000402}
403
404/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000405/* Inclusion stack testing. */
406/******************************************************************************/
407
408void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
409 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000410
Ted Kremenek16b55a72010-01-26 19:31:51 +0000411 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000412 CXString fname;
413
414 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000415 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000416 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000417
Ted Kremenek16b55a72010-01-26 19:31:51 +0000418 for (i = 0; i < includeStackLen; ++i) {
419 CXFile includingFile;
420 unsigned line, column;
421 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
422 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000423 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000424 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000425 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000426 }
427 printf("\n");
428}
429
430void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000431 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000432}
433
434/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000435/* Linkage testing. */
436/******************************************************************************/
437
438static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
439 CXClientData d) {
440 const char *linkage = 0;
441
442 if (clang_isInvalid(clang_getCursorKind(cursor)))
443 return CXChildVisit_Recurse;
444
445 switch (clang_getCursorLinkage(cursor)) {
446 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000447 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
448 case CXLinkage_Internal: linkage = "Internal"; break;
449 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
450 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000451 }
452
453 if (linkage) {
454 PrintCursor(cursor);
455 printf("linkage=%s\n", linkage);
456 }
457
458 return CXChildVisit_Recurse;
459}
460
461/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000462/* Typekind testing. */
463/******************************************************************************/
464
465static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
466 CXClientData d) {
467
468 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
469 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000470 CXString S = clang_getTypeKindSpelling(T.kind);
471 PrintCursor(cursor);
472 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000473 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000474 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000475 {
476 CXType CT = clang_getCanonicalType(T);
477 if (!clang_equalTypes(T, CT)) {
478 CXString CS = clang_getTypeKindSpelling(CT.kind);
479 printf(" [canonical=%s]", clang_getCString(CS));
480 clang_disposeString(CS);
481 }
482 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000483 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000484 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000485 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000486 if (RT.kind != CXType_Invalid) {
487 CXString RS = clang_getTypeKindSpelling(RT.kind);
488 printf(" [result=%s]", clang_getCString(RS));
489 clang_disposeString(RS);
490 }
491 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000492 /* Print if this is a non-POD type. */
493 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000494
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000495 printf("\n");
496 }
497 return CXChildVisit_Recurse;
498}
499
500
501/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000502/* Loading ASTs/source. */
503/******************************************************************************/
504
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000505static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000506 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000507 CXCursorVisitor Visitor,
508 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000509
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000510 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000511 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000512
513 if (Visitor) {
514 enum CXCursorKind K = CXCursor_NotImplemented;
515 enum CXCursorKind *ck = &K;
516 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000517
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000518 /* Perform some simple filtering. */
519 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000520 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000521 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
522 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
523 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
524 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
525 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
526 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
527 else {
528 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
529 return 1;
530 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000531
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000532 Data.TU = TU;
533 Data.Filter = ck;
534 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000535 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000536
Ted Kremenekce2ae882010-01-26 17:59:48 +0000537 if (PV)
538 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000539
Douglas Gregora88084b2010-02-18 18:08:43 +0000540 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000541 clang_disposeTranslationUnit(TU);
542 return 0;
543}
544
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000545int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000546 const char *prefix, CXCursorVisitor Visitor,
547 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000548 CXIndex Idx;
549 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000550 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000551 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000552 !strcmp(filter, "local") ? 1 : 0,
553 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000554
Ted Kremenek020a0952010-02-11 07:41:25 +0000555 if (!CreateTranslationUnit(Idx, file, &TU)) {
556 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000557 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000558 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000559
Ted Kremenek020a0952010-02-11 07:41:25 +0000560 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
561 clang_disposeIndex(Idx);
562 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000563}
564
Ted Kremenekce2ae882010-01-26 17:59:48 +0000565int perform_test_load_source(int argc, const char **argv,
566 const char *filter, CXCursorVisitor Visitor,
567 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000568 const char *UseExternalASTs =
569 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000570 CXIndex Idx;
571 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000572 struct CXUnsavedFile *unsaved_files = 0;
573 int num_unsaved_files = 0;
574 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000575
Daniel Dunbarada487d2009-12-01 02:03:10 +0000576 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000577 !strcmp(filter, "local") ? 1 : 0,
578 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000579
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000580 if (UseExternalASTs && strlen(UseExternalASTs))
581 clang_setUseExternalASTGeneration(Idx, 1);
582
Ted Kremenek020a0952010-02-11 07:41:25 +0000583 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
584 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000585 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000586 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000587
Ted Kremeneke68fff62010-02-17 00:41:32 +0000588 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
589 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000590 argv + num_unsaved_files,
591 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000592 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000593 if (!TU) {
594 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000595 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000596 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000597 return 1;
598 }
599
Ted Kremenekce2ae882010-01-26 17:59:48 +0000600 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000601 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000602 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000603 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000604}
605
Douglas Gregorabc563f2010-07-19 21:46:24 +0000606int perform_test_reparse_source(int argc, const char **argv, int trials,
607 const char *filter, CXCursorVisitor Visitor,
608 PostVisitTU PV) {
609 const char *UseExternalASTs =
610 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
611 CXIndex Idx;
612 CXTranslationUnit TU;
613 struct CXUnsavedFile *unsaved_files = 0;
614 int num_unsaved_files = 0;
615 int result;
616 int trial;
617
618 Idx = clang_createIndex(/* excludeDeclsFromPCH */
619 !strcmp(filter, "local") ? 1 : 0,
620 /* displayDiagnosics=*/1);
621
622 if (UseExternalASTs && strlen(UseExternalASTs))
623 clang_setUseExternalASTGeneration(Idx, 1);
624
625 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
626 clang_disposeIndex(Idx);
627 return -1;
628 }
629
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000630 /* Load the initial translation unit -- we do this without honoring remapped
631 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000632 TU = clang_parseTranslationUnit(Idx, 0,
633 argv + num_unsaved_files,
634 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000635 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000636 if (!TU) {
637 fprintf(stderr, "Unable to load translation unit!\n");
638 free_remapped_files(unsaved_files, num_unsaved_files);
639 clang_disposeIndex(Idx);
640 return 1;
641 }
642
643 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000644 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
645 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000646 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000647 clang_disposeTranslationUnit(TU);
648 free_remapped_files(unsaved_files, num_unsaved_files);
649 clang_disposeIndex(Idx);
650 return -1;
651 }
652 }
653
654 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
655 free_remapped_files(unsaved_files, num_unsaved_files);
656 clang_disposeIndex(Idx);
657 return result;
658}
659
Ted Kremenek0d435192009-11-17 18:13:31 +0000660/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000661/* Logic for testing clang_getCursor(). */
662/******************************************************************************/
663
664static void print_cursor_file_scan(CXCursor cursor,
665 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000666 unsigned end_line, unsigned end_col,
667 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000668 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000669 if (prefix)
670 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000671 PrintExtent(stdout, start_line, start_col, end_line, end_col);
672 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000673 PrintCursor(cursor);
674 printf("\n");
675}
676
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000677static int perform_file_scan(const char *ast_file, const char *source_file,
678 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000679 CXIndex Idx;
680 CXTranslationUnit TU;
681 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000682 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000683 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000684 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000685 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000686
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000687 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
688 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000689 fprintf(stderr, "Could not create Index\n");
690 return 1;
691 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000692
Ted Kremenek1c6da172009-11-17 19:37:36 +0000693 if (!CreateTranslationUnit(Idx, ast_file, &TU))
694 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000695
Ted Kremenek1c6da172009-11-17 19:37:36 +0000696 if ((fp = fopen(source_file, "r")) == NULL) {
697 fprintf(stderr, "Could not open '%s'\n", source_file);
698 return 1;
699 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000700
Douglas Gregorb9790342010-01-22 21:44:22 +0000701 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000702 for (;;) {
703 CXCursor cursor;
704 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000705
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000706 if (c == '\n') {
707 ++line;
708 col = 1;
709 } else
710 ++col;
711
712 /* Check the cursor at this position, and dump the previous one if we have
713 * found something new.
714 */
715 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
716 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
717 prevCursor.kind != CXCursor_InvalidFile) {
718 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000719 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000720 start_line = line;
721 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000722 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000723 if (c == EOF)
724 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000725
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000726 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000727 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000728
Ted Kremenek1c6da172009-11-17 19:37:36 +0000729 fclose(fp);
730 return 0;
731}
732
733/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000734/* Logic for testing clang_codeComplete(). */
735/******************************************************************************/
736
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000737/* Parse file:line:column from the input string. Returns 0 on success, non-zero
738 on failure. If successful, the pointer *filename will contain newly-allocated
739 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000740int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000741 unsigned *column, unsigned *second_line,
742 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000743 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000744 const char *last_colon = strrchr(input, ':');
745 unsigned values[4], i;
746 unsigned num_values = (second_line && second_column)? 4 : 2;
747
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000748 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000749 if (!last_colon || last_colon == input) {
750 if (num_values == 4)
751 fprintf(stderr, "could not parse filename:line:column:line:column in "
752 "'%s'\n", input);
753 else
754 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000755 return 1;
756 }
757
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000758 for (i = 0; i != num_values; ++i) {
759 const char *prev_colon;
760
761 /* Parse the next line or column. */
762 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
763 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000764 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000765 (i % 2 ? "column" : "line"), input);
766 return 1;
767 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000768
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000769 if (i + 1 == num_values)
770 break;
771
772 /* Find the previous colon. */
773 prev_colon = last_colon - 1;
774 while (prev_colon != input && *prev_colon != ':')
775 --prev_colon;
776 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000777 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000778 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000779 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000780 }
781
782 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000783 }
784
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000785 *line = values[0];
786 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000787
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000788 if (second_line && second_column) {
789 *second_line = values[2];
790 *second_column = values[3];
791 }
792
Douglas Gregor88d23952009-11-09 18:19:57 +0000793 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000794 *filename = (char*)malloc(last_colon - input + 1);
795 memcpy(*filename, input, last_colon - input);
796 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000797 return 0;
798}
799
800const char *
801clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
802 switch (Kind) {
803 case CXCompletionChunk_Optional: return "Optional";
804 case CXCompletionChunk_TypedText: return "TypedText";
805 case CXCompletionChunk_Text: return "Text";
806 case CXCompletionChunk_Placeholder: return "Placeholder";
807 case CXCompletionChunk_Informative: return "Informative";
808 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
809 case CXCompletionChunk_LeftParen: return "LeftParen";
810 case CXCompletionChunk_RightParen: return "RightParen";
811 case CXCompletionChunk_LeftBracket: return "LeftBracket";
812 case CXCompletionChunk_RightBracket: return "RightBracket";
813 case CXCompletionChunk_LeftBrace: return "LeftBrace";
814 case CXCompletionChunk_RightBrace: return "RightBrace";
815 case CXCompletionChunk_LeftAngle: return "LeftAngle";
816 case CXCompletionChunk_RightAngle: return "RightAngle";
817 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000818 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000819 case CXCompletionChunk_Colon: return "Colon";
820 case CXCompletionChunk_SemiColon: return "SemiColon";
821 case CXCompletionChunk_Equal: return "Equal";
822 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
823 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000824 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000825
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000826 return "Unknown";
827}
828
Douglas Gregor3ac73852009-11-09 16:04:45 +0000829void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000830 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000831
Douglas Gregor3ac73852009-11-09 16:04:45 +0000832 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000833 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000834 CXString text;
835 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000836 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000837 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000838
Douglas Gregor3ac73852009-11-09 16:04:45 +0000839 if (Kind == CXCompletionChunk_Optional) {
840 fprintf(file, "{Optional ");
841 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000842 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000843 file);
844 fprintf(file, "}");
845 continue;
846 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000847
Douglas Gregord5a20892009-11-09 17:05:28 +0000848 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000849 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000850 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000851 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000852 cstr ? cstr : "");
853 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000854 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000855
Douglas Gregor3ac73852009-11-09 16:04:45 +0000856}
857
858void print_completion_result(CXCompletionResult *completion_result,
859 CXClientData client_data) {
860 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000861 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
862
863 fprintf(file, "%s:", clang_getCString(ks));
864 clang_disposeString(ks);
865
Douglas Gregor3ac73852009-11-09 16:04:45 +0000866 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor12e13132010-05-26 22:00:08 +0000867 fprintf(file, " (%u)\n",
868 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000869}
870
Douglas Gregor1982c182010-07-12 18:38:41 +0000871int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000872 const char *input = argv[1];
873 char *filename = 0;
874 unsigned line;
875 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000876 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000877 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000878 struct CXUnsavedFile *unsaved_files = 0;
879 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000880 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000881 CXTranslationUnit *TU = 0;
882
Douglas Gregor1982c182010-07-12 18:38:41 +0000883 if (timing_only)
884 input += strlen("-code-completion-timing=");
885 else
886 input += strlen("-code-completion-at=");
887
Ted Kremeneke68fff62010-02-17 00:41:32 +0000888 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000889 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000890 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000891
Douglas Gregor735df882009-12-02 09:21:34 +0000892 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
893 return -1;
894
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000895 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000896 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000897 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +0000898 TU = clang_parseTranslationUnit(CIdx, 0,
899 argv + num_unsaved_files + 2,
900 argc - num_unsaved_files - 2,
901 unsaved_files,
902 num_unsaved_files,
903 getDefaultParsingOptions());
Douglas Gregordf95a132010-08-09 20:45:32 +0000904 for (I = 0; I != Repeats; ++I) {
905 results = clang_codeCompleteAt(TU, filename, line, column,
906 unsaved_files, num_unsaved_files,
907 clang_defaultCodeCompleteOptions());
908 if (I != Repeats-1)
909 clang_disposeCodeCompleteResults(results);
910 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000911 } else
912 results = clang_codeComplete(CIdx,
913 argv[argc - 1], argc - num_unsaved_files - 3,
914 argv + num_unsaved_files + 2,
915 num_unsaved_files, unsaved_files,
916 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000917
Douglas Gregorec6762c2009-12-18 16:20:58 +0000918 if (results) {
919 unsigned i, n = results->NumResults;
Douglas Gregor1982c182010-07-12 18:38:41 +0000920 if (!timing_only)
921 for (i = 0; i != n; ++i)
922 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000923 n = clang_codeCompleteGetNumDiagnostics(results);
924 for (i = 0; i != n; ++i) {
925 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
926 PrintDiagnostic(diag);
927 clang_disposeDiagnostic(diag);
928 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000929 clang_disposeCodeCompleteResults(results);
930 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000931 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000932 clang_disposeIndex(CIdx);
933 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000934
Douglas Gregor735df882009-12-02 09:21:34 +0000935 free_remapped_files(unsaved_files, num_unsaved_files);
936
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000937 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000938}
939
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000940typedef struct {
941 char *filename;
942 unsigned line;
943 unsigned column;
944} CursorSourceLocation;
945
946int inspect_cursor_at(int argc, const char **argv) {
947 CXIndex CIdx;
948 int errorCode;
949 struct CXUnsavedFile *unsaved_files = 0;
950 int num_unsaved_files = 0;
951 CXTranslationUnit TU;
952 CXCursor Cursor;
953 CursorSourceLocation *Locations = 0;
954 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000955
Ted Kremeneke68fff62010-02-17 00:41:32 +0000956 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000957 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
958 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000959
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000960 /* Parse the locations. */
961 assert(NumLocations > 0 && "Unable to count locations?");
962 Locations = (CursorSourceLocation *)malloc(
963 NumLocations * sizeof(CursorSourceLocation));
964 for (Loc = 0; Loc < NumLocations; ++Loc) {
965 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000966 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
967 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000968 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000969 return errorCode;
970 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000971
972 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000973 &num_unsaved_files))
974 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000975
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000976 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000977 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
978 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000979 argv + num_unsaved_files + 1 + NumLocations,
980 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000981 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000982 if (!TU) {
983 fprintf(stderr, "unable to parse input\n");
984 return -1;
985 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000986
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000987 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000988 CXFile file = clang_getFile(TU, Locations[Loc].filename);
989 if (!file)
990 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000991
992 Cursor = clang_getCursor(TU,
993 clang_getLocation(TU, file, Locations[Loc].line,
994 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000995 PrintCursor(Cursor);
996 printf("\n");
997 free(Locations[Loc].filename);
998 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000999
Douglas Gregora88084b2010-02-18 18:08:43 +00001000 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001001 clang_disposeTranslationUnit(TU);
1002 clang_disposeIndex(CIdx);
1003 free(Locations);
1004 free_remapped_files(unsaved_files, num_unsaved_files);
1005 return 0;
1006}
1007
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001008int perform_token_annotation(int argc, const char **argv) {
1009 const char *input = argv[1];
1010 char *filename = 0;
1011 unsigned line, second_line;
1012 unsigned column, second_column;
1013 CXIndex CIdx;
1014 CXTranslationUnit TU = 0;
1015 int errorCode;
1016 struct CXUnsavedFile *unsaved_files = 0;
1017 int num_unsaved_files = 0;
1018 CXToken *tokens;
1019 unsigned num_tokens;
1020 CXSourceRange range;
1021 CXSourceLocation startLoc, endLoc;
1022 CXFile file = 0;
1023 CXCursor *cursors = 0;
1024 unsigned i;
1025
1026 input += strlen("-test-annotate-tokens=");
1027 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1028 &second_line, &second_column)))
1029 return errorCode;
1030
1031 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1032 return -1;
1033
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001034 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001035 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1036 argc - num_unsaved_files - 3,
1037 argv + num_unsaved_files + 2,
1038 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001039 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001040 if (!TU) {
1041 fprintf(stderr, "unable to parse input\n");
1042 clang_disposeIndex(CIdx);
1043 free(filename);
1044 free_remapped_files(unsaved_files, num_unsaved_files);
1045 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001046 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001047 errorCode = 0;
1048
1049 file = clang_getFile(TU, filename);
1050 if (!file) {
1051 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1052 errorCode = -1;
1053 goto teardown;
1054 }
1055
1056 startLoc = clang_getLocation(TU, file, line, column);
1057 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001058 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001059 column);
1060 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001061 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001062 }
1063
1064 endLoc = clang_getLocation(TU, file, second_line, second_column);
1065 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001066 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001067 second_line, second_column);
1068 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001069 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001070 }
1071
1072 range = clang_getRange(startLoc, endLoc);
1073 clang_tokenize(TU, range, &tokens, &num_tokens);
1074 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1075 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1076 for (i = 0; i != num_tokens; ++i) {
1077 const char *kind = "<unknown>";
1078 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1079 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1080 unsigned start_line, start_column, end_line, end_column;
1081
1082 switch (clang_getTokenKind(tokens[i])) {
1083 case CXToken_Punctuation: kind = "Punctuation"; break;
1084 case CXToken_Keyword: kind = "Keyword"; break;
1085 case CXToken_Identifier: kind = "Identifier"; break;
1086 case CXToken_Literal: kind = "Literal"; break;
1087 case CXToken_Comment: kind = "Comment"; break;
1088 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001089 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001090 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001091 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001092 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001093 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1094 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001095 if (!clang_isInvalid(cursors[i].kind)) {
1096 printf(" ");
1097 PrintCursor(cursors[i]);
1098 }
1099 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001100 }
1101 free(cursors);
1102
1103 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001104 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001105 clang_disposeTranslationUnit(TU);
1106 clang_disposeIndex(CIdx);
1107 free(filename);
1108 free_remapped_files(unsaved_files, num_unsaved_files);
1109 return errorCode;
1110}
1111
Ted Kremenek0d435192009-11-17 18:13:31 +00001112/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001113/* USR printing. */
1114/******************************************************************************/
1115
1116static int insufficient_usr(const char *kind, const char *usage) {
1117 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1118 return 1;
1119}
1120
1121static unsigned isUSR(const char *s) {
1122 return s[0] == 'c' && s[1] == ':';
1123}
1124
1125static int not_usr(const char *s, const char *arg) {
1126 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1127 return 1;
1128}
1129
1130static void print_usr(CXString usr) {
1131 const char *s = clang_getCString(usr);
1132 printf("%s\n", s);
1133 clang_disposeString(usr);
1134}
1135
1136static void display_usrs() {
1137 fprintf(stderr, "-print-usrs options:\n"
1138 " ObjCCategory <class name> <category name>\n"
1139 " ObjCClass <class name>\n"
1140 " ObjCIvar <ivar name> <class USR>\n"
1141 " ObjCMethod <selector> [0=class method|1=instance method] "
1142 "<class USR>\n"
1143 " ObjCProperty <property name> <class USR>\n"
1144 " ObjCProtocol <protocol name>\n");
1145}
1146
1147int print_usrs(const char **I, const char **E) {
1148 while (I != E) {
1149 const char *kind = *I;
1150 unsigned len = strlen(kind);
1151 switch (len) {
1152 case 8:
1153 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1154 if (I + 2 >= E)
1155 return insufficient_usr(kind, "<ivar name> <class USR>");
1156 if (!isUSR(I[2]))
1157 return not_usr("<class USR>", I[2]);
1158 else {
1159 CXString x;
1160 x.Spelling = I[2];
1161 x.MustFreeString = 0;
1162 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1163 }
1164
1165 I += 3;
1166 continue;
1167 }
1168 break;
1169 case 9:
1170 if (memcmp(kind, "ObjCClass", 9) == 0) {
1171 if (I + 1 >= E)
1172 return insufficient_usr(kind, "<class name>");
1173 print_usr(clang_constructUSR_ObjCClass(I[1]));
1174 I += 2;
1175 continue;
1176 }
1177 break;
1178 case 10:
1179 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1180 if (I + 3 >= E)
1181 return insufficient_usr(kind, "<method selector> "
1182 "[0=class method|1=instance method] <class USR>");
1183 if (!isUSR(I[3]))
1184 return not_usr("<class USR>", I[3]);
1185 else {
1186 CXString x;
1187 x.Spelling = I[3];
1188 x.MustFreeString = 0;
1189 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1190 }
1191 I += 4;
1192 continue;
1193 }
1194 break;
1195 case 12:
1196 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1197 if (I + 2 >= E)
1198 return insufficient_usr(kind, "<class name> <category name>");
1199 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1200 I += 3;
1201 continue;
1202 }
1203 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1204 if (I + 1 >= E)
1205 return insufficient_usr(kind, "<protocol name>");
1206 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1207 I += 2;
1208 continue;
1209 }
1210 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1211 if (I + 2 >= E)
1212 return insufficient_usr(kind, "<property name> <class USR>");
1213 if (!isUSR(I[2]))
1214 return not_usr("<class USR>", I[2]);
1215 else {
1216 CXString x;
1217 x.Spelling = I[2];
1218 x.MustFreeString = 0;
1219 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1220 }
1221 I += 3;
1222 continue;
1223 }
1224 break;
1225 default:
1226 break;
1227 }
1228 break;
1229 }
1230
1231 if (I != E) {
1232 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1233 display_usrs();
1234 return 1;
1235 }
1236 return 0;
1237}
1238
1239int print_usrs_file(const char *file_name) {
1240 char line[2048];
1241 const char *args[128];
1242 unsigned numChars = 0;
1243
1244 FILE *fp = fopen(file_name, "r");
1245 if (!fp) {
1246 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1247 return 1;
1248 }
1249
1250 /* This code is not really all that safe, but it works fine for testing. */
1251 while (!feof(fp)) {
1252 char c = fgetc(fp);
1253 if (c == '\n') {
1254 unsigned i = 0;
1255 const char *s = 0;
1256
1257 if (numChars == 0)
1258 continue;
1259
1260 line[numChars] = '\0';
1261 numChars = 0;
1262
1263 if (line[0] == '/' && line[1] == '/')
1264 continue;
1265
1266 s = strtok(line, " ");
1267 while (s) {
1268 args[i] = s;
1269 ++i;
1270 s = strtok(0, " ");
1271 }
1272 if (print_usrs(&args[0], &args[i]))
1273 return 1;
1274 }
1275 else
1276 line[numChars++] = c;
1277 }
1278
1279 fclose(fp);
1280 return 0;
1281}
1282
1283/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001284/* Command line processing. */
1285/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001286int write_pch_file(const char *filename, int argc, const char *argv[]) {
1287 CXIndex Idx;
1288 CXTranslationUnit TU;
1289 struct CXUnsavedFile *unsaved_files = 0;
1290 int num_unsaved_files = 0;
1291
1292 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1293
1294 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1295 clang_disposeIndex(Idx);
1296 return -1;
1297 }
1298
1299 TU = clang_parseTranslationUnit(Idx, 0,
1300 argv + num_unsaved_files,
1301 argc - num_unsaved_files,
1302 unsaved_files,
1303 num_unsaved_files,
1304 CXTranslationUnit_Incomplete);
1305 if (!TU) {
1306 fprintf(stderr, "Unable to load translation unit!\n");
1307 free_remapped_files(unsaved_files, num_unsaved_files);
1308 clang_disposeIndex(Idx);
1309 return 1;
1310 }
1311
Douglas Gregor19998442010-08-13 15:35:05 +00001312 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001313 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1314 clang_disposeTranslationUnit(TU);
1315 free_remapped_files(unsaved_files, num_unsaved_files);
1316 clang_disposeIndex(Idx);
1317 return 0;
1318}
1319
1320/******************************************************************************/
1321/* Command line processing. */
1322/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001323
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001324static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001325 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001326 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001327 if (strcmp(s, "-usrs") == 0)
1328 return USRVisitor;
1329 return NULL;
1330}
1331
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001332static void print_usage(void) {
1333 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001334 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001335 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001336 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001337 " c-index-test -test-file-scan <AST file> <source file> "
1338 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001339 " c-index-test -test-load-tu <AST file> <symbol filter> "
1340 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001341 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1342 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001343 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001344 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001345 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1346 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001347 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001348 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1349 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001350 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001351 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001352 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001353 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1354 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001355 " c-index-test -print-usr-file <file>\n"
1356 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001357 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001358 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001359 " all - load all symbols, including those from PCH\n"
1360 " local - load all symbols except those in PCH\n"
1361 " category - only load ObjC categories (non-PCH)\n"
1362 " interface - only load ObjC interfaces (non-PCH)\n"
1363 " protocol - only load ObjC protocols (non-PCH)\n"
1364 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001365 " typedef - only load typdefs (non-PCH)\n"
1366 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001367}
1368
1369int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001370 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001371 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001372 return perform_code_completion(argc, argv, 0);
1373 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1374 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001375 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1376 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001377 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001378 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001379 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001380 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1381 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001382 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001383 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1384 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1385 if (I) {
1386 int trials = atoi(argv[2]);
1387 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1388 NULL);
1389 }
1390 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001391 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001392 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001393 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001394 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001395 }
1396 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001397 return perform_file_scan(argv[2], argv[3],
1398 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001399 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1400 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001401 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1402 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1403 PrintInclusionStack);
1404 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1405 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1406 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001407 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1408 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1409 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001410 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1411 return perform_test_load_source(argc - 2, argv + 2, "all",
1412 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001413 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1414 if (argc > 2)
1415 return print_usrs(argv + 2, argv + argc);
1416 else {
1417 display_usrs();
1418 return 1;
1419 }
1420 }
1421 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1422 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001423 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1424 return write_pch_file(argv[2], argc - 3, argv + 3);
1425
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001426 print_usage();
1427 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001428}