blob: 330fa545f9310fb1cc86f8caebc77ed78acf9ce6 [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 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000198 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000199}
Steve Naroff89922f82009-08-31 00:59:03 +0000200
Ted Kremeneke68fff62010-02-17 00:41:32 +0000201static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000202 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000203 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000204 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000205 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000206 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000207 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000208 clang_disposeString(source);
209 return "<invalid loc>";
210 }
211 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000212 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000213 clang_disposeString(source);
214 return b;
215 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000216}
217
Ted Kremenek0d435192009-11-17 18:13:31 +0000218/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000219/* Callbacks. */
220/******************************************************************************/
221
222typedef void (*PostVisitTU)(CXTranslationUnit);
223
Douglas Gregora88084b2010-02-18 18:08:43 +0000224void PrintDiagnostic(CXDiagnostic Diagnostic) {
225 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000226 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000227 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000228 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
229 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
230 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000231
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000232 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000233 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000234
Douglas Gregor274f1902010-02-22 23:17:23 +0000235 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
236 fprintf(stderr, "%s\n", clang_getCString(Msg));
237 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000238
Douglas Gregor5352ac02010-01-28 00:27:43 +0000239 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000240 &file, 0, 0, 0);
241 if (!file)
242 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000243
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000244 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
245 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000246 CXSourceRange range;
247 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
248 CXSourceLocation start = clang_getRangeStart(range);
249 CXSourceLocation end = clang_getRangeEnd(range);
250 unsigned start_line, start_column, end_line, end_column;
251 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000252 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000253 &start_column, 0);
254 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
255 if (clang_equalLocations(start, end)) {
256 /* Insertion. */
257 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000258 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000259 clang_getCString(insertion_text), start_line, start_column);
260 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
261 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000262 if (start_file == file && end_file == file) {
263 fprintf(out, "FIX-IT: Remove ");
264 PrintExtent(out, start_line, start_column, end_line, end_column);
265 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000266 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000267 } else {
268 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000269 if (start_file == end_file) {
270 fprintf(out, "FIX-IT: Replace ");
271 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000272 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000273 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000274 break;
275 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000276 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000277 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000278}
279
Douglas Gregora88084b2010-02-18 18:08:43 +0000280void PrintDiagnostics(CXTranslationUnit TU) {
281 int i, n = clang_getNumDiagnostics(TU);
282 for (i = 0; i != n; ++i) {
283 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
284 PrintDiagnostic(Diag);
285 clang_disposeDiagnostic(Diag);
286 }
287}
288
Ted Kremenekce2ae882010-01-26 17:59:48 +0000289/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000290/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000291/******************************************************************************/
292
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000293static const char *FileCheckPrefix = "CHECK";
294
Douglas Gregora7bde202010-01-19 00:34:46 +0000295static void PrintCursorExtent(CXCursor C) {
296 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000297 CXFile begin_file, end_file;
298 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000299
Douglas Gregor1db19de2010-01-19 21:36:55 +0000300 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000301 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000302 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000303 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000304 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000305 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000306
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000307 printf(" Extent=");
308 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000309}
310
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000311/* Data used by all of the visitors. */
312typedef struct {
313 CXTranslationUnit TU;
314 enum CXCursorKind *Filter;
315} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000316
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000317
Ted Kremeneke68fff62010-02-17 00:41:32 +0000318enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000319 CXCursor Parent,
320 CXClientData ClientData) {
321 VisitorData *Data = (VisitorData *)ClientData;
322 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000323 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000324 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000325 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000326 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000327 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000328 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000329 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000330 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000331 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000332 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000333
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000334 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000335}
Steve Naroff50398192009-08-28 15:28:48 +0000336
Ted Kremeneke68fff62010-02-17 00:41:32 +0000337static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000338 CXCursor Parent,
339 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000340 const char *startBuf, *endBuf;
341 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
342 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000343 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000344
Douglas Gregorb6998662010-01-19 19:34:47 +0000345 if (Cursor.kind != CXCursor_FunctionDecl ||
346 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000347 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000348
349 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
350 &startLine, &startColumn,
351 &endLine, &endColumn);
352 /* Probe the entire body, looking for both decls and refs. */
353 curLine = startLine;
354 curColumn = startColumn;
355
356 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000357 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000358 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000359 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000360
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000361 if (*startBuf == '\n') {
362 startBuf++;
363 curLine++;
364 curColumn = 1;
365 } else if (*startBuf != '\t')
366 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000367
Douglas Gregor98258af2010-01-18 22:46:11 +0000368 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000369 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000370
Douglas Gregor1db19de2010-01-19 21:36:55 +0000371 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000372 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000373 CXSourceLocation RefLoc
374 = clang_getLocation(Data->TU, file, curLine, curColumn);
375 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000376 if (Ref.kind == CXCursor_NoDeclFound) {
377 /* Nothing found here; that's fine. */
378 } else if (Ref.kind != CXCursor_FunctionDecl) {
379 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
380 curLine, curColumn);
381 PrintCursor(Ref);
382 printf("\n");
383 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000384 }
Ted Kremenek74844072010-02-17 00:41:20 +0000385 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000386 startBuf++;
387 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000388
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000389 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000390}
391
Ted Kremenek7d405622010-01-12 23:34:26 +0000392/******************************************************************************/
393/* USR testing. */
394/******************************************************************************/
395
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000396enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
397 CXClientData ClientData) {
398 VisitorData *Data = (VisitorData *)ClientData;
399 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000400 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000401 const char *cstr = clang_getCString(USR);
402 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000403 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000404 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000405 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000406 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
407
Douglas Gregora7bde202010-01-19 00:34:46 +0000408 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000409 printf("\n");
410 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000411
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000412 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000413 }
414
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000415 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000416}
417
418/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000419/* Inclusion stack testing. */
420/******************************************************************************/
421
422void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
423 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000424
Ted Kremenek16b55a72010-01-26 19:31:51 +0000425 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000426 CXString fname;
427
428 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000429 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000430 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000431
Ted Kremenek16b55a72010-01-26 19:31:51 +0000432 for (i = 0; i < includeStackLen; ++i) {
433 CXFile includingFile;
434 unsigned line, column;
435 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
436 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000437 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000438 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000439 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000440 }
441 printf("\n");
442}
443
444void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000445 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000446}
447
448/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000449/* Linkage testing. */
450/******************************************************************************/
451
452static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
453 CXClientData d) {
454 const char *linkage = 0;
455
456 if (clang_isInvalid(clang_getCursorKind(cursor)))
457 return CXChildVisit_Recurse;
458
459 switch (clang_getCursorLinkage(cursor)) {
460 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000461 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
462 case CXLinkage_Internal: linkage = "Internal"; break;
463 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
464 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000465 }
466
467 if (linkage) {
468 PrintCursor(cursor);
469 printf("linkage=%s\n", linkage);
470 }
471
472 return CXChildVisit_Recurse;
473}
474
475/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000476/* Typekind testing. */
477/******************************************************************************/
478
479static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
480 CXClientData d) {
481
482 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
483 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000484 CXString S = clang_getTypeKindSpelling(T.kind);
485 PrintCursor(cursor);
486 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000487 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000488 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000489 {
490 CXType CT = clang_getCanonicalType(T);
491 if (!clang_equalTypes(T, CT)) {
492 CXString CS = clang_getTypeKindSpelling(CT.kind);
493 printf(" [canonical=%s]", clang_getCString(CS));
494 clang_disposeString(CS);
495 }
496 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000497 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000498 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000499 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000500 if (RT.kind != CXType_Invalid) {
501 CXString RS = clang_getTypeKindSpelling(RT.kind);
502 printf(" [result=%s]", clang_getCString(RS));
503 clang_disposeString(RS);
504 }
505 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000506 /* Print if this is a non-POD type. */
507 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000508
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000509 printf("\n");
510 }
511 return CXChildVisit_Recurse;
512}
513
514
515/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000516/* Loading ASTs/source. */
517/******************************************************************************/
518
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000519static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000520 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000521 CXCursorVisitor Visitor,
522 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000523
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000524 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000525 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000526
527 if (Visitor) {
528 enum CXCursorKind K = CXCursor_NotImplemented;
529 enum CXCursorKind *ck = &K;
530 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000531
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000532 /* Perform some simple filtering. */
533 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000534 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000535 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
536 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
537 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
538 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
539 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
540 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
541 else {
542 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
543 return 1;
544 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000545
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000546 Data.TU = TU;
547 Data.Filter = ck;
548 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000549 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000550
Ted Kremenekce2ae882010-01-26 17:59:48 +0000551 if (PV)
552 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000553
Douglas Gregora88084b2010-02-18 18:08:43 +0000554 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000555 clang_disposeTranslationUnit(TU);
556 return 0;
557}
558
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000559int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000560 const char *prefix, CXCursorVisitor Visitor,
561 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000562 CXIndex Idx;
563 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000564 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000565 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000566 !strcmp(filter, "local") ? 1 : 0,
567 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000568
Ted Kremenek020a0952010-02-11 07:41:25 +0000569 if (!CreateTranslationUnit(Idx, file, &TU)) {
570 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000571 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000572 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000573
Ted Kremenek020a0952010-02-11 07:41:25 +0000574 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
575 clang_disposeIndex(Idx);
576 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000577}
578
Ted Kremenekce2ae882010-01-26 17:59:48 +0000579int perform_test_load_source(int argc, const char **argv,
580 const char *filter, CXCursorVisitor Visitor,
581 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000582 const char *UseExternalASTs =
583 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000584 CXIndex Idx;
585 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000586 struct CXUnsavedFile *unsaved_files = 0;
587 int num_unsaved_files = 0;
588 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000589
Daniel Dunbarada487d2009-12-01 02:03:10 +0000590 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000591 !strcmp(filter, "local") ? 1 : 0,
592 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000593
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000594 if (UseExternalASTs && strlen(UseExternalASTs))
595 clang_setUseExternalASTGeneration(Idx, 1);
596
Ted Kremenek020a0952010-02-11 07:41:25 +0000597 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
598 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000599 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000600 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000601
Ted Kremeneke68fff62010-02-17 00:41:32 +0000602 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
603 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000604 argv + num_unsaved_files,
605 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000606 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000607 if (!TU) {
608 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000609 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000610 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000611 return 1;
612 }
613
Ted Kremenekce2ae882010-01-26 17:59:48 +0000614 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000615 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000616 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000617 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000618}
619
Douglas Gregorabc563f2010-07-19 21:46:24 +0000620int perform_test_reparse_source(int argc, const char **argv, int trials,
621 const char *filter, CXCursorVisitor Visitor,
622 PostVisitTU PV) {
623 const char *UseExternalASTs =
624 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
625 CXIndex Idx;
626 CXTranslationUnit TU;
627 struct CXUnsavedFile *unsaved_files = 0;
628 int num_unsaved_files = 0;
629 int result;
630 int trial;
631
632 Idx = clang_createIndex(/* excludeDeclsFromPCH */
633 !strcmp(filter, "local") ? 1 : 0,
634 /* displayDiagnosics=*/1);
635
636 if (UseExternalASTs && strlen(UseExternalASTs))
637 clang_setUseExternalASTGeneration(Idx, 1);
638
639 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
640 clang_disposeIndex(Idx);
641 return -1;
642 }
643
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000644 /* Load the initial translation unit -- we do this without honoring remapped
645 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000646 TU = clang_parseTranslationUnit(Idx, 0,
647 argv + num_unsaved_files,
648 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000649 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000650 if (!TU) {
651 fprintf(stderr, "Unable to load translation unit!\n");
652 free_remapped_files(unsaved_files, num_unsaved_files);
653 clang_disposeIndex(Idx);
654 return 1;
655 }
656
657 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000658 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
659 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000660 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000661 clang_disposeTranslationUnit(TU);
662 free_remapped_files(unsaved_files, num_unsaved_files);
663 clang_disposeIndex(Idx);
664 return -1;
665 }
666 }
667
668 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
669 free_remapped_files(unsaved_files, num_unsaved_files);
670 clang_disposeIndex(Idx);
671 return result;
672}
673
Ted Kremenek0d435192009-11-17 18:13:31 +0000674/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000675/* Logic for testing clang_getCursor(). */
676/******************************************************************************/
677
678static void print_cursor_file_scan(CXCursor cursor,
679 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000680 unsigned end_line, unsigned end_col,
681 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000682 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000683 if (prefix)
684 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000685 PrintExtent(stdout, start_line, start_col, end_line, end_col);
686 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000687 PrintCursor(cursor);
688 printf("\n");
689}
690
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000691static int perform_file_scan(const char *ast_file, const char *source_file,
692 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000693 CXIndex Idx;
694 CXTranslationUnit TU;
695 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000696 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000697 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000698 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000699 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000700
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000701 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
702 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000703 fprintf(stderr, "Could not create Index\n");
704 return 1;
705 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000706
Ted Kremenek1c6da172009-11-17 19:37:36 +0000707 if (!CreateTranslationUnit(Idx, ast_file, &TU))
708 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000709
Ted Kremenek1c6da172009-11-17 19:37:36 +0000710 if ((fp = fopen(source_file, "r")) == NULL) {
711 fprintf(stderr, "Could not open '%s'\n", source_file);
712 return 1;
713 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000714
Douglas Gregorb9790342010-01-22 21:44:22 +0000715 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000716 for (;;) {
717 CXCursor cursor;
718 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000719
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000720 if (c == '\n') {
721 ++line;
722 col = 1;
723 } else
724 ++col;
725
726 /* Check the cursor at this position, and dump the previous one if we have
727 * found something new.
728 */
729 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
730 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
731 prevCursor.kind != CXCursor_InvalidFile) {
732 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000733 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000734 start_line = line;
735 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000736 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000737 if (c == EOF)
738 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000739
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000740 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000741 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000742
Ted Kremenek1c6da172009-11-17 19:37:36 +0000743 fclose(fp);
744 return 0;
745}
746
747/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000748/* Logic for testing clang_codeComplete(). */
749/******************************************************************************/
750
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000751/* Parse file:line:column from the input string. Returns 0 on success, non-zero
752 on failure. If successful, the pointer *filename will contain newly-allocated
753 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000754int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000755 unsigned *column, unsigned *second_line,
756 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000757 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000758 const char *last_colon = strrchr(input, ':');
759 unsigned values[4], i;
760 unsigned num_values = (second_line && second_column)? 4 : 2;
761
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000762 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000763 if (!last_colon || last_colon == input) {
764 if (num_values == 4)
765 fprintf(stderr, "could not parse filename:line:column:line:column in "
766 "'%s'\n", input);
767 else
768 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000769 return 1;
770 }
771
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000772 for (i = 0; i != num_values; ++i) {
773 const char *prev_colon;
774
775 /* Parse the next line or column. */
776 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
777 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000778 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000779 (i % 2 ? "column" : "line"), input);
780 return 1;
781 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000782
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000783 if (i + 1 == num_values)
784 break;
785
786 /* Find the previous colon. */
787 prev_colon = last_colon - 1;
788 while (prev_colon != input && *prev_colon != ':')
789 --prev_colon;
790 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000791 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000792 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000793 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000794 }
795
796 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000797 }
798
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000799 *line = values[0];
800 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000801
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000802 if (second_line && second_column) {
803 *second_line = values[2];
804 *second_column = values[3];
805 }
806
Douglas Gregor88d23952009-11-09 18:19:57 +0000807 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000808 *filename = (char*)malloc(last_colon - input + 1);
809 memcpy(*filename, input, last_colon - input);
810 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000811 return 0;
812}
813
814const char *
815clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
816 switch (Kind) {
817 case CXCompletionChunk_Optional: return "Optional";
818 case CXCompletionChunk_TypedText: return "TypedText";
819 case CXCompletionChunk_Text: return "Text";
820 case CXCompletionChunk_Placeholder: return "Placeholder";
821 case CXCompletionChunk_Informative: return "Informative";
822 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
823 case CXCompletionChunk_LeftParen: return "LeftParen";
824 case CXCompletionChunk_RightParen: return "RightParen";
825 case CXCompletionChunk_LeftBracket: return "LeftBracket";
826 case CXCompletionChunk_RightBracket: return "RightBracket";
827 case CXCompletionChunk_LeftBrace: return "LeftBrace";
828 case CXCompletionChunk_RightBrace: return "RightBrace";
829 case CXCompletionChunk_LeftAngle: return "LeftAngle";
830 case CXCompletionChunk_RightAngle: return "RightAngle";
831 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000832 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000833 case CXCompletionChunk_Colon: return "Colon";
834 case CXCompletionChunk_SemiColon: return "SemiColon";
835 case CXCompletionChunk_Equal: return "Equal";
836 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
837 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000838 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000839
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000840 return "Unknown";
841}
842
Douglas Gregor3ac73852009-11-09 16:04:45 +0000843void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000844 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000845
Douglas Gregor3ac73852009-11-09 16:04:45 +0000846 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000847 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000848 CXString text;
849 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000850 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000851 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000852
Douglas Gregor3ac73852009-11-09 16:04:45 +0000853 if (Kind == CXCompletionChunk_Optional) {
854 fprintf(file, "{Optional ");
855 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000856 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000857 file);
858 fprintf(file, "}");
859 continue;
860 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000861
Douglas Gregord5a20892009-11-09 17:05:28 +0000862 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000863 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000864 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000865 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000866 cstr ? cstr : "");
867 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000868 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000869
Douglas Gregor3ac73852009-11-09 16:04:45 +0000870}
871
872void print_completion_result(CXCompletionResult *completion_result,
873 CXClientData client_data) {
874 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000875 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
876
877 fprintf(file, "%s:", clang_getCString(ks));
878 clang_disposeString(ks);
879
Douglas Gregor3ac73852009-11-09 16:04:45 +0000880 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000881 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000882 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000883 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
884 case CXAvailability_Available:
885 break;
886
887 case CXAvailability_Deprecated:
888 fprintf(file, " (deprecated)");
889 break;
890
891 case CXAvailability_NotAvailable:
892 fprintf(file, " (unavailable)");
893 break;
894 }
895 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000896}
897
Douglas Gregor1982c182010-07-12 18:38:41 +0000898int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000899 const char *input = argv[1];
900 char *filename = 0;
901 unsigned line;
902 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000903 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000904 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000905 struct CXUnsavedFile *unsaved_files = 0;
906 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000907 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000908 CXTranslationUnit *TU = 0;
909
Douglas Gregor1982c182010-07-12 18:38:41 +0000910 if (timing_only)
911 input += strlen("-code-completion-timing=");
912 else
913 input += strlen("-code-completion-at=");
914
Ted Kremeneke68fff62010-02-17 00:41:32 +0000915 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000916 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000917 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000918
Douglas Gregor735df882009-12-02 09:21:34 +0000919 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
920 return -1;
921
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000922 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000923 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000924 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +0000925 TU = clang_parseTranslationUnit(CIdx, 0,
926 argv + num_unsaved_files + 2,
927 argc - num_unsaved_files - 2,
Daniel Dunbar43f331d2010-08-19 23:44:04 +0000928 0, 0, getDefaultParsingOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000929 if (!TU) {
930 fprintf(stderr, "Unable to load translation unit!\n");
931 return 1;
932 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000933 for (I = 0; I != Repeats; ++I) {
934 results = clang_codeCompleteAt(TU, filename, line, column,
935 unsaved_files, num_unsaved_files,
936 clang_defaultCodeCompleteOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000937 if (!results) {
938 fprintf(stderr, "Unable to perform code completion!\n");
939 return 1;
940 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000941 if (I != Repeats-1)
942 clang_disposeCodeCompleteResults(results);
943 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000944 } else
945 results = clang_codeComplete(CIdx,
946 argv[argc - 1], argc - num_unsaved_files - 3,
947 argv + num_unsaved_files + 2,
948 num_unsaved_files, unsaved_files,
949 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +0000950
Douglas Gregorec6762c2009-12-18 16:20:58 +0000951 if (results) {
952 unsigned i, n = results->NumResults;
Douglas Gregor92148192010-08-26 00:30:24 +0000953 if (!timing_only)
Douglas Gregor1982c182010-07-12 18:38:41 +0000954 for (i = 0; i != n; ++i)
955 print_completion_result(results->Results + i, stdout);
Douglas Gregora88084b2010-02-18 18:08:43 +0000956 n = clang_codeCompleteGetNumDiagnostics(results);
957 for (i = 0; i != n; ++i) {
958 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
959 PrintDiagnostic(diag);
960 clang_disposeDiagnostic(diag);
961 }
Douglas Gregorec6762c2009-12-18 16:20:58 +0000962 clang_disposeCodeCompleteResults(results);
963 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000964 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000965 clang_disposeIndex(CIdx);
966 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000967
Douglas Gregor735df882009-12-02 09:21:34 +0000968 free_remapped_files(unsaved_files, num_unsaved_files);
969
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000970 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000971}
972
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000973typedef struct {
974 char *filename;
975 unsigned line;
976 unsigned column;
977} CursorSourceLocation;
978
979int inspect_cursor_at(int argc, const char **argv) {
980 CXIndex CIdx;
981 int errorCode;
982 struct CXUnsavedFile *unsaved_files = 0;
983 int num_unsaved_files = 0;
984 CXTranslationUnit TU;
985 CXCursor Cursor;
986 CursorSourceLocation *Locations = 0;
987 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000988
Ted Kremeneke68fff62010-02-17 00:41:32 +0000989 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000990 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
991 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000992
Douglas Gregorf2c87bd2010-01-15 19:40:17 +0000993 /* Parse the locations. */
994 assert(NumLocations > 0 && "Unable to count locations?");
995 Locations = (CursorSourceLocation *)malloc(
996 NumLocations * sizeof(CursorSourceLocation));
997 for (Loc = 0; Loc < NumLocations; ++Loc) {
998 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +0000999 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1000 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001001 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001002 return errorCode;
1003 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001004
1005 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001006 &num_unsaved_files))
1007 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001008
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001009 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001010 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1011 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001012 argv + num_unsaved_files + 1 + NumLocations,
1013 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001014 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001015 if (!TU) {
1016 fprintf(stderr, "unable to parse input\n");
1017 return -1;
1018 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001019
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001020 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001021 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1022 if (!file)
1023 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001024
1025 Cursor = clang_getCursor(TU,
1026 clang_getLocation(TU, file, Locations[Loc].line,
1027 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001028 PrintCursor(Cursor);
1029 printf("\n");
1030 free(Locations[Loc].filename);
1031 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001032
Douglas Gregora88084b2010-02-18 18:08:43 +00001033 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001034 clang_disposeTranslationUnit(TU);
1035 clang_disposeIndex(CIdx);
1036 free(Locations);
1037 free_remapped_files(unsaved_files, num_unsaved_files);
1038 return 0;
1039}
1040
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001041int perform_token_annotation(int argc, const char **argv) {
1042 const char *input = argv[1];
1043 char *filename = 0;
1044 unsigned line, second_line;
1045 unsigned column, second_column;
1046 CXIndex CIdx;
1047 CXTranslationUnit TU = 0;
1048 int errorCode;
1049 struct CXUnsavedFile *unsaved_files = 0;
1050 int num_unsaved_files = 0;
1051 CXToken *tokens;
1052 unsigned num_tokens;
1053 CXSourceRange range;
1054 CXSourceLocation startLoc, endLoc;
1055 CXFile file = 0;
1056 CXCursor *cursors = 0;
1057 unsigned i;
1058
1059 input += strlen("-test-annotate-tokens=");
1060 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1061 &second_line, &second_column)))
1062 return errorCode;
1063
1064 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1065 return -1;
1066
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001067 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001068 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1069 argc - num_unsaved_files - 3,
1070 argv + num_unsaved_files + 2,
1071 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001072 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001073 if (!TU) {
1074 fprintf(stderr, "unable to parse input\n");
1075 clang_disposeIndex(CIdx);
1076 free(filename);
1077 free_remapped_files(unsaved_files, num_unsaved_files);
1078 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001079 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001080 errorCode = 0;
1081
1082 file = clang_getFile(TU, filename);
1083 if (!file) {
1084 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1085 errorCode = -1;
1086 goto teardown;
1087 }
1088
1089 startLoc = clang_getLocation(TU, file, line, column);
1090 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001091 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001092 column);
1093 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001094 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001095 }
1096
1097 endLoc = clang_getLocation(TU, file, second_line, second_column);
1098 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001099 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001100 second_line, second_column);
1101 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001102 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001103 }
1104
1105 range = clang_getRange(startLoc, endLoc);
1106 clang_tokenize(TU, range, &tokens, &num_tokens);
1107 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1108 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1109 for (i = 0; i != num_tokens; ++i) {
1110 const char *kind = "<unknown>";
1111 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1112 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1113 unsigned start_line, start_column, end_line, end_column;
1114
1115 switch (clang_getTokenKind(tokens[i])) {
1116 case CXToken_Punctuation: kind = "Punctuation"; break;
1117 case CXToken_Keyword: kind = "Keyword"; break;
1118 case CXToken_Identifier: kind = "Identifier"; break;
1119 case CXToken_Literal: kind = "Literal"; break;
1120 case CXToken_Comment: kind = "Comment"; break;
1121 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001122 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001123 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001124 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001125 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001126 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1127 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001128 if (!clang_isInvalid(cursors[i].kind)) {
1129 printf(" ");
1130 PrintCursor(cursors[i]);
1131 }
1132 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001133 }
1134 free(cursors);
1135
1136 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001137 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001138 clang_disposeTranslationUnit(TU);
1139 clang_disposeIndex(CIdx);
1140 free(filename);
1141 free_remapped_files(unsaved_files, num_unsaved_files);
1142 return errorCode;
1143}
1144
Ted Kremenek0d435192009-11-17 18:13:31 +00001145/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001146/* USR printing. */
1147/******************************************************************************/
1148
1149static int insufficient_usr(const char *kind, const char *usage) {
1150 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1151 return 1;
1152}
1153
1154static unsigned isUSR(const char *s) {
1155 return s[0] == 'c' && s[1] == ':';
1156}
1157
1158static int not_usr(const char *s, const char *arg) {
1159 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1160 return 1;
1161}
1162
1163static void print_usr(CXString usr) {
1164 const char *s = clang_getCString(usr);
1165 printf("%s\n", s);
1166 clang_disposeString(usr);
1167}
1168
1169static void display_usrs() {
1170 fprintf(stderr, "-print-usrs options:\n"
1171 " ObjCCategory <class name> <category name>\n"
1172 " ObjCClass <class name>\n"
1173 " ObjCIvar <ivar name> <class USR>\n"
1174 " ObjCMethod <selector> [0=class method|1=instance method] "
1175 "<class USR>\n"
1176 " ObjCProperty <property name> <class USR>\n"
1177 " ObjCProtocol <protocol name>\n");
1178}
1179
1180int print_usrs(const char **I, const char **E) {
1181 while (I != E) {
1182 const char *kind = *I;
1183 unsigned len = strlen(kind);
1184 switch (len) {
1185 case 8:
1186 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1187 if (I + 2 >= E)
1188 return insufficient_usr(kind, "<ivar name> <class USR>");
1189 if (!isUSR(I[2]))
1190 return not_usr("<class USR>", I[2]);
1191 else {
1192 CXString x;
1193 x.Spelling = I[2];
1194 x.MustFreeString = 0;
1195 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1196 }
1197
1198 I += 3;
1199 continue;
1200 }
1201 break;
1202 case 9:
1203 if (memcmp(kind, "ObjCClass", 9) == 0) {
1204 if (I + 1 >= E)
1205 return insufficient_usr(kind, "<class name>");
1206 print_usr(clang_constructUSR_ObjCClass(I[1]));
1207 I += 2;
1208 continue;
1209 }
1210 break;
1211 case 10:
1212 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1213 if (I + 3 >= E)
1214 return insufficient_usr(kind, "<method selector> "
1215 "[0=class method|1=instance method] <class USR>");
1216 if (!isUSR(I[3]))
1217 return not_usr("<class USR>", I[3]);
1218 else {
1219 CXString x;
1220 x.Spelling = I[3];
1221 x.MustFreeString = 0;
1222 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1223 }
1224 I += 4;
1225 continue;
1226 }
1227 break;
1228 case 12:
1229 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1230 if (I + 2 >= E)
1231 return insufficient_usr(kind, "<class name> <category name>");
1232 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1233 I += 3;
1234 continue;
1235 }
1236 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1237 if (I + 1 >= E)
1238 return insufficient_usr(kind, "<protocol name>");
1239 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1240 I += 2;
1241 continue;
1242 }
1243 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1244 if (I + 2 >= E)
1245 return insufficient_usr(kind, "<property name> <class USR>");
1246 if (!isUSR(I[2]))
1247 return not_usr("<class USR>", I[2]);
1248 else {
1249 CXString x;
1250 x.Spelling = I[2];
1251 x.MustFreeString = 0;
1252 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1253 }
1254 I += 3;
1255 continue;
1256 }
1257 break;
1258 default:
1259 break;
1260 }
1261 break;
1262 }
1263
1264 if (I != E) {
1265 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1266 display_usrs();
1267 return 1;
1268 }
1269 return 0;
1270}
1271
1272int print_usrs_file(const char *file_name) {
1273 char line[2048];
1274 const char *args[128];
1275 unsigned numChars = 0;
1276
1277 FILE *fp = fopen(file_name, "r");
1278 if (!fp) {
1279 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1280 return 1;
1281 }
1282
1283 /* This code is not really all that safe, but it works fine for testing. */
1284 while (!feof(fp)) {
1285 char c = fgetc(fp);
1286 if (c == '\n') {
1287 unsigned i = 0;
1288 const char *s = 0;
1289
1290 if (numChars == 0)
1291 continue;
1292
1293 line[numChars] = '\0';
1294 numChars = 0;
1295
1296 if (line[0] == '/' && line[1] == '/')
1297 continue;
1298
1299 s = strtok(line, " ");
1300 while (s) {
1301 args[i] = s;
1302 ++i;
1303 s = strtok(0, " ");
1304 }
1305 if (print_usrs(&args[0], &args[i]))
1306 return 1;
1307 }
1308 else
1309 line[numChars++] = c;
1310 }
1311
1312 fclose(fp);
1313 return 0;
1314}
1315
1316/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001317/* Command line processing. */
1318/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001319int write_pch_file(const char *filename, int argc, const char *argv[]) {
1320 CXIndex Idx;
1321 CXTranslationUnit TU;
1322 struct CXUnsavedFile *unsaved_files = 0;
1323 int num_unsaved_files = 0;
1324
1325 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1326
1327 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1328 clang_disposeIndex(Idx);
1329 return -1;
1330 }
1331
1332 TU = clang_parseTranslationUnit(Idx, 0,
1333 argv + num_unsaved_files,
1334 argc - num_unsaved_files,
1335 unsaved_files,
1336 num_unsaved_files,
1337 CXTranslationUnit_Incomplete);
1338 if (!TU) {
1339 fprintf(stderr, "Unable to load translation unit!\n");
1340 free_remapped_files(unsaved_files, num_unsaved_files);
1341 clang_disposeIndex(Idx);
1342 return 1;
1343 }
1344
Douglas Gregor19998442010-08-13 15:35:05 +00001345 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001346 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1347 clang_disposeTranslationUnit(TU);
1348 free_remapped_files(unsaved_files, num_unsaved_files);
1349 clang_disposeIndex(Idx);
1350 return 0;
1351}
1352
1353/******************************************************************************/
1354/* Command line processing. */
1355/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001356
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001357static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001358 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001359 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001360 if (strcmp(s, "-usrs") == 0)
1361 return USRVisitor;
1362 return NULL;
1363}
1364
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001365static void print_usage(void) {
1366 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001367 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001368 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001369 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001370 " c-index-test -test-file-scan <AST file> <source file> "
1371 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001372 " c-index-test -test-load-tu <AST file> <symbol filter> "
1373 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001374 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1375 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001376 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001377 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001378 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1379 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001380 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001381 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1382 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001383 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001384 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001385 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001386 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1387 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001388 " c-index-test -print-usr-file <file>\n"
1389 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001390 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001391 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001392 " all - load all symbols, including those from PCH\n"
1393 " local - load all symbols except those in PCH\n"
1394 " category - only load ObjC categories (non-PCH)\n"
1395 " interface - only load ObjC interfaces (non-PCH)\n"
1396 " protocol - only load ObjC protocols (non-PCH)\n"
1397 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001398 " typedef - only load typdefs (non-PCH)\n"
1399 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001400}
1401
1402int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001403 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001404 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001405 return perform_code_completion(argc, argv, 0);
1406 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1407 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001408 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1409 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001410 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001411 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001412 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001413 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1414 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001415 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001416 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1417 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1418 if (I) {
1419 int trials = atoi(argv[2]);
1420 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1421 NULL);
1422 }
1423 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001424 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001425 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001426 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001427 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001428 }
1429 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001430 return perform_file_scan(argv[2], argv[3],
1431 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001432 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1433 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001434 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1435 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1436 PrintInclusionStack);
1437 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1438 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1439 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001440 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1441 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1442 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001443 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1444 return perform_test_load_source(argc - 2, argv + 2, "all",
1445 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001446 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1447 if (argc > 2)
1448 return print_usrs(argv + 2, argv + argc);
1449 else {
1450 display_usrs();
1451 return 1;
1452 }
1453 }
1454 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1455 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001456 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1457 return write_pch_file(argv[2], argc - 3, argv + 3);
1458
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001459 print_usage();
1460 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001461}