blob: 3f702904a3dd87071ee66a6e283a0e51e3557c8e [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)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000185
186 switch (clang_getCursorAvailability(Cursor)) {
187 case CXAvailability_Available:
188 break;
189
190 case CXAvailability_Deprecated:
191 printf(" (deprecated)");
192 break;
193
194 case CXAvailability_NotAvailable:
195 printf(" (unavailable)");
196 break;
197 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000198
199 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
200 CXType T =
201 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
202 CXString S = clang_getTypeKindSpelling(T.kind);
203 printf(" [IBOutletCollection=%s]", clang_getCString(S));
204 clang_disposeString(S);
205 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000206 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000207}
Steve Naroff89922f82009-08-31 00:59:03 +0000208
Ted Kremeneke68fff62010-02-17 00:41:32 +0000209static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000210 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000211 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000212 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000213 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000214 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000215 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000216 clang_disposeString(source);
217 return "<invalid loc>";
218 }
219 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000220 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000221 clang_disposeString(source);
222 return b;
223 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000224}
225
Ted Kremenek0d435192009-11-17 18:13:31 +0000226/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000227/* Callbacks. */
228/******************************************************************************/
229
230typedef void (*PostVisitTU)(CXTranslationUnit);
231
Douglas Gregora88084b2010-02-18 18:08:43 +0000232void PrintDiagnostic(CXDiagnostic Diagnostic) {
233 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000234 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000235 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000236 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
237 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
238 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000239
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000240 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000241 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000242
Douglas Gregor274f1902010-02-22 23:17:23 +0000243 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
244 fprintf(stderr, "%s\n", clang_getCString(Msg));
245 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000246
Douglas Gregor5352ac02010-01-28 00:27:43 +0000247 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000248 &file, 0, 0, 0);
249 if (!file)
250 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000251
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000252 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
253 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000254 CXSourceRange range;
255 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
256 CXSourceLocation start = clang_getRangeStart(range);
257 CXSourceLocation end = clang_getRangeEnd(range);
258 unsigned start_line, start_column, end_line, end_column;
259 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000260 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000261 &start_column, 0);
262 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
263 if (clang_equalLocations(start, end)) {
264 /* Insertion. */
265 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000266 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000267 clang_getCString(insertion_text), start_line, start_column);
268 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
269 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000270 if (start_file == file && end_file == file) {
271 fprintf(out, "FIX-IT: Remove ");
272 PrintExtent(out, start_line, start_column, end_line, end_column);
273 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000274 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000275 } else {
276 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000277 if (start_file == end_file) {
278 fprintf(out, "FIX-IT: Replace ");
279 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000280 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000281 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000282 break;
283 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000284 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000285 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000286}
287
Douglas Gregora88084b2010-02-18 18:08:43 +0000288void PrintDiagnostics(CXTranslationUnit TU) {
289 int i, n = clang_getNumDiagnostics(TU);
290 for (i = 0; i != n; ++i) {
291 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
292 PrintDiagnostic(Diag);
293 clang_disposeDiagnostic(Diag);
294 }
295}
296
Ted Kremenekce2ae882010-01-26 17:59:48 +0000297/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000298/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000299/******************************************************************************/
300
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000301static const char *FileCheckPrefix = "CHECK";
302
Douglas Gregora7bde202010-01-19 00:34:46 +0000303static void PrintCursorExtent(CXCursor C) {
304 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000305 CXFile begin_file, end_file;
306 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000307
Douglas Gregor1db19de2010-01-19 21:36:55 +0000308 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000309 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000310 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000311 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000312 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000313 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000314
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000315 printf(" Extent=");
316 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000317}
318
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000319/* Data used by all of the visitors. */
320typedef struct {
321 CXTranslationUnit TU;
322 enum CXCursorKind *Filter;
323} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000324
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000325
Ted Kremeneke68fff62010-02-17 00:41:32 +0000326enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000327 CXCursor Parent,
328 CXClientData ClientData) {
329 VisitorData *Data = (VisitorData *)ClientData;
330 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000331 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000332 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000333 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000334 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000335 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000336 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000337 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000338 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000339 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000340 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000341
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000342 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000343}
Steve Naroff50398192009-08-28 15:28:48 +0000344
Ted Kremeneke68fff62010-02-17 00:41:32 +0000345static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000346 CXCursor Parent,
347 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000348 const char *startBuf, *endBuf;
349 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
350 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000351 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000352
Douglas Gregorb6998662010-01-19 19:34:47 +0000353 if (Cursor.kind != CXCursor_FunctionDecl ||
354 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000355 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000356
357 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
358 &startLine, &startColumn,
359 &endLine, &endColumn);
360 /* Probe the entire body, looking for both decls and refs. */
361 curLine = startLine;
362 curColumn = startColumn;
363
364 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000365 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000366 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000367 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000368
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000369 if (*startBuf == '\n') {
370 startBuf++;
371 curLine++;
372 curColumn = 1;
373 } else if (*startBuf != '\t')
374 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000375
Douglas Gregor98258af2010-01-18 22:46:11 +0000376 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000377 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000378
Douglas Gregor1db19de2010-01-19 21:36:55 +0000379 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000380 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000381 CXSourceLocation RefLoc
382 = clang_getLocation(Data->TU, file, curLine, curColumn);
383 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000384 if (Ref.kind == CXCursor_NoDeclFound) {
385 /* Nothing found here; that's fine. */
386 } else if (Ref.kind != CXCursor_FunctionDecl) {
387 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
388 curLine, curColumn);
389 PrintCursor(Ref);
390 printf("\n");
391 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000392 }
Ted Kremenek74844072010-02-17 00:41:20 +0000393 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000394 startBuf++;
395 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000396
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000397 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000398}
399
Ted Kremenek7d405622010-01-12 23:34:26 +0000400/******************************************************************************/
401/* USR testing. */
402/******************************************************************************/
403
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000404enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
405 CXClientData ClientData) {
406 VisitorData *Data = (VisitorData *)ClientData;
407 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000408 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000409 const char *cstr = clang_getCString(USR);
410 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000411 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000412 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000413 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000414 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
415
Douglas Gregora7bde202010-01-19 00:34:46 +0000416 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000417 printf("\n");
418 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000419
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000420 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000421 }
422
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000423 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000424}
425
426/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000427/* Inclusion stack testing. */
428/******************************************************************************/
429
430void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
431 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000432
Ted Kremenek16b55a72010-01-26 19:31:51 +0000433 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000434 CXString fname;
435
436 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000437 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000438 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000439
Ted Kremenek16b55a72010-01-26 19:31:51 +0000440 for (i = 0; i < includeStackLen; ++i) {
441 CXFile includingFile;
442 unsigned line, column;
443 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
444 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000445 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000446 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000447 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000448 }
449 printf("\n");
450}
451
452void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000453 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000454}
455
456/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000457/* Linkage testing. */
458/******************************************************************************/
459
460static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
461 CXClientData d) {
462 const char *linkage = 0;
463
464 if (clang_isInvalid(clang_getCursorKind(cursor)))
465 return CXChildVisit_Recurse;
466
467 switch (clang_getCursorLinkage(cursor)) {
468 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000469 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
470 case CXLinkage_Internal: linkage = "Internal"; break;
471 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
472 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000473 }
474
475 if (linkage) {
476 PrintCursor(cursor);
477 printf("linkage=%s\n", linkage);
478 }
479
480 return CXChildVisit_Recurse;
481}
482
483/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000484/* Typekind testing. */
485/******************************************************************************/
486
487static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
488 CXClientData d) {
489
490 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
491 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000492 CXString S = clang_getTypeKindSpelling(T.kind);
493 PrintCursor(cursor);
494 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000495 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000496 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000497 {
498 CXType CT = clang_getCanonicalType(T);
499 if (!clang_equalTypes(T, CT)) {
500 CXString CS = clang_getTypeKindSpelling(CT.kind);
501 printf(" [canonical=%s]", clang_getCString(CS));
502 clang_disposeString(CS);
503 }
504 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000505 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000506 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000507 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000508 if (RT.kind != CXType_Invalid) {
509 CXString RS = clang_getTypeKindSpelling(RT.kind);
510 printf(" [result=%s]", clang_getCString(RS));
511 clang_disposeString(RS);
512 }
513 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000514 /* Print if this is a non-POD type. */
515 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000516
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000517 printf("\n");
518 }
519 return CXChildVisit_Recurse;
520}
521
522
523/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000524/* Loading ASTs/source. */
525/******************************************************************************/
526
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000527static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000528 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000529 CXCursorVisitor Visitor,
530 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000531
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000532 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000533 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000534
535 if (Visitor) {
536 enum CXCursorKind K = CXCursor_NotImplemented;
537 enum CXCursorKind *ck = &K;
538 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000539
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000540 /* Perform some simple filtering. */
541 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000542 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000543 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
544 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
545 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
546 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
547 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
548 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
549 else {
550 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
551 return 1;
552 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000553
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000554 Data.TU = TU;
555 Data.Filter = ck;
556 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000557 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000558
Ted Kremenekce2ae882010-01-26 17:59:48 +0000559 if (PV)
560 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000561
Douglas Gregora88084b2010-02-18 18:08:43 +0000562 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000563 clang_disposeTranslationUnit(TU);
564 return 0;
565}
566
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000567int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000568 const char *prefix, CXCursorVisitor Visitor,
569 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000570 CXIndex Idx;
571 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000572 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000573 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000574 !strcmp(filter, "local") ? 1 : 0,
575 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000576
Ted Kremenek020a0952010-02-11 07:41:25 +0000577 if (!CreateTranslationUnit(Idx, file, &TU)) {
578 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000579 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000580 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000581
Ted Kremenek020a0952010-02-11 07:41:25 +0000582 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
583 clang_disposeIndex(Idx);
584 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000585}
586
Ted Kremenekce2ae882010-01-26 17:59:48 +0000587int perform_test_load_source(int argc, const char **argv,
588 const char *filter, CXCursorVisitor Visitor,
589 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000590 const char *UseExternalASTs =
591 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000592 CXIndex Idx;
593 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000594 struct CXUnsavedFile *unsaved_files = 0;
595 int num_unsaved_files = 0;
596 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000597
Daniel Dunbarada487d2009-12-01 02:03:10 +0000598 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000599 !strcmp(filter, "local") ? 1 : 0,
600 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000601
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000602 if (UseExternalASTs && strlen(UseExternalASTs))
603 clang_setUseExternalASTGeneration(Idx, 1);
604
Ted Kremenek020a0952010-02-11 07:41:25 +0000605 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
606 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000607 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000608 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000609
Ted Kremeneke68fff62010-02-17 00:41:32 +0000610 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
611 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000612 argv + num_unsaved_files,
613 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000614 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000615 if (!TU) {
616 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000617 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000618 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000619 return 1;
620 }
621
Ted Kremenekce2ae882010-01-26 17:59:48 +0000622 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000623 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000624 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000625 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000626}
627
Douglas Gregorabc563f2010-07-19 21:46:24 +0000628int perform_test_reparse_source(int argc, const char **argv, int trials,
629 const char *filter, CXCursorVisitor Visitor,
630 PostVisitTU PV) {
631 const char *UseExternalASTs =
632 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
633 CXIndex Idx;
634 CXTranslationUnit TU;
635 struct CXUnsavedFile *unsaved_files = 0;
636 int num_unsaved_files = 0;
637 int result;
638 int trial;
639
640 Idx = clang_createIndex(/* excludeDeclsFromPCH */
641 !strcmp(filter, "local") ? 1 : 0,
642 /* displayDiagnosics=*/1);
643
644 if (UseExternalASTs && strlen(UseExternalASTs))
645 clang_setUseExternalASTGeneration(Idx, 1);
646
647 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
648 clang_disposeIndex(Idx);
649 return -1;
650 }
651
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000652 /* Load the initial translation unit -- we do this without honoring remapped
653 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000654 TU = clang_parseTranslationUnit(Idx, 0,
655 argv + num_unsaved_files,
656 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000657 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000658 if (!TU) {
659 fprintf(stderr, "Unable to load translation unit!\n");
660 free_remapped_files(unsaved_files, num_unsaved_files);
661 clang_disposeIndex(Idx);
662 return 1;
663 }
664
665 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000666 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
667 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000668 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000669 clang_disposeTranslationUnit(TU);
670 free_remapped_files(unsaved_files, num_unsaved_files);
671 clang_disposeIndex(Idx);
672 return -1;
673 }
674 }
675
676 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
677 free_remapped_files(unsaved_files, num_unsaved_files);
678 clang_disposeIndex(Idx);
679 return result;
680}
681
Ted Kremenek0d435192009-11-17 18:13:31 +0000682/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000683/* Logic for testing clang_getCursor(). */
684/******************************************************************************/
685
686static void print_cursor_file_scan(CXCursor cursor,
687 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000688 unsigned end_line, unsigned end_col,
689 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000690 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000691 if (prefix)
692 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000693 PrintExtent(stdout, start_line, start_col, end_line, end_col);
694 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000695 PrintCursor(cursor);
696 printf("\n");
697}
698
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000699static int perform_file_scan(const char *ast_file, const char *source_file,
700 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000701 CXIndex Idx;
702 CXTranslationUnit TU;
703 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000704 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000705 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000706 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000707 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000708
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000709 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
710 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000711 fprintf(stderr, "Could not create Index\n");
712 return 1;
713 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000714
Ted Kremenek1c6da172009-11-17 19:37:36 +0000715 if (!CreateTranslationUnit(Idx, ast_file, &TU))
716 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000717
Ted Kremenek1c6da172009-11-17 19:37:36 +0000718 if ((fp = fopen(source_file, "r")) == NULL) {
719 fprintf(stderr, "Could not open '%s'\n", source_file);
720 return 1;
721 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000722
Douglas Gregorb9790342010-01-22 21:44:22 +0000723 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000724 for (;;) {
725 CXCursor cursor;
726 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000727
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000728 if (c == '\n') {
729 ++line;
730 col = 1;
731 } else
732 ++col;
733
734 /* Check the cursor at this position, and dump the previous one if we have
735 * found something new.
736 */
737 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
738 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
739 prevCursor.kind != CXCursor_InvalidFile) {
740 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000741 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000742 start_line = line;
743 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000744 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000745 if (c == EOF)
746 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000747
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000748 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000749 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000750
Ted Kremenek1c6da172009-11-17 19:37:36 +0000751 fclose(fp);
752 return 0;
753}
754
755/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000756/* Logic for testing clang_codeComplete(). */
757/******************************************************************************/
758
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000759/* Parse file:line:column from the input string. Returns 0 on success, non-zero
760 on failure. If successful, the pointer *filename will contain newly-allocated
761 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000762int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000763 unsigned *column, unsigned *second_line,
764 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000765 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000766 const char *last_colon = strrchr(input, ':');
767 unsigned values[4], i;
768 unsigned num_values = (second_line && second_column)? 4 : 2;
769
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000770 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000771 if (!last_colon || last_colon == input) {
772 if (num_values == 4)
773 fprintf(stderr, "could not parse filename:line:column:line:column in "
774 "'%s'\n", input);
775 else
776 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000777 return 1;
778 }
779
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000780 for (i = 0; i != num_values; ++i) {
781 const char *prev_colon;
782
783 /* Parse the next line or column. */
784 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
785 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000786 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000787 (i % 2 ? "column" : "line"), input);
788 return 1;
789 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000790
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000791 if (i + 1 == num_values)
792 break;
793
794 /* Find the previous colon. */
795 prev_colon = last_colon - 1;
796 while (prev_colon != input && *prev_colon != ':')
797 --prev_colon;
798 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000799 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000800 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000801 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000802 }
803
804 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000805 }
806
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000807 *line = values[0];
808 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000809
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000810 if (second_line && second_column) {
811 *second_line = values[2];
812 *second_column = values[3];
813 }
814
Douglas Gregor88d23952009-11-09 18:19:57 +0000815 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000816 *filename = (char*)malloc(last_colon - input + 1);
817 memcpy(*filename, input, last_colon - input);
818 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000819 return 0;
820}
821
822const char *
823clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
824 switch (Kind) {
825 case CXCompletionChunk_Optional: return "Optional";
826 case CXCompletionChunk_TypedText: return "TypedText";
827 case CXCompletionChunk_Text: return "Text";
828 case CXCompletionChunk_Placeholder: return "Placeholder";
829 case CXCompletionChunk_Informative: return "Informative";
830 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
831 case CXCompletionChunk_LeftParen: return "LeftParen";
832 case CXCompletionChunk_RightParen: return "RightParen";
833 case CXCompletionChunk_LeftBracket: return "LeftBracket";
834 case CXCompletionChunk_RightBracket: return "RightBracket";
835 case CXCompletionChunk_LeftBrace: return "LeftBrace";
836 case CXCompletionChunk_RightBrace: return "RightBrace";
837 case CXCompletionChunk_LeftAngle: return "LeftAngle";
838 case CXCompletionChunk_RightAngle: return "RightAngle";
839 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000840 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000841 case CXCompletionChunk_Colon: return "Colon";
842 case CXCompletionChunk_SemiColon: return "SemiColon";
843 case CXCompletionChunk_Equal: return "Equal";
844 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
845 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000846 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000847
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000848 return "Unknown";
849}
850
Douglas Gregor3ac73852009-11-09 16:04:45 +0000851void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000852 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000853
Douglas Gregor3ac73852009-11-09 16:04:45 +0000854 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000855 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000856 CXString text;
857 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000858 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000859 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000860
Douglas Gregor3ac73852009-11-09 16:04:45 +0000861 if (Kind == CXCompletionChunk_Optional) {
862 fprintf(file, "{Optional ");
863 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000864 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000865 file);
866 fprintf(file, "}");
867 continue;
868 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000869
Douglas Gregord5a20892009-11-09 17:05:28 +0000870 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000871 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000872 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000873 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000874 cstr ? cstr : "");
875 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000876 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000877
Douglas Gregor3ac73852009-11-09 16:04:45 +0000878}
879
880void print_completion_result(CXCompletionResult *completion_result,
881 CXClientData client_data) {
882 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000883 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
884
885 fprintf(file, "%s:", clang_getCString(ks));
886 clang_disposeString(ks);
887
Douglas Gregor3ac73852009-11-09 16:04:45 +0000888 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000889 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000890 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000891 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
892 case CXAvailability_Available:
893 break;
894
895 case CXAvailability_Deprecated:
896 fprintf(file, " (deprecated)");
897 break;
898
899 case CXAvailability_NotAvailable:
900 fprintf(file, " (unavailable)");
901 break;
902 }
903 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000904}
905
Douglas Gregor1982c182010-07-12 18:38:41 +0000906int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000907 const char *input = argv[1];
908 char *filename = 0;
909 unsigned line;
910 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000911 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000912 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000913 struct CXUnsavedFile *unsaved_files = 0;
914 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000915 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000916 CXTranslationUnit *TU = 0;
917
Douglas Gregor1982c182010-07-12 18:38:41 +0000918 if (timing_only)
919 input += strlen("-code-completion-timing=");
920 else
921 input += strlen("-code-completion-at=");
922
Ted Kremeneke68fff62010-02-17 00:41:32 +0000923 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000924 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000925 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000926
Douglas Gregor735df882009-12-02 09:21:34 +0000927 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
928 return -1;
929
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000930 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000931 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000932 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +0000933 TU = clang_parseTranslationUnit(CIdx, 0,
934 argv + num_unsaved_files + 2,
935 argc - num_unsaved_files - 2,
Daniel Dunbar43f331d2010-08-19 23:44:04 +0000936 0, 0, getDefaultParsingOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000937 if (!TU) {
938 fprintf(stderr, "Unable to load translation unit!\n");
939 return 1;
940 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000941 for (I = 0; I != Repeats; ++I) {
942 results = clang_codeCompleteAt(TU, filename, line, column,
943 unsaved_files, num_unsaved_files,
944 clang_defaultCodeCompleteOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000945 if (!results) {
946 fprintf(stderr, "Unable to perform code completion!\n");
947 return 1;
948 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000949 if (I != Repeats-1)
950 clang_disposeCodeCompleteResults(results);
951 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000952 } else
953 results = clang_codeComplete(CIdx,
954 argv[argc - 1], argc - num_unsaved_files - 3,
955 argv + num_unsaved_files + 2,
956 num_unsaved_files, unsaved_files,
957 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000958
Douglas Gregorec6762c2009-12-18 16:20:58 +0000959 if (results) {
960 unsigned i, n = results->NumResults;
Daniel Dunbar1cb237f2010-08-26 03:53:50 +0000961 if (!timing_only)
Douglas Gregor1982c182010-07-12 18:38:41 +0000962 for (i = 0; i != n; ++i)
963 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000964 n = clang_codeCompleteGetNumDiagnostics(results);
965 for (i = 0; i != n; ++i) {
966 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
967 PrintDiagnostic(diag);
968 clang_disposeDiagnostic(diag);
969 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000970 clang_disposeCodeCompleteResults(results);
971 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000972 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000973 clang_disposeIndex(CIdx);
974 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000975
Douglas Gregor735df882009-12-02 09:21:34 +0000976 free_remapped_files(unsaved_files, num_unsaved_files);
977
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000978 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000979}
980
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000981typedef struct {
982 char *filename;
983 unsigned line;
984 unsigned column;
985} CursorSourceLocation;
986
987int inspect_cursor_at(int argc, const char **argv) {
988 CXIndex CIdx;
989 int errorCode;
990 struct CXUnsavedFile *unsaved_files = 0;
991 int num_unsaved_files = 0;
992 CXTranslationUnit TU;
993 CXCursor Cursor;
994 CursorSourceLocation *Locations = 0;
995 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000996
Ted Kremeneke68fff62010-02-17 00:41:32 +0000997 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000998 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
999 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001000
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001001 /* Parse the locations. */
1002 assert(NumLocations > 0 && "Unable to count locations?");
1003 Locations = (CursorSourceLocation *)malloc(
1004 NumLocations * sizeof(CursorSourceLocation));
1005 for (Loc = 0; Loc < NumLocations; ++Loc) {
1006 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001007 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1008 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001009 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001010 return errorCode;
1011 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001012
1013 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001014 &num_unsaved_files))
1015 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001016
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001017 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001018 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1019 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001020 argv + num_unsaved_files + 1 + NumLocations,
1021 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001022 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001023 if (!TU) {
1024 fprintf(stderr, "unable to parse input\n");
1025 return -1;
1026 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001027
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001028 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001029 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1030 if (!file)
1031 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001032
1033 Cursor = clang_getCursor(TU,
1034 clang_getLocation(TU, file, Locations[Loc].line,
1035 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001036 PrintCursor(Cursor);
1037 printf("\n");
1038 free(Locations[Loc].filename);
1039 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001040
Douglas Gregora88084b2010-02-18 18:08:43 +00001041 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001042 clang_disposeTranslationUnit(TU);
1043 clang_disposeIndex(CIdx);
1044 free(Locations);
1045 free_remapped_files(unsaved_files, num_unsaved_files);
1046 return 0;
1047}
1048
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001049int perform_token_annotation(int argc, const char **argv) {
1050 const char *input = argv[1];
1051 char *filename = 0;
1052 unsigned line, second_line;
1053 unsigned column, second_column;
1054 CXIndex CIdx;
1055 CXTranslationUnit TU = 0;
1056 int errorCode;
1057 struct CXUnsavedFile *unsaved_files = 0;
1058 int num_unsaved_files = 0;
1059 CXToken *tokens;
1060 unsigned num_tokens;
1061 CXSourceRange range;
1062 CXSourceLocation startLoc, endLoc;
1063 CXFile file = 0;
1064 CXCursor *cursors = 0;
1065 unsigned i;
1066
1067 input += strlen("-test-annotate-tokens=");
1068 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1069 &second_line, &second_column)))
1070 return errorCode;
1071
1072 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1073 return -1;
1074
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001075 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001076 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1077 argc - num_unsaved_files - 3,
1078 argv + num_unsaved_files + 2,
1079 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001080 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001081 if (!TU) {
1082 fprintf(stderr, "unable to parse input\n");
1083 clang_disposeIndex(CIdx);
1084 free(filename);
1085 free_remapped_files(unsaved_files, num_unsaved_files);
1086 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001087 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001088 errorCode = 0;
1089
1090 file = clang_getFile(TU, filename);
1091 if (!file) {
1092 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1093 errorCode = -1;
1094 goto teardown;
1095 }
1096
1097 startLoc = clang_getLocation(TU, file, line, column);
1098 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001099 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001100 column);
1101 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001102 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001103 }
1104
1105 endLoc = clang_getLocation(TU, file, second_line, second_column);
1106 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001107 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001108 second_line, second_column);
1109 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001110 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001111 }
1112
1113 range = clang_getRange(startLoc, endLoc);
1114 clang_tokenize(TU, range, &tokens, &num_tokens);
1115 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1116 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1117 for (i = 0; i != num_tokens; ++i) {
1118 const char *kind = "<unknown>";
1119 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1120 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1121 unsigned start_line, start_column, end_line, end_column;
1122
1123 switch (clang_getTokenKind(tokens[i])) {
1124 case CXToken_Punctuation: kind = "Punctuation"; break;
1125 case CXToken_Keyword: kind = "Keyword"; break;
1126 case CXToken_Identifier: kind = "Identifier"; break;
1127 case CXToken_Literal: kind = "Literal"; break;
1128 case CXToken_Comment: kind = "Comment"; break;
1129 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001130 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001131 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001132 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001133 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001134 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1135 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001136 if (!clang_isInvalid(cursors[i].kind)) {
1137 printf(" ");
1138 PrintCursor(cursors[i]);
1139 }
1140 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001141 }
1142 free(cursors);
1143
1144 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001145 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001146 clang_disposeTranslationUnit(TU);
1147 clang_disposeIndex(CIdx);
1148 free(filename);
1149 free_remapped_files(unsaved_files, num_unsaved_files);
1150 return errorCode;
1151}
1152
Ted Kremenek0d435192009-11-17 18:13:31 +00001153/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001154/* USR printing. */
1155/******************************************************************************/
1156
1157static int insufficient_usr(const char *kind, const char *usage) {
1158 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1159 return 1;
1160}
1161
1162static unsigned isUSR(const char *s) {
1163 return s[0] == 'c' && s[1] == ':';
1164}
1165
1166static int not_usr(const char *s, const char *arg) {
1167 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1168 return 1;
1169}
1170
1171static void print_usr(CXString usr) {
1172 const char *s = clang_getCString(usr);
1173 printf("%s\n", s);
1174 clang_disposeString(usr);
1175}
1176
1177static void display_usrs() {
1178 fprintf(stderr, "-print-usrs options:\n"
1179 " ObjCCategory <class name> <category name>\n"
1180 " ObjCClass <class name>\n"
1181 " ObjCIvar <ivar name> <class USR>\n"
1182 " ObjCMethod <selector> [0=class method|1=instance method] "
1183 "<class USR>\n"
1184 " ObjCProperty <property name> <class USR>\n"
1185 " ObjCProtocol <protocol name>\n");
1186}
1187
1188int print_usrs(const char **I, const char **E) {
1189 while (I != E) {
1190 const char *kind = *I;
1191 unsigned len = strlen(kind);
1192 switch (len) {
1193 case 8:
1194 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1195 if (I + 2 >= E)
1196 return insufficient_usr(kind, "<ivar name> <class USR>");
1197 if (!isUSR(I[2]))
1198 return not_usr("<class USR>", I[2]);
1199 else {
1200 CXString x;
1201 x.Spelling = I[2];
1202 x.MustFreeString = 0;
1203 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1204 }
1205
1206 I += 3;
1207 continue;
1208 }
1209 break;
1210 case 9:
1211 if (memcmp(kind, "ObjCClass", 9) == 0) {
1212 if (I + 1 >= E)
1213 return insufficient_usr(kind, "<class name>");
1214 print_usr(clang_constructUSR_ObjCClass(I[1]));
1215 I += 2;
1216 continue;
1217 }
1218 break;
1219 case 10:
1220 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1221 if (I + 3 >= E)
1222 return insufficient_usr(kind, "<method selector> "
1223 "[0=class method|1=instance method] <class USR>");
1224 if (!isUSR(I[3]))
1225 return not_usr("<class USR>", I[3]);
1226 else {
1227 CXString x;
1228 x.Spelling = I[3];
1229 x.MustFreeString = 0;
1230 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1231 }
1232 I += 4;
1233 continue;
1234 }
1235 break;
1236 case 12:
1237 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1238 if (I + 2 >= E)
1239 return insufficient_usr(kind, "<class name> <category name>");
1240 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1241 I += 3;
1242 continue;
1243 }
1244 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1245 if (I + 1 >= E)
1246 return insufficient_usr(kind, "<protocol name>");
1247 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1248 I += 2;
1249 continue;
1250 }
1251 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1252 if (I + 2 >= E)
1253 return insufficient_usr(kind, "<property name> <class USR>");
1254 if (!isUSR(I[2]))
1255 return not_usr("<class USR>", I[2]);
1256 else {
1257 CXString x;
1258 x.Spelling = I[2];
1259 x.MustFreeString = 0;
1260 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1261 }
1262 I += 3;
1263 continue;
1264 }
1265 break;
1266 default:
1267 break;
1268 }
1269 break;
1270 }
1271
1272 if (I != E) {
1273 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1274 display_usrs();
1275 return 1;
1276 }
1277 return 0;
1278}
1279
1280int print_usrs_file(const char *file_name) {
1281 char line[2048];
1282 const char *args[128];
1283 unsigned numChars = 0;
1284
1285 FILE *fp = fopen(file_name, "r");
1286 if (!fp) {
1287 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1288 return 1;
1289 }
1290
1291 /* This code is not really all that safe, but it works fine for testing. */
1292 while (!feof(fp)) {
1293 char c = fgetc(fp);
1294 if (c == '\n') {
1295 unsigned i = 0;
1296 const char *s = 0;
1297
1298 if (numChars == 0)
1299 continue;
1300
1301 line[numChars] = '\0';
1302 numChars = 0;
1303
1304 if (line[0] == '/' && line[1] == '/')
1305 continue;
1306
1307 s = strtok(line, " ");
1308 while (s) {
1309 args[i] = s;
1310 ++i;
1311 s = strtok(0, " ");
1312 }
1313 if (print_usrs(&args[0], &args[i]))
1314 return 1;
1315 }
1316 else
1317 line[numChars++] = c;
1318 }
1319
1320 fclose(fp);
1321 return 0;
1322}
1323
1324/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001325/* Command line processing. */
1326/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001327int write_pch_file(const char *filename, int argc, const char *argv[]) {
1328 CXIndex Idx;
1329 CXTranslationUnit TU;
1330 struct CXUnsavedFile *unsaved_files = 0;
1331 int num_unsaved_files = 0;
1332
1333 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1334
1335 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1336 clang_disposeIndex(Idx);
1337 return -1;
1338 }
1339
1340 TU = clang_parseTranslationUnit(Idx, 0,
1341 argv + num_unsaved_files,
1342 argc - num_unsaved_files,
1343 unsaved_files,
1344 num_unsaved_files,
1345 CXTranslationUnit_Incomplete);
1346 if (!TU) {
1347 fprintf(stderr, "Unable to load translation unit!\n");
1348 free_remapped_files(unsaved_files, num_unsaved_files);
1349 clang_disposeIndex(Idx);
1350 return 1;
1351 }
1352
Douglas Gregor19998442010-08-13 15:35:05 +00001353 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001354 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1355 clang_disposeTranslationUnit(TU);
1356 free_remapped_files(unsaved_files, num_unsaved_files);
1357 clang_disposeIndex(Idx);
1358 return 0;
1359}
1360
1361/******************************************************************************/
1362/* Command line processing. */
1363/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001364
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001365static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001366 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001367 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001368 if (strcmp(s, "-usrs") == 0)
1369 return USRVisitor;
1370 return NULL;
1371}
1372
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001373static void print_usage(void) {
1374 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001375 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001376 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001377 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001378 " c-index-test -test-file-scan <AST file> <source file> "
1379 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001380 " c-index-test -test-load-tu <AST file> <symbol filter> "
1381 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001382 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1383 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001384 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001385 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001386 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1387 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001388 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001389 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1390 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001391 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001392 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001393 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001394 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1395 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001396 " c-index-test -print-usr-file <file>\n"
1397 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001398 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001399 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001400 " all - load all symbols, including those from PCH\n"
1401 " local - load all symbols except those in PCH\n"
1402 " category - only load ObjC categories (non-PCH)\n"
1403 " interface - only load ObjC interfaces (non-PCH)\n"
1404 " protocol - only load ObjC protocols (non-PCH)\n"
1405 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001406 " typedef - only load typdefs (non-PCH)\n"
1407 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001408}
1409
1410int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001411 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001412 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001413 return perform_code_completion(argc, argv, 0);
1414 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1415 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001416 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1417 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001418 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001419 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001420 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001421 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1422 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001423 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001424 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1425 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1426 if (I) {
1427 int trials = atoi(argv[2]);
1428 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1429 NULL);
1430 }
1431 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001432 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001433 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001434 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001435 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001436 }
1437 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001438 return perform_file_scan(argv[2], argv[3],
1439 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001440 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1441 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001442 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1443 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1444 PrintInclusionStack);
1445 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1446 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1447 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001448 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1449 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1450 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001451 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1452 return perform_test_load_source(argc - 2, argv + 2, "all",
1453 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001454 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1455 if (argc > 2)
1456 return print_usrs(argv + 2, argv + argc);
1457 else {
1458 display_usrs();
1459 return 1;
1460 }
1461 }
1462 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1463 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001464 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1465 return write_pch_file(argv[2], argc - 3, argv + 3);
1466
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001467 print_usage();
1468 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001469}