blob: 7f5bf4c2edfac5e047c74568882631d201730d7f [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 Gribenkof430da42014-02-12 10:33:14 +00005#include "clang-c/BuildSystem.h"
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00006#include "llvm/Config/config.h"
Douglas Gregor49f67ce2010-08-26 13:48:20 +00007#include <ctype.h>
Douglas Gregor9eb77012009-11-07 00:00:49 +00008#include <stdlib.h>
Steve Naroff1054e602009-08-31 00:59:03 +00009#include <stdio.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +000010#include <string.h>
Douglas Gregor082c3e62010-01-15 19:40:17 +000011#include <assert.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +000012
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +000013#ifdef CLANG_HAVE_LIBXML
14#include <libxml/parser.h>
15#include <libxml/relaxng.h>
16#include <libxml/xmlerror.h>
17#endif
18
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +000019#ifdef _WIN32
20# include <direct.h>
21#else
22# include <unistd.h>
23#endif
24
Ted Kremenek1cd27d52009-11-17 18:13:31 +000025/******************************************************************************/
26/* Utility functions. */
27/******************************************************************************/
28
John Thompsonde258b52009-10-27 13:42:56 +000029#ifdef _MSC_VER
30char *basename(const char* path)
31{
32 char* base1 = (char*)strrchr(path, '/');
33 char* base2 = (char*)strrchr(path, '\\');
34 if (base1 && base2)
35 return((base1 > base2) ? base1 + 1 : base2 + 1);
36 else if (base1)
37 return(base1 + 1);
38 else if (base2)
39 return(base2 + 1);
40
41 return((char*)path);
42}
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000043char *dirname(char* path)
44{
45 char* base1 = (char*)strrchr(path, '/');
46 char* base2 = (char*)strrchr(path, '\\');
47 if (base1 && base2)
48 if (base1 > base2)
49 *base1 = 0;
50 else
51 *base2 = 0;
52 else if (base1)
NAKAMURA Takumi1e43baa62012-06-30 11:47:18 +000053 *base1 = 0;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000054 else if (base2)
NAKAMURA Takumi1e43baa62012-06-30 11:47:18 +000055 *base2 = 0;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000056
57 return path;
58}
John Thompsonde258b52009-10-27 13:42:56 +000059#else
Steve Naroffa7753c42009-09-24 20:03:06 +000060extern char *basename(const char *);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000061extern char *dirname(char *);
John Thompsonde258b52009-10-27 13:42:56 +000062#endif
Steve Naroffa7753c42009-09-24 20:03:06 +000063
Douglas Gregorf2430ba2010-07-25 17:39:21 +000064/** \brief Return the default parsing options. */
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000065static unsigned getDefaultParsingOptions() {
66 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
67
68 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregor4a47bca2010-08-09 22:28:58 +000069 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregorb14904c2010-08-13 22:48:40 +000070 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
71 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidiscb373e32011-11-03 02:20:25 +000072 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
73 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6e922512012-04-12 10:11:59 +000074 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
75 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenko3292d062012-07-02 17:35:10 +000076 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
77 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000078
79 return options;
80}
81
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +000082static int checkForErrors(CXTranslationUnit TU);
83
Daniel Dunbar98c07e02010-02-14 08:32:24 +000084static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
85 unsigned end_line, unsigned end_column) {
86 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbar02968e52010-02-14 10:02:57 +000087 end_line, end_column);
Daniel Dunbar98c07e02010-02-14 08:32:24 +000088}
89
Ted Kremenek2df52dc2009-11-17 19:37:36 +000090static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
91 CXTranslationUnit *TU) {
Ted Kremenek29004672010-02-17 00:41:32 +000092
Douglas Gregor33cdd812010-02-18 18:08:43 +000093 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohmane83f6242010-07-26 21:44:15 +000094 if (!*TU) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +000095 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
96 return 0;
Ted Kremenek29004672010-02-17 00:41:32 +000097 }
Ted Kremenek2df52dc2009-11-17 19:37:36 +000098 return 1;
99}
100
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000101void free_remapped_files(struct CXUnsavedFile *unsaved_files,
102 int num_unsaved_files) {
103 int i;
104 for (i = 0; i != num_unsaved_files; ++i) {
105 free((char *)unsaved_files[i].Filename);
106 free((char *)unsaved_files[i].Contents);
107 }
Douglas Gregor0e3da272010-08-19 20:50:29 +0000108 free(unsaved_files);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000109}
110
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000111static int parse_remapped_files_with_opt(const char *opt_name,
112 int argc, const char **argv,
113 int start_arg,
114 struct CXUnsavedFile **unsaved_files,
115 int *num_unsaved_files) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000116 int i;
117 int arg;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000118 int prefix_len = strlen(opt_name);
119 int arg_indices[20];
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000120 *unsaved_files = 0;
121 *num_unsaved_files = 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000122
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000123 /* Count the number of remapped files. */
124 for (arg = start_arg; arg < argc; ++arg) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000125 if (strncmp(argv[arg], opt_name, prefix_len))
126 continue;
Ted Kremenek29004672010-02-17 00:41:32 +0000127
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000128 assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int)));
129 arg_indices[*num_unsaved_files] = arg;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000130 ++*num_unsaved_files;
131 }
Ted Kremenek29004672010-02-17 00:41:32 +0000132
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000133 if (*num_unsaved_files == 0)
134 return 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000135
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000136 *unsaved_files
Douglas Gregor0e3da272010-08-19 20:50:29 +0000137 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
138 *num_unsaved_files);
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000139 for (i = 0; i != *num_unsaved_files; ++i) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000140 struct CXUnsavedFile *unsaved = *unsaved_files + i;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000141 const char *arg_string = argv[arg_indices[i]] + prefix_len;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000142 int filename_len;
143 char *filename;
144 char *contents;
145 FILE *to_file;
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000146 const char *sep = strchr(arg_string, ',');
147 if (!sep) {
Ted Kremenek29004672010-02-17 00:41:32 +0000148 fprintf(stderr,
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000149 "error: %sfrom:to argument is missing comma\n", opt_name);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000150 free_remapped_files(*unsaved_files, i);
151 *unsaved_files = 0;
152 *num_unsaved_files = 0;
153 return -1;
154 }
Ted Kremenek29004672010-02-17 00:41:32 +0000155
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000156 /* Open the file that we're remapping to. */
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000157 to_file = fopen(sep + 1, "rb");
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000158 if (!to_file) {
159 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000160 sep + 1);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000161 free_remapped_files(*unsaved_files, i);
162 *unsaved_files = 0;
163 *num_unsaved_files = 0;
164 return -1;
165 }
Ted Kremenek29004672010-02-17 00:41:32 +0000166
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000167 /* Determine the length of the file we're remapping to. */
168 fseek(to_file, 0, SEEK_END);
169 unsaved->Length = ftell(to_file);
170 fseek(to_file, 0, SEEK_SET);
Ted Kremenek29004672010-02-17 00:41:32 +0000171
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000172 /* Read the contents of the file we're remapping to. */
173 contents = (char *)malloc(unsaved->Length + 1);
174 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
175 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000176 (feof(to_file) ? "EOF" : "error"), sep + 1);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000177 fclose(to_file);
178 free_remapped_files(*unsaved_files, i);
Richard Smith1ea42eb2012-07-05 08:20:49 +0000179 free(contents);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000180 *unsaved_files = 0;
181 *num_unsaved_files = 0;
182 return -1;
183 }
184 contents[unsaved->Length] = 0;
185 unsaved->Contents = contents;
Ted Kremenek29004672010-02-17 00:41:32 +0000186
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000187 /* Close the file. */
188 fclose(to_file);
Ted Kremenek29004672010-02-17 00:41:32 +0000189
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000190 /* Copy the file name that we're remapping from. */
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000191 filename_len = sep - arg_string;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000192 filename = (char *)malloc(filename_len + 1);
193 memcpy(filename, arg_string, filename_len);
194 filename[filename_len] = 0;
195 unsaved->Filename = filename;
196 }
Ted Kremenek29004672010-02-17 00:41:32 +0000197
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000198 return 0;
199}
200
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000201static int parse_remapped_files(int argc, const char **argv, int start_arg,
202 struct CXUnsavedFile **unsaved_files,
203 int *num_unsaved_files) {
204 return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg,
205 unsaved_files, num_unsaved_files);
206}
207
208static int parse_remapped_files_with_try(int try_idx,
209 int argc, const char **argv,
210 int start_arg,
211 struct CXUnsavedFile **unsaved_files,
212 int *num_unsaved_files) {
213 struct CXUnsavedFile *unsaved_files_no_try_idx;
214 int num_unsaved_files_no_try_idx;
215 struct CXUnsavedFile *unsaved_files_try_idx;
216 int num_unsaved_files_try_idx;
217 int ret;
218 char opt_name[32];
219
220 ret = parse_remapped_files(argc, argv, start_arg,
221 &unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx);
222 if (ret)
223 return ret;
224
225 sprintf(opt_name, "-remap-file-%d=", try_idx);
226 ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
227 &unsaved_files_try_idx, &num_unsaved_files_try_idx);
228 if (ret)
229 return ret;
230
231 *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
232 *unsaved_files
233 = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
234 sizeof(struct CXUnsavedFile) *
235 *num_unsaved_files);
236 memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
237 unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
238 num_unsaved_files_try_idx);
239 free(unsaved_files_try_idx);
240 return 0;
241}
242
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000243static const char *parse_comments_schema(int argc, const char **argv) {
244 const char *CommentsSchemaArg = "-comments-xml-schema=";
245 const char *CommentSchemaFile = NULL;
246
247 if (argc == 0)
248 return CommentSchemaFile;
249
250 if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
251 CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
252
253 return CommentSchemaFile;
254}
255
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000256/******************************************************************************/
257/* Pretty-printing. */
258/******************************************************************************/
259
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000260static const char *FileCheckPrefix = "CHECK";
261
262static void PrintCString(const char *CStr) {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000263 if (CStr != NULL && CStr[0] != '\0') {
264 for ( ; *CStr; ++CStr) {
265 const char C = *CStr;
266 switch (C) {
267 case '\n': printf("\\n"); break;
268 case '\r': printf("\\r"); break;
269 case '\t': printf("\\t"); break;
270 case '\v': printf("\\v"); break;
271 case '\f': printf("\\f"); break;
272 default: putchar(C); break;
273 }
274 }
275 }
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000276}
277
278static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
279 printf(" %s=[", Prefix);
280 PrintCString(CStr);
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000281 printf("]");
282}
283
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000284static void PrintCXStringAndDispose(CXString Str) {
285 PrintCString(clang_getCString(Str));
286 clang_disposeString(Str);
287}
288
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000289static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
290 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
291}
292
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000293static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
294 CXString Str) {
295 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
296 clang_disposeString(Str);
297}
298
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000299static void PrintRange(CXSourceRange R, const char *str) {
300 CXFile begin_file, end_file;
301 unsigned begin_line, begin_column, end_line, end_column;
302
303 clang_getSpellingLocation(clang_getRangeStart(R),
304 &begin_file, &begin_line, &begin_column, 0);
305 clang_getSpellingLocation(clang_getRangeEnd(R),
306 &end_file, &end_line, &end_column, 0);
307 if (!begin_file || !end_file)
308 return;
309
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +0000310 if (str)
311 printf(" %s=", str);
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000312 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
313}
314
Douglas Gregor97c75712010-10-02 22:49:11 +0000315int want_display_name = 0;
316
Douglas Gregord6225d32012-05-08 00:14:45 +0000317static void printVersion(const char *Prefix, CXVersion Version) {
318 if (Version.Major < 0)
319 return;
320 printf("%s%d", Prefix, Version.Major);
321
322 if (Version.Minor < 0)
323 return;
324 printf(".%d", Version.Minor);
325
326 if (Version.Subminor < 0)
327 return;
328 printf(".%d", Version.Subminor);
329}
330
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000331struct CommentASTDumpingContext {
332 int IndentLevel;
333};
334
335static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
336 CXComment Comment) {
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000337 unsigned i;
338 unsigned e;
339 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
340
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000341 Ctx->IndentLevel++;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000342 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000343 printf(" ");
344
345 printf("(");
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000346 switch (Kind) {
347 case CXComment_Null:
348 printf("CXComment_Null");
349 break;
350 case CXComment_Text:
351 printf("CXComment_Text");
352 PrintCXStringWithPrefixAndDispose("Text",
353 clang_TextComment_getText(Comment));
354 if (clang_Comment_isWhitespace(Comment))
355 printf(" IsWhitespace");
356 if (clang_InlineContentComment_hasTrailingNewline(Comment))
357 printf(" HasTrailingNewline");
358 break;
359 case CXComment_InlineCommand:
360 printf("CXComment_InlineCommand");
361 PrintCXStringWithPrefixAndDispose(
362 "CommandName",
363 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000364 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
365 case CXCommentInlineCommandRenderKind_Normal:
366 printf(" RenderNormal");
367 break;
368 case CXCommentInlineCommandRenderKind_Bold:
369 printf(" RenderBold");
370 break;
371 case CXCommentInlineCommandRenderKind_Monospaced:
372 printf(" RenderMonospaced");
373 break;
374 case CXCommentInlineCommandRenderKind_Emphasized:
375 printf(" RenderEmphasized");
376 break;
377 }
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000378 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000379 i != e; ++i) {
380 printf(" Arg[%u]=", i);
381 PrintCXStringAndDispose(
382 clang_InlineCommandComment_getArgText(Comment, i));
383 }
384 if (clang_InlineContentComment_hasTrailingNewline(Comment))
385 printf(" HasTrailingNewline");
386 break;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000387 case CXComment_HTMLStartTag: {
388 unsigned NumAttrs;
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000389 printf("CXComment_HTMLStartTag");
390 PrintCXStringWithPrefixAndDispose(
391 "Name",
392 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000393 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000394 if (NumAttrs != 0) {
395 printf(" Attrs:");
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000396 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000397 printf(" ");
398 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
399 printf("=");
400 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
401 }
402 }
403 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
404 printf(" SelfClosing");
405 if (clang_InlineContentComment_hasTrailingNewline(Comment))
406 printf(" HasTrailingNewline");
407 break;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000408 }
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000409 case CXComment_HTMLEndTag:
410 printf("CXComment_HTMLEndTag");
411 PrintCXStringWithPrefixAndDispose(
412 "Name",
413 clang_HTMLTagComment_getTagName(Comment));
414 if (clang_InlineContentComment_hasTrailingNewline(Comment))
415 printf(" HasTrailingNewline");
416 break;
417 case CXComment_Paragraph:
418 printf("CXComment_Paragraph");
419 if (clang_Comment_isWhitespace(Comment))
420 printf(" IsWhitespace");
421 break;
422 case CXComment_BlockCommand:
423 printf("CXComment_BlockCommand");
424 PrintCXStringWithPrefixAndDispose(
425 "CommandName",
426 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000427 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000428 i != e; ++i) {
429 printf(" Arg[%u]=", i);
430 PrintCXStringAndDispose(
431 clang_BlockCommandComment_getArgText(Comment, i));
432 }
433 break;
434 case CXComment_ParamCommand:
435 printf("CXComment_ParamCommand");
436 switch (clang_ParamCommandComment_getDirection(Comment)) {
437 case CXCommentParamPassDirection_In:
438 printf(" in");
439 break;
440 case CXCommentParamPassDirection_Out:
441 printf(" out");
442 break;
443 case CXCommentParamPassDirection_InOut:
444 printf(" in,out");
445 break;
446 }
447 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
448 printf(" explicitly");
449 else
450 printf(" implicitly");
451 PrintCXStringWithPrefixAndDispose(
452 "ParamName",
453 clang_ParamCommandComment_getParamName(Comment));
454 if (clang_ParamCommandComment_isParamIndexValid(Comment))
455 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
456 else
457 printf(" ParamIndex=Invalid");
458 break;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000459 case CXComment_TParamCommand:
460 printf("CXComment_TParamCommand");
461 PrintCXStringWithPrefixAndDispose(
462 "ParamName",
463 clang_TParamCommandComment_getParamName(Comment));
464 if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
465 printf(" ParamPosition={");
466 for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
467 i != e; ++i) {
468 printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
469 if (i != e - 1)
470 printf(", ");
471 }
472 printf("}");
473 } else
474 printf(" ParamPosition=Invalid");
475 break;
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000476 case CXComment_VerbatimBlockCommand:
477 printf("CXComment_VerbatimBlockCommand");
478 PrintCXStringWithPrefixAndDispose(
479 "CommandName",
480 clang_BlockCommandComment_getCommandName(Comment));
481 break;
482 case CXComment_VerbatimBlockLine:
483 printf("CXComment_VerbatimBlockLine");
484 PrintCXStringWithPrefixAndDispose(
485 "Text",
486 clang_VerbatimBlockLineComment_getText(Comment));
487 break;
488 case CXComment_VerbatimLine:
489 printf("CXComment_VerbatimLine");
490 PrintCXStringWithPrefixAndDispose(
491 "Text",
492 clang_VerbatimLineComment_getText(Comment));
493 break;
494 case CXComment_FullComment:
495 printf("CXComment_FullComment");
496 break;
497 }
498 if (Kind != CXComment_Null) {
499 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000500 unsigned i;
501 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000502 printf("\n// %s: ", FileCheckPrefix);
503 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
504 }
505 }
506 printf(")");
507 Ctx->IndentLevel--;
508}
509
510static void DumpCXComment(CXComment Comment) {
511 struct CommentASTDumpingContext Ctx;
512 Ctx.IndentLevel = 1;
513 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
514 DumpCXCommentInternal(&Ctx, Comment);
515 printf("]");
516}
517
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000518typedef struct {
519 const char *CommentSchemaFile;
520#ifdef CLANG_HAVE_LIBXML
521 xmlRelaxNGParserCtxtPtr RNGParser;
522 xmlRelaxNGPtr Schema;
523#endif
524} CommentXMLValidationData;
525
526static void ValidateCommentXML(const char *Str,
527 CommentXMLValidationData *ValidationData) {
528#ifdef CLANG_HAVE_LIBXML
529 xmlDocPtr Doc;
530 xmlRelaxNGValidCtxtPtr ValidationCtxt;
531 int status;
532
533 if (!ValidationData || !ValidationData->CommentSchemaFile)
534 return;
535
536 if (!ValidationData->RNGParser) {
537 ValidationData->RNGParser =
538 xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile);
539 ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser);
540 }
541 if (!ValidationData->RNGParser) {
542 printf(" libXMLError");
543 return;
544 }
545
546 Doc = xmlParseDoc((const xmlChar *) Str);
547
548 if (!Doc) {
549 xmlErrorPtr Error = xmlGetLastError();
550 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
551 return;
552 }
553
554 ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->Schema);
555 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
556 if (!status)
557 printf(" CommentXMLValid");
558 else if (status > 0) {
559 xmlErrorPtr Error = xmlGetLastError();
560 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
561 } else
562 printf(" libXMLError");
563
564 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
565 xmlFreeDoc(Doc);
566#endif
567}
568
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000569static void PrintCursorComments(CXCursor Cursor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000570 CommentXMLValidationData *ValidationData) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000571 {
572 CXString RawComment;
573 const char *RawCommentCString;
574 CXString BriefComment;
575 const char *BriefCommentCString;
576
577 RawComment = clang_Cursor_getRawCommentText(Cursor);
578 RawCommentCString = clang_getCString(RawComment);
579 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
580 PrintCStringWithPrefix("RawComment", RawCommentCString);
581 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
582
583 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
584 BriefCommentCString = clang_getCString(BriefComment);
585 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
586 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
587 clang_disposeString(BriefComment);
588 }
589 clang_disposeString(RawComment);
590 }
591
592 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000593 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000594 if (clang_Comment_getKind(Comment) != CXComment_Null) {
595 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
596 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000597 {
598 CXString XML;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000599 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000600 PrintCXStringWithPrefix("FullCommentAsXML", XML);
601 ValidateCommentXML(clang_getCString(XML), ValidationData);
602 clang_disposeString(XML);
603 }
604
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000605 DumpCXComment(Comment);
606 }
607 }
608}
609
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000610typedef struct {
611 unsigned line;
612 unsigned col;
613} LineCol;
614
615static int lineCol_cmp(const void *p1, const void *p2) {
616 const LineCol *lhs = p1;
617 const LineCol *rhs = p2;
618 if (lhs->line != rhs->line)
619 return (int)lhs->line - (int)rhs->line;
620 return (int)lhs->col - (int)rhs->col;
621}
622
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000623static void PrintCursor(CXCursor Cursor,
624 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000625 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000626 if (clang_isInvalid(Cursor.kind)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000627 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
Ted Kremenek29004672010-02-17 00:41:32 +0000628 printf("Invalid Cursor => %s", clang_getCString(ks));
629 clang_disposeString(ks);
630 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000631 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000632 CXString string, ks;
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000633 CXCursor Referenced;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000634 unsigned line, column;
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000635 CXCursor SpecializationOf;
Douglas Gregor99a26af2010-10-01 20:25:15 +0000636 CXCursor *overridden;
637 unsigned num_overridden;
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000638 unsigned RefNameRangeNr;
639 CXSourceRange CursorExtent;
640 CXSourceRange RefNameRange;
Douglas Gregord6225d32012-05-08 00:14:45 +0000641 int AlwaysUnavailable;
642 int AlwaysDeprecated;
643 CXString UnavailableMessage;
644 CXString DeprecatedMessage;
645 CXPlatformAvailability PlatformAvailability[2];
646 int NumPlatformAvailability;
647 int I;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000648
Ted Kremenek29004672010-02-17 00:41:32 +0000649 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor97c75712010-10-02 22:49:11 +0000650 string = want_display_name? clang_getCursorDisplayName(Cursor)
651 : clang_getCursorSpelling(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000652 printf("%s=%s", clang_getCString(ks),
653 clang_getCString(string));
654 clang_disposeString(ks);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000655 clang_disposeString(string);
Ted Kremenek29004672010-02-17 00:41:32 +0000656
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000657 Referenced = clang_getCursorReferenced(Cursor);
658 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000659 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
660 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
661 printf("[");
662 for (I = 0; I != N; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000663 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor2967e282010-09-14 00:20:32 +0000664 CXSourceLocation Loc;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000665 if (I)
666 printf(", ");
667
Douglas Gregor2967e282010-09-14 00:20:32 +0000668 Loc = clang_getCursorLocation(Ovl);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000669 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000670 printf("%d:%d", line, column);
671 }
672 printf("]");
673 } else {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000674 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000675 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000676 printf(":%d:%d", line, column);
677 }
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000678 }
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000679
680 if (clang_isCursorDefinition(Cursor))
681 printf(" (Definition)");
Douglas Gregorf757a122010-08-23 23:00:57 +0000682
683 switch (clang_getCursorAvailability(Cursor)) {
684 case CXAvailability_Available:
685 break;
686
687 case CXAvailability_Deprecated:
688 printf(" (deprecated)");
689 break;
690
691 case CXAvailability_NotAvailable:
692 printf(" (unavailable)");
693 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000694
695 case CXAvailability_NotAccessible:
696 printf(" (inaccessible)");
697 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000698 }
Ted Kremeneka5940822010-08-26 01:42:22 +0000699
Douglas Gregord6225d32012-05-08 00:14:45 +0000700 NumPlatformAvailability
701 = clang_getCursorPlatformAvailability(Cursor,
702 &AlwaysDeprecated,
703 &DeprecatedMessage,
704 &AlwaysUnavailable,
705 &UnavailableMessage,
706 PlatformAvailability, 2);
707 if (AlwaysUnavailable) {
708 printf(" (always unavailable: \"%s\")",
709 clang_getCString(UnavailableMessage));
710 } else if (AlwaysDeprecated) {
711 printf(" (always deprecated: \"%s\")",
712 clang_getCString(DeprecatedMessage));
713 } else {
714 for (I = 0; I != NumPlatformAvailability; ++I) {
715 if (I >= 2)
716 break;
717
718 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
719 if (PlatformAvailability[I].Unavailable)
720 printf(", unavailable");
721 else {
722 printVersion(", introduced=", PlatformAvailability[I].Introduced);
723 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
724 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
725 }
726 if (clang_getCString(PlatformAvailability[I].Message)[0])
727 printf(", message=\"%s\"",
728 clang_getCString(PlatformAvailability[I].Message));
729 printf(")");
730 }
731 }
732 for (I = 0; I != NumPlatformAvailability; ++I) {
733 if (I >= 2)
734 break;
735 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
736 }
737
738 clang_disposeString(DeprecatedMessage);
739 clang_disposeString(UnavailableMessage);
740
Douglas Gregora8d0c772011-05-13 15:54:42 +0000741 if (clang_CXXMethod_isStatic(Cursor))
742 printf(" (static)");
743 if (clang_CXXMethod_isVirtual(Cursor))
744 printf(" (virtual)");
Dmitri Gribenko62770be2013-05-17 18:38:35 +0000745 if (clang_CXXMethod_isPureVirtual(Cursor))
746 printf(" (pure)");
Argyrios Kyrtzidis23814e42013-04-18 23:53:05 +0000747 if (clang_Cursor_isVariadic(Cursor))
748 printf(" (variadic)");
Argyrios Kyrtzidis7b50fc52013-07-05 20:44:37 +0000749 if (clang_Cursor_isObjCOptional(Cursor))
750 printf(" (@optional)");
751
Ted Kremeneka5940822010-08-26 01:42:22 +0000752 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000753 CXType T =
754 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
755 CXString S = clang_getTypeKindSpelling(T.kind);
Ted Kremeneka5940822010-08-26 01:42:22 +0000756 printf(" [IBOutletCollection=%s]", clang_getCString(S));
757 clang_disposeString(S);
758 }
Ted Kremenekae9e2212010-08-27 21:34:58 +0000759
760 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
761 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
762 unsigned isVirtual = clang_isVirtualBase(Cursor);
763 const char *accessStr = 0;
764
765 switch (access) {
766 case CX_CXXInvalidAccessSpecifier:
767 accessStr = "invalid"; break;
768 case CX_CXXPublic:
769 accessStr = "public"; break;
770 case CX_CXXProtected:
771 accessStr = "protected"; break;
772 case CX_CXXPrivate:
773 accessStr = "private"; break;
774 }
775
776 printf(" [access=%s isVirtual=%s]", accessStr,
777 isVirtual ? "true" : "false");
778 }
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000779
780 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
781 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000782 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
783 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000784 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000785 printf(" [Specialization of %s:%d:%d]",
786 clang_getCString(Name), line, column);
787 clang_disposeString(Name);
788 }
Douglas Gregor99a26af2010-10-01 20:25:15 +0000789
790 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
791 if (num_overridden) {
792 unsigned I;
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000793 LineCol lineCols[50];
794 assert(num_overridden <= 50);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000795 printf(" [Overrides ");
796 for (I = 0; I != num_overridden; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000797 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000798 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000799 lineCols[I].line = line;
800 lineCols[I].col = column;
801 }
Michael Liaob94f47a2012-08-30 00:45:32 +0000802 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000803 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
804 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor99a26af2010-10-01 20:25:15 +0000805 if (I)
806 printf(", ");
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000807 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000808 }
809 printf("]");
810 clang_disposeOverriddenCursors(overridden);
811 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000812
813 if (Cursor.kind == CXCursor_InclusionDirective) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000814 CXFile File = clang_getIncludedFile(Cursor);
815 CXString Included = clang_getFileName(File);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000816 printf(" (%s)", clang_getCString(Included));
817 clang_disposeString(Included);
Douglas Gregor37aa4932011-05-04 00:14:37 +0000818
819 if (clang_isFileMultipleIncludeGuarded(TU, File))
820 printf(" [multi-include guarded]");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000821 }
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000822
823 CursorExtent = clang_getCursorExtent(Cursor);
824 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
825 CXNameRange_WantQualifier
826 | CXNameRange_WantSinglePiece
827 | CXNameRange_WantTemplateArgs,
828 0);
829 if (!clang_equalRanges(CursorExtent, RefNameRange))
830 PrintRange(RefNameRange, "SingleRefName");
831
832 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
833 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
834 CXNameRange_WantQualifier
835 | CXNameRange_WantTemplateArgs,
836 RefNameRangeNr);
837 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
838 break;
839 if (!clang_equalRanges(CursorExtent, RefNameRange))
840 PrintRange(RefNameRange, "RefName");
841 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000842
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000843 PrintCursorComments(Cursor, ValidationData);
Argyrios Kyrtzidis9adfd8a2013-04-18 22:15:49 +0000844
845 {
846 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
847 if (PropAttrs != CXObjCPropertyAttr_noattr) {
848 printf(" [");
849 #define PRINT_PROP_ATTR(A) \
850 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
851 PRINT_PROP_ATTR(readonly);
852 PRINT_PROP_ATTR(getter);
853 PRINT_PROP_ATTR(assign);
854 PRINT_PROP_ATTR(readwrite);
855 PRINT_PROP_ATTR(retain);
856 PRINT_PROP_ATTR(copy);
857 PRINT_PROP_ATTR(nonatomic);
858 PRINT_PROP_ATTR(setter);
859 PRINT_PROP_ATTR(atomic);
860 PRINT_PROP_ATTR(weak);
861 PRINT_PROP_ATTR(strong);
862 PRINT_PROP_ATTR(unsafe_unretained);
863 printf("]");
864 }
865 }
Argyrios Kyrtzidis9d9bc012013-04-18 23:29:12 +0000866
867 {
868 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
869 if (QT != CXObjCDeclQualifier_None) {
870 printf(" [");
871 #define PRINT_OBJC_QUAL(A) \
872 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
873 PRINT_OBJC_QUAL(In);
874 PRINT_OBJC_QUAL(Inout);
875 PRINT_OBJC_QUAL(Out);
876 PRINT_OBJC_QUAL(Bycopy);
877 PRINT_OBJC_QUAL(Byref);
878 PRINT_OBJC_QUAL(Oneway);
879 printf("]");
880 }
881 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000882 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000883}
Steve Naroff1054e602009-08-31 00:59:03 +0000884
Ted Kremenek29004672010-02-17 00:41:32 +0000885static const char* GetCursorSource(CXCursor Cursor) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000886 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekc560b682010-02-17 00:41:20 +0000887 CXString source;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000888 CXFile file;
Argyrios Kyrtzidis7ca77352011-11-03 02:20:36 +0000889 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000890 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +0000891 if (!clang_getCString(source)) {
Ted Kremenekc560b682010-02-17 00:41:20 +0000892 clang_disposeString(source);
893 return "<invalid loc>";
894 }
895 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000896 const char *b = basename(clang_getCString(source));
Ted Kremenekc560b682010-02-17 00:41:20 +0000897 clang_disposeString(source);
898 return b;
899 }
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000900}
901
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000902/******************************************************************************/
Ted Kremenekb478ff42010-01-26 17:59:48 +0000903/* Callbacks. */
904/******************************************************************************/
905
906typedef void (*PostVisitTU)(CXTranslationUnit);
907
Douglas Gregor33cdd812010-02-18 18:08:43 +0000908void PrintDiagnostic(CXDiagnostic Diagnostic) {
909 FILE *out = stderr;
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000910 CXFile file;
Douglas Gregord770f732010-02-22 23:17:23 +0000911 CXString Msg;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000912 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregora750e8e2010-11-19 16:18:16 +0000913 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
914 | CXDiagnostic_DisplayOption;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000915 unsigned i, num_fixits;
Ted Kremenek599d73a2010-03-25 02:00:39 +0000916
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000917 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000918 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000919
Douglas Gregord770f732010-02-22 23:17:23 +0000920 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
921 fprintf(stderr, "%s\n", clang_getCString(Msg));
922 clang_disposeString(Msg);
Ted Kremenek599d73a2010-03-25 02:00:39 +0000923
Douglas Gregor229bebd2010-11-09 06:24:54 +0000924 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
925 &file, 0, 0, 0);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000926 if (!file)
927 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000928
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000929 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek4a642302012-03-20 20:49:45 +0000930 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000931 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor836ec942010-02-19 18:16:06 +0000932 CXSourceRange range;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000933 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
934 CXSourceLocation start = clang_getRangeStart(range);
935 CXSourceLocation end = clang_getRangeEnd(range);
Douglas Gregor836ec942010-02-19 18:16:06 +0000936 unsigned start_line, start_column, end_line, end_column;
937 CXFile start_file, end_file;
Douglas Gregor229bebd2010-11-09 06:24:54 +0000938 clang_getSpellingLocation(start, &start_file, &start_line,
939 &start_column, 0);
940 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor836ec942010-02-19 18:16:06 +0000941 if (clang_equalLocations(start, end)) {
942 /* Insertion. */
943 if (start_file == file)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000944 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor836ec942010-02-19 18:16:06 +0000945 clang_getCString(insertion_text), start_line, start_column);
946 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
947 /* Removal. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000948 if (start_file == file && end_file == file) {
949 fprintf(out, "FIX-IT: Remove ");
950 PrintExtent(out, start_line, start_column, end_line, end_column);
951 fprintf(out, "\n");
Douglas Gregor60b11f62010-01-29 00:41:11 +0000952 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000953 } else {
954 /* Replacement. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000955 if (start_file == end_file) {
956 fprintf(out, "FIX-IT: Replace ");
957 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor836ec942010-02-19 18:16:06 +0000958 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor9773e3d2010-02-18 22:27:07 +0000959 }
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000960 break;
961 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000962 clang_disposeString(insertion_text);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000963 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000964}
965
Ted Kremenek914c7e62012-02-14 02:46:03 +0000966void PrintDiagnosticSet(CXDiagnosticSet Set) {
967 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
968 for ( ; i != n ; ++i) {
969 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
970 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000971 PrintDiagnostic(Diag);
Ted Kremenek914c7e62012-02-14 02:46:03 +0000972 if (ChildDiags)
973 PrintDiagnosticSet(ChildDiags);
974 }
975}
976
977void PrintDiagnostics(CXTranslationUnit TU) {
978 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
979 PrintDiagnosticSet(TUSet);
980 clang_disposeDiagnosticSet(TUSet);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000981}
982
Ted Kremenek83f642e2011-04-18 22:47:10 +0000983void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayd6238f42011-08-29 16:37:29 +0000984 unsigned long total = 0;
Ted Kremenek11d1a422011-04-18 23:42:53 +0000985 unsigned i = 0;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000986 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet45cc5462011-04-18 23:33:22 +0000987 fprintf(stderr, "Memory usage:\n");
Ted Kremenek11d1a422011-04-18 23:42:53 +0000988 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenek23324122011-04-20 16:41:07 +0000989 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek83f642e2011-04-18 22:47:10 +0000990 unsigned long amount = usage.entries[i].amount;
991 total += amount;
Ted Kremenek11d1a422011-04-18 23:42:53 +0000992 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek83f642e2011-04-18 22:47:10 +0000993 ((double) amount)/(1024*1024));
994 }
Ted Kremenek11d1a422011-04-18 23:42:53 +0000995 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek83f642e2011-04-18 22:47:10 +0000996 ((double) total)/(1024*1024));
Ted Kremenek23324122011-04-20 16:41:07 +0000997 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek83f642e2011-04-18 22:47:10 +0000998}
999
Ted Kremenekb478ff42010-01-26 17:59:48 +00001000/******************************************************************************/
Douglas Gregor720d0052010-01-20 21:32:04 +00001001/* Logic for testing traversal. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001002/******************************************************************************/
1003
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001004static void PrintCursorExtent(CXCursor C) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001005 CXSourceRange extent = clang_getCursorExtent(C);
1006 PrintRange(extent, "Extent");
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001007}
1008
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001009/* Data used by the visitors. */
1010typedef struct {
Douglas Gregor720d0052010-01-20 21:32:04 +00001011 CXTranslationUnit TU;
1012 enum CXCursorKind *Filter;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001013 CommentXMLValidationData ValidationData;
Douglas Gregor720d0052010-01-20 21:32:04 +00001014} VisitorData;
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001015
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001016
Ted Kremenek29004672010-02-17 00:41:32 +00001017enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001018 CXCursor Parent,
1019 CXClientData ClientData) {
1020 VisitorData *Data = (VisitorData *)ClientData;
1021 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001022 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001023 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001024 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001025 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor4f46e782010-01-19 21:36:55 +00001026 GetCursorSource(Cursor), line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001027 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001028 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis1ab09cc2013-04-11 17:02:10 +00001029 if (clang_isDeclaration(Cursor.kind)) {
1030 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
1031 const char *accessStr = 0;
1032
1033 switch (access) {
1034 case CX_CXXInvalidAccessSpecifier: break;
1035 case CX_CXXPublic:
1036 accessStr = "public"; break;
1037 case CX_CXXProtected:
1038 accessStr = "protected"; break;
1039 case CX_CXXPrivate:
1040 accessStr = "private"; break;
1041 }
1042
1043 if (accessStr)
1044 printf(" [access=%s]", accessStr);
1045 }
Ted Kremenek29004672010-02-17 00:41:32 +00001046 printf("\n");
Douglas Gregor720d0052010-01-20 21:32:04 +00001047 return CXChildVisit_Recurse;
Steve Naroff772c1a42009-08-31 14:26:51 +00001048 }
Ted Kremenek29004672010-02-17 00:41:32 +00001049
Douglas Gregor720d0052010-01-20 21:32:04 +00001050 return CXChildVisit_Continue;
Steve Naroff1054e602009-08-31 00:59:03 +00001051}
Steve Naroffa1c72842009-08-28 15:28:48 +00001052
Ted Kremenek29004672010-02-17 00:41:32 +00001053static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001054 CXCursor Parent,
1055 CXClientData ClientData) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001056 const char *startBuf, *endBuf;
1057 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1058 CXCursor Ref;
Douglas Gregor720d0052010-01-20 21:32:04 +00001059 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001060
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001061 if (Cursor.kind != CXCursor_FunctionDecl ||
1062 !clang_isCursorDefinition(Cursor))
Douglas Gregor720d0052010-01-20 21:32:04 +00001063 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001064
1065 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1066 &startLine, &startColumn,
1067 &endLine, &endColumn);
1068 /* Probe the entire body, looking for both decls and refs. */
1069 curLine = startLine;
1070 curColumn = startColumn;
1071
1072 while (startBuf < endBuf) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001073 CXSourceLocation Loc;
Douglas Gregor4f46e782010-01-19 21:36:55 +00001074 CXFile file;
Ted Kremenekc560b682010-02-17 00:41:20 +00001075 CXString source;
Ted Kremenek29004672010-02-17 00:41:32 +00001076
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001077 if (*startBuf == '\n') {
1078 startBuf++;
1079 curLine++;
1080 curColumn = 1;
1081 } else if (*startBuf != '\t')
1082 curColumn++;
Ted Kremenek29004672010-02-17 00:41:32 +00001083
Douglas Gregor66a58812010-01-18 22:46:11 +00001084 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor229bebd2010-11-09 06:24:54 +00001085 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremenek29004672010-02-17 00:41:32 +00001086
Douglas Gregor4f46e782010-01-19 21:36:55 +00001087 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +00001088 if (clang_getCString(source)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001089 CXSourceLocation RefLoc
1090 = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregor816fd362010-01-22 21:44:22 +00001091 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor66a58812010-01-18 22:46:11 +00001092 if (Ref.kind == CXCursor_NoDeclFound) {
1093 /* Nothing found here; that's fine. */
1094 } else if (Ref.kind != CXCursor_FunctionDecl) {
1095 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1096 curLine, curColumn);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001097 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor66a58812010-01-18 22:46:11 +00001098 printf("\n");
1099 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001100 }
Ted Kremenekc560b682010-02-17 00:41:20 +00001101 clang_disposeString(source);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001102 startBuf++;
1103 }
Ted Kremenek29004672010-02-17 00:41:32 +00001104
Douglas Gregor720d0052010-01-20 21:32:04 +00001105 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001106}
1107
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001108/******************************************************************************/
1109/* USR testing. */
1110/******************************************************************************/
1111
Douglas Gregor720d0052010-01-20 21:32:04 +00001112enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1113 CXClientData ClientData) {
1114 VisitorData *Data = (VisitorData *)ClientData;
1115 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001116 CXString USR = clang_getCursorUSR(C);
1117 const char *cstr = clang_getCString(USR);
Ted Kremenek6d159c12010-04-20 23:15:40 +00001118 if (!cstr || cstr[0] == '\0') {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001119 clang_disposeString(USR);
Ted Kremenek7afa85b2010-04-16 21:31:52 +00001120 return CXChildVisit_Recurse;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001121 }
Ted Kremenek6d159c12010-04-20 23:15:40 +00001122 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1123
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001124 PrintCursorExtent(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001125 printf("\n");
1126 clang_disposeString(USR);
Ted Kremenek29004672010-02-17 00:41:32 +00001127
Douglas Gregor720d0052010-01-20 21:32:04 +00001128 return CXChildVisit_Recurse;
Ted Kremenek29004672010-02-17 00:41:32 +00001129 }
1130
Douglas Gregor720d0052010-01-20 21:32:04 +00001131 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001132}
1133
1134/******************************************************************************/
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001135/* Inclusion stack testing. */
1136/******************************************************************************/
1137
1138void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1139 unsigned includeStackLen, CXClientData data) {
Ted Kremenek29004672010-02-17 00:41:32 +00001140
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001141 unsigned i;
Ted Kremenekc560b682010-02-17 00:41:20 +00001142 CXString fname;
1143
1144 fname = clang_getFileName(includedFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001145 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenekc560b682010-02-17 00:41:20 +00001146 clang_disposeString(fname);
Ted Kremenek29004672010-02-17 00:41:32 +00001147
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001148 for (i = 0; i < includeStackLen; ++i) {
1149 CXFile includingFile;
1150 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001151 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1152 &column, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +00001153 fname = clang_getFileName(includingFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001154 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenekc560b682010-02-17 00:41:20 +00001155 clang_disposeString(fname);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001156 }
1157 printf("\n");
1158}
1159
1160void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremenek29004672010-02-17 00:41:32 +00001161 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001162}
1163
1164/******************************************************************************/
Ted Kremenek83b28a22010-03-03 06:37:58 +00001165/* Linkage testing. */
1166/******************************************************************************/
1167
1168static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1169 CXClientData d) {
1170 const char *linkage = 0;
1171
1172 if (clang_isInvalid(clang_getCursorKind(cursor)))
1173 return CXChildVisit_Recurse;
1174
1175 switch (clang_getCursorLinkage(cursor)) {
1176 case CXLinkage_Invalid: break;
Douglas Gregor0b466502010-03-04 19:36:27 +00001177 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1178 case CXLinkage_Internal: linkage = "Internal"; break;
1179 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1180 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek83b28a22010-03-03 06:37:58 +00001181 }
1182
1183 if (linkage) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001184 PrintCursor(cursor, NULL);
Ted Kremenek83b28a22010-03-03 06:37:58 +00001185 printf("linkage=%s\n", linkage);
1186 }
1187
1188 return CXChildVisit_Recurse;
1189}
1190
1191/******************************************************************************/
Ted Kremenek6bca9842010-05-14 21:29:26 +00001192/* Typekind testing. */
1193/******************************************************************************/
1194
Dmitri Gribenko00353722013-02-15 21:15:49 +00001195static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1196 CXString TypeSpelling, TypeKindSpelling;
1197
1198 TypeSpelling = clang_getTypeSpelling(T);
1199 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1200 printf(Format,
1201 clang_getCString(TypeSpelling),
1202 clang_getCString(TypeKindSpelling));
1203 clang_disposeString(TypeSpelling);
1204 clang_disposeString(TypeKindSpelling);
1205}
1206
1207static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1208 CXClientData d) {
Ted Kremenek6bca9842010-05-14 21:29:26 +00001209 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001210 CXType T = clang_getCursorType(cursor);
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001211 enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001212 PrintCursor(cursor, NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00001213 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregor56a63802011-01-27 16:27:11 +00001214 if (clang_isConstQualifiedType(T))
1215 printf(" const");
1216 if (clang_isVolatileQualifiedType(T))
1217 printf(" volatile");
1218 if (clang_isRestrictQualifiedType(T))
1219 printf(" restrict");
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001220 if (RQ == CXRefQualifier_LValue)
1221 printf(" lvalue-ref-qualifier");
1222 if (RQ == CXRefQualifier_RValue)
1223 printf(" rvalue-ref-qualifier");
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001224 /* Print the canonical type if it is different. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001225 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001226 CXType CT = clang_getCanonicalType(T);
Ted Kremenekc1508872010-06-21 20:15:39 +00001227 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001228 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001229 }
1230 }
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001231 /* Print the return type if it exists. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001232 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001233 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenekc1508872010-06-21 20:15:39 +00001234 if (RT.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001235 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001236 }
1237 }
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001238 /* Print the argument types if they exist. */
1239 {
1240 int numArgs = clang_Cursor_getNumArguments(cursor);
1241 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001242 int i;
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001243 printf(" [args=");
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001244 for (i = 0; i < numArgs; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001245 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001246 if (T.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001247 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001248 }
1249 }
1250 printf("]");
1251 }
1252 }
Ted Kremenek0c7476a2010-07-30 00:14:11 +00001253 /* Print if this is a non-POD type. */
1254 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenekc1508872010-06-21 20:15:39 +00001255
Ted Kremenek6bca9842010-05-14 21:29:26 +00001256 printf("\n");
1257 }
1258 return CXChildVisit_Recurse;
1259}
1260
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001261static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1262 CXClientData d) {
1263 CXType T;
1264 enum CXCursorKind K = clang_getCursorKind(cursor);
1265 if (clang_isInvalid(K))
1266 return CXChildVisit_Recurse;
1267 T = clang_getCursorType(cursor);
1268 PrintCursor(cursor, NULL);
1269 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1270 /* Print the type sizeof if applicable. */
1271 {
1272 long long Size = clang_Type_getSizeOf(T);
1273 if (Size >= 0 || Size < -1 ) {
1274 printf(" [sizeof=%lld]", Size);
1275 }
1276 }
1277 /* Print the type alignof if applicable. */
1278 {
1279 long long Align = clang_Type_getAlignOf(T);
1280 if (Align >= 0 || Align < -1) {
1281 printf(" [alignof=%lld]", Align);
1282 }
1283 }
1284 /* Print the record field offset if applicable. */
1285 {
1286 const char *FieldName = clang_getCString(clang_getCursorSpelling(cursor));
1287 /* recurse to get the root anonymous record parent */
1288 CXCursor Parent, Root;
1289 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl ) {
1290 const char *RootParentName;
1291 Root = Parent = p;
1292 do {
1293 Root = Parent;
1294 RootParentName = clang_getCString(clang_getCursorSpelling(Root));
1295 Parent = clang_getCursorSemanticParent(Root);
1296 } while ( clang_getCursorType(Parent).kind == CXType_Record &&
1297 !strcmp(RootParentName, "") );
1298 /* if RootParentName is "", record is anonymous. */
1299 {
1300 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1301 FieldName);
1302 printf(" [offsetof=%lld]", Offset);
1303 }
1304 }
1305 }
1306 /* Print if its a bitfield */
1307 {
1308 int IsBitfield = clang_Cursor_isBitField(cursor);
1309 if (IsBitfield)
1310 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1311 }
1312 printf("\n");
1313 return CXChildVisit_Recurse;
1314}
1315
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001316/******************************************************************************/
1317/* Bitwidth testing. */
1318/******************************************************************************/
1319
1320static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1321 CXClientData d) {
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001322 int Bitwidth;
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001323 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1324 return CXChildVisit_Recurse;
1325
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001326 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001327 if (Bitwidth >= 0) {
1328 PrintCursor(cursor, NULL);
1329 printf(" bitwidth=%d\n", Bitwidth);
1330 }
1331
1332 return CXChildVisit_Recurse;
1333}
Ted Kremenek6bca9842010-05-14 21:29:26 +00001334
1335/******************************************************************************/
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001336/* Loading ASTs/source. */
1337/******************************************************************************/
1338
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001339static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek73eccd22010-01-12 18:53:15 +00001340 const char *filter, const char *prefix,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001341 CXCursorVisitor Visitor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001342 PostVisitTU PV,
1343 const char *CommentSchemaFile) {
Ted Kremenek29004672010-02-17 00:41:32 +00001344
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001345 if (prefix)
Ted Kremenek29004672010-02-17 00:41:32 +00001346 FileCheckPrefix = prefix;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001347
1348 if (Visitor) {
1349 enum CXCursorKind K = CXCursor_NotImplemented;
1350 enum CXCursorKind *ck = &K;
1351 VisitorData Data;
Ted Kremenek29004672010-02-17 00:41:32 +00001352
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001353 /* Perform some simple filtering. */
1354 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor97c75712010-10-02 22:49:11 +00001355 else if (!strcmp(filter, "all-display") ||
1356 !strcmp(filter, "local-display")) {
1357 ck = NULL;
1358 want_display_name = 1;
1359 }
Daniel Dunbard64ce7b2010-02-10 20:42:40 +00001360 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001361 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1362 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1363 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1364 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1365 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1366 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1367 else {
1368 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1369 return 1;
1370 }
Ted Kremenek29004672010-02-17 00:41:32 +00001371
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001372 Data.TU = TU;
1373 Data.Filter = ck;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001374 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1375#ifdef CLANG_HAVE_LIBXML
1376 Data.ValidationData.RNGParser = NULL;
1377 Data.ValidationData.Schema = NULL;
1378#endif
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001379 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001380 }
Ted Kremenek29004672010-02-17 00:41:32 +00001381
Ted Kremenekb478ff42010-01-26 17:59:48 +00001382 if (PV)
1383 PV(TU);
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001384
Douglas Gregor33cdd812010-02-18 18:08:43 +00001385 PrintDiagnostics(TU);
Argyrios Kyrtzidis70480492011-11-13 23:39:14 +00001386 if (checkForErrors(TU) != 0) {
1387 clang_disposeTranslationUnit(TU);
1388 return -1;
1389 }
1390
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001391 clang_disposeTranslationUnit(TU);
1392 return 0;
1393}
1394
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001395int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001396 const char *prefix, CXCursorVisitor Visitor,
1397 PostVisitTU PV) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001398 CXIndex Idx;
1399 CXTranslationUnit TU;
Ted Kremenek50228be2010-02-11 07:41:25 +00001400 int result;
Ted Kremenek29004672010-02-17 00:41:32 +00001401 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001402 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001403 /* displayDiagnostics=*/1);
Ted Kremenek29004672010-02-17 00:41:32 +00001404
Ted Kremenek50228be2010-02-11 07:41:25 +00001405 if (!CreateTranslationUnit(Idx, file, &TU)) {
1406 clang_disposeIndex(Idx);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001407 return 1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001408 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001409
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001410 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek50228be2010-02-11 07:41:25 +00001411 clang_disposeIndex(Idx);
1412 return result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001413}
1414
Ted Kremenekb478ff42010-01-26 17:59:48 +00001415int perform_test_load_source(int argc, const char **argv,
1416 const char *filter, CXCursorVisitor Visitor,
1417 PostVisitTU PV) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001418 CXIndex Idx;
1419 CXTranslationUnit TU;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001420 const char *CommentSchemaFile;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001421 struct CXUnsavedFile *unsaved_files = 0;
1422 int num_unsaved_files = 0;
1423 int result;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001424
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001425 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor97c75712010-10-02 22:49:11 +00001426 (!strcmp(filter, "local") ||
1427 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001428 /* displayDiagnostics=*/1);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001429
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001430 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1431 argc--;
1432 argv++;
1433 }
1434
Ted Kremenek50228be2010-02-11 07:41:25 +00001435 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1436 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001437 return -1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001438 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001439
Douglas Gregor998caea2011-05-06 16:33:08 +00001440 TU = clang_parseTranslationUnit(Idx, 0,
1441 argv + num_unsaved_files,
1442 argc - num_unsaved_files,
1443 unsaved_files, num_unsaved_files,
1444 getDefaultParsingOptions());
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001445 if (!TU) {
1446 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001447 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001448 clang_disposeIndex(Idx);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001449 return 1;
1450 }
1451
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001452 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1453 CommentSchemaFile);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001454 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001455 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001456 return result;
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001457}
1458
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001459int perform_test_reparse_source(int argc, const char **argv, int trials,
1460 const char *filter, CXCursorVisitor Visitor,
1461 PostVisitTU PV) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001462 CXIndex Idx;
1463 CXTranslationUnit TU;
1464 struct CXUnsavedFile *unsaved_files = 0;
1465 int num_unsaved_files = 0;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001466 int compiler_arg_idx = 0;
1467 int result, i;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001468 int trial;
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001469 int remap_after_trial = 0;
1470 char *endptr = 0;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001471
1472 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1473 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001474 /* displayDiagnostics=*/1);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001475
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001476 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1477 clang_disposeIndex(Idx);
1478 return -1;
1479 }
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001480
1481 for (i = 0; i < argc; ++i) {
1482 if (strcmp(argv[i], "--") == 0)
1483 break;
1484 }
1485 if (i < argc)
1486 compiler_arg_idx = i+1;
1487 if (num_unsaved_files > compiler_arg_idx)
1488 compiler_arg_idx = num_unsaved_files;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001489
Daniel Dunbarec29d712010-08-18 23:09:16 +00001490 /* Load the initial translation unit -- we do this without honoring remapped
1491 * files, so that we have a way to test results after changing the source. */
Douglas Gregorbe2d8c62010-07-23 00:33:23 +00001492 TU = clang_parseTranslationUnit(Idx, 0,
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001493 argv + compiler_arg_idx,
1494 argc - compiler_arg_idx,
Daniel Dunbarec29d712010-08-18 23:09:16 +00001495 0, 0, getDefaultParsingOptions());
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001496 if (!TU) {
1497 fprintf(stderr, "Unable to load translation unit!\n");
1498 free_remapped_files(unsaved_files, num_unsaved_files);
1499 clang_disposeIndex(Idx);
1500 return 1;
1501 }
1502
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001503 if (checkForErrors(TU) != 0)
1504 return -1;
1505
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001506 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1507 remap_after_trial =
1508 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1509 }
1510
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001511 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001512 free_remapped_files(unsaved_files, num_unsaved_files);
1513 if (parse_remapped_files_with_try(trial, argc, argv, 0,
1514 &unsaved_files, &num_unsaved_files)) {
1515 clang_disposeTranslationUnit(TU);
1516 clang_disposeIndex(Idx);
1517 return -1;
1518 }
1519
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001520 if (clang_reparseTranslationUnit(TU,
1521 trial >= remap_after_trial ? num_unsaved_files : 0,
1522 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregorde051182010-08-11 15:58:42 +00001523 clang_defaultReparseOptions(TU))) {
Daniel Dunbarec29d712010-08-18 23:09:16 +00001524 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001525 clang_disposeTranslationUnit(TU);
1526 free_remapped_files(unsaved_files, num_unsaved_files);
1527 clang_disposeIndex(Idx);
1528 return -1;
1529 }
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001530
1531 if (checkForErrors(TU) != 0)
1532 return -1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001533 }
1534
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001535 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001536
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001537 free_remapped_files(unsaved_files, num_unsaved_files);
1538 clang_disposeIndex(Idx);
1539 return result;
1540}
1541
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001542/******************************************************************************/
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001543/* Logic for testing clang_getCursor(). */
1544/******************************************************************************/
1545
Douglas Gregor37aa4932011-05-04 00:14:37 +00001546static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001547 unsigned start_line, unsigned start_col,
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001548 unsigned end_line, unsigned end_col,
1549 const char *prefix) {
Ted Kremenekb58514e2010-01-07 01:17:12 +00001550 printf("// %s: ", FileCheckPrefix);
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001551 if (prefix)
1552 printf("-%s", prefix);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00001553 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1554 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001555 PrintCursor(cursor, NULL);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001556 printf("\n");
1557}
1558
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001559static int perform_file_scan(const char *ast_file, const char *source_file,
1560 const char *prefix) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001561 CXIndex Idx;
1562 CXTranslationUnit TU;
1563 FILE *fp;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001564 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregor816fd362010-01-22 21:44:22 +00001565 CXFile file;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001566 unsigned line = 1, col = 1;
Daniel Dunbar6092d502010-02-14 08:32:51 +00001567 unsigned start_line = 1, start_col = 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001568
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001569 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001570 /* displayDiagnostics=*/1))) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001571 fprintf(stderr, "Could not create Index\n");
1572 return 1;
1573 }
Ted Kremenek29004672010-02-17 00:41:32 +00001574
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001575 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1576 return 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001577
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001578 if ((fp = fopen(source_file, "r")) == NULL) {
1579 fprintf(stderr, "Could not open '%s'\n", source_file);
1580 return 1;
1581 }
Ted Kremenek29004672010-02-17 00:41:32 +00001582
Douglas Gregor816fd362010-01-22 21:44:22 +00001583 file = clang_getFile(TU, source_file);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001584 for (;;) {
1585 CXCursor cursor;
1586 int c = fgetc(fp);
Benjamin Kramer10d083172009-11-17 20:51:40 +00001587
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001588 if (c == '\n') {
1589 ++line;
1590 col = 1;
1591 } else
1592 ++col;
1593
1594 /* Check the cursor at this position, and dump the previous one if we have
1595 * found something new.
1596 */
1597 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1598 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1599 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregor37aa4932011-05-04 00:14:37 +00001600 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbar02968e52010-02-14 10:02:57 +00001601 line, col, prefix);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001602 start_line = line;
1603 start_col = col;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001604 }
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001605 if (c == EOF)
1606 break;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001607
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001608 prevCursor = cursor;
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001609 }
Ted Kremenek29004672010-02-17 00:41:32 +00001610
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001611 fclose(fp);
Douglas Gregor7a964ad2011-01-31 22:04:05 +00001612 clang_disposeTranslationUnit(TU);
1613 clang_disposeIndex(Idx);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001614 return 0;
1615}
1616
1617/******************************************************************************/
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001618/* Logic for testing clang code completion. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001619/******************************************************************************/
1620
Douglas Gregor9eb77012009-11-07 00:00:49 +00001621/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1622 on failure. If successful, the pointer *filename will contain newly-allocated
1623 memory (that will be owned by the caller) to store the file name. */
Ted Kremenek29004672010-02-17 00:41:32 +00001624int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001625 unsigned *column, unsigned *second_line,
1626 unsigned *second_column) {
Douglas Gregorf96ea292009-11-09 18:19:57 +00001627 /* Find the second colon. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001628 const char *last_colon = strrchr(input, ':');
1629 unsigned values[4], i;
1630 unsigned num_values = (second_line && second_column)? 4 : 2;
1631
Douglas Gregor9eb77012009-11-07 00:00:49 +00001632 char *endptr = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001633 if (!last_colon || last_colon == input) {
1634 if (num_values == 4)
1635 fprintf(stderr, "could not parse filename:line:column:line:column in "
1636 "'%s'\n", input);
1637 else
1638 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001639 return 1;
1640 }
1641
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001642 for (i = 0; i != num_values; ++i) {
1643 const char *prev_colon;
1644
1645 /* Parse the next line or column. */
1646 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1647 if (*endptr != 0 && *endptr != ':') {
Ted Kremenek29004672010-02-17 00:41:32 +00001648 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001649 (i % 2 ? "column" : "line"), input);
1650 return 1;
1651 }
Ted Kremenek29004672010-02-17 00:41:32 +00001652
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001653 if (i + 1 == num_values)
1654 break;
1655
1656 /* Find the previous colon. */
1657 prev_colon = last_colon - 1;
1658 while (prev_colon != input && *prev_colon != ':')
1659 --prev_colon;
1660 if (prev_colon == input) {
Ted Kremenek29004672010-02-17 00:41:32 +00001661 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001662 (i % 2 == 0? "column" : "line"), input);
Ted Kremenek29004672010-02-17 00:41:32 +00001663 return 1;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001664 }
1665
1666 last_colon = prev_colon;
Douglas Gregorf96ea292009-11-09 18:19:57 +00001667 }
1668
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001669 *line = values[0];
1670 *column = values[1];
Ted Kremenek29004672010-02-17 00:41:32 +00001671
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001672 if (second_line && second_column) {
1673 *second_line = values[2];
1674 *second_column = values[3];
1675 }
1676
Douglas Gregorf96ea292009-11-09 18:19:57 +00001677 /* Copy the file name. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001678 *filename = (char*)malloc(last_colon - input + 1);
1679 memcpy(*filename, input, last_colon - input);
1680 (*filename)[last_colon - input] = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001681 return 0;
1682}
1683
1684const char *
1685clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1686 switch (Kind) {
1687 case CXCompletionChunk_Optional: return "Optional";
1688 case CXCompletionChunk_TypedText: return "TypedText";
1689 case CXCompletionChunk_Text: return "Text";
1690 case CXCompletionChunk_Placeholder: return "Placeholder";
1691 case CXCompletionChunk_Informative: return "Informative";
1692 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1693 case CXCompletionChunk_LeftParen: return "LeftParen";
1694 case CXCompletionChunk_RightParen: return "RightParen";
1695 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1696 case CXCompletionChunk_RightBracket: return "RightBracket";
1697 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1698 case CXCompletionChunk_RightBrace: return "RightBrace";
1699 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1700 case CXCompletionChunk_RightAngle: return "RightAngle";
1701 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001702 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001703 case CXCompletionChunk_Colon: return "Colon";
1704 case CXCompletionChunk_SemiColon: return "SemiColon";
1705 case CXCompletionChunk_Equal: return "Equal";
1706 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1707 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor9eb77012009-11-07 00:00:49 +00001708 }
Ted Kremenek29004672010-02-17 00:41:32 +00001709
Douglas Gregor9eb77012009-11-07 00:00:49 +00001710 return "Unknown";
1711}
1712
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00001713static int checkForErrors(CXTranslationUnit TU) {
1714 unsigned Num, i;
1715 CXDiagnostic Diag;
1716 CXString DiagStr;
1717
1718 if (!getenv("CINDEXTEST_FAILONERROR"))
1719 return 0;
1720
1721 Num = clang_getNumDiagnostics(TU);
1722 for (i = 0; i != Num; ++i) {
1723 Diag = clang_getDiagnostic(TU, i);
1724 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1725 DiagStr = clang_formatDiagnostic(Diag,
1726 clang_defaultDiagnosticDisplayOptions());
1727 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1728 clang_disposeString(DiagStr);
1729 clang_disposeDiagnostic(Diag);
1730 return -1;
1731 }
1732 clang_disposeDiagnostic(Diag);
1733 }
1734
1735 return 0;
1736}
1737
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001738void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001739 int I, N;
Ted Kremenek29004672010-02-17 00:41:32 +00001740
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001741 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001742 for (I = 0; I != N; ++I) {
Ted Kremenekf602f962010-02-17 01:42:24 +00001743 CXString text;
1744 const char *cstr;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001745 enum CXCompletionChunkKind Kind
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001746 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremenek29004672010-02-17 00:41:32 +00001747
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001748 if (Kind == CXCompletionChunk_Optional) {
1749 fprintf(file, "{Optional ");
1750 print_completion_string(
Ted Kremenek29004672010-02-17 00:41:32 +00001751 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001752 file);
1753 fprintf(file, "}");
1754 continue;
Douglas Gregor8ed5b772010-10-08 20:39:29 +00001755 }
1756
1757 if (Kind == CXCompletionChunk_VerticalSpace) {
1758 fprintf(file, "{VerticalSpace }");
1759 continue;
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001760 }
Ted Kremenek29004672010-02-17 00:41:32 +00001761
Douglas Gregorf81f5282009-11-09 17:05:28 +00001762 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenekf602f962010-02-17 01:42:24 +00001763 cstr = clang_getCString(text);
Ted Kremenek29004672010-02-17 00:41:32 +00001764 fprintf(file, "{%s %s}",
Douglas Gregor9eb77012009-11-07 00:00:49 +00001765 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenekf602f962010-02-17 01:42:24 +00001766 cstr ? cstr : "");
1767 clang_disposeString(text);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001768 }
Ted Kremenekf602f962010-02-17 01:42:24 +00001769
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001770}
1771
1772void print_completion_result(CXCompletionResult *completion_result,
1773 CXClientData client_data) {
1774 FILE *file = (FILE *)client_data;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001775 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001776 unsigned annotationCount;
Douglas Gregor78254c82012-03-27 23:34:16 +00001777 enum CXCursorKind ParentKind;
1778 CXString ParentName;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001779 CXString BriefComment;
1780 const char *BriefCommentCString;
Douglas Gregor78254c82012-03-27 23:34:16 +00001781
Ted Kremenek29004672010-02-17 00:41:32 +00001782 fprintf(file, "%s:", clang_getCString(ks));
1783 clang_disposeString(ks);
1784
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001785 print_completion_string(completion_result->CompletionString, file);
Douglas Gregorf757a122010-08-23 23:00:57 +00001786 fprintf(file, " (%u)",
Douglas Gregora2db7932010-05-26 22:00:08 +00001787 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregorf757a122010-08-23 23:00:57 +00001788 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1789 case CXAvailability_Available:
1790 break;
1791
1792 case CXAvailability_Deprecated:
1793 fprintf(file, " (deprecated)");
1794 break;
1795
1796 case CXAvailability_NotAvailable:
1797 fprintf(file, " (unavailable)");
1798 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001799
1800 case CXAvailability_NotAccessible:
1801 fprintf(file, " (inaccessible)");
1802 break;
Douglas Gregorf757a122010-08-23 23:00:57 +00001803 }
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001804
1805 annotationCount = clang_getCompletionNumAnnotations(
1806 completion_result->CompletionString);
1807 if (annotationCount) {
1808 unsigned i;
1809 fprintf(file, " (");
1810 for (i = 0; i < annotationCount; ++i) {
1811 if (i != 0)
1812 fprintf(file, ", ");
1813 fprintf(file, "\"%s\"",
1814 clang_getCString(clang_getCompletionAnnotation(
1815 completion_result->CompletionString, i)));
1816 }
1817 fprintf(file, ")");
1818 }
1819
Douglas Gregor78254c82012-03-27 23:34:16 +00001820 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1821 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1822 &ParentKind);
1823 if (ParentKind != CXCursor_NotImplemented) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001824 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregor78254c82012-03-27 23:34:16 +00001825 fprintf(file, " (parent: %s '%s')",
1826 clang_getCString(KindSpelling),
1827 clang_getCString(ParentName));
1828 clang_disposeString(KindSpelling);
1829 }
1830 clang_disposeString(ParentName);
1831 }
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001832
1833 BriefComment = clang_getCompletionBriefComment(
1834 completion_result->CompletionString);
1835 BriefCommentCString = clang_getCString(BriefComment);
1836 if (BriefCommentCString && *BriefCommentCString != '\0') {
1837 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1838 }
1839 clang_disposeString(BriefComment);
Douglas Gregor78254c82012-03-27 23:34:16 +00001840
Douglas Gregorf757a122010-08-23 23:00:57 +00001841 fprintf(file, "\n");
Douglas Gregor9eb77012009-11-07 00:00:49 +00001842}
1843
Douglas Gregor21325842011-07-07 16:03:39 +00001844void print_completion_contexts(unsigned long long contexts, FILE *file) {
1845 fprintf(file, "Completion contexts:\n");
1846 if (contexts == CXCompletionContext_Unknown) {
1847 fprintf(file, "Unknown\n");
1848 }
1849 if (contexts & CXCompletionContext_AnyType) {
1850 fprintf(file, "Any type\n");
1851 }
1852 if (contexts & CXCompletionContext_AnyValue) {
1853 fprintf(file, "Any value\n");
1854 }
1855 if (contexts & CXCompletionContext_ObjCObjectValue) {
1856 fprintf(file, "Objective-C object value\n");
1857 }
1858 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1859 fprintf(file, "Objective-C selector value\n");
1860 }
1861 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1862 fprintf(file, "C++ class type value\n");
1863 }
1864 if (contexts & CXCompletionContext_DotMemberAccess) {
1865 fprintf(file, "Dot member access\n");
1866 }
1867 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1868 fprintf(file, "Arrow member access\n");
1869 }
1870 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1871 fprintf(file, "Objective-C property access\n");
1872 }
1873 if (contexts & CXCompletionContext_EnumTag) {
1874 fprintf(file, "Enum tag\n");
1875 }
1876 if (contexts & CXCompletionContext_UnionTag) {
1877 fprintf(file, "Union tag\n");
1878 }
1879 if (contexts & CXCompletionContext_StructTag) {
1880 fprintf(file, "Struct tag\n");
1881 }
1882 if (contexts & CXCompletionContext_ClassTag) {
1883 fprintf(file, "Class name\n");
1884 }
1885 if (contexts & CXCompletionContext_Namespace) {
1886 fprintf(file, "Namespace or namespace alias\n");
1887 }
1888 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1889 fprintf(file, "Nested name specifier\n");
1890 }
1891 if (contexts & CXCompletionContext_ObjCInterface) {
1892 fprintf(file, "Objective-C interface\n");
1893 }
1894 if (contexts & CXCompletionContext_ObjCProtocol) {
1895 fprintf(file, "Objective-C protocol\n");
1896 }
1897 if (contexts & CXCompletionContext_ObjCCategory) {
1898 fprintf(file, "Objective-C category\n");
1899 }
1900 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1901 fprintf(file, "Objective-C instance method\n");
1902 }
1903 if (contexts & CXCompletionContext_ObjCClassMessage) {
1904 fprintf(file, "Objective-C class method\n");
1905 }
1906 if (contexts & CXCompletionContext_ObjCSelectorName) {
1907 fprintf(file, "Objective-C selector name\n");
1908 }
1909 if (contexts & CXCompletionContext_MacroName) {
1910 fprintf(file, "Macro name\n");
1911 }
1912 if (contexts & CXCompletionContext_NaturalLanguage) {
1913 fprintf(file, "Natural language\n");
1914 }
1915}
1916
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001917int my_stricmp(const char *s1, const char *s2) {
1918 while (*s1 && *s2) {
NAKAMURA Takumid3cc2202011-03-09 03:02:28 +00001919 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001920 if (c1 < c2)
1921 return -1;
1922 else if (c1 > c2)
1923 return 1;
1924
1925 ++s1;
1926 ++s2;
1927 }
1928
1929 if (*s1)
1930 return 1;
1931 else if (*s2)
1932 return -1;
1933 return 0;
1934}
1935
Douglas Gregor47815d52010-07-12 18:38:41 +00001936int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001937 const char *input = argv[1];
1938 char *filename = 0;
1939 unsigned line;
1940 unsigned column;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001941 CXIndex CIdx;
Ted Kremenekef3339b2009-11-17 18:09:14 +00001942 int errorCode;
Douglas Gregor9485bf92009-12-02 09:21:34 +00001943 struct CXUnsavedFile *unsaved_files = 0;
1944 int num_unsaved_files = 0;
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001945 CXCodeCompleteResults *results = 0;
Dawn Perchik0d3d0b72010-09-30 22:26:05 +00001946 CXTranslationUnit TU = 0;
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001947 unsigned I, Repeats = 1;
1948 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1949
1950 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1951 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001952 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1953 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregor028d3e42010-08-09 20:45:32 +00001954
Douglas Gregor47815d52010-07-12 18:38:41 +00001955 if (timing_only)
1956 input += strlen("-code-completion-timing=");
1957 else
1958 input += strlen("-code-completion-at=");
1959
Ted Kremenek29004672010-02-17 00:41:32 +00001960 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001961 0, 0)))
Ted Kremenekef3339b2009-11-17 18:09:14 +00001962 return errorCode;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001963
Douglas Gregor9485bf92009-12-02 09:21:34 +00001964 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1965 return -1;
1966
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001967 CIdx = clang_createIndex(0, 0);
1968
1969 if (getenv("CINDEXTEST_EDITING"))
1970 Repeats = 5;
1971
1972 TU = clang_parseTranslationUnit(CIdx, 0,
1973 argv + num_unsaved_files + 2,
1974 argc - num_unsaved_files - 2,
1975 0, 0, getDefaultParsingOptions());
1976 if (!TU) {
1977 fprintf(stderr, "Unable to load translation unit!\n");
1978 return 1;
1979 }
Douglas Gregorc6592922010-11-15 23:00:34 +00001980
1981 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1982 fprintf(stderr, "Unable to reparse translation init!\n");
1983 return 1;
1984 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001985
1986 for (I = 0; I != Repeats; ++I) {
1987 results = clang_codeCompleteAt(TU, filename, line, column,
1988 unsaved_files, num_unsaved_files,
1989 completionOptions);
1990 if (!results) {
1991 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar186f7422010-08-19 23:44:06 +00001992 return 1;
1993 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001994 if (I != Repeats-1)
1995 clang_disposeCodeCompleteResults(results);
1996 }
Douglas Gregorba965fb2010-01-28 00:56:43 +00001997
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001998 if (results) {
Douglas Gregor63745d52011-07-21 01:05:26 +00001999 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor21325842011-07-07 16:03:39 +00002000 unsigned long long contexts;
Douglas Gregor63745d52011-07-21 01:05:26 +00002001 enum CXCursorKind containerKind;
Douglas Gregorea777402011-07-26 15:24:30 +00002002 CXString objCSelector;
2003 const char *selectorString;
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002004 if (!timing_only) {
2005 /* Sort the code-completion results based on the typed text. */
2006 clang_sortCodeCompletionResults(results->Results, results->NumResults);
2007
Douglas Gregor47815d52010-07-12 18:38:41 +00002008 for (i = 0; i != n; ++i)
2009 print_completion_result(results->Results + i, stdout);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002010 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00002011 n = clang_codeCompleteGetNumDiagnostics(results);
2012 for (i = 0; i != n; ++i) {
2013 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
2014 PrintDiagnostic(diag);
2015 clang_disposeDiagnostic(diag);
2016 }
Douglas Gregor21325842011-07-07 16:03:39 +00002017
2018 contexts = clang_codeCompleteGetContexts(results);
2019 print_completion_contexts(contexts, stdout);
2020
Douglas Gregorea777402011-07-26 15:24:30 +00002021 containerKind = clang_codeCompleteGetContainerKind(results,
2022 &containerIsIncomplete);
Douglas Gregor63745d52011-07-21 01:05:26 +00002023
2024 if (containerKind != CXCursor_InvalidCode) {
2025 /* We have found a container */
2026 CXString containerUSR, containerKindSpelling;
2027 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
2028 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
2029 clang_disposeString(containerKindSpelling);
2030
2031 if (containerIsIncomplete) {
2032 printf("Container is incomplete\n");
2033 }
2034 else {
2035 printf("Container is complete\n");
2036 }
2037
2038 containerUSR = clang_codeCompleteGetContainerUSR(results);
2039 printf("Container USR: %s\n", clang_getCString(containerUSR));
2040 clang_disposeString(containerUSR);
2041 }
2042
Douglas Gregorea777402011-07-26 15:24:30 +00002043 objCSelector = clang_codeCompleteGetObjCSelector(results);
2044 selectorString = clang_getCString(objCSelector);
2045 if (selectorString && strlen(selectorString) > 0) {
2046 printf("Objective-C selector: %s\n", selectorString);
2047 }
2048 clang_disposeString(objCSelector);
2049
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002050 clang_disposeCodeCompleteResults(results);
2051 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00002052 clang_disposeTranslationUnit(TU);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002053 clang_disposeIndex(CIdx);
2054 free(filename);
Ted Kremenek29004672010-02-17 00:41:32 +00002055
Douglas Gregor9485bf92009-12-02 09:21:34 +00002056 free_remapped_files(unsaved_files, num_unsaved_files);
2057
Ted Kremenekef3339b2009-11-17 18:09:14 +00002058 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002059}
2060
Douglas Gregor082c3e62010-01-15 19:40:17 +00002061typedef struct {
2062 char *filename;
2063 unsigned line;
2064 unsigned column;
2065} CursorSourceLocation;
2066
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002067static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002068 CXIndex CIdx;
2069 int errorCode;
2070 struct CXUnsavedFile *unsaved_files = 0;
2071 int num_unsaved_files = 0;
2072 CXTranslationUnit TU;
2073 CXCursor Cursor;
2074 CursorSourceLocation *Locations = 0;
2075 unsigned NumLocations = 0, Loc;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002076 unsigned Repeats = 1;
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002077 unsigned I;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002078
Ted Kremenek29004672010-02-17 00:41:32 +00002079 /* Count the number of locations. */
Douglas Gregor082c3e62010-01-15 19:40:17 +00002080 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2081 ++NumLocations;
Ted Kremenek29004672010-02-17 00:41:32 +00002082
Douglas Gregor082c3e62010-01-15 19:40:17 +00002083 /* Parse the locations. */
2084 assert(NumLocations > 0 && "Unable to count locations?");
2085 Locations = (CursorSourceLocation *)malloc(
2086 NumLocations * sizeof(CursorSourceLocation));
2087 for (Loc = 0; Loc < NumLocations; ++Loc) {
2088 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremenek29004672010-02-17 00:41:32 +00002089 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2090 &Locations[Loc].line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002091 &Locations[Loc].column, 0, 0)))
Douglas Gregor082c3e62010-01-15 19:40:17 +00002092 return errorCode;
2093 }
Ted Kremenek29004672010-02-17 00:41:32 +00002094
2095 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002096 &num_unsaved_files))
2097 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00002098
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002099 if (getenv("CINDEXTEST_EDITING"))
2100 Repeats = 5;
2101
2102 /* Parse the translation unit. When we're testing clang_getCursor() after
2103 reparsing, don't remap unsaved files until the second parse. */
2104 CIdx = clang_createIndex(1, 1);
2105 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2106 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002107 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002108 unsaved_files,
2109 Repeats > 1? 0 : num_unsaved_files,
2110 getDefaultParsingOptions());
2111
Douglas Gregor082c3e62010-01-15 19:40:17 +00002112 if (!TU) {
2113 fprintf(stderr, "unable to parse input\n");
2114 return -1;
2115 }
Ted Kremenek29004672010-02-17 00:41:32 +00002116
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002117 if (checkForErrors(TU) != 0)
2118 return -1;
2119
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002120 for (I = 0; I != Repeats; ++I) {
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002121 if (Repeats > 1 &&
2122 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2123 clang_defaultReparseOptions(TU))) {
2124 clang_disposeTranslationUnit(TU);
2125 return 1;
2126 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002127
2128 if (checkForErrors(TU) != 0)
2129 return -1;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002130
2131 for (Loc = 0; Loc < NumLocations; ++Loc) {
2132 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2133 if (!file)
2134 continue;
Ted Kremenek29004672010-02-17 00:41:32 +00002135
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002136 Cursor = clang_getCursor(TU,
2137 clang_getLocation(TU, file, Locations[Loc].line,
2138 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002139
2140 if (checkForErrors(TU) != 0)
2141 return -1;
2142
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002143 if (I + 1 == Repeats) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002144 CXCompletionString completionString = clang_getCursorCompletionString(
2145 Cursor);
2146 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002147 CXString Spelling;
2148 const char *cspell;
2149 unsigned line, column;
2150 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2151 printf("%d:%d ", line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002152 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002153 PrintCursorExtent(Cursor);
2154 Spelling = clang_getCursorSpelling(Cursor);
2155 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002156 if (cspell && strlen(cspell) != 0) {
2157 unsigned pieceIndex;
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002158 printf(" Spelling=%s (", cspell);
2159 for (pieceIndex = 0; ; ++pieceIndex) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002160 CXSourceRange range =
2161 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002162 if (clang_Range_isNull(range))
2163 break;
2164 PrintRange(range, 0);
2165 }
2166 printf(")");
2167 }
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002168 clang_disposeString(Spelling);
Argyrios Kyrtzidis210f29f2012-03-30 22:15:48 +00002169 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2170 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002171 if (clang_Cursor_isDynamicCall(Cursor))
2172 printf(" Dynamic-call");
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002173 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002174 CXType T = clang_Cursor_getReceiverType(Cursor);
2175 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002176 printf(" Receiver-type=%s", clang_getCString(S));
2177 clang_disposeString(S);
2178 }
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002179
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002180 {
2181 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002182 CXFile astFile;
2183 CXString name, astFilename;
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002184 unsigned i, numHeaders;
2185 if (mod) {
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002186 astFile = clang_Module_getASTFile(mod);
2187 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002188 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002189 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002190 printf(" ModuleName=%s (%s) Headers(%d):",
2191 clang_getCString(name), clang_getCString(astFilename),
2192 numHeaders);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002193 clang_disposeString(name);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002194 clang_disposeString(astFilename);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002195 for (i = 0; i < numHeaders; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002196 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2197 CXString filename = clang_getFileName(file);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002198 printf("\n%s", clang_getCString(filename));
2199 clang_disposeString(filename);
2200 }
2201 }
2202 }
2203
Douglas Gregor3f35bb22011-08-04 20:04:59 +00002204 if (completionString != NULL) {
2205 printf("\nCompletion string: ");
2206 print_completion_string(completionString, stdout);
2207 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002208 printf("\n");
2209 free(Locations[Loc].filename);
2210 }
2211 }
Douglas Gregor082c3e62010-01-15 19:40:17 +00002212 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002213
Douglas Gregor33cdd812010-02-18 18:08:43 +00002214 PrintDiagnostics(TU);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002215 clang_disposeTranslationUnit(TU);
2216 clang_disposeIndex(CIdx);
2217 free(Locations);
2218 free_remapped_files(unsaved_files, num_unsaved_files);
2219 return 0;
2220}
2221
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002222static enum CXVisitorResult findFileRefsVisit(void *context,
2223 CXCursor cursor, CXSourceRange range) {
2224 if (clang_Range_isNull(range))
2225 return CXVisit_Continue;
2226
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002227 PrintCursor(cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002228 PrintRange(range, "");
2229 printf("\n");
2230 return CXVisit_Continue;
2231}
2232
2233static int find_file_refs_at(int argc, const char **argv) {
2234 CXIndex CIdx;
2235 int errorCode;
2236 struct CXUnsavedFile *unsaved_files = 0;
2237 int num_unsaved_files = 0;
2238 CXTranslationUnit TU;
2239 CXCursor Cursor;
2240 CursorSourceLocation *Locations = 0;
2241 unsigned NumLocations = 0, Loc;
2242 unsigned Repeats = 1;
2243 unsigned I;
2244
2245 /* Count the number of locations. */
2246 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2247 ++NumLocations;
2248
2249 /* Parse the locations. */
2250 assert(NumLocations > 0 && "Unable to count locations?");
2251 Locations = (CursorSourceLocation *)malloc(
2252 NumLocations * sizeof(CursorSourceLocation));
2253 for (Loc = 0; Loc < NumLocations; ++Loc) {
2254 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2255 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2256 &Locations[Loc].line,
2257 &Locations[Loc].column, 0, 0)))
2258 return errorCode;
2259 }
2260
2261 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2262 &num_unsaved_files))
2263 return -1;
2264
2265 if (getenv("CINDEXTEST_EDITING"))
2266 Repeats = 5;
2267
2268 /* Parse the translation unit. When we're testing clang_getCursor() after
2269 reparsing, don't remap unsaved files until the second parse. */
2270 CIdx = clang_createIndex(1, 1);
2271 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2272 argv + num_unsaved_files + 1 + NumLocations,
2273 argc - num_unsaved_files - 2 - NumLocations,
2274 unsaved_files,
2275 Repeats > 1? 0 : num_unsaved_files,
2276 getDefaultParsingOptions());
2277
2278 if (!TU) {
2279 fprintf(stderr, "unable to parse input\n");
2280 return -1;
2281 }
2282
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002283 if (checkForErrors(TU) != 0)
2284 return -1;
2285
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002286 for (I = 0; I != Repeats; ++I) {
2287 if (Repeats > 1 &&
2288 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2289 clang_defaultReparseOptions(TU))) {
2290 clang_disposeTranslationUnit(TU);
2291 return 1;
2292 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002293
2294 if (checkForErrors(TU) != 0)
2295 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002296
2297 for (Loc = 0; Loc < NumLocations; ++Loc) {
2298 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2299 if (!file)
2300 continue;
2301
2302 Cursor = clang_getCursor(TU,
2303 clang_getLocation(TU, file, Locations[Loc].line,
2304 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002305
2306 if (checkForErrors(TU) != 0)
2307 return -1;
2308
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002309 if (I + 1 == Repeats) {
Erik Verbruggen338b55c2011-10-06 11:38:08 +00002310 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002311 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002312 printf("\n");
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002313 clang_findReferencesInFile(Cursor, file, visitor);
2314 free(Locations[Loc].filename);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002315
2316 if (checkForErrors(TU) != 0)
2317 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002318 }
2319 }
2320 }
2321
2322 PrintDiagnostics(TU);
2323 clang_disposeTranslationUnit(TU);
2324 clang_disposeIndex(CIdx);
2325 free(Locations);
2326 free_remapped_files(unsaved_files, num_unsaved_files);
2327 return 0;
2328}
2329
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002330static enum CXVisitorResult findFileIncludesVisit(void *context,
2331 CXCursor cursor, CXSourceRange range) {
2332 PrintCursor(cursor, NULL);
2333 PrintRange(range, "");
2334 printf("\n");
2335 return CXVisit_Continue;
2336}
2337
2338static int find_file_includes_in(int argc, const char **argv) {
2339 CXIndex CIdx;
2340 struct CXUnsavedFile *unsaved_files = 0;
2341 int num_unsaved_files = 0;
2342 CXTranslationUnit TU;
2343 const char **Filenames = 0;
2344 unsigned NumFilenames = 0;
2345 unsigned Repeats = 1;
2346 unsigned I, FI;
2347
2348 /* Count the number of locations. */
2349 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2350 ++NumFilenames;
2351
2352 /* Parse the locations. */
2353 assert(NumFilenames > 0 && "Unable to count filenames?");
2354 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2355 for (I = 0; I < NumFilenames; ++I) {
2356 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2357 /* Copy the file name. */
2358 Filenames[I] = input;
2359 }
2360
2361 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2362 &num_unsaved_files))
2363 return -1;
2364
2365 if (getenv("CINDEXTEST_EDITING"))
2366 Repeats = 2;
2367
2368 /* Parse the translation unit. When we're testing clang_getCursor() after
2369 reparsing, don't remap unsaved files until the second parse. */
2370 CIdx = clang_createIndex(1, 1);
2371 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2372 argv + num_unsaved_files + 1 + NumFilenames,
2373 argc - num_unsaved_files - 2 - NumFilenames,
2374 unsaved_files,
2375 Repeats > 1? 0 : num_unsaved_files,
2376 getDefaultParsingOptions());
2377
2378 if (!TU) {
2379 fprintf(stderr, "unable to parse input\n");
2380 return -1;
2381 }
2382
2383 if (checkForErrors(TU) != 0)
2384 return -1;
2385
2386 for (I = 0; I != Repeats; ++I) {
2387 if (Repeats > 1 &&
2388 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2389 clang_defaultReparseOptions(TU))) {
2390 clang_disposeTranslationUnit(TU);
2391 return 1;
2392 }
2393
2394 if (checkForErrors(TU) != 0)
2395 return -1;
2396
2397 for (FI = 0; FI < NumFilenames; ++FI) {
2398 CXFile file = clang_getFile(TU, Filenames[FI]);
2399 if (!file)
2400 continue;
2401
2402 if (checkForErrors(TU) != 0)
2403 return -1;
2404
2405 if (I + 1 == Repeats) {
2406 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2407 clang_findIncludesInFile(TU, file, visitor);
2408
2409 if (checkForErrors(TU) != 0)
2410 return -1;
2411 }
2412 }
2413 }
2414
2415 PrintDiagnostics(TU);
2416 clang_disposeTranslationUnit(TU);
2417 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis1b5b1ce2013-03-11 16:03:17 +00002418 free((void *)Filenames);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002419 free_remapped_files(unsaved_files, num_unsaved_files);
2420 return 0;
2421}
2422
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002423#define MAX_IMPORTED_ASTFILES 200
2424
2425typedef struct {
2426 char **filenames;
2427 unsigned num_files;
2428} ImportedASTFilesData;
2429
2430static ImportedASTFilesData *importedASTs_create() {
2431 ImportedASTFilesData *p;
2432 p = malloc(sizeof(ImportedASTFilesData));
2433 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2434 p->num_files = 0;
2435 return p;
2436}
2437
2438static void importedASTs_dispose(ImportedASTFilesData *p) {
2439 unsigned i;
2440 if (!p)
2441 return;
2442
2443 for (i = 0; i < p->num_files; ++i)
2444 free(p->filenames[i]);
2445 free(p->filenames);
2446 free(p);
2447}
2448
2449static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2450 unsigned i;
2451 assert(p && file);
2452 for (i = 0; i < p->num_files; ++i)
2453 if (strcmp(file, p->filenames[i]) == 0)
2454 return;
2455 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2456 p->filenames[p->num_files++] = strdup(file);
2457}
2458
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002459typedef struct {
2460 const char *check_prefix;
2461 int first_check_printed;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002462 int fail_for_error;
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002463 int abort;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002464 const char *main_filename;
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002465 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002466} IndexData;
2467
2468static void printCheck(IndexData *data) {
2469 if (data->check_prefix) {
2470 if (data->first_check_printed) {
2471 printf("// %s-NEXT: ", data->check_prefix);
2472 } else {
2473 printf("// %s : ", data->check_prefix);
2474 data->first_check_printed = 1;
2475 }
2476 }
2477}
2478
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002479static void printCXIndexFile(CXIdxClientFile file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002480 CXString filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002481 printf("%s", clang_getCString(filename));
2482 clang_disposeString(filename);
2483}
2484
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002485static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2486 IndexData *index_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002487 CXString filename;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002488 const char *cname;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002489 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002490 unsigned line, column;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002491 int isMainFile;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002492
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002493 index_data = (IndexData *)client_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002494 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2495 if (line == 0) {
Argyrios Kyrtzidis9f571862012-10-11 19:00:44 +00002496 printf("<invalid>");
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002497 return;
2498 }
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002499 if (!file) {
2500 printf("<no idxfile>");
2501 return;
2502 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002503 filename = clang_getFileName((CXFile)file);
2504 cname = clang_getCString(filename);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002505 if (strcmp(cname, index_data->main_filename) == 0)
2506 isMainFile = 1;
2507 else
2508 isMainFile = 0;
2509 clang_disposeString(filename);
2510
2511 if (!isMainFile) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002512 printCXIndexFile(file);
2513 printf(":");
2514 }
2515 printf("%d:%d", line, column);
2516}
2517
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002518static unsigned digitCount(unsigned val) {
2519 unsigned c = 1;
2520 while (1) {
2521 if (val < 10)
2522 return c;
2523 ++c;
2524 val /= 10;
2525 }
2526}
2527
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002528static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2529 CXIdxLoc loc) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002530 const char *name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002531 char *newStr;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002532 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002533 unsigned line, column;
2534
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002535 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002536 if (!name)
2537 name = "<anon-tag>";
2538
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002539 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidis90068072011-10-20 17:21:46 +00002540 /* FIXME: free these.*/
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002541 newStr = (char *)malloc(strlen(name) +
2542 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002543 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002544 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002545}
2546
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002547static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2548 CXIdxClientContainer container;
2549 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidisdf15c202011-11-16 02:35:05 +00002550 if (!container)
2551 printf("[<<NULL>>]");
2552 else
2553 printf("[%s]", (const char *)container);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002554}
2555
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002556static const char *getEntityKindString(CXIdxEntityKind kind) {
2557 switch (kind) {
2558 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2559 case CXIdxEntity_Typedef: return "typedef";
2560 case CXIdxEntity_Function: return "function";
2561 case CXIdxEntity_Variable: return "variable";
2562 case CXIdxEntity_Field: return "field";
2563 case CXIdxEntity_EnumConstant: return "enumerator";
2564 case CXIdxEntity_ObjCClass: return "objc-class";
2565 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2566 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002567 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2568 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002569 case CXIdxEntity_ObjCProperty: return "objc-property";
2570 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2571 case CXIdxEntity_Enum: return "enum";
2572 case CXIdxEntity_Struct: return "struct";
2573 case CXIdxEntity_Union: return "union";
2574 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002575 case CXIdxEntity_CXXNamespace: return "namespace";
2576 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2577 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2578 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2579 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2580 case CXIdxEntity_CXXConstructor: return "constructor";
2581 case CXIdxEntity_CXXDestructor: return "destructor";
2582 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2583 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikiedcefd952012-08-31 21:55:26 +00002584 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002585 }
2586 assert(0 && "Garbage entity kind");
2587 return 0;
2588}
2589
2590static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2591 switch (kind) {
2592 case CXIdxEntity_NonTemplate: return "";
2593 case CXIdxEntity_Template: return "-template";
2594 case CXIdxEntity_TemplatePartialSpecialization:
2595 return "-template-partial-spec";
2596 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002597 }
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002598 assert(0 && "Garbage entity kind");
2599 return 0;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002600}
2601
Argyrios Kyrtzidis52002882011-12-07 20:44:12 +00002602static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2603 switch (kind) {
2604 case CXIdxEntityLang_None: return "<none>";
2605 case CXIdxEntityLang_C: return "C";
2606 case CXIdxEntityLang_ObjC: return "ObjC";
2607 case CXIdxEntityLang_CXX: return "C++";
2608 }
2609 assert(0 && "Garbage language kind");
2610 return 0;
2611}
2612
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002613static void printEntityInfo(const char *cb,
2614 CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002615 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002616 const char *name;
2617 IndexData *index_data;
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002618 unsigned i;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002619 index_data = (IndexData *)client_data;
2620 printCheck(index_data);
2621
Argyrios Kyrtzidise4acd232011-11-16 02:34:59 +00002622 if (!info) {
2623 printf("%s: <<NULL>>", cb);
2624 return;
2625 }
2626
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002627 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002628 if (!name)
2629 name = "<anon-tag>";
2630
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002631 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2632 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002633 printf(" | name: %s", name);
2634 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002635 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002636
2637 for (i = 0; i != info->numAttributes; ++i) {
2638 const CXIdxAttrInfo *Attr = info->attributes[i];
2639 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002640 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002641 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002642}
2643
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002644static void printBaseClassInfo(CXClientData client_data,
2645 const CXIdxBaseClassInfo *info) {
2646 printEntityInfo(" <base>", client_data, info->base);
2647 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002648 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002649 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002650 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002651}
2652
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002653static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2654 CXClientData client_data) {
2655 unsigned i;
2656 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2657 printEntityInfo(" <protocol>", client_data,
2658 ProtoInfo->protocols[i]->protocol);
2659 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002660 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002661 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002662 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002663 printf("\n");
2664 }
2665}
2666
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002667static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002668 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002669 CXString str;
2670 const char *cstr;
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002671 unsigned numDiags, i;
2672 CXDiagnostic diag;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002673 IndexData *index_data;
2674 index_data = (IndexData *)client_data;
2675 printCheck(index_data);
2676
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002677 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2678 for (i = 0; i != numDiags; ++i) {
2679 diag = clang_getDiagnosticInSet(diagSet, i);
2680 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2681 cstr = clang_getCString(str);
2682 printf("[diagnostic]: %s\n", cstr);
2683 clang_disposeString(str);
2684
2685 if (getenv("CINDEXTEST_FAILONERROR") &&
2686 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2687 index_data->fail_for_error = 1;
2688 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002689 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002690}
2691
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002692static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2693 CXFile file, void *reserved) {
2694 IndexData *index_data;
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002695 CXString filename;
2696
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002697 index_data = (IndexData *)client_data;
2698 printCheck(index_data);
2699
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002700 filename = clang_getFileName(file);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002701 index_data->main_filename = clang_getCString(filename);
2702 clang_disposeString(filename);
2703
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002704 printf("[enteredMainFile]: ");
2705 printCXIndexFile((CXIdxClientFile)file);
2706 printf("\n");
2707
2708 return (CXIdxClientFile)file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002709}
2710
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002711static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002712 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002713 IndexData *index_data;
2714 index_data = (IndexData *)client_data;
2715 printCheck(index_data);
2716
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002717 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002718 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002719 printf(" | name: \"%s\"", info->filename);
2720 printf(" | hash loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002721 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis5e2ec482012-10-18 00:17:05 +00002722 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2723 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002724
2725 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002726}
2727
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002728static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2729 const CXIdxImportedASTFileInfo *info) {
2730 IndexData *index_data;
2731 index_data = (IndexData *)client_data;
2732 printCheck(index_data);
2733
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002734 if (index_data->importedASTs) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002735 CXString filename = clang_getFileName(info->file);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002736 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2737 clang_disposeString(filename);
2738 }
2739
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002740 printf("[importedASTFile]: ");
2741 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002742 if (info->module) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002743 CXString name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002744 printf(" | loc: ");
2745 printCXIndexLoc(info->loc, client_data);
2746 printf(" | name: \"%s\"", clang_getCString(name));
2747 printf(" | isImplicit: %d\n", info->isImplicit);
2748 clang_disposeString(name);
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002749 } else {
NAKAMURA Takumie259d912012-10-12 14:25:52 +00002750 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002751 printf("\n");
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002752 }
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002753
2754 return (CXIdxClientFile)info->file;
2755}
2756
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002757static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002758 void *reserved) {
2759 IndexData *index_data;
2760 index_data = (IndexData *)client_data;
2761 printCheck(index_data);
2762
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002763 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002764 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002765}
2766
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002767static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002768 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002769 IndexData *index_data;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002770 const CXIdxObjCCategoryDeclInfo *CatInfo;
2771 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002772 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002773 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002774 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002775 unsigned i;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002776 index_data = (IndexData *)client_data;
2777
2778 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2779 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002780 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002781 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002782 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis663c8ec2011-12-07 20:44:19 +00002783 printf(" | semantic-container: ");
2784 printCXIndexContainer(info->semanticContainer);
2785 printf(" | lexical-container: ");
2786 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002787 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002788 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002789 if (info->flags & CXIdxDeclFlag_Skipped) {
2790 assert(!info->isContainer);
2791 printf(" | isContainer: skipped");
2792 } else {
2793 printf(" | isContainer: %d", info->isContainer);
2794 }
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002795 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002796
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002797 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi2a4859a2011-11-18 00:51:03 +00002798 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002799 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002800 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002801 printf("\n");
2802 }
2803
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002804 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2805 const char *kindName = 0;
2806 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2807 switch (K) {
2808 case CXIdxObjCContainer_ForwardRef:
2809 kindName = "forward-ref"; break;
2810 case CXIdxObjCContainer_Interface:
2811 kindName = "interface"; break;
2812 case CXIdxObjCContainer_Implementation:
2813 kindName = "implementation"; break;
2814 }
2815 printCheck(index_data);
2816 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2817 }
2818
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002819 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002820 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2821 CatInfo->objcClass);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002822 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002823 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002824 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002825 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002826 printf("\n");
2827 }
2828
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002829 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2830 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002831 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002832 printf("\n");
2833 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002834 }
2835
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002836 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2837 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002838 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002839
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002840 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2841 if (PropInfo->getter) {
2842 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2843 printf("\n");
2844 }
2845 if (PropInfo->setter) {
2846 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2847 printf("\n");
2848 }
2849 }
2850
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002851 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2852 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2853 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2854 printf("\n");
2855 }
2856 }
2857
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002858 if (info->declAsContainer)
2859 clang_index_setClientContainer(info->declAsContainer,
2860 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002861}
2862
2863static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002864 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002865 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002866 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002867 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002868 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002869 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002870 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002871 printf(" | container: ");
2872 printCXIndexContainer(info->container);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002873 printf(" | refkind: ");
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002874 switch (info->kind) {
2875 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002876 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002877 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002878 printf("\n");
2879}
2880
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002881static int index_abortQuery(CXClientData client_data, void *reserved) {
2882 IndexData *index_data;
2883 index_data = (IndexData *)client_data;
2884 return index_data->abort;
2885}
2886
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002887static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002888 index_abortQuery,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002889 index_diagnostic,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002890 index_enteredMainFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002891 index_ppIncludedFile,
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002892 index_importedASTFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002893 index_startedTranslationUnit,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002894 index_indexDeclaration,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002895 index_indexEntityReference
2896};
2897
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00002898static unsigned getIndexOptions(void) {
2899 unsigned index_opts;
2900 index_opts = 0;
2901 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2902 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2903 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2904 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002905 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
2906 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00002907
2908 return index_opts;
2909}
2910
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002911static int index_compile_args(int num_args, const char **args,
2912 CXIndexAction idxAction,
2913 ImportedASTFilesData *importedASTs,
2914 const char *check_prefix) {
2915 IndexData index_data;
2916 unsigned index_opts;
2917 int result;
2918
2919 if (num_args == 0) {
2920 fprintf(stderr, "no compiler arguments\n");
2921 return -1;
2922 }
2923
2924 index_data.check_prefix = check_prefix;
2925 index_data.first_check_printed = 0;
2926 index_data.fail_for_error = 0;
2927 index_data.abort = 0;
2928 index_data.main_filename = "";
2929 index_data.importedASTs = importedASTs;
2930
2931 index_opts = getIndexOptions();
2932 result = clang_indexSourceFile(idxAction, &index_data,
2933 &IndexCB,sizeof(IndexCB), index_opts,
2934 0, args, num_args, 0, 0, 0,
2935 getDefaultParsingOptions());
2936 if (index_data.fail_for_error)
2937 result = -1;
2938
2939 return result;
2940}
2941
2942static int index_ast_file(const char *ast_file,
2943 CXIndex Idx,
2944 CXIndexAction idxAction,
2945 ImportedASTFilesData *importedASTs,
2946 const char *check_prefix) {
2947 CXTranslationUnit TU;
2948 IndexData index_data;
2949 unsigned index_opts;
2950 int result;
2951
2952 if (!CreateTranslationUnit(Idx, ast_file, &TU))
2953 return -1;
2954
2955 index_data.check_prefix = check_prefix;
2956 index_data.first_check_printed = 0;
2957 index_data.fail_for_error = 0;
2958 index_data.abort = 0;
2959 index_data.main_filename = "";
2960 index_data.importedASTs = importedASTs;
2961
2962 index_opts = getIndexOptions();
2963 result = clang_indexTranslationUnit(idxAction, &index_data,
2964 &IndexCB,sizeof(IndexCB),
2965 index_opts, TU);
2966 if (index_data.fail_for_error)
2967 result = -1;
2968
2969 clang_disposeTranslationUnit(TU);
2970 return result;
2971}
2972
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002973static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002974 const char *check_prefix;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002975 CXIndex Idx;
2976 CXIndexAction idxAction;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002977 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002978 int result;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002979
2980 check_prefix = 0;
2981 if (argc > 0) {
2982 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2983 check_prefix = argv[0] + strlen("-check-prefix=");
2984 ++argv;
2985 --argc;
2986 }
2987 }
2988
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002989 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00002990 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002991 fprintf(stderr, "Could not create Index\n");
2992 return 1;
2993 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002994 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002995 importedASTs = 0;
2996 if (full)
2997 importedASTs = importedASTs_create();
2998
2999 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
3000 if (result != 0)
3001 goto finished;
3002
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003003 if (full) {
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003004 unsigned i;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003005 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
3006 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
3007 importedASTs, check_prefix);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003008 }
3009 }
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003010
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003011finished:
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003012 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003013 clang_IndexAction_dispose(idxAction);
3014 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003015 return result;
3016}
3017
3018static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003019 const char *check_prefix;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003020 CXIndex Idx;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003021 CXIndexAction idxAction;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003022 int result;
3023
3024 check_prefix = 0;
3025 if (argc > 0) {
3026 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3027 check_prefix = argv[0] + strlen("-check-prefix=");
3028 ++argv;
3029 --argc;
3030 }
3031 }
3032
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003033 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003034 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003035 fprintf(stderr, "Could not create Index\n");
3036 return 1;
3037 }
3038 idxAction = clang_IndexAction_create(Idx);
3039
3040 result = index_ast_file(argv[0], Idx, idxAction,
3041 /*importedASTs=*/0, check_prefix);
3042
3043 clang_IndexAction_dispose(idxAction);
3044 clang_disposeIndex(Idx);
3045 return result;
3046}
3047
3048static int index_compile_db(int argc, const char **argv) {
3049 const char *check_prefix;
3050 CXIndex Idx;
3051 CXIndexAction idxAction;
3052 int errorCode = 0;
3053
3054 check_prefix = 0;
3055 if (argc > 0) {
3056 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3057 check_prefix = argv[0] + strlen("-check-prefix=");
3058 ++argv;
3059 --argc;
3060 }
3061 }
3062
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003063 if (argc == 0) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003064 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003065 return -1;
3066 }
3067
3068 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003069 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003070 fprintf(stderr, "Could not create Index\n");
3071 return 1;
3072 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003073 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003074
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003075 {
3076 const char *database = argv[0];
3077 CXCompilationDatabase db = 0;
3078 CXCompileCommands CCmds = 0;
3079 CXCompileCommand CCmd;
3080 CXCompilationDatabase_Error ec;
3081 CXString wd;
3082#define MAX_COMPILE_ARGS 512
3083 CXString cxargs[MAX_COMPILE_ARGS];
3084 const char *args[MAX_COMPILE_ARGS];
3085 char *tmp;
3086 unsigned len;
3087 char *buildDir;
3088 int i, a, numCmds, numArgs;
3089
3090 len = strlen(database);
3091 tmp = (char *) malloc(len+1);
3092 memcpy(tmp, database, len+1);
3093 buildDir = dirname(tmp);
3094
3095 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3096
3097 if (db) {
3098
3099 if (ec!=CXCompilationDatabase_NoError) {
3100 printf("unexpected error %d code while loading compilation database\n", ec);
3101 errorCode = -1;
3102 goto cdb_end;
3103 }
3104
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003105 if (chdir(buildDir) != 0) {
3106 printf("Could not chdir to %s\n", buildDir);
3107 errorCode = -1;
3108 goto cdb_end;
3109 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003110
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003111 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003112 if (!CCmds) {
3113 printf("compilation db is empty\n");
3114 errorCode = -1;
3115 goto cdb_end;
3116 }
3117
3118 numCmds = clang_CompileCommands_getSize(CCmds);
3119
3120 if (numCmds==0) {
3121 fprintf(stderr, "should not get an empty compileCommand set\n");
3122 errorCode = -1;
3123 goto cdb_end;
3124 }
3125
3126 for (i=0; i<numCmds && errorCode == 0; ++i) {
3127 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3128
3129 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003130 if (chdir(clang_getCString(wd)) != 0) {
3131 printf("Could not chdir to %s\n", clang_getCString(wd));
3132 errorCode = -1;
3133 goto cdb_end;
3134 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003135 clang_disposeString(wd);
3136
3137 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3138 if (numArgs > MAX_COMPILE_ARGS){
3139 fprintf(stderr, "got more compile arguments than maximum\n");
3140 errorCode = -1;
3141 goto cdb_end;
3142 }
3143 for (a=0; a<numArgs; ++a) {
3144 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3145 args[a] = clang_getCString(cxargs[a]);
3146 }
3147
3148 errorCode = index_compile_args(numArgs, args, idxAction,
3149 /*importedASTs=*/0, check_prefix);
3150
3151 for (a=0; a<numArgs; ++a)
3152 clang_disposeString(cxargs[a]);
3153 }
3154 } else {
3155 printf("database loading failed with error code %d.\n", ec);
3156 errorCode = -1;
3157 }
3158
3159 cdb_end:
3160 clang_CompileCommands_dispose(CCmds);
3161 clang_CompilationDatabase_dispose(db);
3162 free(tmp);
3163
3164 }
3165
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003166 clang_IndexAction_dispose(idxAction);
3167 clang_disposeIndex(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003168 return errorCode;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003169}
3170
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003171int perform_token_annotation(int argc, const char **argv) {
3172 const char *input = argv[1];
3173 char *filename = 0;
3174 unsigned line, second_line;
3175 unsigned column, second_column;
3176 CXIndex CIdx;
3177 CXTranslationUnit TU = 0;
3178 int errorCode;
3179 struct CXUnsavedFile *unsaved_files = 0;
3180 int num_unsaved_files = 0;
3181 CXToken *tokens;
3182 unsigned num_tokens;
3183 CXSourceRange range;
3184 CXSourceLocation startLoc, endLoc;
3185 CXFile file = 0;
3186 CXCursor *cursors = 0;
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003187 CXSourceRangeList *skipped_ranges = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003188 unsigned i;
3189
3190 input += strlen("-test-annotate-tokens=");
3191 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3192 &second_line, &second_column)))
3193 return errorCode;
3194
Richard Smith1ea42eb2012-07-05 08:20:49 +00003195 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3196 free(filename);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003197 return -1;
Richard Smith1ea42eb2012-07-05 08:20:49 +00003198 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003199
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003200 CIdx = clang_createIndex(0, 1);
Douglas Gregor998caea2011-05-06 16:33:08 +00003201 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
3202 argv + num_unsaved_files + 2,
3203 argc - num_unsaved_files - 3,
3204 unsaved_files,
3205 num_unsaved_files,
3206 getDefaultParsingOptions());
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003207 if (!TU) {
3208 fprintf(stderr, "unable to parse input\n");
3209 clang_disposeIndex(CIdx);
3210 free(filename);
3211 free_remapped_files(unsaved_files, num_unsaved_files);
3212 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003213 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003214 errorCode = 0;
3215
Richard Smith1ea42eb2012-07-05 08:20:49 +00003216 if (checkForErrors(TU) != 0) {
3217 errorCode = -1;
3218 goto teardown;
3219 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003220
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003221 if (getenv("CINDEXTEST_EDITING")) {
3222 for (i = 0; i < 5; ++i) {
3223 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3224 clang_defaultReparseOptions(TU))) {
3225 fprintf(stderr, "Unable to reparse translation unit!\n");
3226 errorCode = -1;
3227 goto teardown;
3228 }
3229 }
3230 }
3231
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003232 if (checkForErrors(TU) != 0) {
3233 errorCode = -1;
3234 goto teardown;
3235 }
3236
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003237 file = clang_getFile(TU, filename);
3238 if (!file) {
3239 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3240 errorCode = -1;
3241 goto teardown;
3242 }
3243
3244 startLoc = clang_getLocation(TU, file, line, column);
3245 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003246 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003247 column);
3248 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003249 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003250 }
3251
3252 endLoc = clang_getLocation(TU, file, second_line, second_column);
3253 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003254 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003255 second_line, second_column);
3256 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003257 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003258 }
3259
3260 range = clang_getRange(startLoc, endLoc);
3261 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003262
3263 if (checkForErrors(TU) != 0) {
3264 errorCode = -1;
3265 goto teardown;
3266 }
3267
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003268 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3269 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003270
3271 if (checkForErrors(TU) != 0) {
3272 errorCode = -1;
3273 goto teardown;
3274 }
3275
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003276 skipped_ranges = clang_getSkippedRanges(TU, file);
3277 for (i = 0; i != skipped_ranges->count; ++i) {
3278 unsigned start_line, start_column, end_line, end_column;
3279 clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
3280 0, &start_line, &start_column, 0);
3281 clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
3282 0, &end_line, &end_column, 0);
3283 printf("Skipping: ");
3284 PrintExtent(stdout, start_line, start_column, end_line, end_column);
3285 printf("\n");
3286 }
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003287 clang_disposeSourceRangeList(skipped_ranges);
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003288
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003289 for (i = 0; i != num_tokens; ++i) {
3290 const char *kind = "<unknown>";
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003291 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3292 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003293 unsigned start_line, start_column, end_line, end_column;
3294
3295 switch (clang_getTokenKind(tokens[i])) {
3296 case CXToken_Punctuation: kind = "Punctuation"; break;
3297 case CXToken_Keyword: kind = "Keyword"; break;
3298 case CXToken_Identifier: kind = "Identifier"; break;
3299 case CXToken_Literal: kind = "Literal"; break;
3300 case CXToken_Comment: kind = "Comment"; break;
3301 }
Douglas Gregor229bebd2010-11-09 06:24:54 +00003302 clang_getSpellingLocation(clang_getRangeStart(extent),
3303 0, &start_line, &start_column, 0);
3304 clang_getSpellingLocation(clang_getRangeEnd(extent),
3305 0, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003306 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Krameraf7ae312012-04-14 09:11:51 +00003307 clang_disposeString(spelling);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003308 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor61656112010-01-26 18:31:56 +00003309 if (!clang_isInvalid(cursors[i].kind)) {
3310 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003311 PrintCursor(cursors[i], NULL);
Douglas Gregor61656112010-01-26 18:31:56 +00003312 }
3313 printf("\n");
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003314 }
3315 free(cursors);
Ted Kremenek983fb5de2010-10-20 21:22:15 +00003316 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003317
3318 teardown:
Douglas Gregor33cdd812010-02-18 18:08:43 +00003319 PrintDiagnostics(TU);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003320 clang_disposeTranslationUnit(TU);
3321 clang_disposeIndex(CIdx);
3322 free(filename);
3323 free_remapped_files(unsaved_files, num_unsaved_files);
3324 return errorCode;
3325}
3326
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003327static int
3328perform_test_compilation_db(const char *database, int argc, const char **argv) {
3329 CXCompilationDatabase db;
3330 CXCompileCommands CCmds;
3331 CXCompileCommand CCmd;
3332 CXCompilationDatabase_Error ec;
3333 CXString wd;
3334 CXString arg;
3335 int errorCode = 0;
3336 char *tmp;
3337 unsigned len;
3338 char *buildDir;
3339 int i, j, a, numCmds, numArgs;
3340
3341 len = strlen(database);
3342 tmp = (char *) malloc(len+1);
3343 memcpy(tmp, database, len+1);
3344 buildDir = dirname(tmp);
3345
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003346 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003347
3348 if (db) {
3349
3350 if (ec!=CXCompilationDatabase_NoError) {
3351 printf("unexpected error %d code while loading compilation database\n", ec);
3352 errorCode = -1;
3353 goto cdb_end;
3354 }
3355
3356 for (i=0; i<argc && errorCode==0; ) {
3357 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003358 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003359
3360 if (!CCmds) {
3361 printf("file %s not found in compilation db\n", argv[i+1]);
3362 errorCode = -1;
3363 break;
3364 }
3365
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003366 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003367
3368 if (numCmds==0) {
3369 fprintf(stderr, "should not get an empty compileCommand set for file"
3370 " '%s'\n", argv[i+1]);
3371 errorCode = -1;
3372 break;
3373 }
3374
3375 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003376 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003377
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003378 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003379 printf("workdir:'%s'", clang_getCString(wd));
3380 clang_disposeString(wd);
3381
3382 printf(" cmdline:'");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003383 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003384 for (a=0; a<numArgs; ++a) {
3385 if (a) printf(" ");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003386 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003387 printf("%s", clang_getCString(arg));
3388 clang_disposeString(arg);
3389 }
3390 printf("'\n");
3391 }
3392
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003393 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003394
3395 i += 2;
3396 }
3397 }
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003398 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003399 } else {
3400 printf("database loading failed with error code %d.\n", ec);
3401 errorCode = -1;
3402 }
3403
3404cdb_end:
3405 free(tmp);
3406
3407 return errorCode;
3408}
3409
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003410/******************************************************************************/
Ted Kremenek599d73a2010-03-25 02:00:39 +00003411/* USR printing. */
3412/******************************************************************************/
3413
3414static int insufficient_usr(const char *kind, const char *usage) {
3415 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3416 return 1;
3417}
3418
3419static unsigned isUSR(const char *s) {
3420 return s[0] == 'c' && s[1] == ':';
3421}
3422
3423static int not_usr(const char *s, const char *arg) {
3424 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3425 return 1;
3426}
3427
3428static void print_usr(CXString usr) {
3429 const char *s = clang_getCString(usr);
3430 printf("%s\n", s);
3431 clang_disposeString(usr);
3432}
3433
3434static void display_usrs() {
3435 fprintf(stderr, "-print-usrs options:\n"
3436 " ObjCCategory <class name> <category name>\n"
3437 " ObjCClass <class name>\n"
3438 " ObjCIvar <ivar name> <class USR>\n"
3439 " ObjCMethod <selector> [0=class method|1=instance method] "
3440 "<class USR>\n"
3441 " ObjCProperty <property name> <class USR>\n"
3442 " ObjCProtocol <protocol name>\n");
3443}
3444
3445int print_usrs(const char **I, const char **E) {
3446 while (I != E) {
3447 const char *kind = *I;
3448 unsigned len = strlen(kind);
3449 switch (len) {
3450 case 8:
3451 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3452 if (I + 2 >= E)
3453 return insufficient_usr(kind, "<ivar name> <class USR>");
3454 if (!isUSR(I[2]))
3455 return not_usr("<class USR>", I[2]);
3456 else {
3457 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003458 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003459 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003460 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3461 }
3462
3463 I += 3;
3464 continue;
3465 }
3466 break;
3467 case 9:
3468 if (memcmp(kind, "ObjCClass", 9) == 0) {
3469 if (I + 1 >= E)
3470 return insufficient_usr(kind, "<class name>");
3471 print_usr(clang_constructUSR_ObjCClass(I[1]));
3472 I += 2;
3473 continue;
3474 }
3475 break;
3476 case 10:
3477 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3478 if (I + 3 >= E)
3479 return insufficient_usr(kind, "<method selector> "
3480 "[0=class method|1=instance method] <class USR>");
3481 if (!isUSR(I[3]))
3482 return not_usr("<class USR>", I[3]);
3483 else {
3484 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003485 x.data = (void*) I[3];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003486 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003487 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3488 }
3489 I += 4;
3490 continue;
3491 }
3492 break;
3493 case 12:
3494 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3495 if (I + 2 >= E)
3496 return insufficient_usr(kind, "<class name> <category name>");
3497 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3498 I += 3;
3499 continue;
3500 }
3501 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3502 if (I + 1 >= E)
3503 return insufficient_usr(kind, "<protocol name>");
3504 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3505 I += 2;
3506 continue;
3507 }
3508 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3509 if (I + 2 >= E)
3510 return insufficient_usr(kind, "<property name> <class USR>");
3511 if (!isUSR(I[2]))
3512 return not_usr("<class USR>", I[2]);
3513 else {
3514 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003515 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003516 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003517 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3518 }
3519 I += 3;
3520 continue;
3521 }
3522 break;
3523 default:
3524 break;
3525 }
3526 break;
3527 }
3528
3529 if (I != E) {
3530 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3531 display_usrs();
3532 return 1;
3533 }
3534 return 0;
3535}
3536
3537int print_usrs_file(const char *file_name) {
3538 char line[2048];
3539 const char *args[128];
3540 unsigned numChars = 0;
3541
3542 FILE *fp = fopen(file_name, "r");
3543 if (!fp) {
3544 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3545 return 1;
3546 }
3547
3548 /* This code is not really all that safe, but it works fine for testing. */
3549 while (!feof(fp)) {
3550 char c = fgetc(fp);
3551 if (c == '\n') {
3552 unsigned i = 0;
3553 const char *s = 0;
3554
3555 if (numChars == 0)
3556 continue;
3557
3558 line[numChars] = '\0';
3559 numChars = 0;
3560
3561 if (line[0] == '/' && line[1] == '/')
3562 continue;
3563
3564 s = strtok(line, " ");
3565 while (s) {
3566 args[i] = s;
3567 ++i;
3568 s = strtok(0, " ");
3569 }
3570 if (print_usrs(&args[0], &args[i]))
3571 return 1;
3572 }
3573 else
3574 line[numChars++] = c;
3575 }
3576
3577 fclose(fp);
3578 return 0;
3579}
3580
3581/******************************************************************************/
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003582/* Command line processing. */
3583/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003584int write_pch_file(const char *filename, int argc, const char *argv[]) {
3585 CXIndex Idx;
3586 CXTranslationUnit TU;
3587 struct CXUnsavedFile *unsaved_files = 0;
3588 int num_unsaved_files = 0;
Francois Pichetabcfbec2011-07-06 22:09:44 +00003589 int result = 0;
Douglas Gregore9386682010-08-13 05:36:37 +00003590
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003591 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregore9386682010-08-13 05:36:37 +00003592
3593 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3594 clang_disposeIndex(Idx);
3595 return -1;
3596 }
3597
3598 TU = clang_parseTranslationUnit(Idx, 0,
3599 argv + num_unsaved_files,
3600 argc - num_unsaved_files,
3601 unsaved_files,
3602 num_unsaved_files,
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00003603 CXTranslationUnit_Incomplete |
Argyrios Kyrtzidisd3afa0c2013-04-26 21:33:40 +00003604 CXTranslationUnit_DetailedPreprocessingRecord|
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00003605 CXTranslationUnit_ForSerialization);
Douglas Gregore9386682010-08-13 05:36:37 +00003606 if (!TU) {
3607 fprintf(stderr, "Unable to load translation unit!\n");
3608 free_remapped_files(unsaved_files, num_unsaved_files);
3609 clang_disposeIndex(Idx);
3610 return 1;
3611 }
3612
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003613 switch (clang_saveTranslationUnit(TU, filename,
3614 clang_defaultSaveOptions(TU))) {
3615 case CXSaveError_None:
3616 break;
3617
3618 case CXSaveError_TranslationErrors:
3619 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3620 filename);
3621 result = 2;
3622 break;
3623
3624 case CXSaveError_InvalidTU:
3625 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3626 filename);
3627 result = 3;
3628 break;
3629
3630 case CXSaveError_Unknown:
3631 default:
3632 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3633 result = 1;
3634 break;
3635 }
3636
Douglas Gregore9386682010-08-13 05:36:37 +00003637 clang_disposeTranslationUnit(TU);
3638 free_remapped_files(unsaved_files, num_unsaved_files);
3639 clang_disposeIndex(Idx);
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003640 return result;
Douglas Gregore9386682010-08-13 05:36:37 +00003641}
3642
3643/******************************************************************************/
Ted Kremenekd010ba42011-11-10 08:43:12 +00003644/* Serialized diagnostics. */
3645/******************************************************************************/
3646
3647static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3648 switch (error) {
3649 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3650 case CXLoadDiag_None: break;
3651 case CXLoadDiag_Unknown: return "Unknown";
3652 case CXLoadDiag_InvalidFile: return "Invalid File";
3653 }
3654 return "None";
3655}
3656
3657static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3658 switch (severity) {
3659 case CXDiagnostic_Note: return "note";
3660 case CXDiagnostic_Error: return "error";
3661 case CXDiagnostic_Fatal: return "fatal";
3662 case CXDiagnostic_Ignored: return "ignored";
3663 case CXDiagnostic_Warning: return "warning";
3664 }
3665 return "unknown";
3666}
3667
3668static void printIndent(unsigned indent) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003669 if (indent == 0)
3670 return;
3671 fprintf(stderr, "+");
3672 --indent;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003673 while (indent > 0) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003674 fprintf(stderr, "-");
Ted Kremenekd010ba42011-11-10 08:43:12 +00003675 --indent;
3676 }
3677}
3678
3679static void printLocation(CXSourceLocation L) {
3680 CXFile File;
3681 CXString FileName;
3682 unsigned line, column, offset;
3683
3684 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3685 FileName = clang_getFileName(File);
3686
3687 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3688 clang_disposeString(FileName);
3689}
3690
3691static void printRanges(CXDiagnostic D, unsigned indent) {
3692 unsigned i, n = clang_getDiagnosticNumRanges(D);
3693
3694 for (i = 0; i < n; ++i) {
3695 CXSourceLocation Start, End;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003696 CXSourceRange SR = clang_getDiagnosticRange(D, i);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003697 Start = clang_getRangeStart(SR);
3698 End = clang_getRangeEnd(SR);
3699
3700 printIndent(indent);
3701 fprintf(stderr, "Range: ");
3702 printLocation(Start);
3703 fprintf(stderr, " ");
3704 printLocation(End);
3705 fprintf(stderr, "\n");
3706 }
3707}
3708
3709static void printFixIts(CXDiagnostic D, unsigned indent) {
3710 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek4a642302012-03-20 20:49:45 +00003711 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003712 for (i = 0 ; i < n; ++i) {
3713 CXSourceRange ReplacementRange;
3714 CXString text;
3715 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3716
3717 printIndent(indent);
3718 fprintf(stderr, "FIXIT: (");
3719 printLocation(clang_getRangeStart(ReplacementRange));
3720 fprintf(stderr, " - ");
3721 printLocation(clang_getRangeEnd(ReplacementRange));
3722 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3723 clang_disposeString(text);
3724 }
3725}
3726
3727static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003728 unsigned i, n;
3729
Ted Kremenekd010ba42011-11-10 08:43:12 +00003730 if (!Diags)
3731 return;
3732
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003733 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003734 for (i = 0; i < n; ++i) {
3735 CXSourceLocation DiagLoc;
3736 CXDiagnostic D;
3737 CXFile File;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003738 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003739 unsigned line, column, offset;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003740 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003741
3742 D = clang_getDiagnosticInSet(Diags, i);
3743 DiagLoc = clang_getDiagnosticLocation(D);
3744 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3745 FileName = clang_getFileName(File);
3746 DiagSpelling = clang_getDiagnosticSpelling(D);
3747
3748 printIndent(indent);
3749
3750 fprintf(stderr, "%s:%d:%d: %s: %s",
3751 clang_getCString(FileName),
3752 line,
3753 column,
3754 getSeverityString(clang_getDiagnosticSeverity(D)),
3755 clang_getCString(DiagSpelling));
3756
3757 DiagOption = clang_getDiagnosticOption(D, 0);
3758 DiagOptionStr = clang_getCString(DiagOption);
3759 if (DiagOptionStr) {
3760 fprintf(stderr, " [%s]", DiagOptionStr);
3761 }
3762
Ted Kremenek26a6d492012-04-12 00:03:31 +00003763 DiagCat = clang_getDiagnosticCategoryText(D);
3764 DiagCatStr = clang_getCString(DiagCat);
3765 if (DiagCatStr) {
3766 fprintf(stderr, " [%s]", DiagCatStr);
3767 }
3768
Ted Kremenekd010ba42011-11-10 08:43:12 +00003769 fprintf(stderr, "\n");
3770
3771 printRanges(D, indent);
3772 printFixIts(D, indent);
3773
NAKAMURA Takumi27dd3962011-11-10 10:07:57 +00003774 /* Print subdiagnostics. */
Ted Kremenekd010ba42011-11-10 08:43:12 +00003775 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3776
3777 clang_disposeString(FileName);
3778 clang_disposeString(DiagSpelling);
3779 clang_disposeString(DiagOption);
3780 }
3781}
3782
3783static int read_diagnostics(const char *filename) {
3784 enum CXLoadDiag_Error error;
3785 CXString errorString;
3786 CXDiagnosticSet Diags = 0;
3787
3788 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3789 if (!Diags) {
3790 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3791 getDiagnosticCodeStr(error),
3792 clang_getCString(errorString));
3793 clang_disposeString(errorString);
3794 return 1;
3795 }
3796
3797 printDiagnosticSet(Diags, 0);
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003798 fprintf(stderr, "Number of diagnostics: %d\n",
3799 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenekd010ba42011-11-10 08:43:12 +00003800 clang_disposeDiagnosticSet(Diags);
3801 return 0;
3802}
3803
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003804static int perform_print_build_session_timestamp(void) {
3805 printf("%lld\n", clang_getBuildSessionTimestamp());
3806 return 0;
3807}
3808
Ted Kremenekd010ba42011-11-10 08:43:12 +00003809/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003810/* Command line processing. */
3811/******************************************************************************/
Ted Kremenekef3339b2009-11-17 18:09:14 +00003812
Douglas Gregor720d0052010-01-20 21:32:04 +00003813static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003814 if (s[0] == '\0')
Douglas Gregor720d0052010-01-20 21:32:04 +00003815 return FilteredPrintingVisitor;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003816 if (strcmp(s, "-usrs") == 0)
3817 return USRVisitor;
Ted Kremenek83f642e2011-04-18 22:47:10 +00003818 if (strncmp(s, "-memory-usage", 13) == 0)
3819 return GetVisitor(s + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003820 return NULL;
3821}
3822
Ted Kremenekef3339b2009-11-17 18:09:14 +00003823static void print_usage(void) {
3824 fprintf(stderr,
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003825 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003826 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregor082c3e62010-01-15 19:40:17 +00003827 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003828 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3829 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi4deb9a92012-10-24 22:52:04 +00003830 fprintf(stderr,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003831 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003832 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003833 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003834 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek0469b7e2009-11-18 02:02:52 +00003835 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen338b55c2011-10-06 11:38:08 +00003836 "[FileCheck prefix]\n");
3837 fprintf(stderr,
Ted Kremeneka44d99c2010-01-05 23:18:49 +00003838 " c-index-test -test-load-tu <AST file> <symbol filter> "
3839 "[FileCheck prefix]\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003840 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3841 "[FileCheck prefix]\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003842 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregor082c3e62010-01-15 19:40:17 +00003843 fprintf(stderr,
Ted Kremenek83f642e2011-04-18 22:47:10 +00003844 " c-index-test -test-load-source-memory-usage "
3845 "<symbol filter> {<args>}*\n"
Douglas Gregoraa21cc42010-07-19 21:46:24 +00003846 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3847 " {<args>}*\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003848 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003849 " c-index-test -test-load-source-usrs-memory-usage "
3850 "<symbol filter> {<args>}*\n"
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00003851 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3852 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek11d1a422011-04-18 23:42:53 +00003853 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth718df592010-07-22 06:29:13 +00003854 fprintf(stderr,
Ted Kremenek11d1a422011-04-18 23:42:53 +00003855 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenko00353722013-02-15 21:15:49 +00003856 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00003857 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00003858 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003859 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregore9386682010-08-13 05:36:37 +00003860 " c-index-test -print-usr-file <file>\n"
Ted Kremenekd010ba42011-11-10 08:43:12 +00003861 " c-index-test -write-pch <file> <compiler arguments>\n");
3862 fprintf(stderr,
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003863 " c-index-test -compilation-db [lookup <filename>] database\n");
3864 fprintf(stderr,
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003865 " c-index-test -print-build-session-timestamp\n");
3866 fprintf(stderr,
Ted Kremenekd010ba42011-11-10 08:43:12 +00003867 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregor73a18fd2010-07-20 14:34:35 +00003868 fprintf(stderr,
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003869 " <symbol filter> values:\n%s",
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003870 " all - load all symbols, including those from PCH\n"
3871 " local - load all symbols except those in PCH\n"
3872 " category - only load ObjC categories (non-PCH)\n"
3873 " interface - only load ObjC interfaces (non-PCH)\n"
3874 " protocol - only load ObjC protocols (non-PCH)\n"
3875 " function - only load functions (non-PCH)\n"
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00003876 " typedef - only load typdefs (non-PCH)\n"
3877 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekef3339b2009-11-17 18:09:14 +00003878}
3879
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003880/***/
3881
3882int cindextest_main(int argc, const char **argv) {
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003883 clang_enableStackTraces();
Ted Kremenekd010ba42011-11-10 08:43:12 +00003884 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3885 return read_diagnostics(argv[2]);
Ted Kremenekef3339b2009-11-17 18:09:14 +00003886 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor47815d52010-07-12 18:38:41 +00003887 return perform_code_completion(argc, argv, 0);
3888 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3889 return perform_code_completion(argc, argv, 1);
Douglas Gregor082c3e62010-01-15 19:40:17 +00003890 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3891 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00003892 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3893 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003894 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
3895 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003896 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003897 return index_file(argc - 2, argv + 2, /*full=*/0);
3898 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3899 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003900 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3901 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003902 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
3903 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003904 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00003905 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003906 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00003907 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3908 NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003909 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00003910 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3911 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3912 if (I) {
3913 int trials = atoi(argv[2]);
3914 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3915 NULL);
3916 }
3917 }
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003918 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00003919 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek83f642e2011-04-18 22:47:10 +00003920
3921 PostVisitTU postVisit = 0;
3922 if (strstr(argv[1], "-memory-usage"))
3923 postVisit = PrintMemoryUsage;
3924
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003925 if (I)
Ted Kremenek83f642e2011-04-18 22:47:10 +00003926 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3927 postVisit);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003928 }
3929 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek0469b7e2009-11-18 02:02:52 +00003930 return perform_file_scan(argv[2], argv[3],
3931 argc >= 5 ? argv[4] : 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003932 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3933 return perform_token_annotation(argc, argv);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00003934 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3935 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3936 PrintInclusionStack);
3937 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3938 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3939 PrintInclusionStack);
Ted Kremenek83b28a22010-03-03 06:37:58 +00003940 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3941 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3942 NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00003943 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek6bca9842010-05-14 21:29:26 +00003944 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenko00353722013-02-15 21:15:49 +00003945 PrintType, 0);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00003946 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
3947 return perform_test_load_source(argc - 2, argv + 2, "all",
3948 PrintTypeSize, 0);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00003949 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
3950 return perform_test_load_source(argc - 2, argv + 2, "all",
3951 PrintBitWidth, 0);
Ted Kremenek599d73a2010-03-25 02:00:39 +00003952 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3953 if (argc > 2)
3954 return print_usrs(argv + 2, argv + argc);
3955 else {
3956 display_usrs();
3957 return 1;
3958 }
3959 }
3960 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3961 return print_usrs_file(argv[2]);
Douglas Gregore9386682010-08-13 05:36:37 +00003962 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3963 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003964 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3965 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003966 else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0)
3967 return perform_print_build_session_timestamp();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003968
Ted Kremenekef3339b2009-11-17 18:09:14 +00003969 print_usage();
3970 return 1;
Steve Naroffa1c72842009-08-28 15:28:48 +00003971}
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003972
3973/***/
3974
3975/* We intentionally run in a separate thread to ensure we at least minimal
3976 * testing of a multithreaded environment (for example, having a reduced stack
3977 * size). */
3978
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003979typedef struct thread_info {
3980 int argc;
3981 const char **argv;
3982 int result;
3983} thread_info;
Benjamin Kramer112fc6c2010-11-04 19:11:31 +00003984void thread_runner(void *client_data_v) {
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003985 thread_info *client_data = client_data_v;
3986 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumia7d49882012-04-07 06:59:28 +00003987#ifdef __CYGWIN__
3988 fflush(stdout); /* stdout is not flushed on Cygwin. */
3989#endif
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003990}
3991
3992int main(int argc, const char **argv) {
Benjamin Kramer3a913ed2012-08-10 10:06:13 +00003993 thread_info client_data;
3994
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003995#ifdef CLANG_HAVE_LIBXML
3996 LIBXML_TEST_VERSION
3997#endif
3998
Douglas Gregorf428bf82010-10-27 16:00:01 +00003999 if (getenv("CINDEXTEST_NOTHREADS"))
4000 return cindextest_main(argc, argv);
4001
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004002 client_data.argc = argc;
4003 client_data.argv = argv;
Daniel Dunbar23397c32010-11-04 01:26:31 +00004004 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004005 return client_data.result;
4006}