blob: 4d4e117620830a107e210d4499c42c758b354b2d [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())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000180 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
181 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
182 printf("[");
183 for (I = 0; I != N; ++I) {
184 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000185 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000186 if (I)
187 printf(", ");
188
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000189 Loc = clang_getCursorLocation(Ovl);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000190 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
191 printf("%d:%d", line, column);
192 }
193 printf("]");
194 } else {
195 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
196 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
197 printf(":%d:%d", line, column);
198 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000199 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000200
201 if (clang_isCursorDefinition(Cursor))
202 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000203
204 switch (clang_getCursorAvailability(Cursor)) {
205 case CXAvailability_Available:
206 break;
207
208 case CXAvailability_Deprecated:
209 printf(" (deprecated)");
210 break;
211
212 case CXAvailability_NotAvailable:
213 printf(" (unavailable)");
214 break;
215 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000216
217 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
218 CXType T =
219 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
220 CXString S = clang_getTypeKindSpelling(T.kind);
221 printf(" [IBOutletCollection=%s]", clang_getCString(S));
222 clang_disposeString(S);
223 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000224
225 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
226 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
227 unsigned isVirtual = clang_isVirtualBase(Cursor);
228 const char *accessStr = 0;
229
230 switch (access) {
231 case CX_CXXInvalidAccessSpecifier:
232 accessStr = "invalid"; break;
233 case CX_CXXPublic:
234 accessStr = "public"; break;
235 case CX_CXXProtected:
236 accessStr = "protected"; break;
237 case CX_CXXPrivate:
238 accessStr = "private"; break;
239 }
240
241 printf(" [access=%s isVirtual=%s]", accessStr,
242 isVirtual ? "true" : "false");
243 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000244
245 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
246 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
247 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
248 CXString Name = clang_getCursorSpelling(SpecializationOf);
249 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
250 printf(" [Specialization of %s:%d:%d]",
251 clang_getCString(Name), line, column);
252 clang_disposeString(Name);
253 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000254 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000255}
Steve Naroff89922f82009-08-31 00:59:03 +0000256
Ted Kremeneke68fff62010-02-17 00:41:32 +0000257static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000258 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000259 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000260 CXFile file;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000261 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000262 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000263 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000264 clang_disposeString(source);
265 return "<invalid loc>";
266 }
267 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000268 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000269 clang_disposeString(source);
270 return b;
271 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000272}
273
Ted Kremenek0d435192009-11-17 18:13:31 +0000274/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000275/* Callbacks. */
276/******************************************************************************/
277
278typedef void (*PostVisitTU)(CXTranslationUnit);
279
Douglas Gregora88084b2010-02-18 18:08:43 +0000280void PrintDiagnostic(CXDiagnostic Diagnostic) {
281 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000282 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000283 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000284 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
285 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges;
286 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000287
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000288 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000289 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000290
Douglas Gregor274f1902010-02-22 23:17:23 +0000291 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
292 fprintf(stderr, "%s\n", clang_getCString(Msg));
293 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000294
Douglas Gregor5352ac02010-01-28 00:27:43 +0000295 clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic),
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000296 &file, 0, 0, 0);
297 if (!file)
298 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000299
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000300 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
301 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000302 CXSourceRange range;
303 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
304 CXSourceLocation start = clang_getRangeStart(range);
305 CXSourceLocation end = clang_getRangeEnd(range);
306 unsigned start_line, start_column, end_line, end_column;
307 CXFile start_file, end_file;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000308 clang_getInstantiationLocation(start, &start_file, &start_line,
Douglas Gregor473d7012010-02-19 18:16:06 +0000309 &start_column, 0);
310 clang_getInstantiationLocation(end, &end_file, &end_line, &end_column, 0);
311 if (clang_equalLocations(start, end)) {
312 /* Insertion. */
313 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000314 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000315 clang_getCString(insertion_text), start_line, start_column);
316 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
317 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000318 if (start_file == file && end_file == file) {
319 fprintf(out, "FIX-IT: Remove ");
320 PrintExtent(out, start_line, start_column, end_line, end_column);
321 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000322 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000323 } else {
324 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000325 if (start_file == end_file) {
326 fprintf(out, "FIX-IT: Replace ");
327 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000328 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000329 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000330 break;
331 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000332 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000333 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000334}
335
Douglas Gregora88084b2010-02-18 18:08:43 +0000336void PrintDiagnostics(CXTranslationUnit TU) {
337 int i, n = clang_getNumDiagnostics(TU);
338 for (i = 0; i != n; ++i) {
339 CXDiagnostic Diag = clang_getDiagnostic(TU, i);
340 PrintDiagnostic(Diag);
341 clang_disposeDiagnostic(Diag);
342 }
343}
344
Ted Kremenekce2ae882010-01-26 17:59:48 +0000345/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000346/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000347/******************************************************************************/
348
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000349static const char *FileCheckPrefix = "CHECK";
350
Douglas Gregora7bde202010-01-19 00:34:46 +0000351static void PrintCursorExtent(CXCursor C) {
352 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000353 CXFile begin_file, end_file;
354 unsigned begin_line, begin_column, end_line, end_column;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000355
Douglas Gregor1db19de2010-01-19 21:36:55 +0000356 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000357 &begin_file, &begin_line, &begin_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000358 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +0000359 &end_file, &end_line, &end_column, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000360 if (!begin_file || !end_file)
Ted Kremenek70ee5422010-01-16 01:44:12 +0000361 return;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000362
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000363 printf(" Extent=");
364 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000365}
366
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000367/* Data used by all of the visitors. */
368typedef struct {
369 CXTranslationUnit TU;
370 enum CXCursorKind *Filter;
371} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000372
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000373
Ted Kremeneke68fff62010-02-17 00:41:32 +0000374enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000375 CXCursor Parent,
376 CXClientData ClientData) {
377 VisitorData *Data = (VisitorData *)ClientData;
378 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000379 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000380 unsigned line, column;
Douglas Gregor46766dc2010-01-26 19:19:08 +0000381 clang_getInstantiationLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000382 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000383 GetCursorSource(Cursor), line, column);
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000384 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000385 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000386 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000387 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000388 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000389
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000390 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000391}
Steve Naroff50398192009-08-28 15:28:48 +0000392
Ted Kremeneke68fff62010-02-17 00:41:32 +0000393static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000394 CXCursor Parent,
395 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000396 const char *startBuf, *endBuf;
397 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
398 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000399 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000400
Douglas Gregorb6998662010-01-19 19:34:47 +0000401 if (Cursor.kind != CXCursor_FunctionDecl ||
402 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000403 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000404
405 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
406 &startLine, &startColumn,
407 &endLine, &endColumn);
408 /* Probe the entire body, looking for both decls and refs. */
409 curLine = startLine;
410 curColumn = startColumn;
411
412 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000413 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000414 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000415 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000416
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000417 if (*startBuf == '\n') {
418 startBuf++;
419 curLine++;
420 curColumn = 1;
421 } else if (*startBuf != '\t')
422 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000423
Douglas Gregor98258af2010-01-18 22:46:11 +0000424 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor46766dc2010-01-26 19:19:08 +0000425 clang_getInstantiationLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000426
Douglas Gregor1db19de2010-01-19 21:36:55 +0000427 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000428 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000429 CXSourceLocation RefLoc
430 = clang_getLocation(Data->TU, file, curLine, curColumn);
431 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000432 if (Ref.kind == CXCursor_NoDeclFound) {
433 /* Nothing found here; that's fine. */
434 } else if (Ref.kind != CXCursor_FunctionDecl) {
435 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
436 curLine, curColumn);
437 PrintCursor(Ref);
438 printf("\n");
439 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000440 }
Ted Kremenek74844072010-02-17 00:41:20 +0000441 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000442 startBuf++;
443 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000444
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000445 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000446}
447
Ted Kremenek7d405622010-01-12 23:34:26 +0000448/******************************************************************************/
449/* USR testing. */
450/******************************************************************************/
451
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000452enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
453 CXClientData ClientData) {
454 VisitorData *Data = (VisitorData *)ClientData;
455 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000456 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000457 const char *cstr = clang_getCString(USR);
458 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000459 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000460 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000461 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000462 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
463
Douglas Gregora7bde202010-01-19 00:34:46 +0000464 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000465 printf("\n");
466 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000467
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000468 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000469 }
470
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000471 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000472}
473
474/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000475/* Inclusion stack testing. */
476/******************************************************************************/
477
478void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
479 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000480
Ted Kremenek16b55a72010-01-26 19:31:51 +0000481 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000482 CXString fname;
483
484 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000485 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000486 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000487
Ted Kremenek16b55a72010-01-26 19:31:51 +0000488 for (i = 0; i < includeStackLen; ++i) {
489 CXFile includingFile;
490 unsigned line, column;
491 clang_getInstantiationLocation(includeStack[i], &includingFile, &line,
492 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000493 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000494 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000495 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000496 }
497 printf("\n");
498}
499
500void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000501 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000502}
503
504/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000505/* Linkage testing. */
506/******************************************************************************/
507
508static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
509 CXClientData d) {
510 const char *linkage = 0;
511
512 if (clang_isInvalid(clang_getCursorKind(cursor)))
513 return CXChildVisit_Recurse;
514
515 switch (clang_getCursorLinkage(cursor)) {
516 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000517 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
518 case CXLinkage_Internal: linkage = "Internal"; break;
519 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
520 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000521 }
522
523 if (linkage) {
524 PrintCursor(cursor);
525 printf("linkage=%s\n", linkage);
526 }
527
528 return CXChildVisit_Recurse;
529}
530
531/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000532/* Typekind testing. */
533/******************************************************************************/
534
535static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
536 CXClientData d) {
537
538 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
539 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000540 CXString S = clang_getTypeKindSpelling(T.kind);
541 PrintCursor(cursor);
542 printf(" typekind=%s", clang_getCString(S));
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000543 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000544 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000545 {
546 CXType CT = clang_getCanonicalType(T);
547 if (!clang_equalTypes(T, CT)) {
548 CXString CS = clang_getTypeKindSpelling(CT.kind);
549 printf(" [canonical=%s]", clang_getCString(CS));
550 clang_disposeString(CS);
551 }
552 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000553 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000554 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000555 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000556 if (RT.kind != CXType_Invalid) {
557 CXString RS = clang_getTypeKindSpelling(RT.kind);
558 printf(" [result=%s]", clang_getCString(RS));
559 clang_disposeString(RS);
560 }
561 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000562 /* Print if this is a non-POD type. */
563 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000564
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000565 printf("\n");
566 }
567 return CXChildVisit_Recurse;
568}
569
570
571/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000572/* Loading ASTs/source. */
573/******************************************************************************/
574
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000575static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000576 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000577 CXCursorVisitor Visitor,
578 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000580 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +0000581 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000582
583 if (Visitor) {
584 enum CXCursorKind K = CXCursor_NotImplemented;
585 enum CXCursorKind *ck = &K;
586 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000587
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000588 /* Perform some simple filtering. */
589 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Daniel Dunbarb1ffee62010-02-10 20:42:40 +0000590 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000591 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
592 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
593 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
594 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
595 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
596 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
597 else {
598 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
599 return 1;
600 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000601
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000602 Data.TU = TU;
603 Data.Filter = ck;
604 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +0000605 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000606
Ted Kremenekce2ae882010-01-26 17:59:48 +0000607 if (PV)
608 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +0000609
Douglas Gregora88084b2010-02-18 18:08:43 +0000610 PrintDiagnostics(TU);
Ted Kremenek0d435192009-11-17 18:13:31 +0000611 clang_disposeTranslationUnit(TU);
612 return 0;
613}
614
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000615int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +0000616 const char *prefix, CXCursorVisitor Visitor,
617 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000618 CXIndex Idx;
619 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +0000620 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000621 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000622 !strcmp(filter, "local") ? 1 : 0,
623 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000624
Ted Kremenek020a0952010-02-11 07:41:25 +0000625 if (!CreateTranslationUnit(Idx, file, &TU)) {
626 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000627 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000628 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000629
Ted Kremenek020a0952010-02-11 07:41:25 +0000630 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
631 clang_disposeIndex(Idx);
632 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000633}
634
Ted Kremenekce2ae882010-01-26 17:59:48 +0000635int perform_test_load_source(int argc, const char **argv,
636 const char *filter, CXCursorVisitor Visitor,
637 PostVisitTU PV) {
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000638 const char *UseExternalASTs =
639 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
Daniel Dunbarada487d2009-12-01 02:03:10 +0000640 CXIndex Idx;
641 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +0000642 struct CXUnsavedFile *unsaved_files = 0;
643 int num_unsaved_files = 0;
644 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +0000645
Daniel Dunbarada487d2009-12-01 02:03:10 +0000646 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000647 !strcmp(filter, "local") ? 1 : 0,
648 /* displayDiagnosics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000649
Daniel Dunbar8506dde2009-12-03 01:54:28 +0000650 if (UseExternalASTs && strlen(UseExternalASTs))
651 clang_setUseExternalASTGeneration(Idx, 1);
652
Ted Kremenek020a0952010-02-11 07:41:25 +0000653 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
654 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000655 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +0000656 }
Douglas Gregor4db64a42010-01-23 00:14:00 +0000657
Ted Kremeneke68fff62010-02-17 00:41:32 +0000658 TU = clang_createTranslationUnitFromSourceFile(Idx, 0,
659 argc - num_unsaved_files,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000660 argv + num_unsaved_files,
661 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +0000662 unsaved_files);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000663 if (!TU) {
664 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000665 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000666 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +0000667 return 1;
668 }
669
Ted Kremenekce2ae882010-01-26 17:59:48 +0000670 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000671 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +0000672 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000673 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +0000674}
675
Douglas Gregorabc563f2010-07-19 21:46:24 +0000676int perform_test_reparse_source(int argc, const char **argv, int trials,
677 const char *filter, CXCursorVisitor Visitor,
678 PostVisitTU PV) {
679 const char *UseExternalASTs =
680 getenv("CINDEXTEST_USE_EXTERNAL_AST_GENERATION");
681 CXIndex Idx;
682 CXTranslationUnit TU;
683 struct CXUnsavedFile *unsaved_files = 0;
684 int num_unsaved_files = 0;
685 int result;
686 int trial;
687
688 Idx = clang_createIndex(/* excludeDeclsFromPCH */
689 !strcmp(filter, "local") ? 1 : 0,
690 /* displayDiagnosics=*/1);
691
692 if (UseExternalASTs && strlen(UseExternalASTs))
693 clang_setUseExternalASTGeneration(Idx, 1);
694
695 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
696 clang_disposeIndex(Idx);
697 return -1;
698 }
699
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000700 /* Load the initial translation unit -- we do this without honoring remapped
701 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +0000702 TU = clang_parseTranslationUnit(Idx, 0,
703 argv + num_unsaved_files,
704 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000705 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +0000706 if (!TU) {
707 fprintf(stderr, "Unable to load translation unit!\n");
708 free_remapped_files(unsaved_files, num_unsaved_files);
709 clang_disposeIndex(Idx);
710 return 1;
711 }
712
713 for (trial = 0; trial < trials; ++trial) {
Douglas Gregore1e13bf2010-08-11 15:58:42 +0000714 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
715 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +0000716 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +0000717 clang_disposeTranslationUnit(TU);
718 free_remapped_files(unsaved_files, num_unsaved_files);
719 clang_disposeIndex(Idx);
720 return -1;
721 }
722 }
723
724 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
725 free_remapped_files(unsaved_files, num_unsaved_files);
726 clang_disposeIndex(Idx);
727 return result;
728}
729
Ted Kremenek0d435192009-11-17 18:13:31 +0000730/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +0000731/* Logic for testing clang_getCursor(). */
732/******************************************************************************/
733
734static void print_cursor_file_scan(CXCursor cursor,
735 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000736 unsigned end_line, unsigned end_col,
737 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +0000738 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000739 if (prefix)
740 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +0000741 PrintExtent(stdout, start_line, start_col, end_line, end_col);
742 printf(" ");
Ted Kremenek1c6da172009-11-17 19:37:36 +0000743 PrintCursor(cursor);
744 printf("\n");
745}
746
Ted Kremenek1d5fdf32009-11-18 02:02:52 +0000747static int perform_file_scan(const char *ast_file, const char *source_file,
748 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000749 CXIndex Idx;
750 CXTranslationUnit TU;
751 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000752 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +0000753 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000754 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +0000755 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000756
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000757 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
758 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +0000759 fprintf(stderr, "Could not create Index\n");
760 return 1;
761 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000762
Ted Kremenek1c6da172009-11-17 19:37:36 +0000763 if (!CreateTranslationUnit(Idx, ast_file, &TU))
764 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000765
Ted Kremenek1c6da172009-11-17 19:37:36 +0000766 if ((fp = fopen(source_file, "r")) == NULL) {
767 fprintf(stderr, "Could not open '%s'\n", source_file);
768 return 1;
769 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000770
Douglas Gregorb9790342010-01-22 21:44:22 +0000771 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000772 for (;;) {
773 CXCursor cursor;
774 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +0000775
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000776 if (c == '\n') {
777 ++line;
778 col = 1;
779 } else
780 ++col;
781
782 /* Check the cursor at this position, and dump the previous one if we have
783 * found something new.
784 */
785 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
786 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
787 prevCursor.kind != CXCursor_InvalidFile) {
788 print_cursor_file_scan(prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +0000789 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000790 start_line = line;
791 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000792 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000793 if (c == EOF)
794 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +0000795
Daniel Dunbar2389eff2010-02-14 08:32:32 +0000796 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +0000797 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000798
Ted Kremenek1c6da172009-11-17 19:37:36 +0000799 fclose(fp);
800 return 0;
801}
802
803/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +0000804/* Logic for testing clang_codeComplete(). */
805/******************************************************************************/
806
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000807/* Parse file:line:column from the input string. Returns 0 on success, non-zero
808 on failure. If successful, the pointer *filename will contain newly-allocated
809 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +0000810int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000811 unsigned *column, unsigned *second_line,
812 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +0000813 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000814 const char *last_colon = strrchr(input, ':');
815 unsigned values[4], i;
816 unsigned num_values = (second_line && second_column)? 4 : 2;
817
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000818 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000819 if (!last_colon || last_colon == input) {
820 if (num_values == 4)
821 fprintf(stderr, "could not parse filename:line:column:line:column in "
822 "'%s'\n", input);
823 else
824 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000825 return 1;
826 }
827
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000828 for (i = 0; i != num_values; ++i) {
829 const char *prev_colon;
830
831 /* Parse the next line or column. */
832 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
833 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000834 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000835 (i % 2 ? "column" : "line"), input);
836 return 1;
837 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000838
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000839 if (i + 1 == num_values)
840 break;
841
842 /* Find the previous colon. */
843 prev_colon = last_colon - 1;
844 while (prev_colon != input && *prev_colon != ':')
845 --prev_colon;
846 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000847 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000848 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000849 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000850 }
851
852 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +0000853 }
854
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000855 *line = values[0];
856 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +0000857
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000858 if (second_line && second_column) {
859 *second_line = values[2];
860 *second_column = values[3];
861 }
862
Douglas Gregor88d23952009-11-09 18:19:57 +0000863 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000864 *filename = (char*)malloc(last_colon - input + 1);
865 memcpy(*filename, input, last_colon - input);
866 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000867 return 0;
868}
869
870const char *
871clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
872 switch (Kind) {
873 case CXCompletionChunk_Optional: return "Optional";
874 case CXCompletionChunk_TypedText: return "TypedText";
875 case CXCompletionChunk_Text: return "Text";
876 case CXCompletionChunk_Placeholder: return "Placeholder";
877 case CXCompletionChunk_Informative: return "Informative";
878 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
879 case CXCompletionChunk_LeftParen: return "LeftParen";
880 case CXCompletionChunk_RightParen: return "RightParen";
881 case CXCompletionChunk_LeftBracket: return "LeftBracket";
882 case CXCompletionChunk_RightBracket: return "RightBracket";
883 case CXCompletionChunk_LeftBrace: return "LeftBrace";
884 case CXCompletionChunk_RightBrace: return "RightBrace";
885 case CXCompletionChunk_LeftAngle: return "LeftAngle";
886 case CXCompletionChunk_RightAngle: return "RightAngle";
887 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +0000888 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +0000889 case CXCompletionChunk_Colon: return "Colon";
890 case CXCompletionChunk_SemiColon: return "SemiColon";
891 case CXCompletionChunk_Equal: return "Equal";
892 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
893 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000894 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000895
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000896 return "Unknown";
897}
898
Douglas Gregor3ac73852009-11-09 16:04:45 +0000899void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000900 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000901
Douglas Gregor3ac73852009-11-09 16:04:45 +0000902 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000903 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000904 CXString text;
905 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000906 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +0000907 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000908
Douglas Gregor3ac73852009-11-09 16:04:45 +0000909 if (Kind == CXCompletionChunk_Optional) {
910 fprintf(file, "{Optional ");
911 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +0000912 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +0000913 file);
914 fprintf(file, "}");
915 continue;
916 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000917
Douglas Gregord5a20892009-11-09 17:05:28 +0000918 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000919 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000920 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000921 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000922 cstr ? cstr : "");
923 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000924 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +0000925
Douglas Gregor3ac73852009-11-09 16:04:45 +0000926}
927
928void print_completion_result(CXCompletionResult *completion_result,
929 CXClientData client_data) {
930 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000931 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
932
933 fprintf(file, "%s:", clang_getCString(ks));
934 clang_disposeString(ks);
935
Douglas Gregor3ac73852009-11-09 16:04:45 +0000936 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +0000937 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +0000938 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +0000939 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
940 case CXAvailability_Available:
941 break;
942
943 case CXAvailability_Deprecated:
944 fprintf(file, " (deprecated)");
945 break;
946
947 case CXAvailability_NotAvailable:
948 fprintf(file, " (unavailable)");
949 break;
950 }
951 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000952}
953
Douglas Gregor1e5e6682010-08-26 13:48:20 +0000954int my_stricmp(const char *s1, const char *s2) {
955 while (*s1 && *s2) {
956 int c1 = tolower(*s1), c2 = tolower(*s2);
957 if (c1 < c2)
958 return -1;
959 else if (c1 > c2)
960 return 1;
961
962 ++s1;
963 ++s2;
964 }
965
966 if (*s1)
967 return 1;
968 else if (*s2)
969 return -1;
970 return 0;
971}
972
Douglas Gregor1982c182010-07-12 18:38:41 +0000973int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000974 const char *input = argv[1];
975 char *filename = 0;
976 unsigned line;
977 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +0000978 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000979 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +0000980 struct CXUnsavedFile *unsaved_files = 0;
981 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +0000982 CXCodeCompleteResults *results = 0;
Douglas Gregordf95a132010-08-09 20:45:32 +0000983 CXTranslationUnit *TU = 0;
984
Douglas Gregor1982c182010-07-12 18:38:41 +0000985 if (timing_only)
986 input += strlen("-code-completion-timing=");
987 else
988 input += strlen("-code-completion-at=");
989
Ted Kremeneke68fff62010-02-17 00:41:32 +0000990 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +0000991 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +0000992 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +0000993
Douglas Gregor735df882009-12-02 09:21:34 +0000994 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
995 return -1;
996
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000997 CIdx = clang_createIndex(0, 1);
Douglas Gregor1abc6bc2010-08-04 16:47:14 +0000998 if (getenv("CINDEXTEST_EDITING")) {
Daniel Dunbard91de2b2010-08-09 21:06:06 +0000999 unsigned I, Repeats = 5;
Douglas Gregordf95a132010-08-09 20:45:32 +00001000 TU = clang_parseTranslationUnit(CIdx, 0,
1001 argv + num_unsaved_files + 2,
1002 argc - num_unsaved_files - 2,
Daniel Dunbar43f331d2010-08-19 23:44:04 +00001003 0, 0, getDefaultParsingOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001004 if (!TU) {
1005 fprintf(stderr, "Unable to load translation unit!\n");
1006 return 1;
1007 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001008 for (I = 0; I != Repeats; ++I) {
1009 results = clang_codeCompleteAt(TU, filename, line, column,
1010 unsaved_files, num_unsaved_files,
1011 clang_defaultCodeCompleteOptions());
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001012 if (!results) {
1013 fprintf(stderr, "Unable to perform code completion!\n");
1014 return 1;
1015 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001016 if (I != Repeats-1)
1017 clang_disposeCodeCompleteResults(results);
1018 }
Douglas Gregor1abc6bc2010-08-04 16:47:14 +00001019 } else
1020 results = clang_codeComplete(CIdx,
1021 argv[argc - 1], argc - num_unsaved_files - 3,
1022 argv + num_unsaved_files + 2,
1023 num_unsaved_files, unsaved_files,
1024 filename, line, column);
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001025
Douglas Gregorec6762c2009-12-18 16:20:58 +00001026 if (results) {
1027 unsigned i, n = results->NumResults;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001028 if (!timing_only) {
1029 /* Sort the code-completion results based on the typed text. */
1030 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1031
Douglas Gregor1982c182010-07-12 18:38:41 +00001032 for (i = 0; i != n; ++i)
1033 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001034 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001035 n = clang_codeCompleteGetNumDiagnostics(results);
1036 for (i = 0; i != n; ++i) {
1037 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1038 PrintDiagnostic(diag);
1039 clang_disposeDiagnostic(diag);
1040 }
Douglas Gregorec6762c2009-12-18 16:20:58 +00001041 clang_disposeCodeCompleteResults(results);
1042 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001043 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001044 clang_disposeIndex(CIdx);
1045 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001046
Douglas Gregor735df882009-12-02 09:21:34 +00001047 free_remapped_files(unsaved_files, num_unsaved_files);
1048
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001049 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001050}
1051
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001052typedef struct {
1053 char *filename;
1054 unsigned line;
1055 unsigned column;
1056} CursorSourceLocation;
1057
1058int inspect_cursor_at(int argc, const char **argv) {
1059 CXIndex CIdx;
1060 int errorCode;
1061 struct CXUnsavedFile *unsaved_files = 0;
1062 int num_unsaved_files = 0;
1063 CXTranslationUnit TU;
1064 CXCursor Cursor;
1065 CursorSourceLocation *Locations = 0;
1066 unsigned NumLocations = 0, Loc;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001067
Ted Kremeneke68fff62010-02-17 00:41:32 +00001068 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001069 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1070 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001071
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001072 /* Parse the locations. */
1073 assert(NumLocations > 0 && "Unable to count locations?");
1074 Locations = (CursorSourceLocation *)malloc(
1075 NumLocations * sizeof(CursorSourceLocation));
1076 for (Loc = 0; Loc < NumLocations; ++Loc) {
1077 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001078 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1079 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001080 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001081 return errorCode;
1082 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001083
1084 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001085 &num_unsaved_files))
1086 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001087
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001088 CIdx = clang_createIndex(0, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001089 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1090 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor4db64a42010-01-23 00:14:00 +00001091 argv + num_unsaved_files + 1 + NumLocations,
1092 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001093 unsaved_files);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001094 if (!TU) {
1095 fprintf(stderr, "unable to parse input\n");
1096 return -1;
1097 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001098
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001099 for (Loc = 0; Loc < NumLocations; ++Loc) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001100 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1101 if (!file)
1102 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001103
1104 Cursor = clang_getCursor(TU,
1105 clang_getLocation(TU, file, Locations[Loc].line,
1106 Locations[Loc].column));
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001107 PrintCursor(Cursor);
1108 printf("\n");
1109 free(Locations[Loc].filename);
1110 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001111
Douglas Gregora88084b2010-02-18 18:08:43 +00001112 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001113 clang_disposeTranslationUnit(TU);
1114 clang_disposeIndex(CIdx);
1115 free(Locations);
1116 free_remapped_files(unsaved_files, num_unsaved_files);
1117 return 0;
1118}
1119
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001120int perform_token_annotation(int argc, const char **argv) {
1121 const char *input = argv[1];
1122 char *filename = 0;
1123 unsigned line, second_line;
1124 unsigned column, second_column;
1125 CXIndex CIdx;
1126 CXTranslationUnit TU = 0;
1127 int errorCode;
1128 struct CXUnsavedFile *unsaved_files = 0;
1129 int num_unsaved_files = 0;
1130 CXToken *tokens;
1131 unsigned num_tokens;
1132 CXSourceRange range;
1133 CXSourceLocation startLoc, endLoc;
1134 CXFile file = 0;
1135 CXCursor *cursors = 0;
1136 unsigned i;
1137
1138 input += strlen("-test-annotate-tokens=");
1139 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
1140 &second_line, &second_column)))
1141 return errorCode;
1142
1143 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1144 return -1;
1145
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001146 CIdx = clang_createIndex(0, 1);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001147 TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1],
1148 argc - num_unsaved_files - 3,
1149 argv + num_unsaved_files + 2,
1150 num_unsaved_files,
Douglas Gregora88084b2010-02-18 18:08:43 +00001151 unsaved_files);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001152 if (!TU) {
1153 fprintf(stderr, "unable to parse input\n");
1154 clang_disposeIndex(CIdx);
1155 free(filename);
1156 free_remapped_files(unsaved_files, num_unsaved_files);
1157 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001158 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001159 errorCode = 0;
1160
1161 file = clang_getFile(TU, filename);
1162 if (!file) {
1163 fprintf(stderr, "file %s is not in this translation unit\n", filename);
1164 errorCode = -1;
1165 goto teardown;
1166 }
1167
1168 startLoc = clang_getLocation(TU, file, line, column);
1169 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001170 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001171 column);
1172 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001173 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001174 }
1175
1176 endLoc = clang_getLocation(TU, file, second_line, second_column);
1177 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001178 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001179 second_line, second_column);
1180 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001181 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001182 }
1183
1184 range = clang_getRange(startLoc, endLoc);
1185 clang_tokenize(TU, range, &tokens, &num_tokens);
1186 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
1187 clang_annotateTokens(TU, tokens, num_tokens, cursors);
1188 for (i = 0; i != num_tokens; ++i) {
1189 const char *kind = "<unknown>";
1190 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
1191 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
1192 unsigned start_line, start_column, end_line, end_column;
1193
1194 switch (clang_getTokenKind(tokens[i])) {
1195 case CXToken_Punctuation: kind = "Punctuation"; break;
1196 case CXToken_Keyword: kind = "Keyword"; break;
1197 case CXToken_Identifier: kind = "Identifier"; break;
1198 case CXToken_Literal: kind = "Literal"; break;
1199 case CXToken_Comment: kind = "Comment"; break;
1200 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001201 clang_getInstantiationLocation(clang_getRangeStart(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001202 0, &start_line, &start_column, 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001203 clang_getInstantiationLocation(clang_getRangeEnd(extent),
Douglas Gregor46766dc2010-01-26 19:19:08 +00001204 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001205 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
1206 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00001207 if (!clang_isInvalid(cursors[i].kind)) {
1208 printf(" ");
1209 PrintCursor(cursors[i]);
1210 }
1211 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001212 }
1213 free(cursors);
1214
1215 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00001216 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001217 clang_disposeTranslationUnit(TU);
1218 clang_disposeIndex(CIdx);
1219 free(filename);
1220 free_remapped_files(unsaved_files, num_unsaved_files);
1221 return errorCode;
1222}
1223
Ted Kremenek0d435192009-11-17 18:13:31 +00001224/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001225/* USR printing. */
1226/******************************************************************************/
1227
1228static int insufficient_usr(const char *kind, const char *usage) {
1229 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
1230 return 1;
1231}
1232
1233static unsigned isUSR(const char *s) {
1234 return s[0] == 'c' && s[1] == ':';
1235}
1236
1237static int not_usr(const char *s, const char *arg) {
1238 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
1239 return 1;
1240}
1241
1242static void print_usr(CXString usr) {
1243 const char *s = clang_getCString(usr);
1244 printf("%s\n", s);
1245 clang_disposeString(usr);
1246}
1247
1248static void display_usrs() {
1249 fprintf(stderr, "-print-usrs options:\n"
1250 " ObjCCategory <class name> <category name>\n"
1251 " ObjCClass <class name>\n"
1252 " ObjCIvar <ivar name> <class USR>\n"
1253 " ObjCMethod <selector> [0=class method|1=instance method] "
1254 "<class USR>\n"
1255 " ObjCProperty <property name> <class USR>\n"
1256 " ObjCProtocol <protocol name>\n");
1257}
1258
1259int print_usrs(const char **I, const char **E) {
1260 while (I != E) {
1261 const char *kind = *I;
1262 unsigned len = strlen(kind);
1263 switch (len) {
1264 case 8:
1265 if (memcmp(kind, "ObjCIvar", 8) == 0) {
1266 if (I + 2 >= E)
1267 return insufficient_usr(kind, "<ivar name> <class USR>");
1268 if (!isUSR(I[2]))
1269 return not_usr("<class USR>", I[2]);
1270 else {
1271 CXString x;
1272 x.Spelling = I[2];
1273 x.MustFreeString = 0;
1274 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
1275 }
1276
1277 I += 3;
1278 continue;
1279 }
1280 break;
1281 case 9:
1282 if (memcmp(kind, "ObjCClass", 9) == 0) {
1283 if (I + 1 >= E)
1284 return insufficient_usr(kind, "<class name>");
1285 print_usr(clang_constructUSR_ObjCClass(I[1]));
1286 I += 2;
1287 continue;
1288 }
1289 break;
1290 case 10:
1291 if (memcmp(kind, "ObjCMethod", 10) == 0) {
1292 if (I + 3 >= E)
1293 return insufficient_usr(kind, "<method selector> "
1294 "[0=class method|1=instance method] <class USR>");
1295 if (!isUSR(I[3]))
1296 return not_usr("<class USR>", I[3]);
1297 else {
1298 CXString x;
1299 x.Spelling = I[3];
1300 x.MustFreeString = 0;
1301 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
1302 }
1303 I += 4;
1304 continue;
1305 }
1306 break;
1307 case 12:
1308 if (memcmp(kind, "ObjCCategory", 12) == 0) {
1309 if (I + 2 >= E)
1310 return insufficient_usr(kind, "<class name> <category name>");
1311 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
1312 I += 3;
1313 continue;
1314 }
1315 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
1316 if (I + 1 >= E)
1317 return insufficient_usr(kind, "<protocol name>");
1318 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
1319 I += 2;
1320 continue;
1321 }
1322 if (memcmp(kind, "ObjCProperty", 12) == 0) {
1323 if (I + 2 >= E)
1324 return insufficient_usr(kind, "<property name> <class USR>");
1325 if (!isUSR(I[2]))
1326 return not_usr("<class USR>", I[2]);
1327 else {
1328 CXString x;
1329 x.Spelling = I[2];
1330 x.MustFreeString = 0;
1331 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
1332 }
1333 I += 3;
1334 continue;
1335 }
1336 break;
1337 default:
1338 break;
1339 }
1340 break;
1341 }
1342
1343 if (I != E) {
1344 fprintf(stderr, "Invalid USR kind: %s\n", *I);
1345 display_usrs();
1346 return 1;
1347 }
1348 return 0;
1349}
1350
1351int print_usrs_file(const char *file_name) {
1352 char line[2048];
1353 const char *args[128];
1354 unsigned numChars = 0;
1355
1356 FILE *fp = fopen(file_name, "r");
1357 if (!fp) {
1358 fprintf(stderr, "error: cannot open '%s'\n", file_name);
1359 return 1;
1360 }
1361
1362 /* This code is not really all that safe, but it works fine for testing. */
1363 while (!feof(fp)) {
1364 char c = fgetc(fp);
1365 if (c == '\n') {
1366 unsigned i = 0;
1367 const char *s = 0;
1368
1369 if (numChars == 0)
1370 continue;
1371
1372 line[numChars] = '\0';
1373 numChars = 0;
1374
1375 if (line[0] == '/' && line[1] == '/')
1376 continue;
1377
1378 s = strtok(line, " ");
1379 while (s) {
1380 args[i] = s;
1381 ++i;
1382 s = strtok(0, " ");
1383 }
1384 if (print_usrs(&args[0], &args[i]))
1385 return 1;
1386 }
1387 else
1388 line[numChars++] = c;
1389 }
1390
1391 fclose(fp);
1392 return 0;
1393}
1394
1395/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00001396/* Command line processing. */
1397/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001398int write_pch_file(const char *filename, int argc, const char *argv[]) {
1399 CXIndex Idx;
1400 CXTranslationUnit TU;
1401 struct CXUnsavedFile *unsaved_files = 0;
1402 int num_unsaved_files = 0;
1403
1404 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
1405
1406 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1407 clang_disposeIndex(Idx);
1408 return -1;
1409 }
1410
1411 TU = clang_parseTranslationUnit(Idx, 0,
1412 argv + num_unsaved_files,
1413 argc - num_unsaved_files,
1414 unsaved_files,
1415 num_unsaved_files,
1416 CXTranslationUnit_Incomplete);
1417 if (!TU) {
1418 fprintf(stderr, "Unable to load translation unit!\n");
1419 free_remapped_files(unsaved_files, num_unsaved_files);
1420 clang_disposeIndex(Idx);
1421 return 1;
1422 }
1423
Douglas Gregor19998442010-08-13 15:35:05 +00001424 if (clang_saveTranslationUnit(TU, filename, clang_defaultSaveOptions(TU)))
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001425 fprintf(stderr, "Unable to write PCH file %s\n", filename);
1426 clang_disposeTranslationUnit(TU);
1427 free_remapped_files(unsaved_files, num_unsaved_files);
1428 clang_disposeIndex(Idx);
1429 return 0;
1430}
1431
1432/******************************************************************************/
1433/* Command line processing. */
1434/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001435
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001436static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00001437 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001438 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00001439 if (strcmp(s, "-usrs") == 0)
1440 return USRVisitor;
1441 return NULL;
1442}
1443
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001444static void print_usage(void) {
1445 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00001446 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001447 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001448 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001449 " c-index-test -test-file-scan <AST file> <source file> "
1450 "[FileCheck prefix]\n"
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001451 " c-index-test -test-load-tu <AST file> <symbol filter> "
1452 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00001453 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
1454 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001455 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001456 fprintf(stderr,
Douglas Gregorabc563f2010-07-19 21:46:24 +00001457 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
1458 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00001459 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00001460 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
1461 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek3bed5272010-03-03 06:37:58 +00001462 " c-index-test -test-inclusion-stack-tu <AST file>\n"
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001463 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001464 " c-index-test -test-print-typekind {<args>}*\n"
Chandler Carruth53513d22010-07-22 06:29:13 +00001465 " c-index-test -print-usr [<CursorKind> {<args>}]*\n");
1466 fprintf(stderr,
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001467 " c-index-test -print-usr-file <file>\n"
1468 " c-index-test -write-pch <file> <compiler arguments>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00001469 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00001470 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00001471 " all - load all symbols, including those from PCH\n"
1472 " local - load all symbols except those in PCH\n"
1473 " category - only load ObjC categories (non-PCH)\n"
1474 " interface - only load ObjC interfaces (non-PCH)\n"
1475 " protocol - only load ObjC protocols (non-PCH)\n"
1476 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001477 " typedef - only load typdefs (non-PCH)\n"
1478 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001479}
1480
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001481/***/
1482
1483int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001484 clang_enableStackTraces();
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001485 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00001486 return perform_code_completion(argc, argv, 0);
1487 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
1488 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001489 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
1490 return inspect_cursor_at(argc, argv);
Ted Kremenek7d405622010-01-12 23:34:26 +00001491 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001492 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00001493 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001494 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
1495 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001496 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00001497 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
1498 CXCursorVisitor I = GetVisitor(argv[1] + 25);
1499 if (I) {
1500 int trials = atoi(argv[2]);
1501 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
1502 NULL);
1503 }
1504 }
Ted Kremenek7d405622010-01-12 23:34:26 +00001505 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001506 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek7d405622010-01-12 23:34:26 +00001507 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00001508 return perform_test_load_source(argc - 3, argv + 3, argv[2], I, NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00001509 }
1510 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001511 return perform_file_scan(argv[2], argv[3],
1512 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001513 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
1514 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001515 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
1516 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
1517 PrintInclusionStack);
1518 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
1519 return perform_test_load_tu(argv[2], "all", NULL, NULL,
1520 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001521 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
1522 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
1523 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001524 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
1525 return perform_test_load_source(argc - 2, argv + 2, "all",
1526 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00001527 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
1528 if (argc > 2)
1529 return print_usrs(argv + 2, argv + argc);
1530 else {
1531 display_usrs();
1532 return 1;
1533 }
1534 }
1535 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
1536 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00001537 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
1538 return write_pch_file(argv[2], argc - 3, argv + 3);
1539
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001540 print_usage();
1541 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00001542}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00001543
1544/***/
1545
1546/* We intentionally run in a separate thread to ensure we at least minimal
1547 * testing of a multithreaded environment (for example, having a reduced stack
1548 * size). */
1549
1550#include "llvm/Config/config.h"
1551#ifdef HAVE_PTHREAD_H
1552
1553#include <pthread.h>
1554
1555typedef struct thread_info {
1556 int argc;
1557 const char **argv;
1558 int result;
1559} thread_info;
1560void *thread_runner(void *client_data_v) {
1561 thread_info *client_data = client_data_v;
1562 client_data->result = cindextest_main(client_data->argc, client_data->argv);
1563 return 0;
1564}
1565
1566int main(int argc, const char **argv) {
1567 thread_info client_data;
1568 pthread_t thread;
1569 int res;
1570
1571 client_data.argc = argc;
1572 client_data.argv = argv;
1573 res = pthread_create(&thread, 0, thread_runner, &client_data);
1574 if (res != 0) {
1575 perror("thread creation failed");
1576 return 1;
1577 }
1578
1579 res = pthread_join(thread, 0);
1580 if (res != 0) {
1581 perror("thread join failed");
1582 return 1;
1583 }
1584
1585 return client_data.result;
1586}
1587
1588#else
1589
1590int main(int argc, const char **argv) {
1591 return cindextest_main(argc, argv);
1592}
1593
1594#endif