blob: 81462132251a1c38dc26a65d0bc6b4483b26a8e9 [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"))
36 options |= CXTranslationUnit_Editing;
37
38 return options;
39}
40
Daniel Dunbar51b058c2010-02-14 08:32:24 +000041static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
42 unsigned end_line, unsigned end_column) {
43 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000044 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000045}
46
Ted Kremenek1c6da172009-11-17 19:37:36 +000047static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
48 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000049
Douglas Gregora88084b2010-02-18 18:08:43 +000050 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000051 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000052 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
53 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000054 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000055 return 1;
56}
57
Douglas Gregor4db64a42010-01-23 00:14:00 +000058void free_remapped_files(struct CXUnsavedFile *unsaved_files,
59 int num_unsaved_files) {
60 int i;
61 for (i = 0; i != num_unsaved_files; ++i) {
62 free((char *)unsaved_files[i].Filename);
63 free((char *)unsaved_files[i].Contents);
64 }
65}
66
67int parse_remapped_files(int argc, const char **argv, int start_arg,
68 struct CXUnsavedFile **unsaved_files,
69 int *num_unsaved_files) {
70 int i;
71 int arg;
72 int prefix_len = strlen("-remap-file=");
73 *unsaved_files = 0;
74 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000075
Douglas Gregor4db64a42010-01-23 00:14:00 +000076 /* Count the number of remapped files. */
77 for (arg = start_arg; arg < argc; ++arg) {
78 if (strncmp(argv[arg], "-remap-file=", prefix_len))
79 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000080
Douglas Gregor4db64a42010-01-23 00:14:00 +000081 ++*num_unsaved_files;
82 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000083
Douglas Gregor4db64a42010-01-23 00:14:00 +000084 if (*num_unsaved_files == 0)
85 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000086
Douglas Gregor4db64a42010-01-23 00:14:00 +000087 *unsaved_files
Ted Kremeneke68fff62010-02-17 00:41:32 +000088 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
Douglas Gregor4db64a42010-01-23 00:14:00 +000089 *num_unsaved_files);
90 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
91 struct CXUnsavedFile *unsaved = *unsaved_files + i;
92 const char *arg_string = argv[arg] + prefix_len;
93 int filename_len;
94 char *filename;
95 char *contents;
96 FILE *to_file;
97 const char *semi = strchr(arg_string, ';');
98 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000099 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100 "error: -remap-file=from;to argument is missing semicolon\n");
101 free_remapped_files(*unsaved_files, i);
102 *unsaved_files = 0;
103 *num_unsaved_files = 0;
104 return -1;
105 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000106
Douglas Gregor4db64a42010-01-23 00:14:00 +0000107 /* Open the file that we're remapping to. */
108 to_file = fopen(semi + 1, "r");
109 if (!to_file) {
110 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
111 semi + 1);
112 free_remapped_files(*unsaved_files, i);
113 *unsaved_files = 0;
114 *num_unsaved_files = 0;
115 return -1;
116 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000117
Douglas Gregor4db64a42010-01-23 00:14:00 +0000118 /* Determine the length of the file we're remapping to. */
119 fseek(to_file, 0, SEEK_END);
120 unsaved->Length = ftell(to_file);
121 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000122
Douglas Gregor4db64a42010-01-23 00:14:00 +0000123 /* Read the contents of the file we're remapping to. */
124 contents = (char *)malloc(unsaved->Length + 1);
125 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
126 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
127 (feof(to_file) ? "EOF" : "error"), semi + 1);
128 fclose(to_file);
129 free_remapped_files(*unsaved_files, i);
130 *unsaved_files = 0;
131 *num_unsaved_files = 0;
132 return -1;
133 }
134 contents[unsaved->Length] = 0;
135 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000136
Douglas Gregor4db64a42010-01-23 00:14:00 +0000137 /* Close the file. */
138 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000139
Douglas Gregor4db64a42010-01-23 00:14:00 +0000140 /* Copy the file name that we're remapping from. */
141 filename_len = semi - arg_string;
142 filename = (char *)malloc(filename_len + 1);
143 memcpy(filename, arg_string, filename_len);
144 filename[filename_len] = 0;
145 unsaved->Filename = filename;
146 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000147
Douglas Gregor4db64a42010-01-23 00:14:00 +0000148 return 0;
149}
150
Ted Kremenek0d435192009-11-17 18:13:31 +0000151/******************************************************************************/
152/* Pretty-printing. */
153/******************************************************************************/
154
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000155static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000156 if (clang_isInvalid(Cursor.kind)) {
157 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
158 printf("Invalid Cursor => %s", clang_getCString(ks));
159 clang_disposeString(ks);
160 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000161 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000162 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000163 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000164 unsigned line, column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000165
166 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000167 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000168 printf("%s=%s", clang_getCString(ks),
169 clang_getCString(string));
170 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000171 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000172
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000173 Referenced = clang_getCursorReferenced(Cursor);
174 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
175 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000176 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000177 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000178 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000179
180 if (clang_isCursorDefinition(Cursor))
181 printf(" (Definition)");
Steve Naroff699a07d2009-09-25 21:32:34 +0000182 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000183}
Steve Naroff89922f82009-08-31 00:59:03 +0000184
Ted Kremeneke68fff62010-02-17 00:41:32 +0000185static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000186 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000187 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000188 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000189 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000190 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000191 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000192 clang_disposeString(source);
193 return "<invalid loc>";
194 }
195 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000196 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000197 clang_disposeString(source);
198 return b;
199 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000200}
201
Ted Kremenek0d435192009-11-17 18:13:31 +0000202/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000203/* Callbacks. */
204/******************************************************************************/
205
206typedef void (*PostVisitTU)(CXTranslationUnit);
207
Douglas Gregora88084b2010-02-18 18:08:43 +0000208void PrintDiagnostic(CXDiagnostic Diagnostic) {
209 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000210 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000211 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000212 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
213 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
214 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000215
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000216 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000217 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000218
Douglas Gregor274f1902010-02-22 23:17:23 +0000219 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
220 fprintf(stderr, "%s\n", clang_getCString(Msg));
221 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000222
Douglas Gregor5352ac02010-01-28 00:27:43 +0000223 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000224 &file, 0, 0, 0);
225 if (!file)
226 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000227
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000228 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
229 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000230 CXSourceRange range;
231 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
232 CXSourceLocation start = clang_getRangeStart(range);
233 CXSourceLocation end = clang_getRangeEnd(range);
234 unsigned start_line, start_column, end_line, end_column;
235 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000236 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000237 &start_column, 0);
238 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
239 if (clang_equalLocations(start, end)) {
240 /* Insertion. */
241 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000242 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000243 clang_getCString(insertion_text), start_line, start_column);
244 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
245 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000246 if (start_file == file && end_file == file) {
247 fprintf(out, "FIX-IT: Remove ");
248 PrintExtent(out, start_line, start_column, end_line, end_column);
249 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000250 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000251 } else {
252 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000253 if (start_file == end_file) {
254 fprintf(out, "FIX-IT: Replace ");
255 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000256 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000257 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000258 break;
259 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000260 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000261 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000262}
263
Douglas Gregora88084b2010-02-18 18:08:43 +0000264void PrintDiagnostics(CXTranslationUnit TU) {
265 int i, n = clang_getNumDiagnostics(TU);
266 for (i = 0; i != n; ++i) {
267 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
268 PrintDiagnostic(Diag);
269 clang_disposeDiagnostic(Diag);
270 }
271}
272
Ted Kremenekce2ae882010-01-26 17:59:48 +0000273/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000274/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000275/******************************************************************************/
276
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000277static const char *FileCheckPrefix = "CHECK";
278
Douglas Gregora7bde202010-01-19 00:34:46 +0000279static void PrintCursorExtent(CXCursor C) {
280 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000281 CXFile begin_file, end_file;
282 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000283
Douglas Gregor1db19de2010-01-19 21:36:55 +0000284 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000285 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000286 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000287 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000288 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000289 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000290
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000291 printf(" Extent=");
292 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000293}
294
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000295/* Data used by all of the visitors. */
296typedef struct {
297 CXTranslationUnit TU;
298 enum CXCursorKind *Filter;
299} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000300
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000301
Ted Kremeneke68fff62010-02-17 00:41:32 +0000302enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000303 CXCursor Parent,
304 CXClientData ClientData) {
305 VisitorData *Data = (VisitorData *)ClientData;
306 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000307 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000308 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000309 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000310 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000311 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000312 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000313 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000314 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000315 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000316 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000317
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000318 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000319}
Steve Naroff50398192009-08-28 15:28:48 +0000320
Ted Kremeneke68fff62010-02-17 00:41:32 +0000321static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000322 CXCursor Parent,
323 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000324 const char *startBuf, *endBuf;
325 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
326 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000327 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000328
Douglas Gregorb6998662010-01-19 19:34:47 +0000329 if (Cursor.kind != CXCursor_FunctionDecl ||
330 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000331 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000332
333 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
334 &startLine, &startColumn,
335 &endLine, &endColumn);
336 /* Probe the entire body, looking for both decls and refs. */
337 curLine = startLine;
338 curColumn = startColumn;
339
340 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000341 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000343 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000344
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000345 if (*startBuf == '\n') {
346 startBuf++;
347 curLine++;
348 curColumn = 1;
349 } else if (*startBuf != '\t')
350 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000351
Douglas Gregor98258af2010-01-18 22:46:11 +0000352 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000353 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000354
Douglas Gregor1db19de2010-01-19 21:36:55 +0000355 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000356 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000357 CXSourceLocation RefLoc
358 = clang_getLocation(Data->TU, file, curLine, curColumn);
359 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000360 if (Ref.kind == CXCursor_NoDeclFound) {
361 /* Nothing found here; that's fine. */
362 } else if (Ref.kind != CXCursor_FunctionDecl) {
363 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
364 curLine, curColumn);
365 PrintCursor(Ref);
366 printf("\n");
367 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000368 }
Ted Kremenek74844072010-02-17 00:41:20 +0000369 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000370 startBuf++;
371 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000372
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000373 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000374}
375
Ted Kremenek7d405622010-01-12 23:34:26 +0000376/******************************************************************************/
377/* USR testing. */
378/******************************************************************************/
379
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000380enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
381 CXClientData ClientData) {
382 VisitorData *Data = (VisitorData *)ClientData;
383 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000384 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000385 const char *cstr = clang_getCString(USR);
386 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000387 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000388 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000389 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000390 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
391
Douglas Gregora7bde202010-01-19 00:34:46 +0000392 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000393 printf("\n");
394 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000395
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000396 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000397 }
398
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000399 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000400}
401
402/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000403/* Inclusion stack testing. */
404/******************************************************************************/
405
406void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
407 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000408
Ted Kremenek16b55a72010-01-26 19:31:51 +0000409 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000410 CXString fname;
411
412 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000413 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000414 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000415
Ted Kremenek16b55a72010-01-26 19:31:51 +0000416 for (i = 0; i < includeStackLen; ++i) {
417 CXFile includingFile;
418 unsigned line, column;
419 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
420 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000421 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000422 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000423 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000424 }
425 printf("\n");
426}
427
428void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000429 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000430}
431
432/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000433/* Linkage testing. */
434/******************************************************************************/
435
436static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
437 CXClientData d) {
438 const char *linkage = 0;
439
440 if (clang_isInvalid(clang_getCursorKind(cursor)))
441 return CXChildVisit_Recurse;
442
443 switch (clang_getCursorLinkage(cursor)) {
444 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000445 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
446 case CXLinkage_Internal: linkage = "Internal"; break;
447 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
448 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000449 }
450
451 if (linkage) {
452 PrintCursor(cursor);
453 printf("linkage=%s\n", linkage);
454 }
455
456 return CXChildVisit_Recurse;
457}
458
459/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000460/* Typekind testing. */
461/******************************************************************************/
462
463static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
464 CXClientData d) {
465
466 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
467 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000468 CXString S = clang_getTypeKindSpelling(T.kind);
469 PrintCursor(cursor);
470 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000471 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000472 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000473 {
474 CXType CT = clang_getCanonicalType(T);
475 if (!clang_equalTypes(T, CT)) {
476 CXString CS = clang_getTypeKindSpelling(CT.kind);
477 printf(" [canonical=%s]", clang_getCString(CS));
478 clang_disposeString(CS);
479 }
480 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000481 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000482 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000483 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000484 if (RT.kind != CXType_Invalid) {
485 CXString RS = clang_getTypeKindSpelling(RT.kind);
486 printf(" [result=%s]", clang_getCString(RS));
487 clang_disposeString(RS);
488 }
489 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000490 /* Print if this is a non-POD type. */
491 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000492
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000493 printf("\n");
494 }
495 return CXChildVisit_Recurse;
496}
497
498
499/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000500/* Loading ASTs/source. */
501/******************************************************************************/
502
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000503static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000504 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000505 CXCursorVisitor Visitor,
506 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000507
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000508 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000509 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000510
511 if (Visitor) {
512 enum CXCursorKind K = CXCursor_NotImplemented;
513 enum CXCursorKind *ck = &K;
514 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000515
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000516 /* Perform some simple filtering. */
517 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000518 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000519 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
520 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
521 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
522 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
523 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
524 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
525 else {
526 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
527 return 1;
528 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000529
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000530 Data.TU = TU;
531 Data.Filter = ck;
532 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000533 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000534
Ted Kremenekce2ae882010-01-26 17:59:48 +0000535 if (PV)
536 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000537
Douglas Gregora88084b2010-02-18 18:08:43 +0000538 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000539 clang_disposeTranslationUnit(TU);
540 return 0;
541}
542
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000543int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000544 const char *prefix, CXCursorVisitor Visitor,
545 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000546 CXIndex Idx;
547 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000548 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000549 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000550 !strcmp(filter, "local") ? 1 : 0,
551 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000552
Ted Kremenek020a0952010-02-11 07:41:25 +0000553 if (!CreateTranslationUnit(Idx, file, &TU)) {
554 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000555 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000556 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000557
Ted Kremenek020a0952010-02-11 07:41:25 +0000558 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
559 clang_disposeIndex(Idx);
560 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000561}
562
Ted Kremenekce2ae882010-01-26 17:59:48 +0000563int perform_test_load_source(int argc, const char **argv,
564 const char *filter, CXCursorVisitor Visitor,
565 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000566 const char *UseExternalASTs =
567 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000568 CXIndex Idx;
569 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000570 struct CXUnsavedFile *unsaved_files = 0;
571 int num_unsaved_files = 0;
572 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000573
Daniel Dunbarada487d2009-12-01 02:03:10 +0000574 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000575 !strcmp(filter, "local") ? 1 : 0,
576 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000577
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000578 if (UseExternalASTs && strlen(UseExternalASTs))
579 clang_setUseExternalASTGeneration(Idx, 1);
580
Ted Kremenek020a0952010-02-11 07:41:25 +0000581 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
582 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000583 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000584 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000585
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
587 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000588 argv + num_unsaved_files,
589 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000590 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000591 if (!TU) {
592 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000593 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000594 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000595 return 1;
596 }
597
Ted Kremenekce2ae882010-01-26 17:59:48 +0000598 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000599 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000600 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000601 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000602}
603
Douglas Gregorabc563f2010-07-19 21:46:24 +0000604int perform_test_reparse_source(int argc, const char **argv, int trials,
605 const char *filter, CXCursorVisitor Visitor,
606 PostVisitTU PV) {
607 const char *UseExternalASTs =
608 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
609 CXIndex Idx;
610 CXTranslationUnit TU;
611 struct CXUnsavedFile *unsaved_files = 0;
612 int num_unsaved_files = 0;
613 int result;
614 int trial;
615
616 Idx = clang_createIndex(/* excludeDeclsFromPCH */
617 !strcmp(filter, "local") ? 1 : 0,
618 /* displayDiagnosics=*/1);
619
620 if (UseExternalASTs && strlen(UseExternalASTs))
621 clang_setUseExternalASTGeneration(Idx, 1);
622
623 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
624 clang_disposeIndex(Idx);
625 return -1;
626 }
627
Douglas Gregor44c181a2010-07-23 00:33:23 +0000628 TU = clang_parseTranslationUnit(Idx, 0,
629 argv + num_unsaved_files,
630 argc - num_unsaved_files,
631 unsaved_files,
632 num_unsaved_files,
633 getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000634 if (!TU) {
635 fprintf(stderr, "Unable to load translation unit!\n");
636 free_remapped_files(unsaved_files, num_unsaved_files);
637 clang_disposeIndex(Idx);
638 return 1;
639 }
640
641 for (trial = 0; trial < trials; ++trial) {
642 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files)) {
643 clang_disposeTranslationUnit(TU);
644 free_remapped_files(unsaved_files, num_unsaved_files);
645 clang_disposeIndex(Idx);
646 return -1;
647 }
648 }
649
650 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
651 free_remapped_files(unsaved_files, num_unsaved_files);
652 clang_disposeIndex(Idx);
653 return result;
654}
655
Ted Kremenek0d435192009-11-17 18:13:31 +0000656/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000657/* Logic for testing clang_getCursor(). */
658/******************************************************************************/
659
660static void print_cursor_file_scan(CXCursor cursor,
661 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000662 unsigned end_line, unsigned end_col,
663 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000664 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000665 if (prefix)
666 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000667 PrintExtent(stdout, start_line, start_col, end_line, end_col);
668 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000669 PrintCursor(cursor);
670 printf("\n");
671}
672
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000673static int perform_file_scan(const char *ast_file, const char *source_file,
674 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000675 CXIndex Idx;
676 CXTranslationUnit TU;
677 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000678 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000679 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000680 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000681 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000682
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000683 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
684 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000685 fprintf(stderr, "Could not create Index\n");
686 return 1;
687 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000688
Ted Kremenek1c6da172009-11-17 19:37:36 +0000689 if (!CreateTranslationUnit(Idx, ast_file, &TU))
690 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000691
Ted Kremenek1c6da172009-11-17 19:37:36 +0000692 if ((fp = fopen(source_file, "r")) == NULL) {
693 fprintf(stderr, "Could not open '%s'\n", source_file);
694 return 1;
695 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000696
Douglas Gregorb9790342010-01-22 21:44:22 +0000697 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000698 for (;;) {
699 CXCursor cursor;
700 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000701
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000702 if (c == '\n') {
703 ++line;
704 col = 1;
705 } else
706 ++col;
707
708 /* Check the cursor at this position, and dump the previous one if we have
709 * found something new.
710 */
711 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
712 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
713 prevCursor.kind != CXCursor_InvalidFile) {
714 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000715 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000716 start_line = line;
717 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000718 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000719 if (c == EOF)
720 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000721
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000722 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000723 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000724
Ted Kremenek1c6da172009-11-17 19:37:36 +0000725 fclose(fp);
726 return 0;
727}
728
729/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000730/* Logic for testing clang_codeComplete(). */
731/******************************************************************************/
732
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000733/* Parse file:line:column from the input string. Returns 0 on success, non-zero
734 on failure. If successful, the pointer *filename will contain newly-allocated
735 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000736int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000737 unsigned *column, unsigned *second_line,
738 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000739 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000740 const char *last_colon = strrchr(input, ':');
741 unsigned values[4], i;
742 unsigned num_values = (second_line && second_column)? 4 : 2;
743
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000744 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000745 if (!last_colon || last_colon == input) {
746 if (num_values == 4)
747 fprintf(stderr, "could not parse filename:line:column:line:column in "
748 "'%s'\n", input);
749 else
750 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000751 return 1;
752 }
753
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000754 for (i = 0; i != num_values; ++i) {
755 const char *prev_colon;
756
757 /* Parse the next line or column. */
758 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
759 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000760 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000761 (i % 2 ? "column" : "line"), input);
762 return 1;
763 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000764
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000765 if (i + 1 == num_values)
766 break;
767
768 /* Find the previous colon. */
769 prev_colon = last_colon - 1;
770 while (prev_colon != input && *prev_colon != ':')
771 --prev_colon;
772 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000773 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000774 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000775 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000776 }
777
778 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000779 }
780
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000781 *line = values[0];
782 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000783
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000784 if (second_line && second_column) {
785 *second_line = values[2];
786 *second_column = values[3];
787 }
788
Douglas Gregor88d23952009-11-09 18:19:57 +0000789 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000790 *filename = (char*)malloc(last_colon - input + 1);
791 memcpy(*filename, input, last_colon - input);
792 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000793 return 0;
794}
795
796const char *
797clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
798 switch (Kind) {
799 case CXCompletionChunk_Optional: return "Optional";
800 case CXCompletionChunk_TypedText: return "TypedText";
801 case CXCompletionChunk_Text: return "Text";
802 case CXCompletionChunk_Placeholder: return "Placeholder";
803 case CXCompletionChunk_Informative: return "Informative";
804 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
805 case CXCompletionChunk_LeftParen: return "LeftParen";
806 case CXCompletionChunk_RightParen: return "RightParen";
807 case CXCompletionChunk_LeftBracket: return "LeftBracket";
808 case CXCompletionChunk_RightBracket: return "RightBracket";
809 case CXCompletionChunk_LeftBrace: return "LeftBrace";
810 case CXCompletionChunk_RightBrace: return "RightBrace";
811 case CXCompletionChunk_LeftAngle: return "LeftAngle";
812 case CXCompletionChunk_RightAngle: return "RightAngle";
813 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000814 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000815 case CXCompletionChunk_Colon: return "Colon";
816 case CXCompletionChunk_SemiColon: return "SemiColon";
817 case CXCompletionChunk_Equal: return "Equal";
818 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
819 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000820 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000821
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000822 return "Unknown";
823}
824
Douglas Gregor3ac73852009-11-09 16:04:45 +0000825void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000826 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000827
Douglas Gregor3ac73852009-11-09 16:04:45 +0000828 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000829 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000830 CXString text;
831 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000832 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000833 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000834
Douglas Gregor3ac73852009-11-09 16:04:45 +0000835 if (Kind == CXCompletionChunk_Optional) {
836 fprintf(file, "{Optional ");
837 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000838 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000839 file);
840 fprintf(file, "}");
841 continue;
842 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000843
Douglas Gregord5a20892009-11-09 17:05:28 +0000844 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000845 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000846 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000847 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000848 cstr ? cstr : "");
849 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000850 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000851
Douglas Gregor3ac73852009-11-09 16:04:45 +0000852}
853
854void print_completion_result(CXCompletionResult *completion_result,
855 CXClientData client_data) {
856 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000857 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
858
859 fprintf(file, "%s:", clang_getCString(ks));
860 clang_disposeString(ks);
861
Douglas Gregor3ac73852009-11-09 16:04:45 +0000862 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor12e13132010-05-26 22:00:08 +0000863 fprintf(file, " (%u)\n",
864 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000865}
866
Douglas Gregor1982c182010-07-12 18:38:41 +0000867int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000868 const char *input = argv[1];
869 char *filename = 0;
870 unsigned line;
871 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000872 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000873 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000874 struct CXUnsavedFile *unsaved_files = 0;
875 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000876 CXCodeCompleteResults *results = 0;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000877
Douglas Gregor1982c182010-07-12 18:38:41 +0000878 if (timing_only)
879 input += strlen("-code-completion-timing=");
880 else
881 input += strlen("-code-completion-at=");
882
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000884 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000885 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000886
Douglas Gregor735df882009-12-02 09:21:34 +0000887 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
888 return -1;
889
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000890 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000891 if (getenv("CINDEXTEST_EDITING")) {
892 CXTranslationUnit *TU = clang_parseTranslationUnit(CIdx, 0,
893 argv + num_unsaved_files + 2,
894 argc - num_unsaved_files - 2,
895 unsaved_files,
896 num_unsaved_files,
897 getDefaultParsingOptions());
898 results = clang_codeCompleteAt(TU, filename, line, column,
899 unsaved_files, num_unsaved_files);
900 } else
901 results = clang_codeComplete(CIdx,
902 argv[argc - 1], argc - num_unsaved_files - 3,
903 argv + num_unsaved_files + 2,
904 num_unsaved_files, unsaved_files,
905 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000906
Douglas Gregorec6762c2009-12-18 16:20:58 +0000907 if (results) {
908 unsigned i, n = results->NumResults;
Douglas Gregor1982c182010-07-12 18:38:41 +0000909 if (!timing_only)
910 for (i = 0; i != n; ++i)
911 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000912 n = clang_codeCompleteGetNumDiagnostics(results);
913 for (i = 0; i != n; ++i) {
914 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
915 PrintDiagnostic(diag);
916 clang_disposeDiagnostic(diag);
917 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000918 clang_disposeCodeCompleteResults(results);
919 }
920
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000921 clang_disposeIndex(CIdx);
922 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000923
Douglas Gregor735df882009-12-02 09:21:34 +0000924 free_remapped_files(unsaved_files, num_unsaved_files);
925
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000926 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000927}
928
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000929typedef struct {
930 char *filename;
931 unsigned line;
932 unsigned column;
933} CursorSourceLocation;
934
935int inspect_cursor_at(int argc, const char **argv) {
936 CXIndex CIdx;
937 int errorCode;
938 struct CXUnsavedFile *unsaved_files = 0;
939 int num_unsaved_files = 0;
940 CXTranslationUnit TU;
941 CXCursor Cursor;
942 CursorSourceLocation *Locations = 0;
943 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000944
Ted Kremeneke68fff62010-02-17 00:41:32 +0000945 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000946 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
947 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000948
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000949 /* Parse the locations. */
950 assert(NumLocations > 0 && "Unable to count locations?");
951 Locations = (CursorSourceLocation *)malloc(
952 NumLocations * sizeof(CursorSourceLocation));
953 for (Loc = 0; Loc < NumLocations; ++Loc) {
954 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000955 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
956 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000957 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000958 return errorCode;
959 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000960
961 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000962 &num_unsaved_files))
963 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000964
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000965 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000966 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
967 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000968 argv + num_unsaved_files + 1 + NumLocations,
969 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000970 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000971 if (!TU) {
972 fprintf(stderr, "unable to parse input\n");
973 return -1;
974 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000975
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000976 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000977 CXFile file = clang_getFile(TU, Locations[Loc].filename);
978 if (!file)
979 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000980
981 Cursor = clang_getCursor(TU,
982 clang_getLocation(TU, file, Locations[Loc].line,
983 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000984 PrintCursor(Cursor);
985 printf("\n");
986 free(Locations[Loc].filename);
987 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000988
Douglas Gregora88084b2010-02-18 18:08:43 +0000989 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000990 clang_disposeTranslationUnit(TU);
991 clang_disposeIndex(CIdx);
992 free(Locations);
993 free_remapped_files(unsaved_files, num_unsaved_files);
994 return 0;
995}
996
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000997int perform_token_annotation(int argc, const char **argv) {
998 const char *input = argv[1];
999 char *filename = 0;
1000 unsigned line, second_line;
1001 unsigned column, second_column;
1002 CXIndex CIdx;
1003 CXTranslationUnit TU = 0;
1004 int errorCode;
1005 struct CXUnsavedFile *unsaved_files = 0;
1006 int num_unsaved_files = 0;
1007 CXToken *tokens;
1008 unsigned num_tokens;
1009 CXSourceRange range;
1010 CXSourceLocation startLoc, endLoc;
1011 CXFile file = 0;
1012 CXCursor *cursors = 0;
1013 unsigned i;
1014
1015 input += strlen("-test-annotate-tokens=");
1016 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1017 &second_line, &second_column)))
1018 return errorCode;
1019
1020 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1021 return -1;
1022
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001023 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001024 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1025 argc - num_unsaved_files - 3,
1026 argv + num_unsaved_files + 2,
1027 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001028 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001029 if (!TU) {
1030 fprintf(stderr, "unable to parse input\n");
1031 clang_disposeIndex(CIdx);
1032 free(filename);
1033 free_remapped_files(unsaved_files, num_unsaved_files);
1034 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001035 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001036 errorCode = 0;
1037
1038 file = clang_getFile(TU, filename);
1039 if (!file) {
1040 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1041 errorCode = -1;
1042 goto teardown;
1043 }
1044
1045 startLoc = clang_getLocation(TU, file, line, column);
1046 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001047 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001048 column);
1049 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001050 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001051 }
1052
1053 endLoc = clang_getLocation(TU, file, second_line, second_column);
1054 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001055 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001056 second_line, second_column);
1057 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001058 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001059 }
1060
1061 range = clang_getRange(startLoc, endLoc);
1062 clang_tokenize(TU, range, &tokens, &num_tokens);
1063 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1064 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1065 for (i = 0; i != num_tokens; ++i) {
1066 const char *kind = "<unknown>";
1067 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1068 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1069 unsigned start_line, start_column, end_line, end_column;
1070
1071 switch (clang_getTokenKind(tokens[i])) {
1072 case CXToken_Punctuation: kind = "Punctuation"; break;
1073 case CXToken_Keyword: kind = "Keyword"; break;
1074 case CXToken_Identifier: kind = "Identifier"; break;
1075 case CXToken_Literal: kind = "Literal"; break;
1076 case CXToken_Comment: kind = "Comment"; break;
1077 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001078 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001079 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001080 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001081 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001082 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1083 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001084 if (!clang_isInvalid(cursors[i].kind)) {
1085 printf(" ");
1086 PrintCursor(cursors[i]);
1087 }
1088 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001089 }
1090 free(cursors);
1091
1092 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001093 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001094 clang_disposeTranslationUnit(TU);
1095 clang_disposeIndex(CIdx);
1096 free(filename);
1097 free_remapped_files(unsaved_files, num_unsaved_files);
1098 return errorCode;
1099}
1100
Ted Kremenek0d435192009-11-17 18:13:31 +00001101/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001102/* USR printing. */
1103/******************************************************************************/
1104
1105static int insufficient_usr(const char *kind, const char *usage) {
1106 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1107 return 1;
1108}
1109
1110static unsigned isUSR(const char *s) {
1111 return s[0] == 'c' && s[1] == ':';
1112}
1113
1114static int not_usr(const char *s, const char *arg) {
1115 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1116 return 1;
1117}
1118
1119static void print_usr(CXString usr) {
1120 const char *s = clang_getCString(usr);
1121 printf("%s\n", s);
1122 clang_disposeString(usr);
1123}
1124
1125static void display_usrs() {
1126 fprintf(stderr, "-print-usrs options:\n"
1127 " ObjCCategory <class name> <category name>\n"
1128 " ObjCClass <class name>\n"
1129 " ObjCIvar <ivar name> <class USR>\n"
1130 " ObjCMethod <selector> [0=class method|1=instance method] "
1131 "<class USR>\n"
1132 " ObjCProperty <property name> <class USR>\n"
1133 " ObjCProtocol <protocol name>\n");
1134}
1135
1136int print_usrs(const char **I, const char **E) {
1137 while (I != E) {
1138 const char *kind = *I;
1139 unsigned len = strlen(kind);
1140 switch (len) {
1141 case 8:
1142 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1143 if (I + 2 >= E)
1144 return insufficient_usr(kind, "<ivar name> <class USR>");
1145 if (!isUSR(I[2]))
1146 return not_usr("<class USR>", I[2]);
1147 else {
1148 CXString x;
1149 x.Spelling = I[2];
1150 x.MustFreeString = 0;
1151 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1152 }
1153
1154 I += 3;
1155 continue;
1156 }
1157 break;
1158 case 9:
1159 if (memcmp(kind, "ObjCClass", 9) == 0) {
1160 if (I + 1 >= E)
1161 return insufficient_usr(kind, "<class name>");
1162 print_usr(clang_constructUSR_ObjCClass(I[1]));
1163 I += 2;
1164 continue;
1165 }
1166 break;
1167 case 10:
1168 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1169 if (I + 3 >= E)
1170 return insufficient_usr(kind, "<method selector> "
1171 "[0=class method|1=instance method] <class USR>");
1172 if (!isUSR(I[3]))
1173 return not_usr("<class USR>", I[3]);
1174 else {
1175 CXString x;
1176 x.Spelling = I[3];
1177 x.MustFreeString = 0;
1178 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1179 }
1180 I += 4;
1181 continue;
1182 }
1183 break;
1184 case 12:
1185 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1186 if (I + 2 >= E)
1187 return insufficient_usr(kind, "<class name> <category name>");
1188 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1189 I += 3;
1190 continue;
1191 }
1192 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1193 if (I + 1 >= E)
1194 return insufficient_usr(kind, "<protocol name>");
1195 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1196 I += 2;
1197 continue;
1198 }
1199 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1200 if (I + 2 >= E)
1201 return insufficient_usr(kind, "<property name> <class USR>");
1202 if (!isUSR(I[2]))
1203 return not_usr("<class USR>", I[2]);
1204 else {
1205 CXString x;
1206 x.Spelling = I[2];
1207 x.MustFreeString = 0;
1208 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1209 }
1210 I += 3;
1211 continue;
1212 }
1213 break;
1214 default:
1215 break;
1216 }
1217 break;
1218 }
1219
1220 if (I != E) {
1221 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1222 display_usrs();
1223 return 1;
1224 }
1225 return 0;
1226}
1227
1228int print_usrs_file(const char *file_name) {
1229 char line[2048];
1230 const char *args[128];
1231 unsigned numChars = 0;
1232
1233 FILE *fp = fopen(file_name, "r");
1234 if (!fp) {
1235 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1236 return 1;
1237 }
1238
1239 /* This code is not really all that safe, but it works fine for testing. */
1240 while (!feof(fp)) {
1241 char c = fgetc(fp);
1242 if (c == '\n') {
1243 unsigned i = 0;
1244 const char *s = 0;
1245
1246 if (numChars == 0)
1247 continue;
1248
1249 line[numChars] = '\0';
1250 numChars = 0;
1251
1252 if (line[0] == '/' && line[1] == '/')
1253 continue;
1254
1255 s = strtok(line, " ");
1256 while (s) {
1257 args[i] = s;
1258 ++i;
1259 s = strtok(0, " ");
1260 }
1261 if (print_usrs(&args[0], &args[i]))
1262 return 1;
1263 }
1264 else
1265 line[numChars++] = c;
1266 }
1267
1268 fclose(fp);
1269 return 0;
1270}
1271
1272/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001273/* Command line processing. */
1274/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001275
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001276static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001277 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001278 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001279 if (strcmp(s, "-usrs") == 0)
1280 return USRVisitor;
1281 return NULL;
1282}
1283
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001284static void print_usage(void) {
1285 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001286 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001287 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001288 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001289 " c-index-test -test-file-scan <AST file> <source file> "
1290 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001291 " c-index-test -test-load-tu <AST file> <symbol filter> "
1292 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001293 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1294 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001295 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001296 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001297 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1298 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001299 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001300 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1301 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001302 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001303 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001304 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001305 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1306 fprintf(stderr,
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001307 " c-index-test -print-usr-file <file>\n\n");
1308 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001309 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001310 " all - load all symbols, including those from PCH\n"
1311 " local - load all symbols except those in PCH\n"
1312 " category - only load ObjC categories (non-PCH)\n"
1313 " interface - only load ObjC interfaces (non-PCH)\n"
1314 " protocol - only load ObjC protocols (non-PCH)\n"
1315 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001316 " typedef - only load typdefs (non-PCH)\n"
1317 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001318}
1319
1320int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001321 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001322 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001323 return perform_code_completion(argc, argv, 0);
1324 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1325 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001326 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1327 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001328 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001329 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001330 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001331 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1332 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001333 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001334 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1335 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1336 if (I) {
1337 int trials = atoi(argv[2]);
1338 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1339 NULL);
1340 }
1341 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001342 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001343 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001344 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001345 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001346 }
1347 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001348 return perform_file_scan(argv[2], argv[3],
1349 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001350 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1351 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001352 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1353 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1354 PrintInclusionStack);
1355 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1356 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1357 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001358 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1359 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1360 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001361 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1362 return perform_test_load_source(argc - 2, argv + 2, "all",
1363 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001364 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1365 if (argc > 2)
1366 return print_usrs(argv + 2, argv + argc);
1367 else {
1368 display_usrs();
1369 return 1;
1370 }
1371 }
1372 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1373 return print_usrs_file(argv[2]);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001374
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001375 print_usage();
1376 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001377}