blob: 58eff97ef8f88df2d348d1351ccfb0383927fdef [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 Gregor1e5e6682010-08-26 13:48:20 +00004#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00005#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00006#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00007#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00008#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009
Ted Kremenek0d435192009-11-17 18:13:31 +000010/******************************************************************************/
11/* Utility functions. */
12/******************************************************************************/
13
John Thompson2e06fc82009-10-27 13:42:56 +000014#ifdef _MSC_VER
15char *basename(const char* path)
16{
17 char* base1 = (char*)strrchr(path, '/');
18 char* base2 = (char*)strrchr(path, '\\');
19 if (base1 && base2)
20 return((base1 > base2) ? base1 + 1 : base2 + 1);
21 else if (base1)
22 return(base1 + 1);
23 else if (base2)
24 return(base2 + 1);
25
26 return((char*)path);
27}
28#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000029extern char *basename(const char *);
John Thompson2e06fc82009-10-27 13:42:56 +000030#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000031
Douglas Gregor45ba9a12010-07-25 17:39:21 +000032/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000033static unsigned getDefaultParsingOptions() {
34 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
35
36 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000037 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000038 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
39 options |= CXTranslationUnit_CacheCompletionResults;
Douglas Gregor44c181a2010-07-23 00:33:23 +000040
41 return options;
42}
43
Daniel Dunbar51b058c2010-02-14 08:32:24 +000044static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
45 unsigned end_line, unsigned end_column) {
46 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000047 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000048}
49
Ted Kremenek1c6da172009-11-17 19:37:36 +000050static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
51 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000052
Douglas Gregora88084b2010-02-18 18:08:43 +000053 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000054 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000055 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
56 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000057 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000058 return 1;
59}
60
Douglas Gregor4db64a42010-01-23 00:14:00 +000061void free_remapped_files(struct CXUnsavedFile *unsaved_files,
62 int num_unsaved_files) {
63 int i;
64 for (i = 0; i != num_unsaved_files; ++i) {
65 free((char *)unsaved_files[i].Filename);
66 free((char *)unsaved_files[i].Contents);
67 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000068 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000069}
70
71int parse_remapped_files(int argc, const char **argv, int start_arg,
72 struct CXUnsavedFile **unsaved_files,
73 int *num_unsaved_files) {
74 int i;
75 int arg;
76 int prefix_len = strlen("-remap-file=");
77 *unsaved_files = 0;
78 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000079
Douglas Gregor4db64a42010-01-23 00:14:00 +000080 /* Count the number of remapped files. */
81 for (arg = start_arg; arg < argc; ++arg) {
82 if (strncmp(argv[arg], "-remap-file=", prefix_len))
83 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +000084
Douglas Gregor4db64a42010-01-23 00:14:00 +000085 ++*num_unsaved_files;
86 }
Ted Kremeneke68fff62010-02-17 00:41:32 +000087
Douglas Gregor4db64a42010-01-23 00:14:00 +000088 if (*num_unsaved_files == 0)
89 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090
Douglas Gregor4db64a42010-01-23 00:14:00 +000091 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +000092 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
93 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000094 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
95 struct CXUnsavedFile *unsaved = *unsaved_files + i;
96 const char *arg_string = argv[arg] + prefix_len;
97 int filename_len;
98 char *filename;
99 char *contents;
100 FILE *to_file;
101 const char *semi = strchr(arg_string, ';');
102 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000103 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000104 "error: -remap-file=from;to argument is missing semicolon\n");
105 free_remapped_files(*unsaved_files, i);
106 *unsaved_files = 0;
107 *num_unsaved_files = 0;
108 return -1;
109 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000110
Douglas Gregor4db64a42010-01-23 00:14:00 +0000111 /* Open the file that we're remapping to. */
112 to_file = fopen(semi + 1, "r");
113 if (!to_file) {
114 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
115 semi + 1);
116 free_remapped_files(*unsaved_files, i);
117 *unsaved_files = 0;
118 *num_unsaved_files = 0;
119 return -1;
120 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000121
Douglas Gregor4db64a42010-01-23 00:14:00 +0000122 /* Determine the length of the file we're remapping to. */
123 fseek(to_file, 0, SEEK_END);
124 unsaved->Length = ftell(to_file);
125 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 /* Read the contents of the file we're remapping to. */
128 contents = (char *)malloc(unsaved->Length + 1);
129 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
130 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
131 (feof(to_file) ? "EOF" : "error"), semi + 1);
132 fclose(to_file);
133 free_remapped_files(*unsaved_files, i);
134 *unsaved_files = 0;
135 *num_unsaved_files = 0;
136 return -1;
137 }
138 contents[unsaved->Length] = 0;
139 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000140
Douglas Gregor4db64a42010-01-23 00:14:00 +0000141 /* Close the file. */
142 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000143
Douglas Gregor4db64a42010-01-23 00:14:00 +0000144 /* Copy the file name that we're remapping from. */
145 filename_len = semi - arg_string;
146 filename = (char *)malloc(filename_len + 1);
147 memcpy(filename, arg_string, filename_len);
148 filename[filename_len] = 0;
149 unsaved->Filename = filename;
150 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000151
Douglas Gregor4db64a42010-01-23 00:14:00 +0000152 return 0;
153}
154
Ted Kremenek0d435192009-11-17 18:13:31 +0000155/******************************************************************************/
156/* Pretty-printing. */
157/******************************************************************************/
158
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000159static void PrintCursor(CXCursor Cursor) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000160 if (clang_isInvalid(Cursor.kind)) {
161 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
162 printf("Invalid Cursor => %s", clang_getCString(ks));
163 clang_disposeString(ks);
164 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000165 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000166 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000167 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000168 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000169 CXCursor SpecializationOf;
170
Ted Kremeneke68fff62010-02-17 00:41:32 +0000171 ks = clang_getCursorKindSpelling(Cursor.kind);
Steve Naroffef0cef62009-11-09 17:45:52 +0000172 string = clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000173 printf("%s=%s", clang_getCString(ks),
174 clang_getCString(string));
175 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000176 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000177
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000178 Referenced = clang_getCursorReferenced(Cursor);
179 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
180 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000181 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000182 printf(":%d:%d", line, column);
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000183 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000184
185 if (clang_isCursorDefinition(Cursor))
186 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000187
188 switch (clang_getCursorAvailability(Cursor)) {
189 case CXAvailability_Available:
190 break;
191
192 case CXAvailability_Deprecated:
193 printf(" (deprecated)");
194 break;
195
196 case CXAvailability_NotAvailable:
197 printf(" (unavailable)");
198 break;
199 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000200
201 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
202 CXType T =
203 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
204 CXString S = clang_getTypeKindSpelling(T.kind);
205 printf(" [IBOutletCollection=%s]", clang_getCString(S));
206 clang_disposeString(S);
207 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000208
209 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
210 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
211 unsigned isVirtual = clang_isVirtualBase(Cursor);
212 const char *accessStr = 0;
213
214 switch (access) {
215 case CX_CXXInvalidAccessSpecifier:
216 accessStr = "invalid"; break;
217 case CX_CXXPublic:
218 accessStr = "public"; break;
219 case CX_CXXProtected:
220 accessStr = "protected"; break;
221 case CX_CXXPrivate:
222 accessStr = "private"; break;
223 }
224
225 printf(" [access=%s isVirtual=%s]", accessStr,
226 isVirtual ? "true" : "false");
227 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000228
229 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
230 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
231 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
232 CXString Name = clang_getCursorSpelling(SpecializationOf);
233 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
234 printf(" [Specialization of %s:%d:%d]",
235 clang_getCString(Name), line, column);
236 clang_disposeString(Name);
237 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000238 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000239}
Steve Naroff89922f82009-08-31 00:59:03 +0000240
Ted Kremeneke68fff62010-02-17 00:41:32 +0000241static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000242 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000243 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000244 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000245 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000246 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000247 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000248 clang_disposeString(source);
249 return "<invalid loc>";
250 }
251 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000252 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000253 clang_disposeString(source);
254 return b;
255 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000256}
257
Ted Kremenek0d435192009-11-17 18:13:31 +0000258/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000259/* Callbacks. */
260/******************************************************************************/
261
262typedef void (*PostVisitTU)(CXTranslationUnit);
263
Douglas Gregora88084b2010-02-18 18:08:43 +0000264void PrintDiagnostic(CXDiagnostic Diagnostic) {
265 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000266 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000267 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000268 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
269 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
270 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000271
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000272 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000273 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000274
Douglas Gregor274f1902010-02-22 23:17:23 +0000275 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
276 fprintf(stderr, "%s\n", clang_getCString(Msg));
277 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000278
Douglas Gregor5352ac02010-01-28 00:27:43 +0000279 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000280 &file, 0, 0, 0);
281 if (!file)
282 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000283
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000284 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
285 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000286 CXSourceRange range;
287 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
288 CXSourceLocation start = clang_getRangeStart(range);
289 CXSourceLocation end = clang_getRangeEnd(range);
290 unsigned start_line, start_column, end_line, end_column;
291 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000292 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000293 &start_column, 0);
294 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
295 if (clang_equalLocations(start, end)) {
296 /* Insertion. */
297 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000298 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000299 clang_getCString(insertion_text), start_line, start_column);
300 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
301 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000302 if (start_file == file && end_file == file) {
303 fprintf(out, "FIX-IT: Remove ");
304 PrintExtent(out, start_line, start_column, end_line, end_column);
305 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000306 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000307 } else {
308 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000309 if (start_file == end_file) {
310 fprintf(out, "FIX-IT: Replace ");
311 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000312 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000313 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000314 break;
315 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000316 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000317 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000318}
319
Douglas Gregora88084b2010-02-18 18:08:43 +0000320void PrintDiagnostics(CXTranslationUnit TU) {
321 int i, n = clang_getNumDiagnostics(TU);
322 for (i = 0; i != n; ++i) {
323 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
324 PrintDiagnostic(Diag);
325 clang_disposeDiagnostic(Diag);
326 }
327}
328
Ted Kremenekce2ae882010-01-26 17:59:48 +0000329/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000330/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000331/******************************************************************************/
332
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000333static const char *FileCheckPrefix = "CHECK";
334
Douglas Gregora7bde202010-01-19 00:34:46 +0000335static void PrintCursorExtent(CXCursor C) {
336 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000337 CXFile begin_file, end_file;
338 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000339
Douglas Gregor1db19de2010-01-19 21:36:55 +0000340 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000341 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000342 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000343 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000344 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000345 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000346
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000347 printf(" Extent=");
348 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000349}
350
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000351/* Data used by all of the visitors. */
352typedef struct {
353 CXTranslationUnit TU;
354 enum CXCursorKind *Filter;
355} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000356
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000357
Ted Kremeneke68fff62010-02-17 00:41:32 +0000358enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000359 CXCursor Parent,
360 CXClientData ClientData) {
361 VisitorData *Data = (VisitorData *)ClientData;
362 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000363 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000364 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000365 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000366 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000367 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000368 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000369 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000370 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000371 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000372 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000373
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000374 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000375}
Steve Naroff50398192009-08-28 15:28:48 +0000376
Ted Kremeneke68fff62010-02-17 00:41:32 +0000377static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000378 CXCursor Parent,
379 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000380 const char *startBuf, *endBuf;
381 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
382 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000383 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000384
Douglas Gregorb6998662010-01-19 19:34:47 +0000385 if (Cursor.kind != CXCursor_FunctionDecl ||
386 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000387 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000388
389 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
390 &startLine, &startColumn,
391 &endLine, &endColumn);
392 /* Probe the entire body, looking for both decls and refs. */
393 curLine = startLine;
394 curColumn = startColumn;
395
396 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000397 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000398 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000399 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000400
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000401 if (*startBuf == '\n') {
402 startBuf++;
403 curLine++;
404 curColumn = 1;
405 } else if (*startBuf != '\t')
406 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000407
Douglas Gregor98258af2010-01-18 22:46:11 +0000408 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000409 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000410
Douglas Gregor1db19de2010-01-19 21:36:55 +0000411 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000412 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000413 CXSourceLocation RefLoc
414 = clang_getLocation(Data->TU, file, curLine, curColumn);
415 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000416 if (Ref.kind == CXCursor_NoDeclFound) {
417 /* Nothing found here; that's fine. */
418 } else if (Ref.kind != CXCursor_FunctionDecl) {
419 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
420 curLine, curColumn);
421 PrintCursor(Ref);
422 printf("\n");
423 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000424 }
Ted Kremenek74844072010-02-17 00:41:20 +0000425 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000426 startBuf++;
427 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000428
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000429 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000430}
431
Ted Kremenek7d405622010-01-12 23:34:26 +0000432/******************************************************************************/
433/* USR testing. */
434/******************************************************************************/
435
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000436enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
437 CXClientData ClientData) {
438 VisitorData *Data = (VisitorData *)ClientData;
439 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000440 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000441 const char *cstr = clang_getCString(USR);
442 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000443 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000444 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000445 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000446 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
447
Douglas Gregora7bde202010-01-19 00:34:46 +0000448 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000449 printf("\n");
450 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000451
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000452 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000453 }
454
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000455 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000456}
457
458/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000459/* Inclusion stack testing. */
460/******************************************************************************/
461
462void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
463 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000464
Ted Kremenek16b55a72010-01-26 19:31:51 +0000465 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000466 CXString fname;
467
468 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000469 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000470 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000471
Ted Kremenek16b55a72010-01-26 19:31:51 +0000472 for (i = 0; i < includeStackLen; ++i) {
473 CXFile includingFile;
474 unsigned line, column;
475 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
476 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000477 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000478 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000479 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000480 }
481 printf("\n");
482}
483
484void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000485 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000486}
487
488/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000489/* Linkage testing. */
490/******************************************************************************/
491
492static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
493 CXClientData d) {
494 const char *linkage = 0;
495
496 if (clang_isInvalid(clang_getCursorKind(cursor)))
497 return CXChildVisit_Recurse;
498
499 switch (clang_getCursorLinkage(cursor)) {
500 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000501 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
502 case CXLinkage_Internal: linkage = "Internal"; break;
503 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
504 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000505 }
506
507 if (linkage) {
508 PrintCursor(cursor);
509 printf("linkage=%s\n", linkage);
510 }
511
512 return CXChildVisit_Recurse;
513}
514
515/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000516/* Typekind testing. */
517/******************************************************************************/
518
519static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
520 CXClientData d) {
521
522 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
523 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000524 CXString S = clang_getTypeKindSpelling(T.kind);
525 PrintCursor(cursor);
526 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000527 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000528 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000529 {
530 CXType CT = clang_getCanonicalType(T);
531 if (!clang_equalTypes(T, CT)) {
532 CXString CS = clang_getTypeKindSpelling(CT.kind);
533 printf(" [canonical=%s]", clang_getCString(CS));
534 clang_disposeString(CS);
535 }
536 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000537 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000538 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000539 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000540 if (RT.kind != CXType_Invalid) {
541 CXString RS = clang_getTypeKindSpelling(RT.kind);
542 printf(" [result=%s]", clang_getCString(RS));
543 clang_disposeString(RS);
544 }
545 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000546 /* Print if this is a non-POD type. */
547 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000548
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000549 printf("\n");
550 }
551 return CXChildVisit_Recurse;
552}
553
554
555/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000556/* Loading ASTs/source. */
557/******************************************************************************/
558
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000559static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000560 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000561 CXCursorVisitor Visitor,
562 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000563
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000564 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000565 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000566
567 if (Visitor) {
568 enum CXCursorKind K = CXCursor_NotImplemented;
569 enum CXCursorKind *ck = &K;
570 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000571
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000572 /* Perform some simple filtering. */
573 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000574 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000575 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
576 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
577 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
578 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
579 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
580 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
581 else {
582 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
583 return 1;
584 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000585
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000586 Data.TU = TU;
587 Data.Filter = ck;
588 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000589 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000590
Ted Kremenekce2ae882010-01-26 17:59:48 +0000591 if (PV)
592 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000593
Douglas Gregora88084b2010-02-18 18:08:43 +0000594 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000595 clang_disposeTranslationUnit(TU);
596 return 0;
597}
598
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000599int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000600 const char *prefix, CXCursorVisitor Visitor,
601 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000602 CXIndex Idx;
603 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000604 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000605 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000606 !strcmp(filter, "local") ? 1 : 0,
607 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000608
Ted Kremenek020a0952010-02-11 07:41:25 +0000609 if (!CreateTranslationUnit(Idx, file, &TU)) {
610 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000611 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000612 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000613
Ted Kremenek020a0952010-02-11 07:41:25 +0000614 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
615 clang_disposeIndex(Idx);
616 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000617}
618
Ted Kremenekce2ae882010-01-26 17:59:48 +0000619int perform_test_load_source(int argc, const char **argv,
620 const char *filter, CXCursorVisitor Visitor,
621 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000622 const char *UseExternalASTs =
623 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000624 CXIndex Idx;
625 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000626 struct CXUnsavedFile *unsaved_files = 0;
627 int num_unsaved_files = 0;
628 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000629
Daniel Dunbarada487d2009-12-01 02:03:10 +0000630 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000631 !strcmp(filter, "local") ? 1 : 0,
632 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000633
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000634 if (UseExternalASTs && strlen(UseExternalASTs))
635 clang_setUseExternalASTGeneration(Idx, 1);
636
Ted Kremenek020a0952010-02-11 07:41:25 +0000637 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
638 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000639 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000640 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000641
Ted Kremeneke68fff62010-02-17 00:41:32 +0000642 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
643 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000644 argv + num_unsaved_files,
645 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000646 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000647 if (!TU) {
648 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000649 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000650 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000651 return 1;
652 }
653
Ted Kremenekce2ae882010-01-26 17:59:48 +0000654 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000655 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000656 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000657 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000658}
659
Douglas Gregorabc563f2010-07-19 21:46:24 +0000660int perform_test_reparse_source(int argc, const char **argv, int trials,
661 const char *filter, CXCursorVisitor Visitor,
662 PostVisitTU PV) {
663 const char *UseExternalASTs =
664 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
665 CXIndex Idx;
666 CXTranslationUnit TU;
667 struct CXUnsavedFile *unsaved_files = 0;
668 int num_unsaved_files = 0;
669 int result;
670 int trial;
671
672 Idx = clang_createIndex(/* excludeDeclsFromPCH */
673 !strcmp(filter, "local") ? 1 : 0,
674 /* displayDiagnosics=*/1);
675
676 if (UseExternalASTs && strlen(UseExternalASTs))
677 clang_setUseExternalASTGeneration(Idx, 1);
678
679 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
680 clang_disposeIndex(Idx);
681 return -1;
682 }
683
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000684 /* Load the initial translation unit -- we do this without honoring remapped
685 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000686 TU = clang_parseTranslationUnit(Idx, 0,
687 argv + num_unsaved_files,
688 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000689 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000690 if (!TU) {
691 fprintf(stderr, "Unable to load translation unit!\n");
692 free_remapped_files(unsaved_files, num_unsaved_files);
693 clang_disposeIndex(Idx);
694 return 1;
695 }
696
697 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000698 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
699 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000700 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000701 clang_disposeTranslationUnit(TU);
702 free_remapped_files(unsaved_files, num_unsaved_files);
703 clang_disposeIndex(Idx);
704 return -1;
705 }
706 }
707
708 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
709 free_remapped_files(unsaved_files, num_unsaved_files);
710 clang_disposeIndex(Idx);
711 return result;
712}
713
Ted Kremenek0d435192009-11-17 18:13:31 +0000714/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000715/* Logic for testing clang_getCursor(). */
716/******************************************************************************/
717
718static void print_cursor_file_scan(CXCursor cursor,
719 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000720 unsigned end_line, unsigned end_col,
721 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000722 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000723 if (prefix)
724 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000725 PrintExtent(stdout, start_line, start_col, end_line, end_col);
726 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000727 PrintCursor(cursor);
728 printf("\n");
729}
730
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000731static int perform_file_scan(const char *ast_file, const char *source_file,
732 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000733 CXIndex Idx;
734 CXTranslationUnit TU;
735 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000736 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000737 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000738 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000739 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000740
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000741 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
742 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000743 fprintf(stderr, "Could not create Index\n");
744 return 1;
745 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000746
Ted Kremenek1c6da172009-11-17 19:37:36 +0000747 if (!CreateTranslationUnit(Idx, ast_file, &TU))
748 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000749
Ted Kremenek1c6da172009-11-17 19:37:36 +0000750 if ((fp = fopen(source_file, "r")) == NULL) {
751 fprintf(stderr, "Could not open '%s'\n", source_file);
752 return 1;
753 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000754
Douglas Gregorb9790342010-01-22 21:44:22 +0000755 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000756 for (;;) {
757 CXCursor cursor;
758 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000759
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000760 if (c == '\n') {
761 ++line;
762 col = 1;
763 } else
764 ++col;
765
766 /* Check the cursor at this position, and dump the previous one if we have
767 * found something new.
768 */
769 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
770 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
771 prevCursor.kind != CXCursor_InvalidFile) {
772 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000773 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000774 start_line = line;
775 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000776 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000777 if (c == EOF)
778 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000779
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000780 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000781 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000782
Ted Kremenek1c6da172009-11-17 19:37:36 +0000783 fclose(fp);
784 return 0;
785}
786
787/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000788/* Logic for testing clang_codeComplete(). */
789/******************************************************************************/
790
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000791/* Parse file:line:column from the input string. Returns 0 on success, non-zero
792 on failure. If successful, the pointer *filename will contain newly-allocated
793 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000794int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000795 unsigned *column, unsigned *second_line,
796 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000797 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000798 const char *last_colon = strrchr(input, ':');
799 unsigned values[4], i;
800 unsigned num_values = (second_line && second_column)? 4 : 2;
801
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000802 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000803 if (!last_colon || last_colon == input) {
804 if (num_values == 4)
805 fprintf(stderr, "could not parse filename:line:column:line:column in "
806 "'%s'\n", input);
807 else
808 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000809 return 1;
810 }
811
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000812 for (i = 0; i != num_values; ++i) {
813 const char *prev_colon;
814
815 /* Parse the next line or column. */
816 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
817 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000818 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000819 (i % 2 ? "column" : "line"), input);
820 return 1;
821 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000822
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000823 if (i + 1 == num_values)
824 break;
825
826 /* Find the previous colon. */
827 prev_colon = last_colon - 1;
828 while (prev_colon != input && *prev_colon != ':')
829 --prev_colon;
830 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000831 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000832 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000833 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000834 }
835
836 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000837 }
838
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000839 *line = values[0];
840 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000841
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000842 if (second_line && second_column) {
843 *second_line = values[2];
844 *second_column = values[3];
845 }
846
Douglas Gregor88d23952009-11-09 18:19:57 +0000847 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000848 *filename = (char*)malloc(last_colon - input + 1);
849 memcpy(*filename, input, last_colon - input);
850 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000851 return 0;
852}
853
854const char *
855clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
856 switch (Kind) {
857 case CXCompletionChunk_Optional: return "Optional";
858 case CXCompletionChunk_TypedText: return "TypedText";
859 case CXCompletionChunk_Text: return "Text";
860 case CXCompletionChunk_Placeholder: return "Placeholder";
861 case CXCompletionChunk_Informative: return "Informative";
862 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
863 case CXCompletionChunk_LeftParen: return "LeftParen";
864 case CXCompletionChunk_RightParen: return "RightParen";
865 case CXCompletionChunk_LeftBracket: return "LeftBracket";
866 case CXCompletionChunk_RightBracket: return "RightBracket";
867 case CXCompletionChunk_LeftBrace: return "LeftBrace";
868 case CXCompletionChunk_RightBrace: return "RightBrace";
869 case CXCompletionChunk_LeftAngle: return "LeftAngle";
870 case CXCompletionChunk_RightAngle: return "RightAngle";
871 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000872 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000873 case CXCompletionChunk_Colon: return "Colon";
874 case CXCompletionChunk_SemiColon: return "SemiColon";
875 case CXCompletionChunk_Equal: return "Equal";
876 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
877 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000878 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000879
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000880 return "Unknown";
881}
882
Douglas Gregor3ac73852009-11-09 16:04:45 +0000883void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000884 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000885
Douglas Gregor3ac73852009-11-09 16:04:45 +0000886 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000887 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000888 CXString text;
889 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000890 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000891 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000892
Douglas Gregor3ac73852009-11-09 16:04:45 +0000893 if (Kind == CXCompletionChunk_Optional) {
894 fprintf(file, "{Optional ");
895 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000896 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000897 file);
898 fprintf(file, "}");
899 continue;
900 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000901
Douglas Gregord5a20892009-11-09 17:05:28 +0000902 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000903 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000904 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000905 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000906 cstr ? cstr : "");
907 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000908 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000909
Douglas Gregor3ac73852009-11-09 16:04:45 +0000910}
911
912void print_completion_result(CXCompletionResult *completion_result,
913 CXClientData client_data) {
914 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000915 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
916
917 fprintf(file, "%s:", clang_getCString(ks));
918 clang_disposeString(ks);
919
Douglas Gregor3ac73852009-11-09 16:04:45 +0000920 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000921 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000922 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000923 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
924 case CXAvailability_Available:
925 break;
926
927 case CXAvailability_Deprecated:
928 fprintf(file, " (deprecated)");
929 break;
930
931 case CXAvailability_NotAvailable:
932 fprintf(file, " (unavailable)");
933 break;
934 }
935 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000936}
937
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000938int my_stricmp(const char *s1, const char *s2) {
939 while (*s1 && *s2) {
940 int c1 = tolower(*s1), c2 = tolower(*s2);
941 if (c1 < c2)
942 return -1;
943 else if (c1 > c2)
944 return 1;
945
946 ++s1;
947 ++s2;
948 }
949
950 if (*s1)
951 return 1;
952 else if (*s2)
953 return -1;
954 return 0;
955}
956
Douglas Gregor1982c182010-07-12 18:38:41 +0000957int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000958 const char *input = argv[1];
959 char *filename = 0;
960 unsigned line;
961 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000962 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000963 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000964 struct CXUnsavedFile *unsaved_files = 0;
965 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000966 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000967 CXTranslationUnit *TU = 0;
968
Douglas Gregor1982c182010-07-12 18:38:41 +0000969 if (timing_only)
970 input += strlen("-code-completion-timing=");
971 else
972 input += strlen("-code-completion-at=");
973
Ted Kremeneke68fff62010-02-17 00:41:32 +0000974 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000975 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000976 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000977
Douglas Gregor735df882009-12-02 09:21:34 +0000978 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
979 return -1;
980
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000981 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000982 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000983 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +0000984 TU = clang_parseTranslationUnit(CIdx, 0,
985 argv + num_unsaved_files + 2,
986 argc - num_unsaved_files - 2,
Daniel Dunbar43f331d2010-08-19 23:44:04 +0000987 0, 0, getDefaultParsingOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000988 if (!TU) {
989 fprintf(stderr, "Unable to load translation unit!\n");
990 return 1;
991 }
Douglas Gregordf95a132010-08-09 20:45:32 +0000992 for (I = 0; I != Repeats; ++I) {
993 results = clang_codeCompleteAt(TU, filename, line, column,
994 unsaved_files, num_unsaved_files,
995 clang_defaultCodeCompleteOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +0000996 if (!results) {
997 fprintf(stderr, "Unable to perform code completion!\n");
998 return 1;
999 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001000 if (I != Repeats-1)
1001 clang_disposeCodeCompleteResults(results);
1002 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00001003 } else
1004 results = clang_codeComplete(CIdx,
1005 argv[argc - 1], argc - num_unsaved_files - 3,
1006 argv + num_unsaved_files + 2,
1007 num_unsaved_files, unsaved_files,
1008 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001009
Douglas Gregorec6762c2009-12-18 16:20:58 +00001010 if (results) {
1011 unsigned i, n = results->NumResults;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001012 if (!timing_only) {
1013 /* Sort the code-completion results based on the typed text. */
1014 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1015
Douglas Gregor1982c182010-07-12 18:38:41 +00001016 for (i = 0; i != n; ++i)
1017 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001018 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001019 n = clang_codeCompleteGetNumDiagnostics(results);
1020 for (i = 0; i != n; ++i) {
1021 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1022 PrintDiagnostic(diag);
1023 clang_disposeDiagnostic(diag);
1024 }
Douglas Gregorec6762c2009-12-18 16:20:58 +00001025 clang_disposeCodeCompleteResults(results);
1026 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001027 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001028 clang_disposeIndex(CIdx);
1029 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001030
Douglas Gregor735df882009-12-02 09:21:34 +00001031 free_remapped_files(unsaved_files, num_unsaved_files);
1032
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001033 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001034}
1035
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001036typedef struct {
1037 char *filename;
1038 unsigned line;
1039 unsigned column;
1040} CursorSourceLocation;
1041
1042int inspect_cursor_at(int argc, const char **argv) {
1043 CXIndex CIdx;
1044 int errorCode;
1045 struct CXUnsavedFile *unsaved_files = 0;
1046 int num_unsaved_files = 0;
1047 CXTranslationUnit TU;
1048 CXCursor Cursor;
1049 CursorSourceLocation *Locations = 0;
1050 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001051
Ted Kremeneke68fff62010-02-17 00:41:32 +00001052 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001053 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1054 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001055
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001056 /* Parse the locations. */
1057 assert(NumLocations > 0 && "Unable to count locations?");
1058 Locations = (CursorSourceLocation *)malloc(
1059 NumLocations * sizeof(CursorSourceLocation));
1060 for (Loc = 0; Loc < NumLocations; ++Loc) {
1061 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001062 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1063 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001064 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001065 return errorCode;
1066 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001067
1068 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001069 &num_unsaved_files))
1070 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001071
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001072 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001073 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1074 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001075 argv + num_unsaved_files + 1 + NumLocations,
1076 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001077 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001078 if (!TU) {
1079 fprintf(stderr, "unable to parse input\n");
1080 return -1;
1081 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001082
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001083 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001084 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1085 if (!file)
1086 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001087
1088 Cursor = clang_getCursor(TU,
1089 clang_getLocation(TU, file, Locations[Loc].line,
1090 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001091 PrintCursor(Cursor);
1092 printf("\n");
1093 free(Locations[Loc].filename);
1094 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001095
Douglas Gregora88084b2010-02-18 18:08:43 +00001096 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001097 clang_disposeTranslationUnit(TU);
1098 clang_disposeIndex(CIdx);
1099 free(Locations);
1100 free_remapped_files(unsaved_files, num_unsaved_files);
1101 return 0;
1102}
1103
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001104int perform_token_annotation(int argc, const char **argv) {
1105 const char *input = argv[1];
1106 char *filename = 0;
1107 unsigned line, second_line;
1108 unsigned column, second_column;
1109 CXIndex CIdx;
1110 CXTranslationUnit TU = 0;
1111 int errorCode;
1112 struct CXUnsavedFile *unsaved_files = 0;
1113 int num_unsaved_files = 0;
1114 CXToken *tokens;
1115 unsigned num_tokens;
1116 CXSourceRange range;
1117 CXSourceLocation startLoc, endLoc;
1118 CXFile file = 0;
1119 CXCursor *cursors = 0;
1120 unsigned i;
1121
1122 input += strlen("-test-annotate-tokens=");
1123 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1124 &second_line, &second_column)))
1125 return errorCode;
1126
1127 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1128 return -1;
1129
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001130 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001131 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1132 argc - num_unsaved_files - 3,
1133 argv + num_unsaved_files + 2,
1134 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001135 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001136 if (!TU) {
1137 fprintf(stderr, "unable to parse input\n");
1138 clang_disposeIndex(CIdx);
1139 free(filename);
1140 free_remapped_files(unsaved_files, num_unsaved_files);
1141 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001142 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001143 errorCode = 0;
1144
1145 file = clang_getFile(TU, filename);
1146 if (!file) {
1147 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1148 errorCode = -1;
1149 goto teardown;
1150 }
1151
1152 startLoc = clang_getLocation(TU, file, line, column);
1153 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001154 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001155 column);
1156 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001157 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001158 }
1159
1160 endLoc = clang_getLocation(TU, file, second_line, second_column);
1161 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001162 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001163 second_line, second_column);
1164 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001165 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001166 }
1167
1168 range = clang_getRange(startLoc, endLoc);
1169 clang_tokenize(TU, range, &tokens, &num_tokens);
1170 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1171 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1172 for (i = 0; i != num_tokens; ++i) {
1173 const char *kind = "<unknown>";
1174 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1175 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1176 unsigned start_line, start_column, end_line, end_column;
1177
1178 switch (clang_getTokenKind(tokens[i])) {
1179 case CXToken_Punctuation: kind = "Punctuation"; break;
1180 case CXToken_Keyword: kind = "Keyword"; break;
1181 case CXToken_Identifier: kind = "Identifier"; break;
1182 case CXToken_Literal: kind = "Literal"; break;
1183 case CXToken_Comment: kind = "Comment"; break;
1184 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001185 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001186 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001187 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001188 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001189 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1190 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001191 if (!clang_isInvalid(cursors[i].kind)) {
1192 printf(" ");
1193 PrintCursor(cursors[i]);
1194 }
1195 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001196 }
1197 free(cursors);
1198
1199 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001200 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001201 clang_disposeTranslationUnit(TU);
1202 clang_disposeIndex(CIdx);
1203 free(filename);
1204 free_remapped_files(unsaved_files, num_unsaved_files);
1205 return errorCode;
1206}
1207
Ted Kremenek0d435192009-11-17 18:13:31 +00001208/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001209/* USR printing. */
1210/******************************************************************************/
1211
1212static int insufficient_usr(const char *kind, const char *usage) {
1213 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1214 return 1;
1215}
1216
1217static unsigned isUSR(const char *s) {
1218 return s[0] == 'c' && s[1] == ':';
1219}
1220
1221static int not_usr(const char *s, const char *arg) {
1222 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1223 return 1;
1224}
1225
1226static void print_usr(CXString usr) {
1227 const char *s = clang_getCString(usr);
1228 printf("%s\n", s);
1229 clang_disposeString(usr);
1230}
1231
1232static void display_usrs() {
1233 fprintf(stderr, "-print-usrs options:\n"
1234 " ObjCCategory <class name> <category name>\n"
1235 " ObjCClass <class name>\n"
1236 " ObjCIvar <ivar name> <class USR>\n"
1237 " ObjCMethod <selector> [0=class method|1=instance method] "
1238 "<class USR>\n"
1239 " ObjCProperty <property name> <class USR>\n"
1240 " ObjCProtocol <protocol name>\n");
1241}
1242
1243int print_usrs(const char **I, const char **E) {
1244 while (I != E) {
1245 const char *kind = *I;
1246 unsigned len = strlen(kind);
1247 switch (len) {
1248 case 8:
1249 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1250 if (I + 2 >= E)
1251 return insufficient_usr(kind, "<ivar name> <class USR>");
1252 if (!isUSR(I[2]))
1253 return not_usr("<class USR>", I[2]);
1254 else {
1255 CXString x;
1256 x.Spelling = I[2];
1257 x.MustFreeString = 0;
1258 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1259 }
1260
1261 I += 3;
1262 continue;
1263 }
1264 break;
1265 case 9:
1266 if (memcmp(kind, "ObjCClass", 9) == 0) {
1267 if (I + 1 >= E)
1268 return insufficient_usr(kind, "<class name>");
1269 print_usr(clang_constructUSR_ObjCClass(I[1]));
1270 I += 2;
1271 continue;
1272 }
1273 break;
1274 case 10:
1275 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1276 if (I + 3 >= E)
1277 return insufficient_usr(kind, "<method selector> "
1278 "[0=class method|1=instance method] <class USR>");
1279 if (!isUSR(I[3]))
1280 return not_usr("<class USR>", I[3]);
1281 else {
1282 CXString x;
1283 x.Spelling = I[3];
1284 x.MustFreeString = 0;
1285 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1286 }
1287 I += 4;
1288 continue;
1289 }
1290 break;
1291 case 12:
1292 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1293 if (I + 2 >= E)
1294 return insufficient_usr(kind, "<class name> <category name>");
1295 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1296 I += 3;
1297 continue;
1298 }
1299 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1300 if (I + 1 >= E)
1301 return insufficient_usr(kind, "<protocol name>");
1302 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1303 I += 2;
1304 continue;
1305 }
1306 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1307 if (I + 2 >= E)
1308 return insufficient_usr(kind, "<property name> <class USR>");
1309 if (!isUSR(I[2]))
1310 return not_usr("<class USR>", I[2]);
1311 else {
1312 CXString x;
1313 x.Spelling = I[2];
1314 x.MustFreeString = 0;
1315 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1316 }
1317 I += 3;
1318 continue;
1319 }
1320 break;
1321 default:
1322 break;
1323 }
1324 break;
1325 }
1326
1327 if (I != E) {
1328 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1329 display_usrs();
1330 return 1;
1331 }
1332 return 0;
1333}
1334
1335int print_usrs_file(const char *file_name) {
1336 char line[2048];
1337 const char *args[128];
1338 unsigned numChars = 0;
1339
1340 FILE *fp = fopen(file_name, "r");
1341 if (!fp) {
1342 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1343 return 1;
1344 }
1345
1346 /* This code is not really all that safe, but it works fine for testing. */
1347 while (!feof(fp)) {
1348 char c = fgetc(fp);
1349 if (c == '\n') {
1350 unsigned i = 0;
1351 const char *s = 0;
1352
1353 if (numChars == 0)
1354 continue;
1355
1356 line[numChars] = '\0';
1357 numChars = 0;
1358
1359 if (line[0] == '/' && line[1] == '/')
1360 continue;
1361
1362 s = strtok(line, " ");
1363 while (s) {
1364 args[i] = s;
1365 ++i;
1366 s = strtok(0, " ");
1367 }
1368 if (print_usrs(&args[0], &args[i]))
1369 return 1;
1370 }
1371 else
1372 line[numChars++] = c;
1373 }
1374
1375 fclose(fp);
1376 return 0;
1377}
1378
1379/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001380/* Command line processing. */
1381/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001382int write_pch_file(const char *filename, int argc, const char *argv[]) {
1383 CXIndex Idx;
1384 CXTranslationUnit TU;
1385 struct CXUnsavedFile *unsaved_files = 0;
1386 int num_unsaved_files = 0;
1387
1388 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1389
1390 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1391 clang_disposeIndex(Idx);
1392 return -1;
1393 }
1394
1395 TU = clang_parseTranslationUnit(Idx, 0,
1396 argv + num_unsaved_files,
1397 argc - num_unsaved_files,
1398 unsaved_files,
1399 num_unsaved_files,
1400 CXTranslationUnit_Incomplete);
1401 if (!TU) {
1402 fprintf(stderr, "Unable to load translation unit!\n");
1403 free_remapped_files(unsaved_files, num_unsaved_files);
1404 clang_disposeIndex(Idx);
1405 return 1;
1406 }
1407
Douglas Gregor19998442010-08-13 15:35:05 +00001408 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001409 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1410 clang_disposeTranslationUnit(TU);
1411 free_remapped_files(unsaved_files, num_unsaved_files);
1412 clang_disposeIndex(Idx);
1413 return 0;
1414}
1415
1416/******************************************************************************/
1417/* Command line processing. */
1418/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001419
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001420static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001421 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001422 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001423 if (strcmp(s, "-usrs") == 0)
1424 return USRVisitor;
1425 return NULL;
1426}
1427
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001428static void print_usage(void) {
1429 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001430 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001431 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001432 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001433 " c-index-test -test-file-scan <AST file> <source file> "
1434 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001435 " c-index-test -test-load-tu <AST file> <symbol filter> "
1436 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001437 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1438 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001439 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001440 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001441 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1442 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001443 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001444 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1445 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001446 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001447 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001448 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001449 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1450 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001451 " c-index-test -print-usr-file <file>\n"
1452 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001453 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001454 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001455 " all - load all symbols, including those from PCH\n"
1456 " local - load all symbols except those in PCH\n"
1457 " category - only load ObjC categories (non-PCH)\n"
1458 " interface - only load ObjC interfaces (non-PCH)\n"
1459 " protocol - only load ObjC protocols (non-PCH)\n"
1460 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001461 " typedef - only load typdefs (non-PCH)\n"
1462 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001463}
1464
1465int main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001466 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001467 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001468 return perform_code_completion(argc, argv, 0);
1469 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1470 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001471 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1472 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001473 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001474 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001475 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001476 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1477 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001478 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001479 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1480 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1481 if (I) {
1482 int trials = atoi(argv[2]);
1483 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1484 NULL);
1485 }
1486 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001487 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001488 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001489 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001490 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001491 }
1492 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001493 return perform_file_scan(argv[2], argv[3],
1494 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001495 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1496 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001497 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1498 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1499 PrintInclusionStack);
1500 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1501 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1502 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001503 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1504 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1505 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001506 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1507 return perform_test_load_source(argc - 2, argv + 2, "all",
1508 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001509 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1510 if (argc > 2)
1511 return print_usrs(argv + 2, argv + argc);
1512 else {
1513 display_usrs();
1514 return 1;
1515 }
1516 }
1517 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1518 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001519 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1520 return write_pch_file(argv[2], argc - 3, argv + 3);
1521
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001522 print_usage();
1523 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001524}