blob: 40794308065a875f686fcfb6d51a8a2fe68d611d [file] [log] [blame]
Steve Naroff69b10fd2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroffa1c72842009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004#include "clang-c/CXCompilationDatabase.h"
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00005#include "llvm/Config/config.h"
Douglas Gregor49f67ce2010-08-26 13:48:20 +00006#include <ctype.h>
Douglas Gregor9eb77012009-11-07 00:00:49 +00007#include <stdlib.h>
Steve Naroff1054e602009-08-31 00:59:03 +00008#include <stdio.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +00009#include <string.h>
Douglas Gregor082c3e62010-01-15 19:40:17 +000010#include <assert.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +000011
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +000012#ifdef CLANG_HAVE_LIBXML
13#include <libxml/parser.h>
14#include <libxml/relaxng.h>
15#include <libxml/xmlerror.h>
16#endif
17
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +000018#ifdef _WIN32
19# include <direct.h>
20#else
21# include <unistd.h>
22#endif
23
Ted Kremenek1cd27d52009-11-17 18:13:31 +000024/******************************************************************************/
25/* Utility functions. */
26/******************************************************************************/
27
John Thompsonde258b52009-10-27 13:42:56 +000028#ifdef _MSC_VER
29char *basename(const char* path)
30{
31 char* base1 = (char*)strrchr(path, '/');
32 char* base2 = (char*)strrchr(path, '\\');
33 if (base1 && base2)
34 return((base1 > base2) ? base1 + 1 : base2 + 1);
35 else if (base1)
36 return(base1 + 1);
37 else if (base2)
38 return(base2 + 1);
39
40 return((char*)path);
41}
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000042char *dirname(char* path)
43{
44 char* base1 = (char*)strrchr(path, '/');
45 char* base2 = (char*)strrchr(path, '\\');
46 if (base1 && base2)
47 if (base1 > base2)
48 *base1 = 0;
49 else
50 *base2 = 0;
51 else if (base1)
NAKAMURA Takumi1e43baa62012-06-30 11:47:18 +000052 *base1 = 0;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000053 else if (base2)
NAKAMURA Takumi1e43baa62012-06-30 11:47:18 +000054 *base2 = 0;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000055
56 return path;
57}
John Thompsonde258b52009-10-27 13:42:56 +000058#else
Steve Naroffa7753c42009-09-24 20:03:06 +000059extern char *basename(const char *);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000060extern char *dirname(char *);
John Thompsonde258b52009-10-27 13:42:56 +000061#endif
Steve Naroffa7753c42009-09-24 20:03:06 +000062
Douglas Gregorf2430ba2010-07-25 17:39:21 +000063/** \brief Return the default parsing options. */
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000064static unsigned getDefaultParsingOptions() {
65 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
66
67 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregor4a47bca2010-08-09 22:28:58 +000068 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregorb14904c2010-08-13 22:48:40 +000069 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
70 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidiscb373e32011-11-03 02:20:25 +000071 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
72 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6e922512012-04-12 10:11:59 +000073 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
74 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenko3292d062012-07-02 17:35:10 +000075 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
76 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000077
78 return options;
79}
80
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +000081static int checkForErrors(CXTranslationUnit TU);
82
Daniel Dunbar98c07e02010-02-14 08:32:24 +000083static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
84 unsigned end_line, unsigned end_column) {
85 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbar02968e52010-02-14 10:02:57 +000086 end_line, end_column);
Daniel Dunbar98c07e02010-02-14 08:32:24 +000087}
88
Ted Kremenek2df52dc2009-11-17 19:37:36 +000089static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
90 CXTranslationUnit *TU) {
Ted Kremenek29004672010-02-17 00:41:32 +000091
Douglas Gregor33cdd812010-02-18 18:08:43 +000092 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohmane83f6242010-07-26 21:44:15 +000093 if (!*TU) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +000094 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
95 return 0;
Ted Kremenek29004672010-02-17 00:41:32 +000096 }
Ted Kremenek2df52dc2009-11-17 19:37:36 +000097 return 1;
98}
99
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000100void free_remapped_files(struct CXUnsavedFile *unsaved_files,
101 int num_unsaved_files) {
102 int i;
103 for (i = 0; i != num_unsaved_files; ++i) {
104 free((char *)unsaved_files[i].Filename);
105 free((char *)unsaved_files[i].Contents);
106 }
Douglas Gregor0e3da272010-08-19 20:50:29 +0000107 free(unsaved_files);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000108}
109
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000110static int parse_remapped_files_with_opt(const char *opt_name,
111 int argc, const char **argv,
112 int start_arg,
113 struct CXUnsavedFile **unsaved_files,
114 int *num_unsaved_files) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000115 int i;
116 int arg;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000117 int prefix_len = strlen(opt_name);
118 int arg_indices[20];
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000119 *unsaved_files = 0;
120 *num_unsaved_files = 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000121
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000122 /* Count the number of remapped files. */
123 for (arg = start_arg; arg < argc; ++arg) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000124 if (strncmp(argv[arg], opt_name, prefix_len))
125 continue;
Ted Kremenek29004672010-02-17 00:41:32 +0000126
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000127 assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int)));
128 arg_indices[*num_unsaved_files] = arg;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000129 ++*num_unsaved_files;
130 }
Ted Kremenek29004672010-02-17 00:41:32 +0000131
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000132 if (*num_unsaved_files == 0)
133 return 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000134
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000135 *unsaved_files
Douglas Gregor0e3da272010-08-19 20:50:29 +0000136 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
137 *num_unsaved_files);
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000138 for (i = 0; i != *num_unsaved_files; ++i) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000139 struct CXUnsavedFile *unsaved = *unsaved_files + i;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000140 const char *arg_string = argv[arg_indices[i]] + prefix_len;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000141 int filename_len;
142 char *filename;
143 char *contents;
144 FILE *to_file;
Argyrios Kyrtzidisa60d8ae2013-12-05 08:19:18 +0000145 const char *colon = strchr(arg_string, ':');
146 if (!colon) {
Ted Kremenek29004672010-02-17 00:41:32 +0000147 fprintf(stderr,
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000148 "error: %sfrom:to argument is missing semicolon\n", opt_name);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000149 free_remapped_files(*unsaved_files, i);
150 *unsaved_files = 0;
151 *num_unsaved_files = 0;
152 return -1;
153 }
Ted Kremenek29004672010-02-17 00:41:32 +0000154
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000155 /* Open the file that we're remapping to. */
Argyrios Kyrtzidisa60d8ae2013-12-05 08:19:18 +0000156 to_file = fopen(colon + 1, "rb");
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000157 if (!to_file) {
158 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
Argyrios Kyrtzidisa60d8ae2013-12-05 08:19:18 +0000159 colon + 1);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000160 free_remapped_files(*unsaved_files, i);
161 *unsaved_files = 0;
162 *num_unsaved_files = 0;
163 return -1;
164 }
Ted Kremenek29004672010-02-17 00:41:32 +0000165
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000166 /* Determine the length of the file we're remapping to. */
167 fseek(to_file, 0, SEEK_END);
168 unsaved->Length = ftell(to_file);
169 fseek(to_file, 0, SEEK_SET);
Ted Kremenek29004672010-02-17 00:41:32 +0000170
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000171 /* Read the contents of the file we're remapping to. */
172 contents = (char *)malloc(unsaved->Length + 1);
173 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
174 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
Argyrios Kyrtzidisa60d8ae2013-12-05 08:19:18 +0000175 (feof(to_file) ? "EOF" : "error"), colon + 1);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000176 fclose(to_file);
177 free_remapped_files(*unsaved_files, i);
Richard Smith1ea42eb2012-07-05 08:20:49 +0000178 free(contents);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000179 *unsaved_files = 0;
180 *num_unsaved_files = 0;
181 return -1;
182 }
183 contents[unsaved->Length] = 0;
184 unsaved->Contents = contents;
Ted Kremenek29004672010-02-17 00:41:32 +0000185
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000186 /* Close the file. */
187 fclose(to_file);
Ted Kremenek29004672010-02-17 00:41:32 +0000188
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000189 /* Copy the file name that we're remapping from. */
Argyrios Kyrtzidisa60d8ae2013-12-05 08:19:18 +0000190 filename_len = colon - arg_string;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000191 filename = (char *)malloc(filename_len + 1);
192 memcpy(filename, arg_string, filename_len);
193 filename[filename_len] = 0;
194 unsaved->Filename = filename;
195 }
Ted Kremenek29004672010-02-17 00:41:32 +0000196
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000197 return 0;
198}
199
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000200static int parse_remapped_files(int argc, const char **argv, int start_arg,
201 struct CXUnsavedFile **unsaved_files,
202 int *num_unsaved_files) {
203 return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg,
204 unsaved_files, num_unsaved_files);
205}
206
207static int parse_remapped_files_with_try(int try_idx,
208 int argc, const char **argv,
209 int start_arg,
210 struct CXUnsavedFile **unsaved_files,
211 int *num_unsaved_files) {
212 struct CXUnsavedFile *unsaved_files_no_try_idx;
213 int num_unsaved_files_no_try_idx;
214 struct CXUnsavedFile *unsaved_files_try_idx;
215 int num_unsaved_files_try_idx;
216 int ret;
217 char opt_name[32];
218
219 ret = parse_remapped_files(argc, argv, start_arg,
220 &unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx);
221 if (ret)
222 return ret;
223
224 sprintf(opt_name, "-remap-file-%d=", try_idx);
225 ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
226 &unsaved_files_try_idx, &num_unsaved_files_try_idx);
227 if (ret)
228 return ret;
229
230 *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
231 *unsaved_files
232 = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
233 sizeof(struct CXUnsavedFile) *
234 *num_unsaved_files);
235 memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
236 unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
237 num_unsaved_files_try_idx);
238 free(unsaved_files_try_idx);
239 return 0;
240}
241
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000242static const char *parse_comments_schema(int argc, const char **argv) {
243 const char *CommentsSchemaArg = "-comments-xml-schema=";
244 const char *CommentSchemaFile = NULL;
245
246 if (argc == 0)
247 return CommentSchemaFile;
248
249 if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
250 CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
251
252 return CommentSchemaFile;
253}
254
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000255/******************************************************************************/
256/* Pretty-printing. */
257/******************************************************************************/
258
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000259static const char *FileCheckPrefix = "CHECK";
260
261static void PrintCString(const char *CStr) {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000262 if (CStr != NULL && CStr[0] != '\0') {
263 for ( ; *CStr; ++CStr) {
264 const char C = *CStr;
265 switch (C) {
266 case '\n': printf("\\n"); break;
267 case '\r': printf("\\r"); break;
268 case '\t': printf("\\t"); break;
269 case '\v': printf("\\v"); break;
270 case '\f': printf("\\f"); break;
271 default: putchar(C); break;
272 }
273 }
274 }
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000275}
276
277static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
278 printf(" %s=[", Prefix);
279 PrintCString(CStr);
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000280 printf("]");
281}
282
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000283static void PrintCXStringAndDispose(CXString Str) {
284 PrintCString(clang_getCString(Str));
285 clang_disposeString(Str);
286}
287
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000288static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
289 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
290}
291
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000292static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
293 CXString Str) {
294 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
295 clang_disposeString(Str);
296}
297
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000298static void PrintRange(CXSourceRange R, const char *str) {
299 CXFile begin_file, end_file;
300 unsigned begin_line, begin_column, end_line, end_column;
301
302 clang_getSpellingLocation(clang_getRangeStart(R),
303 &begin_file, &begin_line, &begin_column, 0);
304 clang_getSpellingLocation(clang_getRangeEnd(R),
305 &end_file, &end_line, &end_column, 0);
306 if (!begin_file || !end_file)
307 return;
308
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +0000309 if (str)
310 printf(" %s=", str);
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000311 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
312}
313
Douglas Gregor97c75712010-10-02 22:49:11 +0000314int want_display_name = 0;
315
Douglas Gregord6225d32012-05-08 00:14:45 +0000316static void printVersion(const char *Prefix, CXVersion Version) {
317 if (Version.Major < 0)
318 return;
319 printf("%s%d", Prefix, Version.Major);
320
321 if (Version.Minor < 0)
322 return;
323 printf(".%d", Version.Minor);
324
325 if (Version.Subminor < 0)
326 return;
327 printf(".%d", Version.Subminor);
328}
329
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000330struct CommentASTDumpingContext {
331 int IndentLevel;
332};
333
334static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
335 CXComment Comment) {
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000336 unsigned i;
337 unsigned e;
338 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
339
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000340 Ctx->IndentLevel++;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000341 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000342 printf(" ");
343
344 printf("(");
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000345 switch (Kind) {
346 case CXComment_Null:
347 printf("CXComment_Null");
348 break;
349 case CXComment_Text:
350 printf("CXComment_Text");
351 PrintCXStringWithPrefixAndDispose("Text",
352 clang_TextComment_getText(Comment));
353 if (clang_Comment_isWhitespace(Comment))
354 printf(" IsWhitespace");
355 if (clang_InlineContentComment_hasTrailingNewline(Comment))
356 printf(" HasTrailingNewline");
357 break;
358 case CXComment_InlineCommand:
359 printf("CXComment_InlineCommand");
360 PrintCXStringWithPrefixAndDispose(
361 "CommandName",
362 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000363 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
364 case CXCommentInlineCommandRenderKind_Normal:
365 printf(" RenderNormal");
366 break;
367 case CXCommentInlineCommandRenderKind_Bold:
368 printf(" RenderBold");
369 break;
370 case CXCommentInlineCommandRenderKind_Monospaced:
371 printf(" RenderMonospaced");
372 break;
373 case CXCommentInlineCommandRenderKind_Emphasized:
374 printf(" RenderEmphasized");
375 break;
376 }
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000377 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000378 i != e; ++i) {
379 printf(" Arg[%u]=", i);
380 PrintCXStringAndDispose(
381 clang_InlineCommandComment_getArgText(Comment, i));
382 }
383 if (clang_InlineContentComment_hasTrailingNewline(Comment))
384 printf(" HasTrailingNewline");
385 break;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000386 case CXComment_HTMLStartTag: {
387 unsigned NumAttrs;
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000388 printf("CXComment_HTMLStartTag");
389 PrintCXStringWithPrefixAndDispose(
390 "Name",
391 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000392 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000393 if (NumAttrs != 0) {
394 printf(" Attrs:");
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000395 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000396 printf(" ");
397 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
398 printf("=");
399 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
400 }
401 }
402 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
403 printf(" SelfClosing");
404 if (clang_InlineContentComment_hasTrailingNewline(Comment))
405 printf(" HasTrailingNewline");
406 break;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000407 }
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000408 case CXComment_HTMLEndTag:
409 printf("CXComment_HTMLEndTag");
410 PrintCXStringWithPrefixAndDispose(
411 "Name",
412 clang_HTMLTagComment_getTagName(Comment));
413 if (clang_InlineContentComment_hasTrailingNewline(Comment))
414 printf(" HasTrailingNewline");
415 break;
416 case CXComment_Paragraph:
417 printf("CXComment_Paragraph");
418 if (clang_Comment_isWhitespace(Comment))
419 printf(" IsWhitespace");
420 break;
421 case CXComment_BlockCommand:
422 printf("CXComment_BlockCommand");
423 PrintCXStringWithPrefixAndDispose(
424 "CommandName",
425 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000426 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000427 i != e; ++i) {
428 printf(" Arg[%u]=", i);
429 PrintCXStringAndDispose(
430 clang_BlockCommandComment_getArgText(Comment, i));
431 }
432 break;
433 case CXComment_ParamCommand:
434 printf("CXComment_ParamCommand");
435 switch (clang_ParamCommandComment_getDirection(Comment)) {
436 case CXCommentParamPassDirection_In:
437 printf(" in");
438 break;
439 case CXCommentParamPassDirection_Out:
440 printf(" out");
441 break;
442 case CXCommentParamPassDirection_InOut:
443 printf(" in,out");
444 break;
445 }
446 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
447 printf(" explicitly");
448 else
449 printf(" implicitly");
450 PrintCXStringWithPrefixAndDispose(
451 "ParamName",
452 clang_ParamCommandComment_getParamName(Comment));
453 if (clang_ParamCommandComment_isParamIndexValid(Comment))
454 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
455 else
456 printf(" ParamIndex=Invalid");
457 break;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000458 case CXComment_TParamCommand:
459 printf("CXComment_TParamCommand");
460 PrintCXStringWithPrefixAndDispose(
461 "ParamName",
462 clang_TParamCommandComment_getParamName(Comment));
463 if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
464 printf(" ParamPosition={");
465 for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
466 i != e; ++i) {
467 printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
468 if (i != e - 1)
469 printf(", ");
470 }
471 printf("}");
472 } else
473 printf(" ParamPosition=Invalid");
474 break;
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000475 case CXComment_VerbatimBlockCommand:
476 printf("CXComment_VerbatimBlockCommand");
477 PrintCXStringWithPrefixAndDispose(
478 "CommandName",
479 clang_BlockCommandComment_getCommandName(Comment));
480 break;
481 case CXComment_VerbatimBlockLine:
482 printf("CXComment_VerbatimBlockLine");
483 PrintCXStringWithPrefixAndDispose(
484 "Text",
485 clang_VerbatimBlockLineComment_getText(Comment));
486 break;
487 case CXComment_VerbatimLine:
488 printf("CXComment_VerbatimLine");
489 PrintCXStringWithPrefixAndDispose(
490 "Text",
491 clang_VerbatimLineComment_getText(Comment));
492 break;
493 case CXComment_FullComment:
494 printf("CXComment_FullComment");
495 break;
496 }
497 if (Kind != CXComment_Null) {
498 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000499 unsigned i;
500 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000501 printf("\n// %s: ", FileCheckPrefix);
502 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
503 }
504 }
505 printf(")");
506 Ctx->IndentLevel--;
507}
508
509static void DumpCXComment(CXComment Comment) {
510 struct CommentASTDumpingContext Ctx;
511 Ctx.IndentLevel = 1;
512 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
513 DumpCXCommentInternal(&Ctx, Comment);
514 printf("]");
515}
516
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000517typedef struct {
518 const char *CommentSchemaFile;
519#ifdef CLANG_HAVE_LIBXML
520 xmlRelaxNGParserCtxtPtr RNGParser;
521 xmlRelaxNGPtr Schema;
522#endif
523} CommentXMLValidationData;
524
525static void ValidateCommentXML(const char *Str,
526 CommentXMLValidationData *ValidationData) {
527#ifdef CLANG_HAVE_LIBXML
528 xmlDocPtr Doc;
529 xmlRelaxNGValidCtxtPtr ValidationCtxt;
530 int status;
531
532 if (!ValidationData || !ValidationData->CommentSchemaFile)
533 return;
534
535 if (!ValidationData->RNGParser) {
536 ValidationData->RNGParser =
537 xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile);
538 ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser);
539 }
540 if (!ValidationData->RNGParser) {
541 printf(" libXMLError");
542 return;
543 }
544
545 Doc = xmlParseDoc((const xmlChar *) Str);
546
547 if (!Doc) {
548 xmlErrorPtr Error = xmlGetLastError();
549 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
550 return;
551 }
552
553 ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->Schema);
554 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
555 if (!status)
556 printf(" CommentXMLValid");
557 else if (status > 0) {
558 xmlErrorPtr Error = xmlGetLastError();
559 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
560 } else
561 printf(" libXMLError");
562
563 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
564 xmlFreeDoc(Doc);
565#endif
566}
567
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000568static void PrintCursorComments(CXCursor Cursor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000569 CommentXMLValidationData *ValidationData) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000570 {
571 CXString RawComment;
572 const char *RawCommentCString;
573 CXString BriefComment;
574 const char *BriefCommentCString;
575
576 RawComment = clang_Cursor_getRawCommentText(Cursor);
577 RawCommentCString = clang_getCString(RawComment);
578 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
579 PrintCStringWithPrefix("RawComment", RawCommentCString);
580 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
581
582 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
583 BriefCommentCString = clang_getCString(BriefComment);
584 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
585 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
586 clang_disposeString(BriefComment);
587 }
588 clang_disposeString(RawComment);
589 }
590
591 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000592 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000593 if (clang_Comment_getKind(Comment) != CXComment_Null) {
594 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
595 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000596 {
597 CXString XML;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000598 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000599 PrintCXStringWithPrefix("FullCommentAsXML", XML);
600 ValidateCommentXML(clang_getCString(XML), ValidationData);
601 clang_disposeString(XML);
602 }
603
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000604 DumpCXComment(Comment);
605 }
606 }
607}
608
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000609typedef struct {
610 unsigned line;
611 unsigned col;
612} LineCol;
613
614static int lineCol_cmp(const void *p1, const void *p2) {
615 const LineCol *lhs = p1;
616 const LineCol *rhs = p2;
617 if (lhs->line != rhs->line)
618 return (int)lhs->line - (int)rhs->line;
619 return (int)lhs->col - (int)rhs->col;
620}
621
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000622static void PrintCursor(CXCursor Cursor,
623 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000624 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000625 if (clang_isInvalid(Cursor.kind)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000626 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
Ted Kremenek29004672010-02-17 00:41:32 +0000627 printf("Invalid Cursor => %s", clang_getCString(ks));
628 clang_disposeString(ks);
629 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000630 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000631 CXString string, ks;
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000632 CXCursor Referenced;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000633 unsigned line, column;
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000634 CXCursor SpecializationOf;
Douglas Gregor99a26af2010-10-01 20:25:15 +0000635 CXCursor *overridden;
636 unsigned num_overridden;
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000637 unsigned RefNameRangeNr;
638 CXSourceRange CursorExtent;
639 CXSourceRange RefNameRange;
Douglas Gregord6225d32012-05-08 00:14:45 +0000640 int AlwaysUnavailable;
641 int AlwaysDeprecated;
642 CXString UnavailableMessage;
643 CXString DeprecatedMessage;
644 CXPlatformAvailability PlatformAvailability[2];
645 int NumPlatformAvailability;
646 int I;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000647
Ted Kremenek29004672010-02-17 00:41:32 +0000648 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor97c75712010-10-02 22:49:11 +0000649 string = want_display_name? clang_getCursorDisplayName(Cursor)
650 : clang_getCursorSpelling(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000651 printf("%s=%s", clang_getCString(ks),
652 clang_getCString(string));
653 clang_disposeString(ks);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000654 clang_disposeString(string);
Ted Kremenek29004672010-02-17 00:41:32 +0000655
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000656 Referenced = clang_getCursorReferenced(Cursor);
657 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000658 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
659 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
660 printf("[");
661 for (I = 0; I != N; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000662 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor2967e282010-09-14 00:20:32 +0000663 CXSourceLocation Loc;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000664 if (I)
665 printf(", ");
666
Douglas Gregor2967e282010-09-14 00:20:32 +0000667 Loc = clang_getCursorLocation(Ovl);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000668 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000669 printf("%d:%d", line, column);
670 }
671 printf("]");
672 } else {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000673 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000674 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000675 printf(":%d:%d", line, column);
676 }
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000677 }
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000678
679 if (clang_isCursorDefinition(Cursor))
680 printf(" (Definition)");
Douglas Gregorf757a122010-08-23 23:00:57 +0000681
682 switch (clang_getCursorAvailability(Cursor)) {
683 case CXAvailability_Available:
684 break;
685
686 case CXAvailability_Deprecated:
687 printf(" (deprecated)");
688 break;
689
690 case CXAvailability_NotAvailable:
691 printf(" (unavailable)");
692 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000693
694 case CXAvailability_NotAccessible:
695 printf(" (inaccessible)");
696 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000697 }
Ted Kremeneka5940822010-08-26 01:42:22 +0000698
Douglas Gregord6225d32012-05-08 00:14:45 +0000699 NumPlatformAvailability
700 = clang_getCursorPlatformAvailability(Cursor,
701 &AlwaysDeprecated,
702 &DeprecatedMessage,
703 &AlwaysUnavailable,
704 &UnavailableMessage,
705 PlatformAvailability, 2);
706 if (AlwaysUnavailable) {
707 printf(" (always unavailable: \"%s\")",
708 clang_getCString(UnavailableMessage));
709 } else if (AlwaysDeprecated) {
710 printf(" (always deprecated: \"%s\")",
711 clang_getCString(DeprecatedMessage));
712 } else {
713 for (I = 0; I != NumPlatformAvailability; ++I) {
714 if (I >= 2)
715 break;
716
717 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
718 if (PlatformAvailability[I].Unavailable)
719 printf(", unavailable");
720 else {
721 printVersion(", introduced=", PlatformAvailability[I].Introduced);
722 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
723 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
724 }
725 if (clang_getCString(PlatformAvailability[I].Message)[0])
726 printf(", message=\"%s\"",
727 clang_getCString(PlatformAvailability[I].Message));
728 printf(")");
729 }
730 }
731 for (I = 0; I != NumPlatformAvailability; ++I) {
732 if (I >= 2)
733 break;
734 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
735 }
736
737 clang_disposeString(DeprecatedMessage);
738 clang_disposeString(UnavailableMessage);
739
Douglas Gregora8d0c772011-05-13 15:54:42 +0000740 if (clang_CXXMethod_isStatic(Cursor))
741 printf(" (static)");
742 if (clang_CXXMethod_isVirtual(Cursor))
743 printf(" (virtual)");
Dmitri Gribenko62770be2013-05-17 18:38:35 +0000744 if (clang_CXXMethod_isPureVirtual(Cursor))
745 printf(" (pure)");
Argyrios Kyrtzidis23814e42013-04-18 23:53:05 +0000746 if (clang_Cursor_isVariadic(Cursor))
747 printf(" (variadic)");
Argyrios Kyrtzidis7b50fc52013-07-05 20:44:37 +0000748 if (clang_Cursor_isObjCOptional(Cursor))
749 printf(" (@optional)");
750
Ted Kremeneka5940822010-08-26 01:42:22 +0000751 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000752 CXType T =
753 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
754 CXString S = clang_getTypeKindSpelling(T.kind);
Ted Kremeneka5940822010-08-26 01:42:22 +0000755 printf(" [IBOutletCollection=%s]", clang_getCString(S));
756 clang_disposeString(S);
757 }
Ted Kremenekae9e2212010-08-27 21:34:58 +0000758
759 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
760 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
761 unsigned isVirtual = clang_isVirtualBase(Cursor);
762 const char *accessStr = 0;
763
764 switch (access) {
765 case CX_CXXInvalidAccessSpecifier:
766 accessStr = "invalid"; break;
767 case CX_CXXPublic:
768 accessStr = "public"; break;
769 case CX_CXXProtected:
770 accessStr = "protected"; break;
771 case CX_CXXPrivate:
772 accessStr = "private"; break;
773 }
774
775 printf(" [access=%s isVirtual=%s]", accessStr,
776 isVirtual ? "true" : "false");
777 }
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000778
779 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
780 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000781 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
782 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000783 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000784 printf(" [Specialization of %s:%d:%d]",
785 clang_getCString(Name), line, column);
786 clang_disposeString(Name);
787 }
Douglas Gregor99a26af2010-10-01 20:25:15 +0000788
789 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
790 if (num_overridden) {
791 unsigned I;
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000792 LineCol lineCols[50];
793 assert(num_overridden <= 50);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000794 printf(" [Overrides ");
795 for (I = 0; I != num_overridden; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000796 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000797 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000798 lineCols[I].line = line;
799 lineCols[I].col = column;
800 }
Michael Liaob94f47a2012-08-30 00:45:32 +0000801 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000802 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
803 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor99a26af2010-10-01 20:25:15 +0000804 if (I)
805 printf(", ");
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000806 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000807 }
808 printf("]");
809 clang_disposeOverriddenCursors(overridden);
810 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000811
812 if (Cursor.kind == CXCursor_InclusionDirective) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000813 CXFile File = clang_getIncludedFile(Cursor);
814 CXString Included = clang_getFileName(File);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000815 printf(" (%s)", clang_getCString(Included));
816 clang_disposeString(Included);
Douglas Gregor37aa4932011-05-04 00:14:37 +0000817
818 if (clang_isFileMultipleIncludeGuarded(TU, File))
819 printf(" [multi-include guarded]");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000820 }
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000821
822 CursorExtent = clang_getCursorExtent(Cursor);
823 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
824 CXNameRange_WantQualifier
825 | CXNameRange_WantSinglePiece
826 | CXNameRange_WantTemplateArgs,
827 0);
828 if (!clang_equalRanges(CursorExtent, RefNameRange))
829 PrintRange(RefNameRange, "SingleRefName");
830
831 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
832 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
833 CXNameRange_WantQualifier
834 | CXNameRange_WantTemplateArgs,
835 RefNameRangeNr);
836 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
837 break;
838 if (!clang_equalRanges(CursorExtent, RefNameRange))
839 PrintRange(RefNameRange, "RefName");
840 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000841
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000842 PrintCursorComments(Cursor, ValidationData);
Argyrios Kyrtzidis9adfd8a2013-04-18 22:15:49 +0000843
844 {
845 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
846 if (PropAttrs != CXObjCPropertyAttr_noattr) {
847 printf(" [");
848 #define PRINT_PROP_ATTR(A) \
849 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
850 PRINT_PROP_ATTR(readonly);
851 PRINT_PROP_ATTR(getter);
852 PRINT_PROP_ATTR(assign);
853 PRINT_PROP_ATTR(readwrite);
854 PRINT_PROP_ATTR(retain);
855 PRINT_PROP_ATTR(copy);
856 PRINT_PROP_ATTR(nonatomic);
857 PRINT_PROP_ATTR(setter);
858 PRINT_PROP_ATTR(atomic);
859 PRINT_PROP_ATTR(weak);
860 PRINT_PROP_ATTR(strong);
861 PRINT_PROP_ATTR(unsafe_unretained);
862 printf("]");
863 }
864 }
Argyrios Kyrtzidis9d9bc012013-04-18 23:29:12 +0000865
866 {
867 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
868 if (QT != CXObjCDeclQualifier_None) {
869 printf(" [");
870 #define PRINT_OBJC_QUAL(A) \
871 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
872 PRINT_OBJC_QUAL(In);
873 PRINT_OBJC_QUAL(Inout);
874 PRINT_OBJC_QUAL(Out);
875 PRINT_OBJC_QUAL(Bycopy);
876 PRINT_OBJC_QUAL(Byref);
877 PRINT_OBJC_QUAL(Oneway);
878 printf("]");
879 }
880 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000881 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000882}
Steve Naroff1054e602009-08-31 00:59:03 +0000883
Ted Kremenek29004672010-02-17 00:41:32 +0000884static const char* GetCursorSource(CXCursor Cursor) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000885 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekc560b682010-02-17 00:41:20 +0000886 CXString source;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000887 CXFile file;
Argyrios Kyrtzidis7ca77352011-11-03 02:20:36 +0000888 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000889 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +0000890 if (!clang_getCString(source)) {
Ted Kremenekc560b682010-02-17 00:41:20 +0000891 clang_disposeString(source);
892 return "<invalid loc>";
893 }
894 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000895 const char *b = basename(clang_getCString(source));
Ted Kremenekc560b682010-02-17 00:41:20 +0000896 clang_disposeString(source);
897 return b;
898 }
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000899}
900
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000901/******************************************************************************/
Ted Kremenekb478ff42010-01-26 17:59:48 +0000902/* Callbacks. */
903/******************************************************************************/
904
905typedef void (*PostVisitTU)(CXTranslationUnit);
906
Douglas Gregor33cdd812010-02-18 18:08:43 +0000907void PrintDiagnostic(CXDiagnostic Diagnostic) {
908 FILE *out = stderr;
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000909 CXFile file;
Douglas Gregord770f732010-02-22 23:17:23 +0000910 CXString Msg;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000911 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregora750e8e2010-11-19 16:18:16 +0000912 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
913 | CXDiagnostic_DisplayOption;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000914 unsigned i, num_fixits;
Ted Kremenek599d73a2010-03-25 02:00:39 +0000915
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000916 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000917 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000918
Douglas Gregord770f732010-02-22 23:17:23 +0000919 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
920 fprintf(stderr, "%s\n", clang_getCString(Msg));
921 clang_disposeString(Msg);
Ted Kremenek599d73a2010-03-25 02:00:39 +0000922
Douglas Gregor229bebd2010-11-09 06:24:54 +0000923 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
924 &file, 0, 0, 0);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000925 if (!file)
926 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000927
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000928 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek4a642302012-03-20 20:49:45 +0000929 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000930 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor836ec942010-02-19 18:16:06 +0000931 CXSourceRange range;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000932 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
933 CXSourceLocation start = clang_getRangeStart(range);
934 CXSourceLocation end = clang_getRangeEnd(range);
Douglas Gregor836ec942010-02-19 18:16:06 +0000935 unsigned start_line, start_column, end_line, end_column;
936 CXFile start_file, end_file;
Douglas Gregor229bebd2010-11-09 06:24:54 +0000937 clang_getSpellingLocation(start, &start_file, &start_line,
938 &start_column, 0);
939 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor836ec942010-02-19 18:16:06 +0000940 if (clang_equalLocations(start, end)) {
941 /* Insertion. */
942 if (start_file == file)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000943 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor836ec942010-02-19 18:16:06 +0000944 clang_getCString(insertion_text), start_line, start_column);
945 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
946 /* Removal. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000947 if (start_file == file && end_file == file) {
948 fprintf(out, "FIX-IT: Remove ");
949 PrintExtent(out, start_line, start_column, end_line, end_column);
950 fprintf(out, "\n");
Douglas Gregor60b11f62010-01-29 00:41:11 +0000951 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000952 } else {
953 /* Replacement. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000954 if (start_file == end_file) {
955 fprintf(out, "FIX-IT: Replace ");
956 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor836ec942010-02-19 18:16:06 +0000957 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor9773e3d2010-02-18 22:27:07 +0000958 }
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000959 break;
960 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000961 clang_disposeString(insertion_text);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000962 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000963}
964
Ted Kremenek914c7e62012-02-14 02:46:03 +0000965void PrintDiagnosticSet(CXDiagnosticSet Set) {
966 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
967 for ( ; i != n ; ++i) {
968 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
969 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000970 PrintDiagnostic(Diag);
Ted Kremenek914c7e62012-02-14 02:46:03 +0000971 if (ChildDiags)
972 PrintDiagnosticSet(ChildDiags);
973 }
974}
975
976void PrintDiagnostics(CXTranslationUnit TU) {
977 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
978 PrintDiagnosticSet(TUSet);
979 clang_disposeDiagnosticSet(TUSet);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000980}
981
Ted Kremenek83f642e2011-04-18 22:47:10 +0000982void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayd6238f42011-08-29 16:37:29 +0000983 unsigned long total = 0;
Ted Kremenek11d1a422011-04-18 23:42:53 +0000984 unsigned i = 0;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000985 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet45cc5462011-04-18 23:33:22 +0000986 fprintf(stderr, "Memory usage:\n");
Ted Kremenek11d1a422011-04-18 23:42:53 +0000987 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenek23324122011-04-20 16:41:07 +0000988 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek83f642e2011-04-18 22:47:10 +0000989 unsigned long amount = usage.entries[i].amount;
990 total += amount;
Ted Kremenek11d1a422011-04-18 23:42:53 +0000991 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek83f642e2011-04-18 22:47:10 +0000992 ((double) amount)/(1024*1024));
993 }
Ted Kremenek11d1a422011-04-18 23:42:53 +0000994 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek83f642e2011-04-18 22:47:10 +0000995 ((double) total)/(1024*1024));
Ted Kremenek23324122011-04-20 16:41:07 +0000996 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek83f642e2011-04-18 22:47:10 +0000997}
998
Ted Kremenekb478ff42010-01-26 17:59:48 +0000999/******************************************************************************/
Douglas Gregor720d0052010-01-20 21:32:04 +00001000/* Logic for testing traversal. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001001/******************************************************************************/
1002
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001003static void PrintCursorExtent(CXCursor C) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001004 CXSourceRange extent = clang_getCursorExtent(C);
1005 PrintRange(extent, "Extent");
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001006}
1007
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001008/* Data used by the visitors. */
1009typedef struct {
Douglas Gregor720d0052010-01-20 21:32:04 +00001010 CXTranslationUnit TU;
1011 enum CXCursorKind *Filter;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001012 CommentXMLValidationData ValidationData;
Douglas Gregor720d0052010-01-20 21:32:04 +00001013} VisitorData;
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001014
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001015
Ted Kremenek29004672010-02-17 00:41:32 +00001016enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001017 CXCursor Parent,
1018 CXClientData ClientData) {
1019 VisitorData *Data = (VisitorData *)ClientData;
1020 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001021 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001022 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001023 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001024 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor4f46e782010-01-19 21:36:55 +00001025 GetCursorSource(Cursor), line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001026 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001027 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis1ab09cc2013-04-11 17:02:10 +00001028 if (clang_isDeclaration(Cursor.kind)) {
1029 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
1030 const char *accessStr = 0;
1031
1032 switch (access) {
1033 case CX_CXXInvalidAccessSpecifier: break;
1034 case CX_CXXPublic:
1035 accessStr = "public"; break;
1036 case CX_CXXProtected:
1037 accessStr = "protected"; break;
1038 case CX_CXXPrivate:
1039 accessStr = "private"; break;
1040 }
1041
1042 if (accessStr)
1043 printf(" [access=%s]", accessStr);
1044 }
Ted Kremenek29004672010-02-17 00:41:32 +00001045 printf("\n");
Douglas Gregor720d0052010-01-20 21:32:04 +00001046 return CXChildVisit_Recurse;
Steve Naroff772c1a42009-08-31 14:26:51 +00001047 }
Ted Kremenek29004672010-02-17 00:41:32 +00001048
Douglas Gregor720d0052010-01-20 21:32:04 +00001049 return CXChildVisit_Continue;
Steve Naroff1054e602009-08-31 00:59:03 +00001050}
Steve Naroffa1c72842009-08-28 15:28:48 +00001051
Ted Kremenek29004672010-02-17 00:41:32 +00001052static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001053 CXCursor Parent,
1054 CXClientData ClientData) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001055 const char *startBuf, *endBuf;
1056 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1057 CXCursor Ref;
Douglas Gregor720d0052010-01-20 21:32:04 +00001058 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001059
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001060 if (Cursor.kind != CXCursor_FunctionDecl ||
1061 !clang_isCursorDefinition(Cursor))
Douglas Gregor720d0052010-01-20 21:32:04 +00001062 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001063
1064 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1065 &startLine, &startColumn,
1066 &endLine, &endColumn);
1067 /* Probe the entire body, looking for both decls and refs. */
1068 curLine = startLine;
1069 curColumn = startColumn;
1070
1071 while (startBuf < endBuf) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001072 CXSourceLocation Loc;
Douglas Gregor4f46e782010-01-19 21:36:55 +00001073 CXFile file;
Ted Kremenekc560b682010-02-17 00:41:20 +00001074 CXString source;
Ted Kremenek29004672010-02-17 00:41:32 +00001075
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001076 if (*startBuf == '\n') {
1077 startBuf++;
1078 curLine++;
1079 curColumn = 1;
1080 } else if (*startBuf != '\t')
1081 curColumn++;
Ted Kremenek29004672010-02-17 00:41:32 +00001082
Douglas Gregor66a58812010-01-18 22:46:11 +00001083 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor229bebd2010-11-09 06:24:54 +00001084 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremenek29004672010-02-17 00:41:32 +00001085
Douglas Gregor4f46e782010-01-19 21:36:55 +00001086 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +00001087 if (clang_getCString(source)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001088 CXSourceLocation RefLoc
1089 = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregor816fd362010-01-22 21:44:22 +00001090 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor66a58812010-01-18 22:46:11 +00001091 if (Ref.kind == CXCursor_NoDeclFound) {
1092 /* Nothing found here; that's fine. */
1093 } else if (Ref.kind != CXCursor_FunctionDecl) {
1094 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1095 curLine, curColumn);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001096 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor66a58812010-01-18 22:46:11 +00001097 printf("\n");
1098 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001099 }
Ted Kremenekc560b682010-02-17 00:41:20 +00001100 clang_disposeString(source);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001101 startBuf++;
1102 }
Ted Kremenek29004672010-02-17 00:41:32 +00001103
Douglas Gregor720d0052010-01-20 21:32:04 +00001104 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001105}
1106
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001107/******************************************************************************/
1108/* USR testing. */
1109/******************************************************************************/
1110
Douglas Gregor720d0052010-01-20 21:32:04 +00001111enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1112 CXClientData ClientData) {
1113 VisitorData *Data = (VisitorData *)ClientData;
1114 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001115 CXString USR = clang_getCursorUSR(C);
1116 const char *cstr = clang_getCString(USR);
Ted Kremenek6d159c12010-04-20 23:15:40 +00001117 if (!cstr || cstr[0] == '\0') {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001118 clang_disposeString(USR);
Ted Kremenek7afa85b2010-04-16 21:31:52 +00001119 return CXChildVisit_Recurse;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001120 }
Ted Kremenek6d159c12010-04-20 23:15:40 +00001121 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1122
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001123 PrintCursorExtent(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001124 printf("\n");
1125 clang_disposeString(USR);
Ted Kremenek29004672010-02-17 00:41:32 +00001126
Douglas Gregor720d0052010-01-20 21:32:04 +00001127 return CXChildVisit_Recurse;
Ted Kremenek29004672010-02-17 00:41:32 +00001128 }
1129
Douglas Gregor720d0052010-01-20 21:32:04 +00001130 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001131}
1132
1133/******************************************************************************/
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001134/* Inclusion stack testing. */
1135/******************************************************************************/
1136
1137void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1138 unsigned includeStackLen, CXClientData data) {
Ted Kremenek29004672010-02-17 00:41:32 +00001139
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001140 unsigned i;
Ted Kremenekc560b682010-02-17 00:41:20 +00001141 CXString fname;
1142
1143 fname = clang_getFileName(includedFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001144 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenekc560b682010-02-17 00:41:20 +00001145 clang_disposeString(fname);
Ted Kremenek29004672010-02-17 00:41:32 +00001146
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001147 for (i = 0; i < includeStackLen; ++i) {
1148 CXFile includingFile;
1149 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001150 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1151 &column, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +00001152 fname = clang_getFileName(includingFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001153 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenekc560b682010-02-17 00:41:20 +00001154 clang_disposeString(fname);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001155 }
1156 printf("\n");
1157}
1158
1159void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremenek29004672010-02-17 00:41:32 +00001160 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001161}
1162
1163/******************************************************************************/
Ted Kremenek83b28a22010-03-03 06:37:58 +00001164/* Linkage testing. */
1165/******************************************************************************/
1166
1167static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1168 CXClientData d) {
1169 const char *linkage = 0;
1170
1171 if (clang_isInvalid(clang_getCursorKind(cursor)))
1172 return CXChildVisit_Recurse;
1173
1174 switch (clang_getCursorLinkage(cursor)) {
1175 case CXLinkage_Invalid: break;
Douglas Gregor0b466502010-03-04 19:36:27 +00001176 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1177 case CXLinkage_Internal: linkage = "Internal"; break;
1178 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1179 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek83b28a22010-03-03 06:37:58 +00001180 }
1181
1182 if (linkage) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001183 PrintCursor(cursor, NULL);
Ted Kremenek83b28a22010-03-03 06:37:58 +00001184 printf("linkage=%s\n", linkage);
1185 }
1186
1187 return CXChildVisit_Recurse;
1188}
1189
1190/******************************************************************************/
Ted Kremenek6bca9842010-05-14 21:29:26 +00001191/* Typekind testing. */
1192/******************************************************************************/
1193
Dmitri Gribenko00353722013-02-15 21:15:49 +00001194static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1195 CXString TypeSpelling, TypeKindSpelling;
1196
1197 TypeSpelling = clang_getTypeSpelling(T);
1198 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1199 printf(Format,
1200 clang_getCString(TypeSpelling),
1201 clang_getCString(TypeKindSpelling));
1202 clang_disposeString(TypeSpelling);
1203 clang_disposeString(TypeKindSpelling);
1204}
1205
1206static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1207 CXClientData d) {
Ted Kremenek6bca9842010-05-14 21:29:26 +00001208 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001209 CXType T = clang_getCursorType(cursor);
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001210 enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001211 PrintCursor(cursor, NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00001212 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregor56a63802011-01-27 16:27:11 +00001213 if (clang_isConstQualifiedType(T))
1214 printf(" const");
1215 if (clang_isVolatileQualifiedType(T))
1216 printf(" volatile");
1217 if (clang_isRestrictQualifiedType(T))
1218 printf(" restrict");
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001219 if (RQ == CXRefQualifier_LValue)
1220 printf(" lvalue-ref-qualifier");
1221 if (RQ == CXRefQualifier_RValue)
1222 printf(" rvalue-ref-qualifier");
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001223 /* Print the canonical type if it is different. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001224 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001225 CXType CT = clang_getCanonicalType(T);
Ted Kremenekc1508872010-06-21 20:15:39 +00001226 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001227 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001228 }
1229 }
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001230 /* Print the return type if it exists. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001231 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001232 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenekc1508872010-06-21 20:15:39 +00001233 if (RT.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001234 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001235 }
1236 }
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001237 /* Print the argument types if they exist. */
1238 {
1239 int numArgs = clang_Cursor_getNumArguments(cursor);
1240 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001241 int i;
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001242 printf(" [args=");
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001243 for (i = 0; i < numArgs; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001244 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001245 if (T.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001246 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001247 }
1248 }
1249 printf("]");
1250 }
1251 }
Ted Kremenek0c7476a2010-07-30 00:14:11 +00001252 /* Print if this is a non-POD type. */
1253 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenekc1508872010-06-21 20:15:39 +00001254
Ted Kremenek6bca9842010-05-14 21:29:26 +00001255 printf("\n");
1256 }
1257 return CXChildVisit_Recurse;
1258}
1259
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001260static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1261 CXClientData d) {
1262 CXType T;
1263 enum CXCursorKind K = clang_getCursorKind(cursor);
1264 if (clang_isInvalid(K))
1265 return CXChildVisit_Recurse;
1266 T = clang_getCursorType(cursor);
1267 PrintCursor(cursor, NULL);
1268 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1269 /* Print the type sizeof if applicable. */
1270 {
1271 long long Size = clang_Type_getSizeOf(T);
1272 if (Size >= 0 || Size < -1 ) {
1273 printf(" [sizeof=%lld]", Size);
1274 }
1275 }
1276 /* Print the type alignof if applicable. */
1277 {
1278 long long Align = clang_Type_getAlignOf(T);
1279 if (Align >= 0 || Align < -1) {
1280 printf(" [alignof=%lld]", Align);
1281 }
1282 }
1283 /* Print the record field offset if applicable. */
1284 {
1285 const char *FieldName = clang_getCString(clang_getCursorSpelling(cursor));
1286 /* recurse to get the root anonymous record parent */
1287 CXCursor Parent, Root;
1288 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl ) {
1289 const char *RootParentName;
1290 Root = Parent = p;
1291 do {
1292 Root = Parent;
1293 RootParentName = clang_getCString(clang_getCursorSpelling(Root));
1294 Parent = clang_getCursorSemanticParent(Root);
1295 } while ( clang_getCursorType(Parent).kind == CXType_Record &&
1296 !strcmp(RootParentName, "") );
1297 /* if RootParentName is "", record is anonymous. */
1298 {
1299 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1300 FieldName);
1301 printf(" [offsetof=%lld]", Offset);
1302 }
1303 }
1304 }
1305 /* Print if its a bitfield */
1306 {
1307 int IsBitfield = clang_Cursor_isBitField(cursor);
1308 if (IsBitfield)
1309 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1310 }
1311 printf("\n");
1312 return CXChildVisit_Recurse;
1313}
1314
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001315/******************************************************************************/
1316/* Bitwidth testing. */
1317/******************************************************************************/
1318
1319static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1320 CXClientData d) {
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001321 int Bitwidth;
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001322 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1323 return CXChildVisit_Recurse;
1324
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001325 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001326 if (Bitwidth >= 0) {
1327 PrintCursor(cursor, NULL);
1328 printf(" bitwidth=%d\n", Bitwidth);
1329 }
1330
1331 return CXChildVisit_Recurse;
1332}
Ted Kremenek6bca9842010-05-14 21:29:26 +00001333
1334/******************************************************************************/
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001335/* Loading ASTs/source. */
1336/******************************************************************************/
1337
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001338static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek73eccd22010-01-12 18:53:15 +00001339 const char *filter, const char *prefix,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001340 CXCursorVisitor Visitor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001341 PostVisitTU PV,
1342 const char *CommentSchemaFile) {
Ted Kremenek29004672010-02-17 00:41:32 +00001343
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001344 if (prefix)
Ted Kremenek29004672010-02-17 00:41:32 +00001345 FileCheckPrefix = prefix;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001346
1347 if (Visitor) {
1348 enum CXCursorKind K = CXCursor_NotImplemented;
1349 enum CXCursorKind *ck = &K;
1350 VisitorData Data;
Ted Kremenek29004672010-02-17 00:41:32 +00001351
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001352 /* Perform some simple filtering. */
1353 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor97c75712010-10-02 22:49:11 +00001354 else if (!strcmp(filter, "all-display") ||
1355 !strcmp(filter, "local-display")) {
1356 ck = NULL;
1357 want_display_name = 1;
1358 }
Daniel Dunbard64ce7b2010-02-10 20:42:40 +00001359 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001360 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1361 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1362 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1363 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1364 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1365 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1366 else {
1367 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1368 return 1;
1369 }
Ted Kremenek29004672010-02-17 00:41:32 +00001370
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001371 Data.TU = TU;
1372 Data.Filter = ck;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001373 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1374#ifdef CLANG_HAVE_LIBXML
1375 Data.ValidationData.RNGParser = NULL;
1376 Data.ValidationData.Schema = NULL;
1377#endif
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001378 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001379 }
Ted Kremenek29004672010-02-17 00:41:32 +00001380
Ted Kremenekb478ff42010-01-26 17:59:48 +00001381 if (PV)
1382 PV(TU);
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001383
Douglas Gregor33cdd812010-02-18 18:08:43 +00001384 PrintDiagnostics(TU);
Argyrios Kyrtzidis70480492011-11-13 23:39:14 +00001385 if (checkForErrors(TU) != 0) {
1386 clang_disposeTranslationUnit(TU);
1387 return -1;
1388 }
1389
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001390 clang_disposeTranslationUnit(TU);
1391 return 0;
1392}
1393
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001394int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001395 const char *prefix, CXCursorVisitor Visitor,
1396 PostVisitTU PV) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001397 CXIndex Idx;
1398 CXTranslationUnit TU;
Ted Kremenek50228be2010-02-11 07:41:25 +00001399 int result;
Ted Kremenek29004672010-02-17 00:41:32 +00001400 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001401 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001402 /* displayDiagnostics=*/1);
Ted Kremenek29004672010-02-17 00:41:32 +00001403
Ted Kremenek50228be2010-02-11 07:41:25 +00001404 if (!CreateTranslationUnit(Idx, file, &TU)) {
1405 clang_disposeIndex(Idx);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001406 return 1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001407 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001408
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001409 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek50228be2010-02-11 07:41:25 +00001410 clang_disposeIndex(Idx);
1411 return result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001412}
1413
Ted Kremenekb478ff42010-01-26 17:59:48 +00001414int perform_test_load_source(int argc, const char **argv,
1415 const char *filter, CXCursorVisitor Visitor,
1416 PostVisitTU PV) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001417 CXIndex Idx;
1418 CXTranslationUnit TU;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001419 const char *CommentSchemaFile;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001420 struct CXUnsavedFile *unsaved_files = 0;
1421 int num_unsaved_files = 0;
1422 int result;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001423
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001424 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor97c75712010-10-02 22:49:11 +00001425 (!strcmp(filter, "local") ||
1426 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001427 /* displayDiagnostics=*/1);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001428
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001429 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1430 argc--;
1431 argv++;
1432 }
1433
Ted Kremenek50228be2010-02-11 07:41:25 +00001434 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1435 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001436 return -1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001437 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001438
Douglas Gregor998caea2011-05-06 16:33:08 +00001439 TU = clang_parseTranslationUnit(Idx, 0,
1440 argv + num_unsaved_files,
1441 argc - num_unsaved_files,
1442 unsaved_files, num_unsaved_files,
1443 getDefaultParsingOptions());
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001444 if (!TU) {
1445 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001446 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001447 clang_disposeIndex(Idx);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001448 return 1;
1449 }
1450
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001451 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1452 CommentSchemaFile);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001453 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001454 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001455 return result;
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001456}
1457
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001458int perform_test_reparse_source(int argc, const char **argv, int trials,
1459 const char *filter, CXCursorVisitor Visitor,
1460 PostVisitTU PV) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001461 CXIndex Idx;
1462 CXTranslationUnit TU;
1463 struct CXUnsavedFile *unsaved_files = 0;
1464 int num_unsaved_files = 0;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001465 int compiler_arg_idx = 0;
1466 int result, i;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001467 int trial;
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001468 int remap_after_trial = 0;
1469 char *endptr = 0;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001470
1471 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1472 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001473 /* displayDiagnostics=*/1);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001474
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001475 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1476 clang_disposeIndex(Idx);
1477 return -1;
1478 }
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001479
1480 for (i = 0; i < argc; ++i) {
1481 if (strcmp(argv[i], "--") == 0)
1482 break;
1483 }
1484 if (i < argc)
1485 compiler_arg_idx = i+1;
1486 if (num_unsaved_files > compiler_arg_idx)
1487 compiler_arg_idx = num_unsaved_files;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001488
Daniel Dunbarec29d712010-08-18 23:09:16 +00001489 /* Load the initial translation unit -- we do this without honoring remapped
1490 * files, so that we have a way to test results after changing the source. */
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001491 TU = clang_parseTranslationUnit(Idx, 0,
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001492 argv + compiler_arg_idx,
1493 argc - compiler_arg_idx,
Daniel Dunbarec29d712010-08-18 23:09:16 +00001494 0, 0, getDefaultParsingOptions());
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001495 if (!TU) {
1496 fprintf(stderr, "Unable to load translation unit!\n");
1497 free_remapped_files(unsaved_files, num_unsaved_files);
1498 clang_disposeIndex(Idx);
1499 return 1;
1500 }
1501
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001502 if (checkForErrors(TU) != 0)
1503 return -1;
1504
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001505 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1506 remap_after_trial =
1507 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1508 }
1509
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001510 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001511 free_remapped_files(unsaved_files, num_unsaved_files);
1512 if (parse_remapped_files_with_try(trial, argc, argv, 0,
1513 &unsaved_files, &num_unsaved_files)) {
1514 clang_disposeTranslationUnit(TU);
1515 clang_disposeIndex(Idx);
1516 return -1;
1517 }
1518
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001519 if (clang_reparseTranslationUnit(TU,
1520 trial >= remap_after_trial ? num_unsaved_files : 0,
1521 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregorde051182010-08-11 15:58:42 +00001522 clang_defaultReparseOptions(TU))) {
Daniel Dunbarec29d712010-08-18 23:09:16 +00001523 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001524 clang_disposeTranslationUnit(TU);
1525 free_remapped_files(unsaved_files, num_unsaved_files);
1526 clang_disposeIndex(Idx);
1527 return -1;
1528 }
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001529
1530 if (checkForErrors(TU) != 0)
1531 return -1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001532 }
1533
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001534 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001535
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001536 free_remapped_files(unsaved_files, num_unsaved_files);
1537 clang_disposeIndex(Idx);
1538 return result;
1539}
1540
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001541/******************************************************************************/
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001542/* Logic for testing clang_getCursor(). */
1543/******************************************************************************/
1544
Douglas Gregor37aa4932011-05-04 00:14:37 +00001545static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001546 unsigned start_line, unsigned start_col,
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001547 unsigned end_line, unsigned end_col,
1548 const char *prefix) {
Ted Kremenekb58514e2010-01-07 01:17:12 +00001549 printf("// %s: ", FileCheckPrefix);
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001550 if (prefix)
1551 printf("-%s", prefix);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00001552 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1553 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001554 PrintCursor(cursor, NULL);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001555 printf("\n");
1556}
1557
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001558static int perform_file_scan(const char *ast_file, const char *source_file,
1559 const char *prefix) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001560 CXIndex Idx;
1561 CXTranslationUnit TU;
1562 FILE *fp;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001563 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregor816fd362010-01-22 21:44:22 +00001564 CXFile file;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001565 unsigned line = 1, col = 1;
Daniel Dunbar6092d502010-02-14 08:32:51 +00001566 unsigned start_line = 1, start_col = 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001567
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001568 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001569 /* displayDiagnostics=*/1))) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001570 fprintf(stderr, "Could not create Index\n");
1571 return 1;
1572 }
Ted Kremenek29004672010-02-17 00:41:32 +00001573
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001574 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1575 return 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001576
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001577 if ((fp = fopen(source_file, "r")) == NULL) {
1578 fprintf(stderr, "Could not open '%s'\n", source_file);
1579 return 1;
1580 }
Ted Kremenek29004672010-02-17 00:41:32 +00001581
Douglas Gregor816fd362010-01-22 21:44:22 +00001582 file = clang_getFile(TU, source_file);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001583 for (;;) {
1584 CXCursor cursor;
1585 int c = fgetc(fp);
Benjamin Kramer10d083172009-11-17 20:51:40 +00001586
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001587 if (c == '\n') {
1588 ++line;
1589 col = 1;
1590 } else
1591 ++col;
1592
1593 /* Check the cursor at this position, and dump the previous one if we have
1594 * found something new.
1595 */
1596 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1597 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1598 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregor37aa4932011-05-04 00:14:37 +00001599 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbar02968e52010-02-14 10:02:57 +00001600 line, col, prefix);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001601 start_line = line;
1602 start_col = col;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001603 }
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001604 if (c == EOF)
1605 break;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001606
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001607 prevCursor = cursor;
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001608 }
Ted Kremenek29004672010-02-17 00:41:32 +00001609
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001610 fclose(fp);
Douglas Gregor7a964ad2011-01-31 22:04:05 +00001611 clang_disposeTranslationUnit(TU);
1612 clang_disposeIndex(Idx);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001613 return 0;
1614}
1615
1616/******************************************************************************/
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001617/* Logic for testing clang code completion. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001618/******************************************************************************/
1619
Douglas Gregor9eb77012009-11-07 00:00:49 +00001620/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1621 on failure. If successful, the pointer *filename will contain newly-allocated
1622 memory (that will be owned by the caller) to store the file name. */
Ted Kremenek29004672010-02-17 00:41:32 +00001623int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001624 unsigned *column, unsigned *second_line,
1625 unsigned *second_column) {
Douglas Gregorf96ea292009-11-09 18:19:57 +00001626 /* Find the second colon. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001627 const char *last_colon = strrchr(input, ':');
1628 unsigned values[4], i;
1629 unsigned num_values = (second_line && second_column)? 4 : 2;
1630
Douglas Gregor9eb77012009-11-07 00:00:49 +00001631 char *endptr = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001632 if (!last_colon || last_colon == input) {
1633 if (num_values == 4)
1634 fprintf(stderr, "could not parse filename:line:column:line:column in "
1635 "'%s'\n", input);
1636 else
1637 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001638 return 1;
1639 }
1640
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001641 for (i = 0; i != num_values; ++i) {
1642 const char *prev_colon;
1643
1644 /* Parse the next line or column. */
1645 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1646 if (*endptr != 0 && *endptr != ':') {
Ted Kremenek29004672010-02-17 00:41:32 +00001647 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001648 (i % 2 ? "column" : "line"), input);
1649 return 1;
1650 }
Ted Kremenek29004672010-02-17 00:41:32 +00001651
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001652 if (i + 1 == num_values)
1653 break;
1654
1655 /* Find the previous colon. */
1656 prev_colon = last_colon - 1;
1657 while (prev_colon != input && *prev_colon != ':')
1658 --prev_colon;
1659 if (prev_colon == input) {
Ted Kremenek29004672010-02-17 00:41:32 +00001660 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001661 (i % 2 == 0? "column" : "line"), input);
Ted Kremenek29004672010-02-17 00:41:32 +00001662 return 1;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001663 }
1664
1665 last_colon = prev_colon;
Douglas Gregorf96ea292009-11-09 18:19:57 +00001666 }
1667
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001668 *line = values[0];
1669 *column = values[1];
Ted Kremenek29004672010-02-17 00:41:32 +00001670
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001671 if (second_line && second_column) {
1672 *second_line = values[2];
1673 *second_column = values[3];
1674 }
1675
Douglas Gregorf96ea292009-11-09 18:19:57 +00001676 /* Copy the file name. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001677 *filename = (char*)malloc(last_colon - input + 1);
1678 memcpy(*filename, input, last_colon - input);
1679 (*filename)[last_colon - input] = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001680 return 0;
1681}
1682
1683const char *
1684clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1685 switch (Kind) {
1686 case CXCompletionChunk_Optional: return "Optional";
1687 case CXCompletionChunk_TypedText: return "TypedText";
1688 case CXCompletionChunk_Text: return "Text";
1689 case CXCompletionChunk_Placeholder: return "Placeholder";
1690 case CXCompletionChunk_Informative: return "Informative";
1691 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1692 case CXCompletionChunk_LeftParen: return "LeftParen";
1693 case CXCompletionChunk_RightParen: return "RightParen";
1694 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1695 case CXCompletionChunk_RightBracket: return "RightBracket";
1696 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1697 case CXCompletionChunk_RightBrace: return "RightBrace";
1698 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1699 case CXCompletionChunk_RightAngle: return "RightAngle";
1700 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001701 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001702 case CXCompletionChunk_Colon: return "Colon";
1703 case CXCompletionChunk_SemiColon: return "SemiColon";
1704 case CXCompletionChunk_Equal: return "Equal";
1705 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1706 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor9eb77012009-11-07 00:00:49 +00001707 }
Ted Kremenek29004672010-02-17 00:41:32 +00001708
Douglas Gregor9eb77012009-11-07 00:00:49 +00001709 return "Unknown";
1710}
1711
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00001712static int checkForErrors(CXTranslationUnit TU) {
1713 unsigned Num, i;
1714 CXDiagnostic Diag;
1715 CXString DiagStr;
1716
1717 if (!getenv("CINDEXTEST_FAILONERROR"))
1718 return 0;
1719
1720 Num = clang_getNumDiagnostics(TU);
1721 for (i = 0; i != Num; ++i) {
1722 Diag = clang_getDiagnostic(TU, i);
1723 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1724 DiagStr = clang_formatDiagnostic(Diag,
1725 clang_defaultDiagnosticDisplayOptions());
1726 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1727 clang_disposeString(DiagStr);
1728 clang_disposeDiagnostic(Diag);
1729 return -1;
1730 }
1731 clang_disposeDiagnostic(Diag);
1732 }
1733
1734 return 0;
1735}
1736
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001737void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001738 int I, N;
Ted Kremenek29004672010-02-17 00:41:32 +00001739
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001740 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001741 for (I = 0; I != N; ++I) {
Ted Kremenekf602f962010-02-17 01:42:24 +00001742 CXString text;
1743 const char *cstr;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001744 enum CXCompletionChunkKind Kind
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001745 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremenek29004672010-02-17 00:41:32 +00001746
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001747 if (Kind == CXCompletionChunk_Optional) {
1748 fprintf(file, "{Optional ");
1749 print_completion_string(
Ted Kremenek29004672010-02-17 00:41:32 +00001750 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001751 file);
1752 fprintf(file, "}");
1753 continue;
Douglas Gregor8ed5b772010-10-08 20:39:29 +00001754 }
1755
1756 if (Kind == CXCompletionChunk_VerticalSpace) {
1757 fprintf(file, "{VerticalSpace }");
1758 continue;
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001759 }
Ted Kremenek29004672010-02-17 00:41:32 +00001760
Douglas Gregorf81f5282009-11-09 17:05:28 +00001761 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenekf602f962010-02-17 01:42:24 +00001762 cstr = clang_getCString(text);
Ted Kremenek29004672010-02-17 00:41:32 +00001763 fprintf(file, "{%s %s}",
Douglas Gregor9eb77012009-11-07 00:00:49 +00001764 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenekf602f962010-02-17 01:42:24 +00001765 cstr ? cstr : "");
1766 clang_disposeString(text);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001767 }
Ted Kremenekf602f962010-02-17 01:42:24 +00001768
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001769}
1770
1771void print_completion_result(CXCompletionResult *completion_result,
1772 CXClientData client_data) {
1773 FILE *file = (FILE *)client_data;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001774 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001775 unsigned annotationCount;
Douglas Gregor78254c82012-03-27 23:34:16 +00001776 enum CXCursorKind ParentKind;
1777 CXString ParentName;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001778 CXString BriefComment;
1779 const char *BriefCommentCString;
Douglas Gregor78254c82012-03-27 23:34:16 +00001780
Ted Kremenek29004672010-02-17 00:41:32 +00001781 fprintf(file, "%s:", clang_getCString(ks));
1782 clang_disposeString(ks);
1783
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001784 print_completion_string(completion_result->CompletionString, file);
Douglas Gregorf757a122010-08-23 23:00:57 +00001785 fprintf(file, " (%u)",
Douglas Gregora2db7932010-05-26 22:00:08 +00001786 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregorf757a122010-08-23 23:00:57 +00001787 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1788 case CXAvailability_Available:
1789 break;
1790
1791 case CXAvailability_Deprecated:
1792 fprintf(file, " (deprecated)");
1793 break;
1794
1795 case CXAvailability_NotAvailable:
1796 fprintf(file, " (unavailable)");
1797 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001798
1799 case CXAvailability_NotAccessible:
1800 fprintf(file, " (inaccessible)");
1801 break;
Douglas Gregorf757a122010-08-23 23:00:57 +00001802 }
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001803
1804 annotationCount = clang_getCompletionNumAnnotations(
1805 completion_result->CompletionString);
1806 if (annotationCount) {
1807 unsigned i;
1808 fprintf(file, " (");
1809 for (i = 0; i < annotationCount; ++i) {
1810 if (i != 0)
1811 fprintf(file, ", ");
1812 fprintf(file, "\"%s\"",
1813 clang_getCString(clang_getCompletionAnnotation(
1814 completion_result->CompletionString, i)));
1815 }
1816 fprintf(file, ")");
1817 }
1818
Douglas Gregor78254c82012-03-27 23:34:16 +00001819 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1820 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1821 &ParentKind);
1822 if (ParentKind != CXCursor_NotImplemented) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001823 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregor78254c82012-03-27 23:34:16 +00001824 fprintf(file, " (parent: %s '%s')",
1825 clang_getCString(KindSpelling),
1826 clang_getCString(ParentName));
1827 clang_disposeString(KindSpelling);
1828 }
1829 clang_disposeString(ParentName);
1830 }
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001831
1832 BriefComment = clang_getCompletionBriefComment(
1833 completion_result->CompletionString);
1834 BriefCommentCString = clang_getCString(BriefComment);
1835 if (BriefCommentCString && *BriefCommentCString != '\0') {
1836 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1837 }
1838 clang_disposeString(BriefComment);
Douglas Gregor78254c82012-03-27 23:34:16 +00001839
Douglas Gregorf757a122010-08-23 23:00:57 +00001840 fprintf(file, "\n");
Douglas Gregor9eb77012009-11-07 00:00:49 +00001841}
1842
Douglas Gregor21325842011-07-07 16:03:39 +00001843void print_completion_contexts(unsigned long long contexts, FILE *file) {
1844 fprintf(file, "Completion contexts:\n");
1845 if (contexts == CXCompletionContext_Unknown) {
1846 fprintf(file, "Unknown\n");
1847 }
1848 if (contexts & CXCompletionContext_AnyType) {
1849 fprintf(file, "Any type\n");
1850 }
1851 if (contexts & CXCompletionContext_AnyValue) {
1852 fprintf(file, "Any value\n");
1853 }
1854 if (contexts & CXCompletionContext_ObjCObjectValue) {
1855 fprintf(file, "Objective-C object value\n");
1856 }
1857 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1858 fprintf(file, "Objective-C selector value\n");
1859 }
1860 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1861 fprintf(file, "C++ class type value\n");
1862 }
1863 if (contexts & CXCompletionContext_DotMemberAccess) {
1864 fprintf(file, "Dot member access\n");
1865 }
1866 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1867 fprintf(file, "Arrow member access\n");
1868 }
1869 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1870 fprintf(file, "Objective-C property access\n");
1871 }
1872 if (contexts & CXCompletionContext_EnumTag) {
1873 fprintf(file, "Enum tag\n");
1874 }
1875 if (contexts & CXCompletionContext_UnionTag) {
1876 fprintf(file, "Union tag\n");
1877 }
1878 if (contexts & CXCompletionContext_StructTag) {
1879 fprintf(file, "Struct tag\n");
1880 }
1881 if (contexts & CXCompletionContext_ClassTag) {
1882 fprintf(file, "Class name\n");
1883 }
1884 if (contexts & CXCompletionContext_Namespace) {
1885 fprintf(file, "Namespace or namespace alias\n");
1886 }
1887 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1888 fprintf(file, "Nested name specifier\n");
1889 }
1890 if (contexts & CXCompletionContext_ObjCInterface) {
1891 fprintf(file, "Objective-C interface\n");
1892 }
1893 if (contexts & CXCompletionContext_ObjCProtocol) {
1894 fprintf(file, "Objective-C protocol\n");
1895 }
1896 if (contexts & CXCompletionContext_ObjCCategory) {
1897 fprintf(file, "Objective-C category\n");
1898 }
1899 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1900 fprintf(file, "Objective-C instance method\n");
1901 }
1902 if (contexts & CXCompletionContext_ObjCClassMessage) {
1903 fprintf(file, "Objective-C class method\n");
1904 }
1905 if (contexts & CXCompletionContext_ObjCSelectorName) {
1906 fprintf(file, "Objective-C selector name\n");
1907 }
1908 if (contexts & CXCompletionContext_MacroName) {
1909 fprintf(file, "Macro name\n");
1910 }
1911 if (contexts & CXCompletionContext_NaturalLanguage) {
1912 fprintf(file, "Natural language\n");
1913 }
1914}
1915
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001916int my_stricmp(const char *s1, const char *s2) {
1917 while (*s1 && *s2) {
NAKAMURA Takumid3cc2202011-03-09 03:02:28 +00001918 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001919 if (c1 < c2)
1920 return -1;
1921 else if (c1 > c2)
1922 return 1;
1923
1924 ++s1;
1925 ++s2;
1926 }
1927
1928 if (*s1)
1929 return 1;
1930 else if (*s2)
1931 return -1;
1932 return 0;
1933}
1934
Douglas Gregor47815d52010-07-12 18:38:41 +00001935int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001936 const char *input = argv[1];
1937 char *filename = 0;
1938 unsigned line;
1939 unsigned column;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001940 CXIndex CIdx;
Ted Kremenekef3339b2009-11-17 18:09:14 +00001941 int errorCode;
Douglas Gregor9485bf92009-12-02 09:21:34 +00001942 struct CXUnsavedFile *unsaved_files = 0;
1943 int num_unsaved_files = 0;
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001944 CXCodeCompleteResults *results = 0;
Dawn Perchik0d3d0b72010-09-30 22:26:05 +00001945 CXTranslationUnit TU = 0;
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001946 unsigned I, Repeats = 1;
1947 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1948
1949 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1950 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001951 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1952 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregor028d3e42010-08-09 20:45:32 +00001953
Douglas Gregor47815d52010-07-12 18:38:41 +00001954 if (timing_only)
1955 input += strlen("-code-completion-timing=");
1956 else
1957 input += strlen("-code-completion-at=");
1958
Ted Kremenek29004672010-02-17 00:41:32 +00001959 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001960 0, 0)))
Ted Kremenekef3339b2009-11-17 18:09:14 +00001961 return errorCode;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001962
Douglas Gregor9485bf92009-12-02 09:21:34 +00001963 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1964 return -1;
1965
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001966 CIdx = clang_createIndex(0, 0);
1967
1968 if (getenv("CINDEXTEST_EDITING"))
1969 Repeats = 5;
1970
1971 TU = clang_parseTranslationUnit(CIdx, 0,
1972 argv + num_unsaved_files + 2,
1973 argc - num_unsaved_files - 2,
1974 0, 0, getDefaultParsingOptions());
1975 if (!TU) {
1976 fprintf(stderr, "Unable to load translation unit!\n");
1977 return 1;
1978 }
Douglas Gregorc6592922010-11-15 23:00:34 +00001979
1980 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1981 fprintf(stderr, "Unable to reparse translation init!\n");
1982 return 1;
1983 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001984
1985 for (I = 0; I != Repeats; ++I) {
1986 results = clang_codeCompleteAt(TU, filename, line, column,
1987 unsaved_files, num_unsaved_files,
1988 completionOptions);
1989 if (!results) {
1990 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar186f7422010-08-19 23:44:06 +00001991 return 1;
1992 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001993 if (I != Repeats-1)
1994 clang_disposeCodeCompleteResults(results);
1995 }
Douglas Gregorba965fb2010-01-28 00:56:43 +00001996
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001997 if (results) {
Douglas Gregor63745d52011-07-21 01:05:26 +00001998 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor21325842011-07-07 16:03:39 +00001999 unsigned long long contexts;
Douglas Gregor63745d52011-07-21 01:05:26 +00002000 enum CXCursorKind containerKind;
Douglas Gregorea777402011-07-26 15:24:30 +00002001 CXString objCSelector;
2002 const char *selectorString;
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002003 if (!timing_only) {
2004 /* Sort the code-completion results based on the typed text. */
2005 clang_sortCodeCompletionResults(results->Results, results->NumResults);
2006
Douglas Gregor47815d52010-07-12 18:38:41 +00002007 for (i = 0; i != n; ++i)
2008 print_completion_result(results->Results + i, stdout);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002009 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00002010 n = clang_codeCompleteGetNumDiagnostics(results);
2011 for (i = 0; i != n; ++i) {
2012 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
2013 PrintDiagnostic(diag);
2014 clang_disposeDiagnostic(diag);
2015 }
Douglas Gregor21325842011-07-07 16:03:39 +00002016
2017 contexts = clang_codeCompleteGetContexts(results);
2018 print_completion_contexts(contexts, stdout);
2019
Douglas Gregorea777402011-07-26 15:24:30 +00002020 containerKind = clang_codeCompleteGetContainerKind(results,
2021 &containerIsIncomplete);
Douglas Gregor63745d52011-07-21 01:05:26 +00002022
2023 if (containerKind != CXCursor_InvalidCode) {
2024 /* We have found a container */
2025 CXString containerUSR, containerKindSpelling;
2026 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
2027 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
2028 clang_disposeString(containerKindSpelling);
2029
2030 if (containerIsIncomplete) {
2031 printf("Container is incomplete\n");
2032 }
2033 else {
2034 printf("Container is complete\n");
2035 }
2036
2037 containerUSR = clang_codeCompleteGetContainerUSR(results);
2038 printf("Container USR: %s\n", clang_getCString(containerUSR));
2039 clang_disposeString(containerUSR);
2040 }
2041
Douglas Gregorea777402011-07-26 15:24:30 +00002042 objCSelector = clang_codeCompleteGetObjCSelector(results);
2043 selectorString = clang_getCString(objCSelector);
2044 if (selectorString && strlen(selectorString) > 0) {
2045 printf("Objective-C selector: %s\n", selectorString);
2046 }
2047 clang_disposeString(objCSelector);
2048
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002049 clang_disposeCodeCompleteResults(results);
2050 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00002051 clang_disposeTranslationUnit(TU);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002052 clang_disposeIndex(CIdx);
2053 free(filename);
Ted Kremenek29004672010-02-17 00:41:32 +00002054
Douglas Gregor9485bf92009-12-02 09:21:34 +00002055 free_remapped_files(unsaved_files, num_unsaved_files);
2056
Ted Kremenekef3339b2009-11-17 18:09:14 +00002057 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002058}
2059
Douglas Gregor082c3e62010-01-15 19:40:17 +00002060typedef struct {
2061 char *filename;
2062 unsigned line;
2063 unsigned column;
2064} CursorSourceLocation;
2065
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002066static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002067 CXIndex CIdx;
2068 int errorCode;
2069 struct CXUnsavedFile *unsaved_files = 0;
2070 int num_unsaved_files = 0;
2071 CXTranslationUnit TU;
2072 CXCursor Cursor;
2073 CursorSourceLocation *Locations = 0;
2074 unsigned NumLocations = 0, Loc;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002075 unsigned Repeats = 1;
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002076 unsigned I;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002077
Ted Kremenek29004672010-02-17 00:41:32 +00002078 /* Count the number of locations. */
Douglas Gregor082c3e62010-01-15 19:40:17 +00002079 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2080 ++NumLocations;
Ted Kremenek29004672010-02-17 00:41:32 +00002081
Douglas Gregor082c3e62010-01-15 19:40:17 +00002082 /* Parse the locations. */
2083 assert(NumLocations > 0 && "Unable to count locations?");
2084 Locations = (CursorSourceLocation *)malloc(
2085 NumLocations * sizeof(CursorSourceLocation));
2086 for (Loc = 0; Loc < NumLocations; ++Loc) {
2087 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremenek29004672010-02-17 00:41:32 +00002088 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2089 &Locations[Loc].line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002090 &Locations[Loc].column, 0, 0)))
Douglas Gregor082c3e62010-01-15 19:40:17 +00002091 return errorCode;
2092 }
Ted Kremenek29004672010-02-17 00:41:32 +00002093
2094 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002095 &num_unsaved_files))
2096 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00002097
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002098 if (getenv("CINDEXTEST_EDITING"))
2099 Repeats = 5;
2100
2101 /* Parse the translation unit. When we're testing clang_getCursor() after
2102 reparsing, don't remap unsaved files until the second parse. */
2103 CIdx = clang_createIndex(1, 1);
2104 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2105 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002106 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002107 unsaved_files,
2108 Repeats > 1? 0 : num_unsaved_files,
2109 getDefaultParsingOptions());
2110
Douglas Gregor082c3e62010-01-15 19:40:17 +00002111 if (!TU) {
2112 fprintf(stderr, "unable to parse input\n");
2113 return -1;
2114 }
Ted Kremenek29004672010-02-17 00:41:32 +00002115
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002116 if (checkForErrors(TU) != 0)
2117 return -1;
2118
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002119 for (I = 0; I != Repeats; ++I) {
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002120 if (Repeats > 1 &&
2121 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2122 clang_defaultReparseOptions(TU))) {
2123 clang_disposeTranslationUnit(TU);
2124 return 1;
2125 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002126
2127 if (checkForErrors(TU) != 0)
2128 return -1;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002129
2130 for (Loc = 0; Loc < NumLocations; ++Loc) {
2131 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2132 if (!file)
2133 continue;
Ted Kremenek29004672010-02-17 00:41:32 +00002134
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002135 Cursor = clang_getCursor(TU,
2136 clang_getLocation(TU, file, Locations[Loc].line,
2137 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002138
2139 if (checkForErrors(TU) != 0)
2140 return -1;
2141
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002142 if (I + 1 == Repeats) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002143 CXCompletionString completionString = clang_getCursorCompletionString(
2144 Cursor);
2145 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002146 CXString Spelling;
2147 const char *cspell;
2148 unsigned line, column;
2149 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2150 printf("%d:%d ", line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002151 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002152 PrintCursorExtent(Cursor);
2153 Spelling = clang_getCursorSpelling(Cursor);
2154 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002155 if (cspell && strlen(cspell) != 0) {
2156 unsigned pieceIndex;
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002157 printf(" Spelling=%s (", cspell);
2158 for (pieceIndex = 0; ; ++pieceIndex) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002159 CXSourceRange range =
2160 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002161 if (clang_Range_isNull(range))
2162 break;
2163 PrintRange(range, 0);
2164 }
2165 printf(")");
2166 }
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002167 clang_disposeString(Spelling);
Argyrios Kyrtzidis210f29f2012-03-30 22:15:48 +00002168 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2169 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002170 if (clang_Cursor_isDynamicCall(Cursor))
2171 printf(" Dynamic-call");
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002172 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002173 CXType T = clang_Cursor_getReceiverType(Cursor);
2174 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002175 printf(" Receiver-type=%s", clang_getCString(S));
2176 clang_disposeString(S);
2177 }
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002178
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002179 {
2180 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002181 CXFile astFile;
2182 CXString name, astFilename;
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002183 unsigned i, numHeaders;
2184 if (mod) {
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002185 astFile = clang_Module_getASTFile(mod);
2186 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002187 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002188 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002189 printf(" ModuleName=%s (%s) Headers(%d):",
2190 clang_getCString(name), clang_getCString(astFilename),
2191 numHeaders);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002192 clang_disposeString(name);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002193 clang_disposeString(astFilename);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002194 for (i = 0; i < numHeaders; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002195 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2196 CXString filename = clang_getFileName(file);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002197 printf("\n%s", clang_getCString(filename));
2198 clang_disposeString(filename);
2199 }
2200 }
2201 }
2202
Douglas Gregor3f35bb22011-08-04 20:04:59 +00002203 if (completionString != NULL) {
2204 printf("\nCompletion string: ");
2205 print_completion_string(completionString, stdout);
2206 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002207 printf("\n");
2208 free(Locations[Loc].filename);
2209 }
2210 }
Douglas Gregor082c3e62010-01-15 19:40:17 +00002211 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002212
Douglas Gregor33cdd812010-02-18 18:08:43 +00002213 PrintDiagnostics(TU);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002214 clang_disposeTranslationUnit(TU);
2215 clang_disposeIndex(CIdx);
2216 free(Locations);
2217 free_remapped_files(unsaved_files, num_unsaved_files);
2218 return 0;
2219}
2220
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002221static enum CXVisitorResult findFileRefsVisit(void *context,
2222 CXCursor cursor, CXSourceRange range) {
2223 if (clang_Range_isNull(range))
2224 return CXVisit_Continue;
2225
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002226 PrintCursor(cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002227 PrintRange(range, "");
2228 printf("\n");
2229 return CXVisit_Continue;
2230}
2231
2232static int find_file_refs_at(int argc, const char **argv) {
2233 CXIndex CIdx;
2234 int errorCode;
2235 struct CXUnsavedFile *unsaved_files = 0;
2236 int num_unsaved_files = 0;
2237 CXTranslationUnit TU;
2238 CXCursor Cursor;
2239 CursorSourceLocation *Locations = 0;
2240 unsigned NumLocations = 0, Loc;
2241 unsigned Repeats = 1;
2242 unsigned I;
2243
2244 /* Count the number of locations. */
2245 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2246 ++NumLocations;
2247
2248 /* Parse the locations. */
2249 assert(NumLocations > 0 && "Unable to count locations?");
2250 Locations = (CursorSourceLocation *)malloc(
2251 NumLocations * sizeof(CursorSourceLocation));
2252 for (Loc = 0; Loc < NumLocations; ++Loc) {
2253 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2254 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2255 &Locations[Loc].line,
2256 &Locations[Loc].column, 0, 0)))
2257 return errorCode;
2258 }
2259
2260 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2261 &num_unsaved_files))
2262 return -1;
2263
2264 if (getenv("CINDEXTEST_EDITING"))
2265 Repeats = 5;
2266
2267 /* Parse the translation unit. When we're testing clang_getCursor() after
2268 reparsing, don't remap unsaved files until the second parse. */
2269 CIdx = clang_createIndex(1, 1);
2270 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2271 argv + num_unsaved_files + 1 + NumLocations,
2272 argc - num_unsaved_files - 2 - NumLocations,
2273 unsaved_files,
2274 Repeats > 1? 0 : num_unsaved_files,
2275 getDefaultParsingOptions());
2276
2277 if (!TU) {
2278 fprintf(stderr, "unable to parse input\n");
2279 return -1;
2280 }
2281
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002282 if (checkForErrors(TU) != 0)
2283 return -1;
2284
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002285 for (I = 0; I != Repeats; ++I) {
2286 if (Repeats > 1 &&
2287 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2288 clang_defaultReparseOptions(TU))) {
2289 clang_disposeTranslationUnit(TU);
2290 return 1;
2291 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002292
2293 if (checkForErrors(TU) != 0)
2294 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002295
2296 for (Loc = 0; Loc < NumLocations; ++Loc) {
2297 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2298 if (!file)
2299 continue;
2300
2301 Cursor = clang_getCursor(TU,
2302 clang_getLocation(TU, file, Locations[Loc].line,
2303 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002304
2305 if (checkForErrors(TU) != 0)
2306 return -1;
2307
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002308 if (I + 1 == Repeats) {
Erik Verbruggen338b55c2011-10-06 11:38:08 +00002309 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002310 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002311 printf("\n");
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002312 clang_findReferencesInFile(Cursor, file, visitor);
2313 free(Locations[Loc].filename);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002314
2315 if (checkForErrors(TU) != 0)
2316 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002317 }
2318 }
2319 }
2320
2321 PrintDiagnostics(TU);
2322 clang_disposeTranslationUnit(TU);
2323 clang_disposeIndex(CIdx);
2324 free(Locations);
2325 free_remapped_files(unsaved_files, num_unsaved_files);
2326 return 0;
2327}
2328
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002329static enum CXVisitorResult findFileIncludesVisit(void *context,
2330 CXCursor cursor, CXSourceRange range) {
2331 PrintCursor(cursor, NULL);
2332 PrintRange(range, "");
2333 printf("\n");
2334 return CXVisit_Continue;
2335}
2336
2337static int find_file_includes_in(int argc, const char **argv) {
2338 CXIndex CIdx;
2339 struct CXUnsavedFile *unsaved_files = 0;
2340 int num_unsaved_files = 0;
2341 CXTranslationUnit TU;
2342 const char **Filenames = 0;
2343 unsigned NumFilenames = 0;
2344 unsigned Repeats = 1;
2345 unsigned I, FI;
2346
2347 /* Count the number of locations. */
2348 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2349 ++NumFilenames;
2350
2351 /* Parse the locations. */
2352 assert(NumFilenames > 0 && "Unable to count filenames?");
2353 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2354 for (I = 0; I < NumFilenames; ++I) {
2355 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2356 /* Copy the file name. */
2357 Filenames[I] = input;
2358 }
2359
2360 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2361 &num_unsaved_files))
2362 return -1;
2363
2364 if (getenv("CINDEXTEST_EDITING"))
2365 Repeats = 2;
2366
2367 /* Parse the translation unit. When we're testing clang_getCursor() after
2368 reparsing, don't remap unsaved files until the second parse. */
2369 CIdx = clang_createIndex(1, 1);
2370 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2371 argv + num_unsaved_files + 1 + NumFilenames,
2372 argc - num_unsaved_files - 2 - NumFilenames,
2373 unsaved_files,
2374 Repeats > 1? 0 : num_unsaved_files,
2375 getDefaultParsingOptions());
2376
2377 if (!TU) {
2378 fprintf(stderr, "unable to parse input\n");
2379 return -1;
2380 }
2381
2382 if (checkForErrors(TU) != 0)
2383 return -1;
2384
2385 for (I = 0; I != Repeats; ++I) {
2386 if (Repeats > 1 &&
2387 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2388 clang_defaultReparseOptions(TU))) {
2389 clang_disposeTranslationUnit(TU);
2390 return 1;
2391 }
2392
2393 if (checkForErrors(TU) != 0)
2394 return -1;
2395
2396 for (FI = 0; FI < NumFilenames; ++FI) {
2397 CXFile file = clang_getFile(TU, Filenames[FI]);
2398 if (!file)
2399 continue;
2400
2401 if (checkForErrors(TU) != 0)
2402 return -1;
2403
2404 if (I + 1 == Repeats) {
2405 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2406 clang_findIncludesInFile(TU, file, visitor);
2407
2408 if (checkForErrors(TU) != 0)
2409 return -1;
2410 }
2411 }
2412 }
2413
2414 PrintDiagnostics(TU);
2415 clang_disposeTranslationUnit(TU);
2416 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis1b5b1ce2013-03-11 16:03:17 +00002417 free((void *)Filenames);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002418 free_remapped_files(unsaved_files, num_unsaved_files);
2419 return 0;
2420}
2421
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002422#define MAX_IMPORTED_ASTFILES 200
2423
2424typedef struct {
2425 char **filenames;
2426 unsigned num_files;
2427} ImportedASTFilesData;
2428
2429static ImportedASTFilesData *importedASTs_create() {
2430 ImportedASTFilesData *p;
2431 p = malloc(sizeof(ImportedASTFilesData));
2432 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2433 p->num_files = 0;
2434 return p;
2435}
2436
2437static void importedASTs_dispose(ImportedASTFilesData *p) {
2438 unsigned i;
2439 if (!p)
2440 return;
2441
2442 for (i = 0; i < p->num_files; ++i)
2443 free(p->filenames[i]);
2444 free(p->filenames);
2445 free(p);
2446}
2447
2448static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2449 unsigned i;
2450 assert(p && file);
2451 for (i = 0; i < p->num_files; ++i)
2452 if (strcmp(file, p->filenames[i]) == 0)
2453 return;
2454 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2455 p->filenames[p->num_files++] = strdup(file);
2456}
2457
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002458typedef struct {
2459 const char *check_prefix;
2460 int first_check_printed;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002461 int fail_for_error;
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002462 int abort;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002463 const char *main_filename;
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002464 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002465} IndexData;
2466
2467static void printCheck(IndexData *data) {
2468 if (data->check_prefix) {
2469 if (data->first_check_printed) {
2470 printf("// %s-NEXT: ", data->check_prefix);
2471 } else {
2472 printf("// %s : ", data->check_prefix);
2473 data->first_check_printed = 1;
2474 }
2475 }
2476}
2477
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002478static void printCXIndexFile(CXIdxClientFile file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002479 CXString filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002480 printf("%s", clang_getCString(filename));
2481 clang_disposeString(filename);
2482}
2483
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002484static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2485 IndexData *index_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002486 CXString filename;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002487 const char *cname;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002488 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002489 unsigned line, column;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002490 int isMainFile;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002491
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002492 index_data = (IndexData *)client_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002493 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2494 if (line == 0) {
Argyrios Kyrtzidis9f571862012-10-11 19:00:44 +00002495 printf("<invalid>");
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002496 return;
2497 }
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002498 if (!file) {
2499 printf("<no idxfile>");
2500 return;
2501 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002502 filename = clang_getFileName((CXFile)file);
2503 cname = clang_getCString(filename);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002504 if (strcmp(cname, index_data->main_filename) == 0)
2505 isMainFile = 1;
2506 else
2507 isMainFile = 0;
2508 clang_disposeString(filename);
2509
2510 if (!isMainFile) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002511 printCXIndexFile(file);
2512 printf(":");
2513 }
2514 printf("%d:%d", line, column);
2515}
2516
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002517static unsigned digitCount(unsigned val) {
2518 unsigned c = 1;
2519 while (1) {
2520 if (val < 10)
2521 return c;
2522 ++c;
2523 val /= 10;
2524 }
2525}
2526
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002527static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2528 CXIdxLoc loc) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002529 const char *name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002530 char *newStr;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002531 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002532 unsigned line, column;
2533
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002534 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002535 if (!name)
2536 name = "<anon-tag>";
2537
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002538 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidis90068072011-10-20 17:21:46 +00002539 /* FIXME: free these.*/
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002540 newStr = (char *)malloc(strlen(name) +
2541 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002542 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002543 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002544}
2545
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002546static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2547 CXIdxClientContainer container;
2548 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidisdf15c202011-11-16 02:35:05 +00002549 if (!container)
2550 printf("[<<NULL>>]");
2551 else
2552 printf("[%s]", (const char *)container);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002553}
2554
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002555static const char *getEntityKindString(CXIdxEntityKind kind) {
2556 switch (kind) {
2557 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2558 case CXIdxEntity_Typedef: return "typedef";
2559 case CXIdxEntity_Function: return "function";
2560 case CXIdxEntity_Variable: return "variable";
2561 case CXIdxEntity_Field: return "field";
2562 case CXIdxEntity_EnumConstant: return "enumerator";
2563 case CXIdxEntity_ObjCClass: return "objc-class";
2564 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2565 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002566 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2567 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002568 case CXIdxEntity_ObjCProperty: return "objc-property";
2569 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2570 case CXIdxEntity_Enum: return "enum";
2571 case CXIdxEntity_Struct: return "struct";
2572 case CXIdxEntity_Union: return "union";
2573 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002574 case CXIdxEntity_CXXNamespace: return "namespace";
2575 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2576 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2577 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2578 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2579 case CXIdxEntity_CXXConstructor: return "constructor";
2580 case CXIdxEntity_CXXDestructor: return "destructor";
2581 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2582 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikiedcefd952012-08-31 21:55:26 +00002583 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002584 }
2585 assert(0 && "Garbage entity kind");
2586 return 0;
2587}
2588
2589static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2590 switch (kind) {
2591 case CXIdxEntity_NonTemplate: return "";
2592 case CXIdxEntity_Template: return "-template";
2593 case CXIdxEntity_TemplatePartialSpecialization:
2594 return "-template-partial-spec";
2595 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002596 }
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002597 assert(0 && "Garbage entity kind");
2598 return 0;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002599}
2600
Argyrios Kyrtzidis52002882011-12-07 20:44:12 +00002601static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2602 switch (kind) {
2603 case CXIdxEntityLang_None: return "<none>";
2604 case CXIdxEntityLang_C: return "C";
2605 case CXIdxEntityLang_ObjC: return "ObjC";
2606 case CXIdxEntityLang_CXX: return "C++";
2607 }
2608 assert(0 && "Garbage language kind");
2609 return 0;
2610}
2611
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002612static void printEntityInfo(const char *cb,
2613 CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002614 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002615 const char *name;
2616 IndexData *index_data;
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002617 unsigned i;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002618 index_data = (IndexData *)client_data;
2619 printCheck(index_data);
2620
Argyrios Kyrtzidise4acd232011-11-16 02:34:59 +00002621 if (!info) {
2622 printf("%s: <<NULL>>", cb);
2623 return;
2624 }
2625
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002626 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002627 if (!name)
2628 name = "<anon-tag>";
2629
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002630 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2631 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002632 printf(" | name: %s", name);
2633 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002634 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002635
2636 for (i = 0; i != info->numAttributes; ++i) {
2637 const CXIdxAttrInfo *Attr = info->attributes[i];
2638 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002639 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002640 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002641}
2642
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002643static void printBaseClassInfo(CXClientData client_data,
2644 const CXIdxBaseClassInfo *info) {
2645 printEntityInfo(" <base>", client_data, info->base);
2646 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002647 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002648 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002649 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002650}
2651
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002652static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2653 CXClientData client_data) {
2654 unsigned i;
2655 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2656 printEntityInfo(" <protocol>", client_data,
2657 ProtoInfo->protocols[i]->protocol);
2658 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002659 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002660 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002661 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002662 printf("\n");
2663 }
2664}
2665
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002666static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002667 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002668 CXString str;
2669 const char *cstr;
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002670 unsigned numDiags, i;
2671 CXDiagnostic diag;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002672 IndexData *index_data;
2673 index_data = (IndexData *)client_data;
2674 printCheck(index_data);
2675
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002676 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2677 for (i = 0; i != numDiags; ++i) {
2678 diag = clang_getDiagnosticInSet(diagSet, i);
2679 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2680 cstr = clang_getCString(str);
2681 printf("[diagnostic]: %s\n", cstr);
2682 clang_disposeString(str);
2683
2684 if (getenv("CINDEXTEST_FAILONERROR") &&
2685 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2686 index_data->fail_for_error = 1;
2687 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002688 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002689}
2690
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002691static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2692 CXFile file, void *reserved) {
2693 IndexData *index_data;
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002694 CXString filename;
2695
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002696 index_data = (IndexData *)client_data;
2697 printCheck(index_data);
2698
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002699 filename = clang_getFileName(file);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002700 index_data->main_filename = clang_getCString(filename);
2701 clang_disposeString(filename);
2702
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002703 printf("[enteredMainFile]: ");
2704 printCXIndexFile((CXIdxClientFile)file);
2705 printf("\n");
2706
2707 return (CXIdxClientFile)file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002708}
2709
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002710static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002711 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002712 IndexData *index_data;
2713 index_data = (IndexData *)client_data;
2714 printCheck(index_data);
2715
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002716 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002717 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002718 printf(" | name: \"%s\"", info->filename);
2719 printf(" | hash loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002720 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis5e2ec482012-10-18 00:17:05 +00002721 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2722 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002723
2724 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002725}
2726
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002727static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2728 const CXIdxImportedASTFileInfo *info) {
2729 IndexData *index_data;
2730 index_data = (IndexData *)client_data;
2731 printCheck(index_data);
2732
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002733 if (index_data->importedASTs) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002734 CXString filename = clang_getFileName(info->file);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002735 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2736 clang_disposeString(filename);
2737 }
2738
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002739 printf("[importedASTFile]: ");
2740 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002741 if (info->module) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002742 CXString name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002743 printf(" | loc: ");
2744 printCXIndexLoc(info->loc, client_data);
2745 printf(" | name: \"%s\"", clang_getCString(name));
2746 printf(" | isImplicit: %d\n", info->isImplicit);
2747 clang_disposeString(name);
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002748 } else {
NAKAMURA Takumie259d912012-10-12 14:25:52 +00002749 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002750 printf("\n");
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002751 }
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002752
2753 return (CXIdxClientFile)info->file;
2754}
2755
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002756static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002757 void *reserved) {
2758 IndexData *index_data;
2759 index_data = (IndexData *)client_data;
2760 printCheck(index_data);
2761
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002762 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002763 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002764}
2765
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002766static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002767 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002768 IndexData *index_data;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002769 const CXIdxObjCCategoryDeclInfo *CatInfo;
2770 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002771 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002772 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002773 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002774 unsigned i;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002775 index_data = (IndexData *)client_data;
2776
2777 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2778 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002779 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002780 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002781 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis663c8ec2011-12-07 20:44:19 +00002782 printf(" | semantic-container: ");
2783 printCXIndexContainer(info->semanticContainer);
2784 printf(" | lexical-container: ");
2785 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002786 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002787 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002788 if (info->flags & CXIdxDeclFlag_Skipped) {
2789 assert(!info->isContainer);
2790 printf(" | isContainer: skipped");
2791 } else {
2792 printf(" | isContainer: %d", info->isContainer);
2793 }
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002794 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002795
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002796 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi2a4859a2011-11-18 00:51:03 +00002797 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002798 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002799 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002800 printf("\n");
2801 }
2802
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002803 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2804 const char *kindName = 0;
2805 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2806 switch (K) {
2807 case CXIdxObjCContainer_ForwardRef:
2808 kindName = "forward-ref"; break;
2809 case CXIdxObjCContainer_Interface:
2810 kindName = "interface"; break;
2811 case CXIdxObjCContainer_Implementation:
2812 kindName = "implementation"; break;
2813 }
2814 printCheck(index_data);
2815 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2816 }
2817
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002818 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002819 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2820 CatInfo->objcClass);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002821 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002822 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002823 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002824 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002825 printf("\n");
2826 }
2827
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002828 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2829 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002830 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002831 printf("\n");
2832 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002833 }
2834
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002835 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2836 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002837 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002838
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002839 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2840 if (PropInfo->getter) {
2841 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2842 printf("\n");
2843 }
2844 if (PropInfo->setter) {
2845 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2846 printf("\n");
2847 }
2848 }
2849
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002850 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2851 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2852 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2853 printf("\n");
2854 }
2855 }
2856
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002857 if (info->declAsContainer)
2858 clang_index_setClientContainer(info->declAsContainer,
2859 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002860}
2861
2862static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002863 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002864 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002865 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002866 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002867 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002868 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002869 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002870 printf(" | container: ");
2871 printCXIndexContainer(info->container);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002872 printf(" | refkind: ");
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002873 switch (info->kind) {
2874 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002875 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002876 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002877 printf("\n");
2878}
2879
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002880static int index_abortQuery(CXClientData client_data, void *reserved) {
2881 IndexData *index_data;
2882 index_data = (IndexData *)client_data;
2883 return index_data->abort;
2884}
2885
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002886static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002887 index_abortQuery,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002888 index_diagnostic,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002889 index_enteredMainFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002890 index_ppIncludedFile,
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002891 index_importedASTFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002892 index_startedTranslationUnit,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002893 index_indexDeclaration,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002894 index_indexEntityReference
2895};
2896
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00002897static unsigned getIndexOptions(void) {
2898 unsigned index_opts;
2899 index_opts = 0;
2900 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2901 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2902 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2903 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002904 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
2905 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00002906
2907 return index_opts;
2908}
2909
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002910static int index_compile_args(int num_args, const char **args,
2911 CXIndexAction idxAction,
2912 ImportedASTFilesData *importedASTs,
2913 const char *check_prefix) {
2914 IndexData index_data;
2915 unsigned index_opts;
2916 int result;
2917
2918 if (num_args == 0) {
2919 fprintf(stderr, "no compiler arguments\n");
2920 return -1;
2921 }
2922
2923 index_data.check_prefix = check_prefix;
2924 index_data.first_check_printed = 0;
2925 index_data.fail_for_error = 0;
2926 index_data.abort = 0;
2927 index_data.main_filename = "";
2928 index_data.importedASTs = importedASTs;
2929
2930 index_opts = getIndexOptions();
2931 result = clang_indexSourceFile(idxAction, &index_data,
2932 &IndexCB,sizeof(IndexCB), index_opts,
2933 0, args, num_args, 0, 0, 0,
2934 getDefaultParsingOptions());
2935 if (index_data.fail_for_error)
2936 result = -1;
2937
2938 return result;
2939}
2940
2941static int index_ast_file(const char *ast_file,
2942 CXIndex Idx,
2943 CXIndexAction idxAction,
2944 ImportedASTFilesData *importedASTs,
2945 const char *check_prefix) {
2946 CXTranslationUnit TU;
2947 IndexData index_data;
2948 unsigned index_opts;
2949 int result;
2950
2951 if (!CreateTranslationUnit(Idx, ast_file, &TU))
2952 return -1;
2953
2954 index_data.check_prefix = check_prefix;
2955 index_data.first_check_printed = 0;
2956 index_data.fail_for_error = 0;
2957 index_data.abort = 0;
2958 index_data.main_filename = "";
2959 index_data.importedASTs = importedASTs;
2960
2961 index_opts = getIndexOptions();
2962 result = clang_indexTranslationUnit(idxAction, &index_data,
2963 &IndexCB,sizeof(IndexCB),
2964 index_opts, TU);
2965 if (index_data.fail_for_error)
2966 result = -1;
2967
2968 clang_disposeTranslationUnit(TU);
2969 return result;
2970}
2971
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002972static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002973 const char *check_prefix;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002974 CXIndex Idx;
2975 CXIndexAction idxAction;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002976 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002977 int result;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002978
2979 check_prefix = 0;
2980 if (argc > 0) {
2981 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2982 check_prefix = argv[0] + strlen("-check-prefix=");
2983 ++argv;
2984 --argc;
2985 }
2986 }
2987
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002988 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00002989 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002990 fprintf(stderr, "Could not create Index\n");
2991 return 1;
2992 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002993 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002994 importedASTs = 0;
2995 if (full)
2996 importedASTs = importedASTs_create();
2997
2998 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
2999 if (result != 0)
3000 goto finished;
3001
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003002 if (full) {
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003003 unsigned i;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003004 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
3005 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
3006 importedASTs, check_prefix);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003007 }
3008 }
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003009
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003010finished:
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003011 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003012 clang_IndexAction_dispose(idxAction);
3013 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003014 return result;
3015}
3016
3017static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003018 const char *check_prefix;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003019 CXIndex Idx;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003020 CXIndexAction idxAction;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003021 int result;
3022
3023 check_prefix = 0;
3024 if (argc > 0) {
3025 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3026 check_prefix = argv[0] + strlen("-check-prefix=");
3027 ++argv;
3028 --argc;
3029 }
3030 }
3031
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003032 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003033 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003034 fprintf(stderr, "Could not create Index\n");
3035 return 1;
3036 }
3037 idxAction = clang_IndexAction_create(Idx);
3038
3039 result = index_ast_file(argv[0], Idx, idxAction,
3040 /*importedASTs=*/0, check_prefix);
3041
3042 clang_IndexAction_dispose(idxAction);
3043 clang_disposeIndex(Idx);
3044 return result;
3045}
3046
3047static int index_compile_db(int argc, const char **argv) {
3048 const char *check_prefix;
3049 CXIndex Idx;
3050 CXIndexAction idxAction;
3051 int errorCode = 0;
3052
3053 check_prefix = 0;
3054 if (argc > 0) {
3055 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3056 check_prefix = argv[0] + strlen("-check-prefix=");
3057 ++argv;
3058 --argc;
3059 }
3060 }
3061
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003062 if (argc == 0) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003063 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003064 return -1;
3065 }
3066
3067 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003068 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003069 fprintf(stderr, "Could not create Index\n");
3070 return 1;
3071 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003072 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003073
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003074 {
3075 const char *database = argv[0];
3076 CXCompilationDatabase db = 0;
3077 CXCompileCommands CCmds = 0;
3078 CXCompileCommand CCmd;
3079 CXCompilationDatabase_Error ec;
3080 CXString wd;
3081#define MAX_COMPILE_ARGS 512
3082 CXString cxargs[MAX_COMPILE_ARGS];
3083 const char *args[MAX_COMPILE_ARGS];
3084 char *tmp;
3085 unsigned len;
3086 char *buildDir;
3087 int i, a, numCmds, numArgs;
3088
3089 len = strlen(database);
3090 tmp = (char *) malloc(len+1);
3091 memcpy(tmp, database, len+1);
3092 buildDir = dirname(tmp);
3093
3094 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3095
3096 if (db) {
3097
3098 if (ec!=CXCompilationDatabase_NoError) {
3099 printf("unexpected error %d code while loading compilation database\n", ec);
3100 errorCode = -1;
3101 goto cdb_end;
3102 }
3103
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003104 if (chdir(buildDir) != 0) {
3105 printf("Could not chdir to %s\n", buildDir);
3106 errorCode = -1;
3107 goto cdb_end;
3108 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003109
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003110 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003111 if (!CCmds) {
3112 printf("compilation db is empty\n");
3113 errorCode = -1;
3114 goto cdb_end;
3115 }
3116
3117 numCmds = clang_CompileCommands_getSize(CCmds);
3118
3119 if (numCmds==0) {
3120 fprintf(stderr, "should not get an empty compileCommand set\n");
3121 errorCode = -1;
3122 goto cdb_end;
3123 }
3124
3125 for (i=0; i<numCmds && errorCode == 0; ++i) {
3126 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3127
3128 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003129 if (chdir(clang_getCString(wd)) != 0) {
3130 printf("Could not chdir to %s\n", clang_getCString(wd));
3131 errorCode = -1;
3132 goto cdb_end;
3133 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003134 clang_disposeString(wd);
3135
3136 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3137 if (numArgs > MAX_COMPILE_ARGS){
3138 fprintf(stderr, "got more compile arguments than maximum\n");
3139 errorCode = -1;
3140 goto cdb_end;
3141 }
3142 for (a=0; a<numArgs; ++a) {
3143 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3144 args[a] = clang_getCString(cxargs[a]);
3145 }
3146
3147 errorCode = index_compile_args(numArgs, args, idxAction,
3148 /*importedASTs=*/0, check_prefix);
3149
3150 for (a=0; a<numArgs; ++a)
3151 clang_disposeString(cxargs[a]);
3152 }
3153 } else {
3154 printf("database loading failed with error code %d.\n", ec);
3155 errorCode = -1;
3156 }
3157
3158 cdb_end:
3159 clang_CompileCommands_dispose(CCmds);
3160 clang_CompilationDatabase_dispose(db);
3161 free(tmp);
3162
3163 }
3164
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003165 clang_IndexAction_dispose(idxAction);
3166 clang_disposeIndex(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003167 return errorCode;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003168}
3169
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003170int perform_token_annotation(int argc, const char **argv) {
3171 const char *input = argv[1];
3172 char *filename = 0;
3173 unsigned line, second_line;
3174 unsigned column, second_column;
3175 CXIndex CIdx;
3176 CXTranslationUnit TU = 0;
3177 int errorCode;
3178 struct CXUnsavedFile *unsaved_files = 0;
3179 int num_unsaved_files = 0;
3180 CXToken *tokens;
3181 unsigned num_tokens;
3182 CXSourceRange range;
3183 CXSourceLocation startLoc, endLoc;
3184 CXFile file = 0;
3185 CXCursor *cursors = 0;
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003186 CXSkippedRanges *skipped_ranges = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003187 unsigned i;
3188
3189 input += strlen("-test-annotate-tokens=");
3190 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3191 &second_line, &second_column)))
3192 return errorCode;
3193
Richard Smith1ea42eb2012-07-05 08:20:49 +00003194 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3195 free(filename);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003196 return -1;
Richard Smith1ea42eb2012-07-05 08:20:49 +00003197 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003198
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003199 CIdx = clang_createIndex(0, 1);
Douglas Gregor998caea2011-05-06 16:33:08 +00003200 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
3201 argv + num_unsaved_files + 2,
3202 argc - num_unsaved_files - 3,
3203 unsaved_files,
3204 num_unsaved_files,
3205 getDefaultParsingOptions());
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003206 if (!TU) {
3207 fprintf(stderr, "unable to parse input\n");
3208 clang_disposeIndex(CIdx);
3209 free(filename);
3210 free_remapped_files(unsaved_files, num_unsaved_files);
3211 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003212 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003213 errorCode = 0;
3214
Richard Smith1ea42eb2012-07-05 08:20:49 +00003215 if (checkForErrors(TU) != 0) {
3216 errorCode = -1;
3217 goto teardown;
3218 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003219
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003220 if (getenv("CINDEXTEST_EDITING")) {
3221 for (i = 0; i < 5; ++i) {
3222 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3223 clang_defaultReparseOptions(TU))) {
3224 fprintf(stderr, "Unable to reparse translation unit!\n");
3225 errorCode = -1;
3226 goto teardown;
3227 }
3228 }
3229 }
3230
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003231 if (checkForErrors(TU) != 0) {
3232 errorCode = -1;
3233 goto teardown;
3234 }
3235
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003236 file = clang_getFile(TU, filename);
3237 if (!file) {
3238 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3239 errorCode = -1;
3240 goto teardown;
3241 }
3242
3243 startLoc = clang_getLocation(TU, file, line, column);
3244 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003245 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003246 column);
3247 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003248 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003249 }
3250
3251 endLoc = clang_getLocation(TU, file, second_line, second_column);
3252 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003253 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003254 second_line, second_column);
3255 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003256 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003257 }
3258
3259 range = clang_getRange(startLoc, endLoc);
3260 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003261
3262 if (checkForErrors(TU) != 0) {
3263 errorCode = -1;
3264 goto teardown;
3265 }
3266
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003267 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3268 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003269
3270 if (checkForErrors(TU) != 0) {
3271 errorCode = -1;
3272 goto teardown;
3273 }
3274
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003275 skipped_ranges = clang_getSkippedRanges(TU, file);
3276 for (i = 0; i != skipped_ranges->count; ++i) {
3277 unsigned start_line, start_column, end_line, end_column;
3278 clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
3279 0, &start_line, &start_column, 0);
3280 clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
3281 0, &end_line, &end_column, 0);
3282 printf("Skipping: ");
3283 PrintExtent(stdout, start_line, start_column, end_line, end_column);
3284 printf("\n");
3285 }
3286 clang_disposeSkippedRanges(skipped_ranges);
3287
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003288 for (i = 0; i != num_tokens; ++i) {
3289 const char *kind = "<unknown>";
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003290 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3291 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003292 unsigned start_line, start_column, end_line, end_column;
3293
3294 switch (clang_getTokenKind(tokens[i])) {
3295 case CXToken_Punctuation: kind = "Punctuation"; break;
3296 case CXToken_Keyword: kind = "Keyword"; break;
3297 case CXToken_Identifier: kind = "Identifier"; break;
3298 case CXToken_Literal: kind = "Literal"; break;
3299 case CXToken_Comment: kind = "Comment"; break;
3300 }
Douglas Gregor229bebd2010-11-09 06:24:54 +00003301 clang_getSpellingLocation(clang_getRangeStart(extent),
3302 0, &start_line, &start_column, 0);
3303 clang_getSpellingLocation(clang_getRangeEnd(extent),
3304 0, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003305 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Krameraf7ae312012-04-14 09:11:51 +00003306 clang_disposeString(spelling);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003307 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor61656112010-01-26 18:31:56 +00003308 if (!clang_isInvalid(cursors[i].kind)) {
3309 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003310 PrintCursor(cursors[i], NULL);
Douglas Gregor61656112010-01-26 18:31:56 +00003311 }
3312 printf("\n");
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003313 }
3314 free(cursors);
Ted Kremenek983fb5de2010-10-20 21:22:15 +00003315 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003316
3317 teardown:
Douglas Gregor33cdd812010-02-18 18:08:43 +00003318 PrintDiagnostics(TU);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003319 clang_disposeTranslationUnit(TU);
3320 clang_disposeIndex(CIdx);
3321 free(filename);
3322 free_remapped_files(unsaved_files, num_unsaved_files);
3323 return errorCode;
3324}
3325
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003326static int
3327perform_test_compilation_db(const char *database, int argc, const char **argv) {
3328 CXCompilationDatabase db;
3329 CXCompileCommands CCmds;
3330 CXCompileCommand CCmd;
3331 CXCompilationDatabase_Error ec;
3332 CXString wd;
3333 CXString arg;
3334 int errorCode = 0;
3335 char *tmp;
3336 unsigned len;
3337 char *buildDir;
3338 int i, j, a, numCmds, numArgs;
3339
3340 len = strlen(database);
3341 tmp = (char *) malloc(len+1);
3342 memcpy(tmp, database, len+1);
3343 buildDir = dirname(tmp);
3344
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003345 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003346
3347 if (db) {
3348
3349 if (ec!=CXCompilationDatabase_NoError) {
3350 printf("unexpected error %d code while loading compilation database\n", ec);
3351 errorCode = -1;
3352 goto cdb_end;
3353 }
3354
3355 for (i=0; i<argc && errorCode==0; ) {
3356 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003357 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003358
3359 if (!CCmds) {
3360 printf("file %s not found in compilation db\n", argv[i+1]);
3361 errorCode = -1;
3362 break;
3363 }
3364
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003365 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003366
3367 if (numCmds==0) {
3368 fprintf(stderr, "should not get an empty compileCommand set for file"
3369 " '%s'\n", argv[i+1]);
3370 errorCode = -1;
3371 break;
3372 }
3373
3374 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003375 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003376
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003377 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003378 printf("workdir:'%s'", clang_getCString(wd));
3379 clang_disposeString(wd);
3380
3381 printf(" cmdline:'");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003382 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003383 for (a=0; a<numArgs; ++a) {
3384 if (a) printf(" ");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003385 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003386 printf("%s", clang_getCString(arg));
3387 clang_disposeString(arg);
3388 }
3389 printf("'\n");
3390 }
3391
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003392 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003393
3394 i += 2;
3395 }
3396 }
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003397 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003398 } else {
3399 printf("database loading failed with error code %d.\n", ec);
3400 errorCode = -1;
3401 }
3402
3403cdb_end:
3404 free(tmp);
3405
3406 return errorCode;
3407}
3408
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003409/******************************************************************************/
Ted Kremenek599d73a2010-03-25 02:00:39 +00003410/* USR printing. */
3411/******************************************************************************/
3412
3413static int insufficient_usr(const char *kind, const char *usage) {
3414 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3415 return 1;
3416}
3417
3418static unsigned isUSR(const char *s) {
3419 return s[0] == 'c' && s[1] == ':';
3420}
3421
3422static int not_usr(const char *s, const char *arg) {
3423 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3424 return 1;
3425}
3426
3427static void print_usr(CXString usr) {
3428 const char *s = clang_getCString(usr);
3429 printf("%s\n", s);
3430 clang_disposeString(usr);
3431}
3432
3433static void display_usrs() {
3434 fprintf(stderr, "-print-usrs options:\n"
3435 " ObjCCategory <class name> <category name>\n"
3436 " ObjCClass <class name>\n"
3437 " ObjCIvar <ivar name> <class USR>\n"
3438 " ObjCMethod <selector> [0=class method|1=instance method] "
3439 "<class USR>\n"
3440 " ObjCProperty <property name> <class USR>\n"
3441 " ObjCProtocol <protocol name>\n");
3442}
3443
3444int print_usrs(const char **I, const char **E) {
3445 while (I != E) {
3446 const char *kind = *I;
3447 unsigned len = strlen(kind);
3448 switch (len) {
3449 case 8:
3450 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3451 if (I + 2 >= E)
3452 return insufficient_usr(kind, "<ivar name> <class USR>");
3453 if (!isUSR(I[2]))
3454 return not_usr("<class USR>", I[2]);
3455 else {
3456 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003457 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003458 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003459 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3460 }
3461
3462 I += 3;
3463 continue;
3464 }
3465 break;
3466 case 9:
3467 if (memcmp(kind, "ObjCClass", 9) == 0) {
3468 if (I + 1 >= E)
3469 return insufficient_usr(kind, "<class name>");
3470 print_usr(clang_constructUSR_ObjCClass(I[1]));
3471 I += 2;
3472 continue;
3473 }
3474 break;
3475 case 10:
3476 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3477 if (I + 3 >= E)
3478 return insufficient_usr(kind, "<method selector> "
3479 "[0=class method|1=instance method] <class USR>");
3480 if (!isUSR(I[3]))
3481 return not_usr("<class USR>", I[3]);
3482 else {
3483 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003484 x.data = (void*) I[3];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003485 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003486 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3487 }
3488 I += 4;
3489 continue;
3490 }
3491 break;
3492 case 12:
3493 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3494 if (I + 2 >= E)
3495 return insufficient_usr(kind, "<class name> <category name>");
3496 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3497 I += 3;
3498 continue;
3499 }
3500 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3501 if (I + 1 >= E)
3502 return insufficient_usr(kind, "<protocol name>");
3503 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3504 I += 2;
3505 continue;
3506 }
3507 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3508 if (I + 2 >= E)
3509 return insufficient_usr(kind, "<property name> <class USR>");
3510 if (!isUSR(I[2]))
3511 return not_usr("<class USR>", I[2]);
3512 else {
3513 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003514 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003515 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003516 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3517 }
3518 I += 3;
3519 continue;
3520 }
3521 break;
3522 default:
3523 break;
3524 }
3525 break;
3526 }
3527
3528 if (I != E) {
3529 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3530 display_usrs();
3531 return 1;
3532 }
3533 return 0;
3534}
3535
3536int print_usrs_file(const char *file_name) {
3537 char line[2048];
3538 const char *args[128];
3539 unsigned numChars = 0;
3540
3541 FILE *fp = fopen(file_name, "r");
3542 if (!fp) {
3543 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3544 return 1;
3545 }
3546
3547 /* This code is not really all that safe, but it works fine for testing. */
3548 while (!feof(fp)) {
3549 char c = fgetc(fp);
3550 if (c == '\n') {
3551 unsigned i = 0;
3552 const char *s = 0;
3553
3554 if (numChars == 0)
3555 continue;
3556
3557 line[numChars] = '\0';
3558 numChars = 0;
3559
3560 if (line[0] == '/' && line[1] == '/')
3561 continue;
3562
3563 s = strtok(line, " ");
3564 while (s) {
3565 args[i] = s;
3566 ++i;
3567 s = strtok(0, " ");
3568 }
3569 if (print_usrs(&args[0], &args[i]))
3570 return 1;
3571 }
3572 else
3573 line[numChars++] = c;
3574 }
3575
3576 fclose(fp);
3577 return 0;
3578}
3579
3580/******************************************************************************/
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003581/* Command line processing. */
3582/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003583int write_pch_file(const char *filename, int argc, const char *argv[]) {
3584 CXIndex Idx;
3585 CXTranslationUnit TU;
3586 struct CXUnsavedFile *unsaved_files = 0;
3587 int num_unsaved_files = 0;
Francois Pichetabcfbec2011-07-06 22:09:44 +00003588 int result = 0;
Douglas Gregore9386682010-08-13 05:36:37 +00003589
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003590 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregore9386682010-08-13 05:36:37 +00003591
3592 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3593 clang_disposeIndex(Idx);
3594 return -1;
3595 }
3596
3597 TU = clang_parseTranslationUnit(Idx, 0,
3598 argv + num_unsaved_files,
3599 argc - num_unsaved_files,
3600 unsaved_files,
3601 num_unsaved_files,
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00003602 CXTranslationUnit_Incomplete |
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00003603 CXTranslationUnit_DetailedPreprocessingRecord|
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00003604 CXTranslationUnit_ForSerialization);
Douglas Gregore9386682010-08-13 05:36:37 +00003605 if (!TU) {
3606 fprintf(stderr, "Unable to load translation unit!\n");
3607 free_remapped_files(unsaved_files, num_unsaved_files);
3608 clang_disposeIndex(Idx);
3609 return 1;
3610 }
3611
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003612 switch (clang_saveTranslationUnit(TU, filename,
3613 clang_defaultSaveOptions(TU))) {
3614 case CXSaveError_None:
3615 break;
3616
3617 case CXSaveError_TranslationErrors:
3618 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3619 filename);
3620 result = 2;
3621 break;
3622
3623 case CXSaveError_InvalidTU:
3624 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3625 filename);
3626 result = 3;
3627 break;
3628
3629 case CXSaveError_Unknown:
3630 default:
3631 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3632 result = 1;
3633 break;
3634 }
3635
Douglas Gregore9386682010-08-13 05:36:37 +00003636 clang_disposeTranslationUnit(TU);
3637 free_remapped_files(unsaved_files, num_unsaved_files);
3638 clang_disposeIndex(Idx);
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003639 return result;
Douglas Gregore9386682010-08-13 05:36:37 +00003640}
3641
3642/******************************************************************************/
Ted Kremenekd010ba42011-11-10 08:43:12 +00003643/* Serialized diagnostics. */
3644/******************************************************************************/
3645
3646static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3647 switch (error) {
3648 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3649 case CXLoadDiag_None: break;
3650 case CXLoadDiag_Unknown: return "Unknown";
3651 case CXLoadDiag_InvalidFile: return "Invalid File";
3652 }
3653 return "None";
3654}
3655
3656static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3657 switch (severity) {
3658 case CXDiagnostic_Note: return "note";
3659 case CXDiagnostic_Error: return "error";
3660 case CXDiagnostic_Fatal: return "fatal";
3661 case CXDiagnostic_Ignored: return "ignored";
3662 case CXDiagnostic_Warning: return "warning";
3663 }
3664 return "unknown";
3665}
3666
3667static void printIndent(unsigned indent) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003668 if (indent == 0)
3669 return;
3670 fprintf(stderr, "+");
3671 --indent;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003672 while (indent > 0) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003673 fprintf(stderr, "-");
Ted Kremenekd010ba42011-11-10 08:43:12 +00003674 --indent;
3675 }
3676}
3677
3678static void printLocation(CXSourceLocation L) {
3679 CXFile File;
3680 CXString FileName;
3681 unsigned line, column, offset;
3682
3683 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3684 FileName = clang_getFileName(File);
3685
3686 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3687 clang_disposeString(FileName);
3688}
3689
3690static void printRanges(CXDiagnostic D, unsigned indent) {
3691 unsigned i, n = clang_getDiagnosticNumRanges(D);
3692
3693 for (i = 0; i < n; ++i) {
3694 CXSourceLocation Start, End;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003695 CXSourceRange SR = clang_getDiagnosticRange(D, i);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003696 Start = clang_getRangeStart(SR);
3697 End = clang_getRangeEnd(SR);
3698
3699 printIndent(indent);
3700 fprintf(stderr, "Range: ");
3701 printLocation(Start);
3702 fprintf(stderr, " ");
3703 printLocation(End);
3704 fprintf(stderr, "\n");
3705 }
3706}
3707
3708static void printFixIts(CXDiagnostic D, unsigned indent) {
3709 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek4a642302012-03-20 20:49:45 +00003710 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003711 for (i = 0 ; i < n; ++i) {
3712 CXSourceRange ReplacementRange;
3713 CXString text;
3714 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3715
3716 printIndent(indent);
3717 fprintf(stderr, "FIXIT: (");
3718 printLocation(clang_getRangeStart(ReplacementRange));
3719 fprintf(stderr, " - ");
3720 printLocation(clang_getRangeEnd(ReplacementRange));
3721 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3722 clang_disposeString(text);
3723 }
3724}
3725
3726static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003727 unsigned i, n;
3728
Ted Kremenekd010ba42011-11-10 08:43:12 +00003729 if (!Diags)
3730 return;
3731
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003732 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003733 for (i = 0; i < n; ++i) {
3734 CXSourceLocation DiagLoc;
3735 CXDiagnostic D;
3736 CXFile File;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003737 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003738 unsigned line, column, offset;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003739 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003740
3741 D = clang_getDiagnosticInSet(Diags, i);
3742 DiagLoc = clang_getDiagnosticLocation(D);
3743 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3744 FileName = clang_getFileName(File);
3745 DiagSpelling = clang_getDiagnosticSpelling(D);
3746
3747 printIndent(indent);
3748
3749 fprintf(stderr, "%s:%d:%d: %s: %s",
3750 clang_getCString(FileName),
3751 line,
3752 column,
3753 getSeverityString(clang_getDiagnosticSeverity(D)),
3754 clang_getCString(DiagSpelling));
3755
3756 DiagOption = clang_getDiagnosticOption(D, 0);
3757 DiagOptionStr = clang_getCString(DiagOption);
3758 if (DiagOptionStr) {
3759 fprintf(stderr, " [%s]", DiagOptionStr);
3760 }
3761
Ted Kremenek26a6d492012-04-12 00:03:31 +00003762 DiagCat = clang_getDiagnosticCategoryText(D);
3763 DiagCatStr = clang_getCString(DiagCat);
3764 if (DiagCatStr) {
3765 fprintf(stderr, " [%s]", DiagCatStr);
3766 }
3767
Ted Kremenekd010ba42011-11-10 08:43:12 +00003768 fprintf(stderr, "\n");
3769
3770 printRanges(D, indent);
3771 printFixIts(D, indent);
3772
NAKAMURA Takumi27dd3962011-11-10 10:07:57 +00003773 /* Print subdiagnostics. */
Ted Kremenekd010ba42011-11-10 08:43:12 +00003774 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3775
3776 clang_disposeString(FileName);
3777 clang_disposeString(DiagSpelling);
3778 clang_disposeString(DiagOption);
3779 }
3780}
3781
3782static int read_diagnostics(const char *filename) {
3783 enum CXLoadDiag_Error error;
3784 CXString errorString;
3785 CXDiagnosticSet Diags = 0;
3786
3787 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3788 if (!Diags) {
3789 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3790 getDiagnosticCodeStr(error),
3791 clang_getCString(errorString));
3792 clang_disposeString(errorString);
3793 return 1;
3794 }
3795
3796 printDiagnosticSet(Diags, 0);
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003797 fprintf(stderr, "Number of diagnostics: %d\n",
3798 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenekd010ba42011-11-10 08:43:12 +00003799 clang_disposeDiagnosticSet(Diags);
3800 return 0;
3801}
3802
3803/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003804/* Command line processing. */
3805/******************************************************************************/
Ted Kremenekef3339b2009-11-17 18:09:14 +00003806
Douglas Gregor720d0052010-01-20 21:32:04 +00003807static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003808 if (s[0] == '\0')
Douglas Gregor720d0052010-01-20 21:32:04 +00003809 return FilteredPrintingVisitor;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003810 if (strcmp(s, "-usrs") == 0)
3811 return USRVisitor;
Ted Kremenek83f642e2011-04-18 22:47:10 +00003812 if (strncmp(s, "-memory-usage", 13) == 0)
3813 return GetVisitor(s + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003814 return NULL;
3815}
3816
Ted Kremenekef3339b2009-11-17 18:09:14 +00003817static void print_usage(void) {
3818 fprintf(stderr,
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003819 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003820 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregor082c3e62010-01-15 19:40:17 +00003821 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003822 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3823 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi4deb9a92012-10-24 22:52:04 +00003824 fprintf(stderr,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003825 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003826 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003827 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003828 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek0469b7e2009-11-18 02:02:52 +00003829 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen338b55c2011-10-06 11:38:08 +00003830 "[FileCheck prefix]\n");
3831 fprintf(stderr,
Ted Kremeneka44d99c2010-01-05 23:18:49 +00003832 " c-index-test -test-load-tu <AST file> <symbol filter> "
3833 "[FileCheck prefix]\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003834 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3835 "[FileCheck prefix]\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003836 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregor082c3e62010-01-15 19:40:17 +00003837 fprintf(stderr,
Ted Kremenek83f642e2011-04-18 22:47:10 +00003838 " c-index-test -test-load-source-memory-usage "
3839 "<symbol filter> {<args>}*\n"
Douglas Gregoraa21cc42010-07-19 21:46:24 +00003840 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3841 " {<args>}*\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003842 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003843 " c-index-test -test-load-source-usrs-memory-usage "
3844 "<symbol filter> {<args>}*\n"
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00003845 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3846 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek11d1a422011-04-18 23:42:53 +00003847 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth718df592010-07-22 06:29:13 +00003848 fprintf(stderr,
Ted Kremenek11d1a422011-04-18 23:42:53 +00003849 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenko00353722013-02-15 21:15:49 +00003850 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00003851 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00003852 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003853 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregore9386682010-08-13 05:36:37 +00003854 " c-index-test -print-usr-file <file>\n"
Ted Kremenekd010ba42011-11-10 08:43:12 +00003855 " c-index-test -write-pch <file> <compiler arguments>\n");
3856 fprintf(stderr,
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003857 " c-index-test -compilation-db [lookup <filename>] database\n");
3858 fprintf(stderr,
Ted Kremenekd010ba42011-11-10 08:43:12 +00003859 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregor73a18fd2010-07-20 14:34:35 +00003860 fprintf(stderr,
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003861 " <symbol filter> values:\n%s",
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003862 " all - load all symbols, including those from PCH\n"
3863 " local - load all symbols except those in PCH\n"
3864 " category - only load ObjC categories (non-PCH)\n"
3865 " interface - only load ObjC interfaces (non-PCH)\n"
3866 " protocol - only load ObjC protocols (non-PCH)\n"
3867 " function - only load functions (non-PCH)\n"
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00003868 " typedef - only load typdefs (non-PCH)\n"
3869 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekef3339b2009-11-17 18:09:14 +00003870}
3871
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003872/***/
3873
3874int cindextest_main(int argc, const char **argv) {
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003875 clang_enableStackTraces();
Ted Kremenekd010ba42011-11-10 08:43:12 +00003876 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3877 return read_diagnostics(argv[2]);
Ted Kremenekef3339b2009-11-17 18:09:14 +00003878 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor47815d52010-07-12 18:38:41 +00003879 return perform_code_completion(argc, argv, 0);
3880 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3881 return perform_code_completion(argc, argv, 1);
Douglas Gregor082c3e62010-01-15 19:40:17 +00003882 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3883 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00003884 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3885 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003886 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
3887 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003888 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003889 return index_file(argc - 2, argv + 2, /*full=*/0);
3890 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3891 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003892 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3893 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003894 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
3895 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003896 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00003897 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003898 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00003899 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3900 NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003901 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00003902 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3903 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3904 if (I) {
3905 int trials = atoi(argv[2]);
3906 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3907 NULL);
3908 }
3909 }
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003910 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00003911 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek83f642e2011-04-18 22:47:10 +00003912
3913 PostVisitTU postVisit = 0;
3914 if (strstr(argv[1], "-memory-usage"))
3915 postVisit = PrintMemoryUsage;
3916
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003917 if (I)
Ted Kremenek83f642e2011-04-18 22:47:10 +00003918 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3919 postVisit);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003920 }
3921 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek0469b7e2009-11-18 02:02:52 +00003922 return perform_file_scan(argv[2], argv[3],
3923 argc >= 5 ? argv[4] : 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003924 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3925 return perform_token_annotation(argc, argv);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00003926 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3927 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3928 PrintInclusionStack);
3929 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3930 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3931 PrintInclusionStack);
Ted Kremenek83b28a22010-03-03 06:37:58 +00003932 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3933 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3934 NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00003935 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek6bca9842010-05-14 21:29:26 +00003936 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenko00353722013-02-15 21:15:49 +00003937 PrintType, 0);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00003938 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
3939 return perform_test_load_source(argc - 2, argv + 2, "all",
3940 PrintTypeSize, 0);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00003941 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
3942 return perform_test_load_source(argc - 2, argv + 2, "all",
3943 PrintBitWidth, 0);
Ted Kremenek599d73a2010-03-25 02:00:39 +00003944 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3945 if (argc > 2)
3946 return print_usrs(argv + 2, argv + argc);
3947 else {
3948 display_usrs();
3949 return 1;
3950 }
3951 }
3952 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3953 return print_usrs_file(argv[2]);
Douglas Gregore9386682010-08-13 05:36:37 +00003954 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3955 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003956 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3957 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3958
Ted Kremenekef3339b2009-11-17 18:09:14 +00003959 print_usage();
3960 return 1;
Steve Naroffa1c72842009-08-28 15:28:48 +00003961}
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003962
3963/***/
3964
3965/* We intentionally run in a separate thread to ensure we at least minimal
3966 * testing of a multithreaded environment (for example, having a reduced stack
3967 * size). */
3968
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003969typedef struct thread_info {
3970 int argc;
3971 const char **argv;
3972 int result;
3973} thread_info;
Benjamin Kramer112fc6c2010-11-04 19:11:31 +00003974void thread_runner(void *client_data_v) {
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003975 thread_info *client_data = client_data_v;
3976 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumia7d49882012-04-07 06:59:28 +00003977#ifdef __CYGWIN__
3978 fflush(stdout); /* stdout is not flushed on Cygwin. */
3979#endif
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003980}
3981
3982int main(int argc, const char **argv) {
Benjamin Kramer3a913ed2012-08-10 10:06:13 +00003983 thread_info client_data;
3984
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003985#ifdef CLANG_HAVE_LIBXML
3986 LIBXML_TEST_VERSION
3987#endif
3988
Douglas Gregorf428bf82010-10-27 16:00:01 +00003989 if (getenv("CINDEXTEST_NOTHREADS"))
3990 return cindextest_main(argc, argv);
3991
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003992 client_data.argc = argc;
3993 client_data.argv = argv;
Daniel Dunbar23397c32010-11-04 01:26:31 +00003994 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003995 return client_data.result;
3996}