blob: 2c4a3386df9e263b48774234b534f639a0f06a3c [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 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000067 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000068}
69
70int parse_remapped_files(int argc, const char **argv, int start_arg,
71 struct CXUnsavedFile **unsaved_files,
72 int *num_unsaved_files) {
73 int i;
74 int arg;
75 int prefix_len = strlen("-remap-file=");
76 *unsaved_files = 0;
77 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000078
Douglas Gregor4db64a42010-01-23 00:14:00 +000079 /* Count the number of remapped files. */
80 for (arg = start_arg; arg < argc; ++arg) {
81 if (strncmp(argv[arg], "-remap-file=", prefix_len))
82 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000083
Douglas Gregor4db64a42010-01-23 00:14:00 +000084 ++*num_unsaved_files;
85 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000086
Douglas Gregor4db64a42010-01-23 00:14:00 +000087 if (*num_unsaved_files == 0)
88 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000089
Douglas Gregor4db64a42010-01-23 00:14:00 +000090 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000091 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
92 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000093 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
94 struct CXUnsavedFile *unsaved = *unsaved_files + i;
95 const char *arg_string = argv[arg] + prefix_len;
96 int filename_len;
97 char *filename;
98 char *contents;
99 FILE *to_file;
100 const char *semi = strchr(arg_string, ';');
101 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000102 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000103 "error: -remap-file=from;to argument is missing semicolon\n");
104 free_remapped_files(*unsaved_files, i);
105 *unsaved_files = 0;
106 *num_unsaved_files = 0;
107 return -1;
108 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000109
Douglas Gregor4db64a42010-01-23 00:14:00 +0000110 /* Open the file that we're remapping to. */
111 to_file = fopen(semi + 1, "r");
112 if (!to_file) {
113 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
114 semi + 1);
115 free_remapped_files(*unsaved_files, i);
116 *unsaved_files = 0;
117 *num_unsaved_files = 0;
118 return -1;
119 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000120
Douglas Gregor4db64a42010-01-23 00:14:00 +0000121 /* Determine the length of the file we're remapping to. */
122 fseek(to_file, 0, SEEK_END);
123 unsaved->Length = ftell(to_file);
124 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000125
Douglas Gregor4db64a42010-01-23 00:14:00 +0000126 /* Read the contents of the file we're remapping to. */
127 contents = (char *)malloc(unsaved->Length + 1);
128 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
129 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
130 (feof(to_file) ? "EOF" : "error"), semi + 1);
131 fclose(to_file);
132 free_remapped_files(*unsaved_files, i);
133 *unsaved_files = 0;
134 *num_unsaved_files = 0;
135 return -1;
136 }
137 contents[unsaved->Length] = 0;
138 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000139
Douglas Gregor4db64a42010-01-23 00:14:00 +0000140 /* Close the file. */
141 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000142
Douglas Gregor4db64a42010-01-23 00:14:00 +0000143 /* Copy the file name that we're remapping from. */
144 filename_len = semi - arg_string;
145 filename = (char *)malloc(filename_len + 1);
146 memcpy(filename, arg_string, filename_len);
147 filename[filename_len] = 0;
148 unsaved->Filename = filename;
149 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000150
Douglas Gregor4db64a42010-01-23 00:14:00 +0000151 return 0;
152}
153
Ted Kremenek0d435192009-11-17 18:13:31 +0000154/******************************************************************************/
155/* Pretty-printing. */
156/******************************************************************************/
157
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000158static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000159 if (clang_isInvalid(Cursor.kind)) {
160 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
161 printf("Invalid Cursor => %s", clang_getCString(ks));
162 clang_disposeString(ks);
163 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000164 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000165 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000166 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000167 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000168
169 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000170 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000171 printf("%s=%s", clang_getCString(ks),
172 clang_getCString(string));
173 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000174 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000175
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000176 Referenced = clang_getCursorReferenced(Cursor);
177 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
178 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000179 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000180 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000181 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000182
183 if (clang_isCursorDefinition(Cursor))
184 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000185 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000186}
Steve Naroff89922f82009-08-31 00:59:03 +0000187
Ted Kremeneke68fff62010-02-17 00:41:32 +0000188static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000189 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000190 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000191 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000192 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000193 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000194 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000195 clang_disposeString(source);
196 return "<invalid loc>";
197 }
198 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000199 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000200 clang_disposeString(source);
201 return b;
202 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000203}
204
Ted Kremenek0d435192009-11-17 18:13:31 +0000205/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000206/* Callbacks. */
207/******************************************************************************/
208
209typedef void (*PostVisitTU)(CXTranslationUnit);
210
Douglas Gregora88084b2010-02-18 18:08:43 +0000211void PrintDiagnostic(CXDiagnostic Diagnostic) {
212 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000213 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000214 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000215 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
216 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
217 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000218
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000219 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000220 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000221
Douglas Gregor274f1902010-02-22 23:17:23 +0000222 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
223 fprintf(stderr, "%s\n", clang_getCString(Msg));
224 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000225
Douglas Gregor5352ac02010-01-28 00:27:43 +0000226 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000227 &file, 0, 0, 0);
228 if (!file)
229 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000230
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000231 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
232 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000233 CXSourceRange range;
234 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
235 CXSourceLocation start = clang_getRangeStart(range);
236 CXSourceLocation end = clang_getRangeEnd(range);
237 unsigned start_line, start_column, end_line, end_column;
238 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000239 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000240 &start_column, 0);
241 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
242 if (clang_equalLocations(start, end)) {
243 /* Insertion. */
244 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000245 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000246 clang_getCString(insertion_text), start_line, start_column);
247 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
248 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000249 if (start_file == file && end_file == file) {
250 fprintf(out, "FIX-IT: Remove ");
251 PrintExtent(out, start_line, start_column, end_line, end_column);
252 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000253 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000254 } else {
255 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000256 if (start_file == end_file) {
257 fprintf(out, "FIX-IT: Replace ");
258 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000259 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000260 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000261 break;
262 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000263 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000264 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000265}
266
Douglas Gregora88084b2010-02-18 18:08:43 +0000267void PrintDiagnostics(CXTranslationUnit TU) {
268 int i, n = clang_getNumDiagnostics(TU);
269 for (i = 0; i != n; ++i) {
270 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
271 PrintDiagnostic(Diag);
272 clang_disposeDiagnostic(Diag);
273 }
274}
275
Ted Kremenekce2ae882010-01-26 17:59:48 +0000276/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000277/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000278/******************************************************************************/
279
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000280static const char *FileCheckPrefix = "CHECK";
281
Douglas Gregora7bde202010-01-19 00:34:46 +0000282static void PrintCursorExtent(CXCursor C) {
283 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000284 CXFile begin_file, end_file;
285 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000286
Douglas Gregor1db19de2010-01-19 21:36:55 +0000287 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000288 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000289 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000290 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000291 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000292 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000293
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000294 printf(" Extent=");
295 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000296}
297
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000298/* Data used by all of the visitors. */
299typedef struct {
300 CXTranslationUnit TU;
301 enum CXCursorKind *Filter;
302} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000303
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000304
Ted Kremeneke68fff62010-02-17 00:41:32 +0000305enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000306 CXCursor Parent,
307 CXClientData ClientData) {
308 VisitorData *Data = (VisitorData *)ClientData;
309 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000310 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000311 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000312 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000313 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000314 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000315 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000316 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000317 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000318 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000319 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000320
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000321 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000322}
Steve Naroff50398192009-08-28 15:28:48 +0000323
Ted Kremeneke68fff62010-02-17 00:41:32 +0000324static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000325 CXCursor Parent,
326 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000327 const char *startBuf, *endBuf;
328 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
329 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000330 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000331
Douglas Gregorb6998662010-01-19 19:34:47 +0000332 if (Cursor.kind != CXCursor_FunctionDecl ||
333 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000334 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000335
336 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
337 &startLine, &startColumn,
338 &endLine, &endColumn);
339 /* Probe the entire body, looking for both decls and refs. */
340 curLine = startLine;
341 curColumn = startColumn;
342
343 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000344 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000345 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000346 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000347
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000348 if (*startBuf == '\n') {
349 startBuf++;
350 curLine++;
351 curColumn = 1;
352 } else if (*startBuf != '\t')
353 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000354
Douglas Gregor98258af2010-01-18 22:46:11 +0000355 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000356 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000357
Douglas Gregor1db19de2010-01-19 21:36:55 +0000358 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000359 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000360 CXSourceLocation RefLoc
361 = clang_getLocation(Data->TU, file, curLine, curColumn);
362 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000363 if (Ref.kind == CXCursor_NoDeclFound) {
364 /* Nothing found here; that's fine. */
365 } else if (Ref.kind != CXCursor_FunctionDecl) {
366 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
367 curLine, curColumn);
368 PrintCursor(Ref);
369 printf("\n");
370 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000371 }
Ted Kremenek74844072010-02-17 00:41:20 +0000372 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000373 startBuf++;
374 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000375
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000376 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000377}
378
Ted Kremenek7d405622010-01-12 23:34:26 +0000379/******************************************************************************/
380/* USR testing. */
381/******************************************************************************/
382
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000383enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
384 CXClientData ClientData) {
385 VisitorData *Data = (VisitorData *)ClientData;
386 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000387 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000388 const char *cstr = clang_getCString(USR);
389 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000390 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000391 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000392 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000393 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
394
Douglas Gregora7bde202010-01-19 00:34:46 +0000395 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000396 printf("\n");
397 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000398
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000399 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000400 }
401
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000402 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000403}
404
405/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000406/* Inclusion stack testing. */
407/******************************************************************************/
408
409void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
410 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000411
Ted Kremenek16b55a72010-01-26 19:31:51 +0000412 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000413 CXString fname;
414
415 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000416 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000417 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000418
Ted Kremenek16b55a72010-01-26 19:31:51 +0000419 for (i = 0; i < includeStackLen; ++i) {
420 CXFile includingFile;
421 unsigned line, column;
422 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
423 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000424 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000425 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000426 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000427 }
428 printf("\n");
429}
430
431void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000432 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000433}
434
435/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000436/* Linkage testing. */
437/******************************************************************************/
438
439static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
440 CXClientData d) {
441 const char *linkage = 0;
442
443 if (clang_isInvalid(clang_getCursorKind(cursor)))
444 return CXChildVisit_Recurse;
445
446 switch (clang_getCursorLinkage(cursor)) {
447 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000448 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
449 case CXLinkage_Internal: linkage = "Internal"; break;
450 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
451 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000452 }
453
454 if (linkage) {
455 PrintCursor(cursor);
456 printf("linkage=%s\n", linkage);
457 }
458
459 return CXChildVisit_Recurse;
460}
461
462/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000463/* Typekind testing. */
464/******************************************************************************/
465
466static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
467 CXClientData d) {
468
469 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
470 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000471 CXString S = clang_getTypeKindSpelling(T.kind);
472 PrintCursor(cursor);
473 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000474 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000475 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000476 {
477 CXType CT = clang_getCanonicalType(T);
478 if (!clang_equalTypes(T, CT)) {
479 CXString CS = clang_getTypeKindSpelling(CT.kind);
480 printf(" [canonical=%s]", clang_getCString(CS));
481 clang_disposeString(CS);
482 }
483 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000484 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000485 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000486 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000487 if (RT.kind != CXType_Invalid) {
488 CXString RS = clang_getTypeKindSpelling(RT.kind);
489 printf(" [result=%s]", clang_getCString(RS));
490 clang_disposeString(RS);
491 }
492 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000493 /* Print if this is a non-POD type. */
494 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000495
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000496 printf("\n");
497 }
498 return CXChildVisit_Recurse;
499}
500
501
502/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000503/* Loading ASTs/source. */
504/******************************************************************************/
505
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000506static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000507 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000508 CXCursorVisitor Visitor,
509 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000510
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000511 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000512 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000513
514 if (Visitor) {
515 enum CXCursorKind K = CXCursor_NotImplemented;
516 enum CXCursorKind *ck = &K;
517 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000518
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000519 /* Perform some simple filtering. */
520 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000521 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000522 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
523 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
524 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
525 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
526 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
527 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
528 else {
529 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
530 return 1;
531 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000532
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000533 Data.TU = TU;
534 Data.Filter = ck;
535 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000536 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000537
Ted Kremenekce2ae882010-01-26 17:59:48 +0000538 if (PV)
539 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000540
Douglas Gregora88084b2010-02-18 18:08:43 +0000541 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000542 clang_disposeTranslationUnit(TU);
543 return 0;
544}
545
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000546int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000547 const char *prefix, CXCursorVisitor Visitor,
548 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000549 CXIndex Idx;
550 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000551 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000552 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000553 !strcmp(filter, "local") ? 1 : 0,
554 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000555
Ted Kremenek020a0952010-02-11 07:41:25 +0000556 if (!CreateTranslationUnit(Idx, file, &TU)) {
557 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000558 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000559 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000560
Ted Kremenek020a0952010-02-11 07:41:25 +0000561 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
562 clang_disposeIndex(Idx);
563 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000564}
565
Ted Kremenekce2ae882010-01-26 17:59:48 +0000566int perform_test_load_source(int argc, const char **argv,
567 const char *filter, CXCursorVisitor Visitor,
568 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000569 const char *UseExternalASTs =
570 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000571 CXIndex Idx;
572 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000573 struct CXUnsavedFile *unsaved_files = 0;
574 int num_unsaved_files = 0;
575 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000576
Daniel Dunbarada487d2009-12-01 02:03:10 +0000577 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000578 !strcmp(filter, "local") ? 1 : 0,
579 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000580
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000581 if (UseExternalASTs && strlen(UseExternalASTs))
582 clang_setUseExternalASTGeneration(Idx, 1);
583
Ted Kremenek020a0952010-02-11 07:41:25 +0000584 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
585 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000586 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000587 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000588
Ted Kremeneke68fff62010-02-17 00:41:32 +0000589 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
590 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000591 argv + num_unsaved_files,
592 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000593 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000594 if (!TU) {
595 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000596 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000597 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000598 return 1;
599 }
600
Ted Kremenekce2ae882010-01-26 17:59:48 +0000601 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000602 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000603 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000604 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000605}
606
Douglas Gregorabc563f2010-07-19 21:46:24 +0000607int perform_test_reparse_source(int argc, const char **argv, int trials,
608 const char *filter, CXCursorVisitor Visitor,
609 PostVisitTU PV) {
610 const char *UseExternalASTs =
611 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
612 CXIndex Idx;
613 CXTranslationUnit TU;
614 struct CXUnsavedFile *unsaved_files = 0;
615 int num_unsaved_files = 0;
616 int result;
617 int trial;
618
619 Idx = clang_createIndex(/* excludeDeclsFromPCH */
620 !strcmp(filter, "local") ? 1 : 0,
621 /* displayDiagnosics=*/1);
622
623 if (UseExternalASTs && strlen(UseExternalASTs))
624 clang_setUseExternalASTGeneration(Idx, 1);
625
626 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
627 clang_disposeIndex(Idx);
628 return -1;
629 }
630
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000631 /* Load the initial translation unit -- we do this without honoring remapped
632 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000633 TU = clang_parseTranslationUnit(Idx, 0,
634 argv + num_unsaved_files,
635 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000636 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000637 if (!TU) {
638 fprintf(stderr, "Unable to load translation unit!\n");
639 free_remapped_files(unsaved_files, num_unsaved_files);
640 clang_disposeIndex(Idx);
641 return 1;
642 }
643
644 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000645 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
646 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000647 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000648 clang_disposeTranslationUnit(TU);
649 free_remapped_files(unsaved_files, num_unsaved_files);
650 clang_disposeIndex(Idx);
651 return -1;
652 }
653 }
654
655 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
656 free_remapped_files(unsaved_files, num_unsaved_files);
657 clang_disposeIndex(Idx);
658 return result;
659}
660
Ted Kremenek0d435192009-11-17 18:13:31 +0000661/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000662/* Logic for testing clang_getCursor(). */
663/******************************************************************************/
664
665static void print_cursor_file_scan(CXCursor cursor,
666 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000667 unsigned end_line, unsigned end_col,
668 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000669 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000670 if (prefix)
671 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000672 PrintExtent(stdout, start_line, start_col, end_line, end_col);
673 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000674 PrintCursor(cursor);
675 printf("\n");
676}
677
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000678static int perform_file_scan(const char *ast_file, const char *source_file,
679 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000680 CXIndex Idx;
681 CXTranslationUnit TU;
682 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000683 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000684 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000685 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000686 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000687
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000688 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
689 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000690 fprintf(stderr, "Could not create Index\n");
691 return 1;
692 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000693
Ted Kremenek1c6da172009-11-17 19:37:36 +0000694 if (!CreateTranslationUnit(Idx, ast_file, &TU))
695 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000696
Ted Kremenek1c6da172009-11-17 19:37:36 +0000697 if ((fp = fopen(source_file, "r")) == NULL) {
698 fprintf(stderr, "Could not open '%s'\n", source_file);
699 return 1;
700 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000701
Douglas Gregorb9790342010-01-22 21:44:22 +0000702 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000703 for (;;) {
704 CXCursor cursor;
705 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000706
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000707 if (c == '\n') {
708 ++line;
709 col = 1;
710 } else
711 ++col;
712
713 /* Check the cursor at this position, and dump the previous one if we have
714 * found something new.
715 */
716 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
717 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
718 prevCursor.kind != CXCursor_InvalidFile) {
719 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000720 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000721 start_line = line;
722 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000723 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000724 if (c == EOF)
725 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000726
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000727 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000728 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000729
Ted Kremenek1c6da172009-11-17 19:37:36 +0000730 fclose(fp);
731 return 0;
732}
733
734/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000735/* Logic for testing clang_codeComplete(). */
736/******************************************************************************/
737
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000738/* Parse file:line:column from the input string. Returns 0 on success, non-zero
739 on failure. If successful, the pointer *filename will contain newly-allocated
740 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000741int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000742 unsigned *column, unsigned *second_line,
743 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000744 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000745 const char *last_colon = strrchr(input, ':');
746 unsigned values[4], i;
747 unsigned num_values = (second_line && second_column)? 4 : 2;
748
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000749 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000750 if (!last_colon || last_colon == input) {
751 if (num_values == 4)
752 fprintf(stderr, "could not parse filename:line:column:line:column in "
753 "'%s'\n", input);
754 else
755 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000756 return 1;
757 }
758
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000759 for (i = 0; i != num_values; ++i) {
760 const char *prev_colon;
761
762 /* Parse the next line or column. */
763 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
764 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000765 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000766 (i % 2 ? "column" : "line"), input);
767 return 1;
768 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000769
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000770 if (i + 1 == num_values)
771 break;
772
773 /* Find the previous colon. */
774 prev_colon = last_colon - 1;
775 while (prev_colon != input && *prev_colon != ':')
776 --prev_colon;
777 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000778 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000779 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000780 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000781 }
782
783 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000784 }
785
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000786 *line = values[0];
787 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000788
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000789 if (second_line && second_column) {
790 *second_line = values[2];
791 *second_column = values[3];
792 }
793
Douglas Gregor88d23952009-11-09 18:19:57 +0000794 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000795 *filename = (char*)malloc(last_colon - input + 1);
796 memcpy(*filename, input, last_colon - input);
797 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000798 return 0;
799}
800
801const char *
802clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
803 switch (Kind) {
804 case CXCompletionChunk_Optional: return "Optional";
805 case CXCompletionChunk_TypedText: return "TypedText";
806 case CXCompletionChunk_Text: return "Text";
807 case CXCompletionChunk_Placeholder: return "Placeholder";
808 case CXCompletionChunk_Informative: return "Informative";
809 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
810 case CXCompletionChunk_LeftParen: return "LeftParen";
811 case CXCompletionChunk_RightParen: return "RightParen";
812 case CXCompletionChunk_LeftBracket: return "LeftBracket";
813 case CXCompletionChunk_RightBracket: return "RightBracket";
814 case CXCompletionChunk_LeftBrace: return "LeftBrace";
815 case CXCompletionChunk_RightBrace: return "RightBrace";
816 case CXCompletionChunk_LeftAngle: return "LeftAngle";
817 case CXCompletionChunk_RightAngle: return "RightAngle";
818 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000819 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000820 case CXCompletionChunk_Colon: return "Colon";
821 case CXCompletionChunk_SemiColon: return "SemiColon";
822 case CXCompletionChunk_Equal: return "Equal";
823 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
824 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000825 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000826
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000827 return "Unknown";
828}
829
Douglas Gregor3ac73852009-11-09 16:04:45 +0000830void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000831 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000832
Douglas Gregor3ac73852009-11-09 16:04:45 +0000833 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000834 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000835 CXString text;
836 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000837 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000838 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000839
Douglas Gregor3ac73852009-11-09 16:04:45 +0000840 if (Kind == CXCompletionChunk_Optional) {
841 fprintf(file, "{Optional ");
842 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000843 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000844 file);
845 fprintf(file, "}");
846 continue;
847 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000848
Douglas Gregord5a20892009-11-09 17:05:28 +0000849 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000850 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000851 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000852 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000853 cstr ? cstr : "");
854 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000855 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000856
Douglas Gregor3ac73852009-11-09 16:04:45 +0000857}
858
859void print_completion_result(CXCompletionResult *completion_result,
860 CXClientData client_data) {
861 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000862 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
863
864 fprintf(file, "%s:", clang_getCString(ks));
865 clang_disposeString(ks);
866
Douglas Gregor3ac73852009-11-09 16:04:45 +0000867 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor12e13132010-05-26 22:00:08 +0000868 fprintf(file, " (%u)\n",
869 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000870}
871
Douglas Gregor1982c182010-07-12 18:38:41 +0000872int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000873 const char *input = argv[1];
874 char *filename = 0;
875 unsigned line;
876 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000877 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000878 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000879 struct CXUnsavedFile *unsaved_files = 0;
880 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000881 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000882 CXTranslationUnit *TU = 0;
883
Douglas Gregor1982c182010-07-12 18:38:41 +0000884 if (timing_only)
885 input += strlen("-code-completion-timing=");
886 else
887 input += strlen("-code-completion-at=");
888
Ted Kremeneke68fff62010-02-17 00:41:32 +0000889 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000890 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000891 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000892
Douglas Gregor735df882009-12-02 09:21:34 +0000893 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
894 return -1;
895
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000896 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000897 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000898 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +0000899 TU = clang_parseTranslationUnit(CIdx, 0,
900 argv + num_unsaved_files + 2,
901 argc - num_unsaved_files - 2,
902 unsaved_files,
903 num_unsaved_files,
904 getDefaultParsingOptions());
Douglas Gregordf95a132010-08-09 20:45:32 +0000905 for (I = 0; I != Repeats; ++I) {
906 results = clang_codeCompleteAt(TU, filename, line, column,
907 unsaved_files, num_unsaved_files,
908 clang_defaultCodeCompleteOptions());
909 if (I != Repeats-1)
910 clang_disposeCodeCompleteResults(results);
911 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000912 } else
913 results = clang_codeComplete(CIdx,
914 argv[argc - 1], argc - num_unsaved_files - 3,
915 argv + num_unsaved_files + 2,
916 num_unsaved_files, unsaved_files,
917 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000918
Douglas Gregorec6762c2009-12-18 16:20:58 +0000919 if (results) {
920 unsigned i, n = results->NumResults;
Douglas Gregor1982c182010-07-12 18:38:41 +0000921 if (!timing_only)
922 for (i = 0; i != n; ++i)
923 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000924 n = clang_codeCompleteGetNumDiagnostics(results);
925 for (i = 0; i != n; ++i) {
926 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
927 PrintDiagnostic(diag);
928 clang_disposeDiagnostic(diag);
929 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000930 clang_disposeCodeCompleteResults(results);
931 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000932 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000933 clang_disposeIndex(CIdx);
934 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000935
Douglas Gregor735df882009-12-02 09:21:34 +0000936 free_remapped_files(unsaved_files, num_unsaved_files);
937
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000938 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000939}
940
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000941typedef struct {
942 char *filename;
943 unsigned line;
944 unsigned column;
945} CursorSourceLocation;
946
947int inspect_cursor_at(int argc, const char **argv) {
948 CXIndex CIdx;
949 int errorCode;
950 struct CXUnsavedFile *unsaved_files = 0;
951 int num_unsaved_files = 0;
952 CXTranslationUnit TU;
953 CXCursor Cursor;
954 CursorSourceLocation *Locations = 0;
955 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000956
Ted Kremeneke68fff62010-02-17 00:41:32 +0000957 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000958 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
959 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000960
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000961 /* Parse the locations. */
962 assert(NumLocations > 0 && "Unable to count locations?");
963 Locations = (CursorSourceLocation *)malloc(
964 NumLocations * sizeof(CursorSourceLocation));
965 for (Loc = 0; Loc < NumLocations; ++Loc) {
966 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000967 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
968 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000969 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000970 return errorCode;
971 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972
973 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000974 &num_unsaved_files))
975 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000976
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000977 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000978 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
979 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000980 argv + num_unsaved_files + 1 + NumLocations,
981 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000982 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000983 if (!TU) {
984 fprintf(stderr, "unable to parse input\n");
985 return -1;
986 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000987
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000988 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000989 CXFile file = clang_getFile(TU, Locations[Loc].filename);
990 if (!file)
991 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000992
993 Cursor = clang_getCursor(TU,
994 clang_getLocation(TU, file, Locations[Loc].line,
995 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000996 PrintCursor(Cursor);
997 printf("\n");
998 free(Locations[Loc].filename);
999 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001000
Douglas Gregora88084b2010-02-18 18:08:43 +00001001 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001002 clang_disposeTranslationUnit(TU);
1003 clang_disposeIndex(CIdx);
1004 free(Locations);
1005 free_remapped_files(unsaved_files, num_unsaved_files);
1006 return 0;
1007}
1008
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001009int perform_token_annotation(int argc, const char **argv) {
1010 const char *input = argv[1];
1011 char *filename = 0;
1012 unsigned line, second_line;
1013 unsigned column, second_column;
1014 CXIndex CIdx;
1015 CXTranslationUnit TU = 0;
1016 int errorCode;
1017 struct CXUnsavedFile *unsaved_files = 0;
1018 int num_unsaved_files = 0;
1019 CXToken *tokens;
1020 unsigned num_tokens;
1021 CXSourceRange range;
1022 CXSourceLocation startLoc, endLoc;
1023 CXFile file = 0;
1024 CXCursor *cursors = 0;
1025 unsigned i;
1026
1027 input += strlen("-test-annotate-tokens=");
1028 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1029 &second_line, &second_column)))
1030 return errorCode;
1031
1032 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1033 return -1;
1034
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001035 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001036 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1037 argc - num_unsaved_files - 3,
1038 argv + num_unsaved_files + 2,
1039 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001040 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001041 if (!TU) {
1042 fprintf(stderr, "unable to parse input\n");
1043 clang_disposeIndex(CIdx);
1044 free(filename);
1045 free_remapped_files(unsaved_files, num_unsaved_files);
1046 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001047 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001048 errorCode = 0;
1049
1050 file = clang_getFile(TU, filename);
1051 if (!file) {
1052 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1053 errorCode = -1;
1054 goto teardown;
1055 }
1056
1057 startLoc = clang_getLocation(TU, file, line, column);
1058 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001059 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001060 column);
1061 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001062 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001063 }
1064
1065 endLoc = clang_getLocation(TU, file, second_line, second_column);
1066 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001067 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001068 second_line, second_column);
1069 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001070 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001071 }
1072
1073 range = clang_getRange(startLoc, endLoc);
1074 clang_tokenize(TU, range, &tokens, &num_tokens);
1075 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1076 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1077 for (i = 0; i != num_tokens; ++i) {
1078 const char *kind = "<unknown>";
1079 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1080 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1081 unsigned start_line, start_column, end_line, end_column;
1082
1083 switch (clang_getTokenKind(tokens[i])) {
1084 case CXToken_Punctuation: kind = "Punctuation"; break;
1085 case CXToken_Keyword: kind = "Keyword"; break;
1086 case CXToken_Identifier: kind = "Identifier"; break;
1087 case CXToken_Literal: kind = "Literal"; break;
1088 case CXToken_Comment: kind = "Comment"; break;
1089 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001090 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001091 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001092 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001093 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001094 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1095 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001096 if (!clang_isInvalid(cursors[i].kind)) {
1097 printf(" ");
1098 PrintCursor(cursors[i]);
1099 }
1100 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001101 }
1102 free(cursors);
1103
1104 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001105 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001106 clang_disposeTranslationUnit(TU);
1107 clang_disposeIndex(CIdx);
1108 free(filename);
1109 free_remapped_files(unsaved_files, num_unsaved_files);
1110 return errorCode;
1111}
1112
Ted Kremenek0d435192009-11-17 18:13:31 +00001113/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001114/* USR printing. */
1115/******************************************************************************/
1116
1117static int insufficient_usr(const char *kind, const char *usage) {
1118 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1119 return 1;
1120}
1121
1122static unsigned isUSR(const char *s) {
1123 return s[0] == 'c' && s[1] == ':';
1124}
1125
1126static int not_usr(const char *s, const char *arg) {
1127 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1128 return 1;
1129}
1130
1131static void print_usr(CXString usr) {
1132 const char *s = clang_getCString(usr);
1133 printf("%s\n", s);
1134 clang_disposeString(usr);
1135}
1136
1137static void display_usrs() {
1138 fprintf(stderr, "-print-usrs options:\n"
1139 " ObjCCategory <class name> <category name>\n"
1140 " ObjCClass <class name>\n"
1141 " ObjCIvar <ivar name> <class USR>\n"
1142 " ObjCMethod <selector> [0=class method|1=instance method] "
1143 "<class USR>\n"
1144 " ObjCProperty <property name> <class USR>\n"
1145 " ObjCProtocol <protocol name>\n");
1146}
1147
1148int print_usrs(const char **I, const char **E) {
1149 while (I != E) {
1150 const char *kind = *I;
1151 unsigned len = strlen(kind);
1152 switch (len) {
1153 case 8:
1154 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1155 if (I + 2 >= E)
1156 return insufficient_usr(kind, "<ivar name> <class USR>");
1157 if (!isUSR(I[2]))
1158 return not_usr("<class USR>", I[2]);
1159 else {
1160 CXString x;
1161 x.Spelling = I[2];
1162 x.MustFreeString = 0;
1163 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1164 }
1165
1166 I += 3;
1167 continue;
1168 }
1169 break;
1170 case 9:
1171 if (memcmp(kind, "ObjCClass", 9) == 0) {
1172 if (I + 1 >= E)
1173 return insufficient_usr(kind, "<class name>");
1174 print_usr(clang_constructUSR_ObjCClass(I[1]));
1175 I += 2;
1176 continue;
1177 }
1178 break;
1179 case 10:
1180 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1181 if (I + 3 >= E)
1182 return insufficient_usr(kind, "<method selector> "
1183 "[0=class method|1=instance method] <class USR>");
1184 if (!isUSR(I[3]))
1185 return not_usr("<class USR>", I[3]);
1186 else {
1187 CXString x;
1188 x.Spelling = I[3];
1189 x.MustFreeString = 0;
1190 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1191 }
1192 I += 4;
1193 continue;
1194 }
1195 break;
1196 case 12:
1197 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1198 if (I + 2 >= E)
1199 return insufficient_usr(kind, "<class name> <category name>");
1200 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1201 I += 3;
1202 continue;
1203 }
1204 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1205 if (I + 1 >= E)
1206 return insufficient_usr(kind, "<protocol name>");
1207 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1208 I += 2;
1209 continue;
1210 }
1211 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1212 if (I + 2 >= E)
1213 return insufficient_usr(kind, "<property name> <class USR>");
1214 if (!isUSR(I[2]))
1215 return not_usr("<class USR>", I[2]);
1216 else {
1217 CXString x;
1218 x.Spelling = I[2];
1219 x.MustFreeString = 0;
1220 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1221 }
1222 I += 3;
1223 continue;
1224 }
1225 break;
1226 default:
1227 break;
1228 }
1229 break;
1230 }
1231
1232 if (I != E) {
1233 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1234 display_usrs();
1235 return 1;
1236 }
1237 return 0;
1238}
1239
1240int print_usrs_file(const char *file_name) {
1241 char line[2048];
1242 const char *args[128];
1243 unsigned numChars = 0;
1244
1245 FILE *fp = fopen(file_name, "r");
1246 if (!fp) {
1247 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1248 return 1;
1249 }
1250
1251 /* This code is not really all that safe, but it works fine for testing. */
1252 while (!feof(fp)) {
1253 char c = fgetc(fp);
1254 if (c == '\n') {
1255 unsigned i = 0;
1256 const char *s = 0;
1257
1258 if (numChars == 0)
1259 continue;
1260
1261 line[numChars] = '\0';
1262 numChars = 0;
1263
1264 if (line[0] == '/' && line[1] == '/')
1265 continue;
1266
1267 s = strtok(line, " ");
1268 while (s) {
1269 args[i] = s;
1270 ++i;
1271 s = strtok(0, " ");
1272 }
1273 if (print_usrs(&args[0], &args[i]))
1274 return 1;
1275 }
1276 else
1277 line[numChars++] = c;
1278 }
1279
1280 fclose(fp);
1281 return 0;
1282}
1283
1284/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001285/* Command line processing. */
1286/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001287int write_pch_file(const char *filename, int argc, const char *argv[]) {
1288 CXIndex Idx;
1289 CXTranslationUnit TU;
1290 struct CXUnsavedFile *unsaved_files = 0;
1291 int num_unsaved_files = 0;
1292
1293 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1294
1295 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1296 clang_disposeIndex(Idx);
1297 return -1;
1298 }
1299
1300 TU = clang_parseTranslationUnit(Idx, 0,
1301 argv + num_unsaved_files,
1302 argc - num_unsaved_files,
1303 unsaved_files,
1304 num_unsaved_files,
1305 CXTranslationUnit_Incomplete);
1306 if (!TU) {
1307 fprintf(stderr, "Unable to load translation unit!\n");
1308 free_remapped_files(unsaved_files, num_unsaved_files);
1309 clang_disposeIndex(Idx);
1310 return 1;
1311 }
1312
Douglas Gregor19998442010-08-13 15:35:05 +00001313 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001314 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1315 clang_disposeTranslationUnit(TU);
1316 free_remapped_files(unsaved_files, num_unsaved_files);
1317 clang_disposeIndex(Idx);
1318 return 0;
1319}
1320
1321/******************************************************************************/
1322/* Command line processing. */
1323/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001324
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001325static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001326 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001327 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001328 if (strcmp(s, "-usrs") == 0)
1329 return USRVisitor;
1330 return NULL;
1331}
1332
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001333static void print_usage(void) {
1334 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001335 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001336 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001337 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001338 " c-index-test -test-file-scan <AST file> <source file> "
1339 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001340 " c-index-test -test-load-tu <AST file> <symbol filter> "
1341 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001342 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1343 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001344 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001345 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001346 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1347 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001348 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001349 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1350 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001351 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001352 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001353 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001354 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1355 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001356 " c-index-test -print-usr-file <file>\n"
1357 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001358 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001359 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001360 " all - load all symbols, including those from PCH\n"
1361 " local - load all symbols except those in PCH\n"
1362 " category - only load ObjC categories (non-PCH)\n"
1363 " interface - only load ObjC interfaces (non-PCH)\n"
1364 " protocol - only load ObjC protocols (non-PCH)\n"
1365 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001366 " typedef - only load typdefs (non-PCH)\n"
1367 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001368}
1369
1370int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001371 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001372 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001373 return perform_code_completion(argc, argv, 0);
1374 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1375 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001376 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1377 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001378 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001379 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001380 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001381 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1382 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001383 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001384 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1385 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1386 if (I) {
1387 int trials = atoi(argv[2]);
1388 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1389 NULL);
1390 }
1391 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001392 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001393 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001394 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001395 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001396 }
1397 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001398 return perform_file_scan(argv[2], argv[3],
1399 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001400 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1401 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001402 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1403 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1404 PrintInclusionStack);
1405 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1406 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1407 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001408 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1409 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1410 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001411 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1412 return perform_test_load_source(argc - 2, argv + 2, "all",
1413 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001414 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1415 if (argc > 2)
1416 return print_usrs(argv + 2, argv + argc);
1417 else {
1418 display_usrs();
1419 return 1;
1420 }
1421 }
1422 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1423 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001424 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1425 return write_pch_file(argv[2], argc - 3, argv + 3);
1426
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001427 print_usage();
1428 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001429}