blob: 56f39c2e12cb50586bbd087b7116a5973d3b7c16 [file] [log] [blame]
Steve Naroff69b10fd2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroffa1c72842009-08-28 15:28:48 +00002
Alp Toker1d257e12014-06-04 03:28:55 +00003#include "clang/Config/config.h"
Steve Naroffa1c72842009-08-28 15:28:48 +00004#include "clang-c/Index.h"
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00005#include "clang-c/CXCompilationDatabase.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +00006#include "clang-c/BuildSystem.h"
Alp Toker59c6bc52014-04-28 02:39:27 +00007#include "clang-c/Documentation.h"
Douglas Gregor49f67ce2010-08-26 13:48:20 +00008#include <ctype.h>
Douglas Gregor9eb77012009-11-07 00:00:49 +00009#include <stdlib.h>
Steve Naroff1054e602009-08-31 00:59:03 +000010#include <stdio.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +000011#include <string.h>
Douglas Gregor082c3e62010-01-15 19:40:17 +000012#include <assert.h>
Steve Naroff38c1a7b2009-09-03 15:49:00 +000013
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +000014#ifdef CLANG_HAVE_LIBXML
15#include <libxml/parser.h>
16#include <libxml/relaxng.h>
17#include <libxml/xmlerror.h>
18#endif
19
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +000020#ifdef _WIN32
21# include <direct.h>
22#else
23# include <unistd.h>
24#endif
25
Ted Kremenek1cd27d52009-11-17 18:13:31 +000026/******************************************************************************/
27/* Utility functions. */
28/******************************************************************************/
29
John Thompsonde258b52009-10-27 13:42:56 +000030#ifdef _MSC_VER
31char *basename(const char* path)
32{
33 char* base1 = (char*)strrchr(path, '/');
34 char* base2 = (char*)strrchr(path, '\\');
35 if (base1 && base2)
36 return((base1 > base2) ? base1 + 1 : base2 + 1);
37 else if (base1)
38 return(base1 + 1);
39 else if (base2)
40 return(base2 + 1);
41
42 return((char*)path);
43}
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000044char *dirname(char* path)
45{
46 char* base1 = (char*)strrchr(path, '/');
47 char* base2 = (char*)strrchr(path, '\\');
48 if (base1 && base2)
49 if (base1 > base2)
50 *base1 = 0;
51 else
52 *base2 = 0;
53 else if (base1)
NAKAMURA Takumi1e43baa62012-06-30 11:47:18 +000054 *base1 = 0;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000055 else if (base2)
NAKAMURA Takumi1e43baa62012-06-30 11:47:18 +000056 *base2 = 0;
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000057
58 return path;
59}
John Thompsonde258b52009-10-27 13:42:56 +000060#else
Steve Naroffa7753c42009-09-24 20:03:06 +000061extern char *basename(const char *);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +000062extern char *dirname(char *);
John Thompsonde258b52009-10-27 13:42:56 +000063#endif
Steve Naroffa7753c42009-09-24 20:03:06 +000064
Douglas Gregorf2430ba2010-07-25 17:39:21 +000065/** \brief Return the default parsing options. */
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000066static unsigned getDefaultParsingOptions() {
67 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
68
69 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregor4a47bca2010-08-09 22:28:58 +000070 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregorb14904c2010-08-13 22:48:40 +000071 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
72 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidiscb373e32011-11-03 02:20:25 +000073 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
74 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6e922512012-04-12 10:11:59 +000075 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
76 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenko3292d062012-07-02 17:35:10 +000077 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
78 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregorbe2d8c62010-07-23 00:33:23 +000079
80 return options;
81}
82
Patrik Hagglund55701d22014-02-17 11:54:08 +000083/** \brief Returns 0 in case of success, non-zero in case of a failure. */
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +000084static int checkForErrors(CXTranslationUnit TU);
85
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +000086static void describeLibclangFailure(enum CXErrorCode Err) {
87 switch (Err) {
88 case CXError_Success:
89 fprintf(stderr, "Success\n");
90 return;
91
92 case CXError_Failure:
93 fprintf(stderr, "Failure (no details available)\n");
94 return;
95
96 case CXError_Crashed:
97 fprintf(stderr, "Failure: libclang crashed\n");
98 return;
99
100 case CXError_InvalidArguments:
101 fprintf(stderr, "Failure: invalid arguments passed to a libclang routine\n");
102 return;
103
104 case CXError_ASTReadError:
105 fprintf(stderr, "Failure: AST deserialization error occurred\n");
106 return;
107 }
108}
109
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000110static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
111 unsigned end_line, unsigned end_column) {
112 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbar02968e52010-02-14 10:02:57 +0000113 end_line, end_column);
Daniel Dunbar98c07e02010-02-14 08:32:24 +0000114}
115
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000116static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
117 CXTranslationUnit *TU) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +0000118 enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU);
119 if (Err != CXError_Success) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000120 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +0000121 describeLibclangFailure(Err);
122 *TU = 0;
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000123 return 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000124 }
Ted Kremenek2df52dc2009-11-17 19:37:36 +0000125 return 1;
126}
127
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000128void free_remapped_files(struct CXUnsavedFile *unsaved_files,
129 int num_unsaved_files) {
130 int i;
131 for (i = 0; i != num_unsaved_files; ++i) {
132 free((char *)unsaved_files[i].Filename);
133 free((char *)unsaved_files[i].Contents);
134 }
Douglas Gregor0e3da272010-08-19 20:50:29 +0000135 free(unsaved_files);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000136}
137
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000138static int parse_remapped_files_with_opt(const char *opt_name,
139 int argc, const char **argv,
140 int start_arg,
141 struct CXUnsavedFile **unsaved_files,
142 int *num_unsaved_files) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000143 int i;
144 int arg;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000145 int prefix_len = strlen(opt_name);
146 int arg_indices[20];
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000147 *unsaved_files = 0;
148 *num_unsaved_files = 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000149
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000150 /* Count the number of remapped files. */
151 for (arg = start_arg; arg < argc; ++arg) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000152 if (strncmp(argv[arg], opt_name, prefix_len))
153 continue;
Ted Kremenek29004672010-02-17 00:41:32 +0000154
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000155 assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int)));
156 arg_indices[*num_unsaved_files] = arg;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000157 ++*num_unsaved_files;
158 }
Ted Kremenek29004672010-02-17 00:41:32 +0000159
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000160 if (*num_unsaved_files == 0)
161 return 0;
Ted Kremenek29004672010-02-17 00:41:32 +0000162
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000163 *unsaved_files
Douglas Gregor0e3da272010-08-19 20:50:29 +0000164 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
165 *num_unsaved_files);
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000166 for (i = 0; i != *num_unsaved_files; ++i) {
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000167 struct CXUnsavedFile *unsaved = *unsaved_files + i;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000168 const char *arg_string = argv[arg_indices[i]] + prefix_len;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000169 int filename_len;
170 char *filename;
171 char *contents;
172 FILE *to_file;
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000173 const char *sep = strchr(arg_string, ',');
174 if (!sep) {
Ted Kremenek29004672010-02-17 00:41:32 +0000175 fprintf(stderr,
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000176 "error: %sfrom:to argument is missing comma\n", opt_name);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000177 free_remapped_files(*unsaved_files, i);
178 *unsaved_files = 0;
179 *num_unsaved_files = 0;
180 return -1;
181 }
Ted Kremenek29004672010-02-17 00:41:32 +0000182
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000183 /* Open the file that we're remapping to. */
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000184 to_file = fopen(sep + 1, "rb");
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000185 if (!to_file) {
186 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000187 sep + 1);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000188 free_remapped_files(*unsaved_files, i);
189 *unsaved_files = 0;
190 *num_unsaved_files = 0;
191 return -1;
192 }
Ted Kremenek29004672010-02-17 00:41:32 +0000193
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000194 /* Determine the length of the file we're remapping to. */
195 fseek(to_file, 0, SEEK_END);
196 unsaved->Length = ftell(to_file);
197 fseek(to_file, 0, SEEK_SET);
Ted Kremenek29004672010-02-17 00:41:32 +0000198
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000199 /* Read the contents of the file we're remapping to. */
200 contents = (char *)malloc(unsaved->Length + 1);
201 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
202 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000203 (feof(to_file) ? "EOF" : "error"), sep + 1);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000204 fclose(to_file);
205 free_remapped_files(*unsaved_files, i);
Richard Smith1ea42eb2012-07-05 08:20:49 +0000206 free(contents);
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000207 *unsaved_files = 0;
208 *num_unsaved_files = 0;
209 return -1;
210 }
211 contents[unsaved->Length] = 0;
212 unsaved->Contents = contents;
Ted Kremenek29004672010-02-17 00:41:32 +0000213
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000214 /* Close the file. */
215 fclose(to_file);
Ted Kremenek29004672010-02-17 00:41:32 +0000216
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000217 /* Copy the file name that we're remapping from. */
Argyrios Kyrtzidis5899e892013-12-05 20:13:27 +0000218 filename_len = sep - arg_string;
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000219 filename = (char *)malloc(filename_len + 1);
220 memcpy(filename, arg_string, filename_len);
221 filename[filename_len] = 0;
222 unsaved->Filename = filename;
223 }
Ted Kremenek29004672010-02-17 00:41:32 +0000224
Douglas Gregoraa98ed92010-01-23 00:14:00 +0000225 return 0;
226}
227
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +0000228static int parse_remapped_files(int argc, const char **argv, int start_arg,
229 struct CXUnsavedFile **unsaved_files,
230 int *num_unsaved_files) {
231 return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg,
232 unsaved_files, num_unsaved_files);
233}
234
235static int parse_remapped_files_with_try(int try_idx,
236 int argc, const char **argv,
237 int start_arg,
238 struct CXUnsavedFile **unsaved_files,
239 int *num_unsaved_files) {
240 struct CXUnsavedFile *unsaved_files_no_try_idx;
241 int num_unsaved_files_no_try_idx;
242 struct CXUnsavedFile *unsaved_files_try_idx;
243 int num_unsaved_files_try_idx;
244 int ret;
245 char opt_name[32];
246
247 ret = parse_remapped_files(argc, argv, start_arg,
248 &unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx);
249 if (ret)
250 return ret;
251
252 sprintf(opt_name, "-remap-file-%d=", try_idx);
253 ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg,
254 &unsaved_files_try_idx, &num_unsaved_files_try_idx);
255 if (ret)
256 return ret;
257
258 *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx;
259 *unsaved_files
260 = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx,
261 sizeof(struct CXUnsavedFile) *
262 *num_unsaved_files);
263 memcpy(*unsaved_files + num_unsaved_files_no_try_idx,
264 unsaved_files_try_idx, sizeof(struct CXUnsavedFile) *
265 num_unsaved_files_try_idx);
266 free(unsaved_files_try_idx);
267 return 0;
268}
269
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000270static const char *parse_comments_schema(int argc, const char **argv) {
271 const char *CommentsSchemaArg = "-comments-xml-schema=";
272 const char *CommentSchemaFile = NULL;
273
274 if (argc == 0)
275 return CommentSchemaFile;
276
277 if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
278 CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
279
280 return CommentSchemaFile;
281}
282
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000283/******************************************************************************/
284/* Pretty-printing. */
285/******************************************************************************/
286
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000287static const char *FileCheckPrefix = "CHECK";
288
289static void PrintCString(const char *CStr) {
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000290 if (CStr != NULL && CStr[0] != '\0') {
291 for ( ; *CStr; ++CStr) {
292 const char C = *CStr;
293 switch (C) {
294 case '\n': printf("\\n"); break;
295 case '\r': printf("\\r"); break;
296 case '\t': printf("\\t"); break;
297 case '\v': printf("\\v"); break;
298 case '\f': printf("\\f"); break;
299 default: putchar(C); break;
300 }
301 }
302 }
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000303}
304
305static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
306 printf(" %s=[", Prefix);
307 PrintCString(CStr);
Dmitri Gribenko5188c4b2012-06-26 20:39:18 +0000308 printf("]");
309}
310
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000311static void PrintCXStringAndDispose(CXString Str) {
312 PrintCString(clang_getCString(Str));
313 clang_disposeString(Str);
314}
315
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000316static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
317 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
318}
319
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000320static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
321 CXString Str) {
322 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
323 clang_disposeString(Str);
324}
325
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000326static void PrintRange(CXSourceRange R, const char *str) {
327 CXFile begin_file, end_file;
328 unsigned begin_line, begin_column, end_line, end_column;
329
330 clang_getSpellingLocation(clang_getRangeStart(R),
331 &begin_file, &begin_line, &begin_column, 0);
332 clang_getSpellingLocation(clang_getRangeEnd(R),
333 &end_file, &end_line, &end_column, 0);
334 if (!begin_file || !end_file)
335 return;
336
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +0000337 if (str)
338 printf(" %s=", str);
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000339 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
340}
341
Douglas Gregor97c75712010-10-02 22:49:11 +0000342int want_display_name = 0;
343
Douglas Gregord6225d32012-05-08 00:14:45 +0000344static void printVersion(const char *Prefix, CXVersion Version) {
345 if (Version.Major < 0)
346 return;
347 printf("%s%d", Prefix, Version.Major);
348
349 if (Version.Minor < 0)
350 return;
351 printf(".%d", Version.Minor);
352
353 if (Version.Subminor < 0)
354 return;
355 printf(".%d", Version.Subminor);
356}
357
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000358struct CommentASTDumpingContext {
359 int IndentLevel;
360};
361
362static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
363 CXComment Comment) {
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000364 unsigned i;
365 unsigned e;
366 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
367
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000368 Ctx->IndentLevel++;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000369 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000370 printf(" ");
371
372 printf("(");
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000373 switch (Kind) {
374 case CXComment_Null:
375 printf("CXComment_Null");
376 break;
377 case CXComment_Text:
378 printf("CXComment_Text");
379 PrintCXStringWithPrefixAndDispose("Text",
380 clang_TextComment_getText(Comment));
381 if (clang_Comment_isWhitespace(Comment))
382 printf(" IsWhitespace");
383 if (clang_InlineContentComment_hasTrailingNewline(Comment))
384 printf(" HasTrailingNewline");
385 break;
386 case CXComment_InlineCommand:
387 printf("CXComment_InlineCommand");
388 PrintCXStringWithPrefixAndDispose(
389 "CommandName",
390 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenkod73e4ce2012-07-23 16:43:01 +0000391 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
392 case CXCommentInlineCommandRenderKind_Normal:
393 printf(" RenderNormal");
394 break;
395 case CXCommentInlineCommandRenderKind_Bold:
396 printf(" RenderBold");
397 break;
398 case CXCommentInlineCommandRenderKind_Monospaced:
399 printf(" RenderMonospaced");
400 break;
401 case CXCommentInlineCommandRenderKind_Emphasized:
402 printf(" RenderEmphasized");
403 break;
404 }
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000405 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000406 i != e; ++i) {
407 printf(" Arg[%u]=", i);
408 PrintCXStringAndDispose(
409 clang_InlineCommandComment_getArgText(Comment, i));
410 }
411 if (clang_InlineContentComment_hasTrailingNewline(Comment))
412 printf(" HasTrailingNewline");
413 break;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000414 case CXComment_HTMLStartTag: {
415 unsigned NumAttrs;
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000416 printf("CXComment_HTMLStartTag");
417 PrintCXStringWithPrefixAndDispose(
418 "Name",
419 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000420 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000421 if (NumAttrs != 0) {
422 printf(" Attrs:");
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000423 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000424 printf(" ");
425 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
426 printf("=");
427 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
428 }
429 }
430 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
431 printf(" SelfClosing");
432 if (clang_InlineContentComment_hasTrailingNewline(Comment))
433 printf(" HasTrailingNewline");
434 break;
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000435 }
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000436 case CXComment_HTMLEndTag:
437 printf("CXComment_HTMLEndTag");
438 PrintCXStringWithPrefixAndDispose(
439 "Name",
440 clang_HTMLTagComment_getTagName(Comment));
441 if (clang_InlineContentComment_hasTrailingNewline(Comment))
442 printf(" HasTrailingNewline");
443 break;
444 case CXComment_Paragraph:
445 printf("CXComment_Paragraph");
446 if (clang_Comment_isWhitespace(Comment))
447 printf(" IsWhitespace");
448 break;
449 case CXComment_BlockCommand:
450 printf("CXComment_BlockCommand");
451 PrintCXStringWithPrefixAndDispose(
452 "CommandName",
453 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000454 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000455 i != e; ++i) {
456 printf(" Arg[%u]=", i);
457 PrintCXStringAndDispose(
458 clang_BlockCommandComment_getArgText(Comment, i));
459 }
460 break;
461 case CXComment_ParamCommand:
462 printf("CXComment_ParamCommand");
463 switch (clang_ParamCommandComment_getDirection(Comment)) {
464 case CXCommentParamPassDirection_In:
465 printf(" in");
466 break;
467 case CXCommentParamPassDirection_Out:
468 printf(" out");
469 break;
470 case CXCommentParamPassDirection_InOut:
471 printf(" in,out");
472 break;
473 }
474 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
475 printf(" explicitly");
476 else
477 printf(" implicitly");
478 PrintCXStringWithPrefixAndDispose(
479 "ParamName",
480 clang_ParamCommandComment_getParamName(Comment));
481 if (clang_ParamCommandComment_isParamIndexValid(Comment))
482 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
483 else
484 printf(" ParamIndex=Invalid");
485 break;
Dmitri Gribenko34df2202012-07-31 22:37:06 +0000486 case CXComment_TParamCommand:
487 printf("CXComment_TParamCommand");
488 PrintCXStringWithPrefixAndDispose(
489 "ParamName",
490 clang_TParamCommandComment_getParamName(Comment));
491 if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
492 printf(" ParamPosition={");
493 for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
494 i != e; ++i) {
495 printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
496 if (i != e - 1)
497 printf(", ");
498 }
499 printf("}");
500 } else
501 printf(" ParamPosition=Invalid");
502 break;
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000503 case CXComment_VerbatimBlockCommand:
504 printf("CXComment_VerbatimBlockCommand");
505 PrintCXStringWithPrefixAndDispose(
506 "CommandName",
507 clang_BlockCommandComment_getCommandName(Comment));
508 break;
509 case CXComment_VerbatimBlockLine:
510 printf("CXComment_VerbatimBlockLine");
511 PrintCXStringWithPrefixAndDispose(
512 "Text",
513 clang_VerbatimBlockLineComment_getText(Comment));
514 break;
515 case CXComment_VerbatimLine:
516 printf("CXComment_VerbatimLine");
517 PrintCXStringWithPrefixAndDispose(
518 "Text",
519 clang_VerbatimLineComment_getText(Comment));
520 break;
521 case CXComment_FullComment:
522 printf("CXComment_FullComment");
523 break;
524 }
525 if (Kind != CXComment_Null) {
526 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenkof267c872012-07-20 22:00:35 +0000527 unsigned i;
528 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000529 printf("\n// %s: ", FileCheckPrefix);
530 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
531 }
532 }
533 printf(")");
534 Ctx->IndentLevel--;
535}
536
537static void DumpCXComment(CXComment Comment) {
538 struct CommentASTDumpingContext Ctx;
539 Ctx.IndentLevel = 1;
540 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
541 DumpCXCommentInternal(&Ctx, Comment);
542 printf("]");
543}
544
Chandler Carruthb2faa592014-05-02 23:30:59 +0000545static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000546#ifdef CLANG_HAVE_LIBXML
547 xmlRelaxNGParserCtxtPtr RNGParser;
548 xmlRelaxNGPtr Schema;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000549 xmlDocPtr Doc;
550 xmlRelaxNGValidCtxtPtr ValidationCtxt;
551 int status;
552
Chandler Carruthb2faa592014-05-02 23:30:59 +0000553 if (!CommentSchemaFile)
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000554 return;
555
Chandler Carruthb2faa592014-05-02 23:30:59 +0000556 RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile);
557 if (!RNGParser) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000558 printf(" libXMLError");
559 return;
560 }
Chandler Carruthb2faa592014-05-02 23:30:59 +0000561 Schema = xmlRelaxNGParse(RNGParser);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000562
563 Doc = xmlParseDoc((const xmlChar *) Str);
564
565 if (!Doc) {
566 xmlErrorPtr Error = xmlGetLastError();
567 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
568 return;
569 }
570
Chandler Carruthb2faa592014-05-02 23:30:59 +0000571 ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000572 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
573 if (!status)
574 printf(" CommentXMLValid");
575 else if (status > 0) {
576 xmlErrorPtr Error = xmlGetLastError();
577 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
578 } else
579 printf(" libXMLError");
580
581 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
582 xmlFreeDoc(Doc);
Chandler Carruthb2faa592014-05-02 23:30:59 +0000583 xmlRelaxNGFree(Schema);
584 xmlRelaxNGFreeParserCtxt(RNGParser);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000585#endif
586}
587
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000588static void PrintCursorComments(CXCursor Cursor,
Chandler Carruthb2faa592014-05-02 23:30:59 +0000589 const char *CommentSchemaFile) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000590 {
591 CXString RawComment;
592 const char *RawCommentCString;
593 CXString BriefComment;
594 const char *BriefCommentCString;
595
596 RawComment = clang_Cursor_getRawCommentText(Cursor);
597 RawCommentCString = clang_getCString(RawComment);
598 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
599 PrintCStringWithPrefix("RawComment", RawCommentCString);
600 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
601
602 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
603 BriefCommentCString = clang_getCString(BriefComment);
604 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
605 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
606 clang_disposeString(BriefComment);
607 }
608 clang_disposeString(RawComment);
609 }
610
611 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000612 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000613 if (clang_Comment_getKind(Comment) != CXComment_Null) {
614 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
615 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000616 {
617 CXString XML;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000618 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000619 PrintCXStringWithPrefix("FullCommentAsXML", XML);
Chandler Carruthb2faa592014-05-02 23:30:59 +0000620 ValidateCommentXML(clang_getCString(XML), CommentSchemaFile);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000621 clang_disposeString(XML);
622 }
623
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000624 DumpCXComment(Comment);
625 }
626 }
627}
628
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000629typedef struct {
630 unsigned line;
631 unsigned col;
632} LineCol;
633
634static int lineCol_cmp(const void *p1, const void *p2) {
635 const LineCol *lhs = p1;
636 const LineCol *rhs = p2;
637 if (lhs->line != rhs->line)
638 return (int)lhs->line - (int)rhs->line;
639 return (int)lhs->col - (int)rhs->col;
640}
641
Chandler Carruthb2faa592014-05-02 23:30:59 +0000642static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000643 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000644 if (clang_isInvalid(Cursor.kind)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000645 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
Ted Kremenek29004672010-02-17 00:41:32 +0000646 printf("Invalid Cursor => %s", clang_getCString(ks));
647 clang_disposeString(ks);
648 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000649 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000650 CXString string, ks;
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000651 CXCursor Referenced;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000652 unsigned line, column;
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000653 CXCursor SpecializationOf;
Douglas Gregor99a26af2010-10-01 20:25:15 +0000654 CXCursor *overridden;
655 unsigned num_overridden;
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000656 unsigned RefNameRangeNr;
657 CXSourceRange CursorExtent;
658 CXSourceRange RefNameRange;
Douglas Gregord6225d32012-05-08 00:14:45 +0000659 int AlwaysUnavailable;
660 int AlwaysDeprecated;
661 CXString UnavailableMessage;
662 CXString DeprecatedMessage;
663 CXPlatformAvailability PlatformAvailability[2];
664 int NumPlatformAvailability;
665 int I;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000666
Ted Kremenek29004672010-02-17 00:41:32 +0000667 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor97c75712010-10-02 22:49:11 +0000668 string = want_display_name? clang_getCursorDisplayName(Cursor)
669 : clang_getCursorSpelling(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000670 printf("%s=%s", clang_getCString(ks),
671 clang_getCString(string));
672 clang_disposeString(ks);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000673 clang_disposeString(string);
Ted Kremenek29004672010-02-17 00:41:32 +0000674
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000675 Referenced = clang_getCursorReferenced(Cursor);
676 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000677 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
678 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
679 printf("[");
680 for (I = 0; I != N; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000681 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor2967e282010-09-14 00:20:32 +0000682 CXSourceLocation Loc;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000683 if (I)
684 printf(", ");
685
Douglas Gregor2967e282010-09-14 00:20:32 +0000686 Loc = clang_getCursorLocation(Ovl);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000687 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000688 printf("%d:%d", line, column);
689 }
690 printf("]");
691 } else {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000692 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000693 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000694 printf(":%d:%d", line, column);
695 }
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000696 }
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000697
698 if (clang_isCursorDefinition(Cursor))
699 printf(" (Definition)");
Douglas Gregorf757a122010-08-23 23:00:57 +0000700
701 switch (clang_getCursorAvailability(Cursor)) {
702 case CXAvailability_Available:
703 break;
704
705 case CXAvailability_Deprecated:
706 printf(" (deprecated)");
707 break;
708
709 case CXAvailability_NotAvailable:
710 printf(" (unavailable)");
711 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000712
713 case CXAvailability_NotAccessible:
714 printf(" (inaccessible)");
715 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000716 }
Ted Kremeneka5940822010-08-26 01:42:22 +0000717
Douglas Gregord6225d32012-05-08 00:14:45 +0000718 NumPlatformAvailability
719 = clang_getCursorPlatformAvailability(Cursor,
720 &AlwaysDeprecated,
721 &DeprecatedMessage,
722 &AlwaysUnavailable,
723 &UnavailableMessage,
724 PlatformAvailability, 2);
725 if (AlwaysUnavailable) {
726 printf(" (always unavailable: \"%s\")",
727 clang_getCString(UnavailableMessage));
728 } else if (AlwaysDeprecated) {
729 printf(" (always deprecated: \"%s\")",
730 clang_getCString(DeprecatedMessage));
731 } else {
732 for (I = 0; I != NumPlatformAvailability; ++I) {
733 if (I >= 2)
734 break;
735
736 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
737 if (PlatformAvailability[I].Unavailable)
738 printf(", unavailable");
739 else {
740 printVersion(", introduced=", PlatformAvailability[I].Introduced);
741 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
742 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
743 }
744 if (clang_getCString(PlatformAvailability[I].Message)[0])
745 printf(", message=\"%s\"",
746 clang_getCString(PlatformAvailability[I].Message));
747 printf(")");
748 }
749 }
750 for (I = 0; I != NumPlatformAvailability; ++I) {
751 if (I >= 2)
752 break;
753 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
754 }
755
756 clang_disposeString(DeprecatedMessage);
757 clang_disposeString(UnavailableMessage);
758
Douglas Gregora8d0c772011-05-13 15:54:42 +0000759 if (clang_CXXMethod_isStatic(Cursor))
760 printf(" (static)");
761 if (clang_CXXMethod_isVirtual(Cursor))
762 printf(" (virtual)");
Dmitri Gribenkoe570ede2014-04-07 14:59:13 +0000763 if (clang_CXXMethod_isConst(Cursor))
764 printf(" (const)");
Dmitri Gribenko62770be2013-05-17 18:38:35 +0000765 if (clang_CXXMethod_isPureVirtual(Cursor))
766 printf(" (pure)");
Argyrios Kyrtzidis23814e42013-04-18 23:53:05 +0000767 if (clang_Cursor_isVariadic(Cursor))
768 printf(" (variadic)");
Argyrios Kyrtzidis7b50fc52013-07-05 20:44:37 +0000769 if (clang_Cursor_isObjCOptional(Cursor))
770 printf(" (@optional)");
771
Ted Kremeneka5940822010-08-26 01:42:22 +0000772 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000773 CXType T =
774 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
775 CXString S = clang_getTypeKindSpelling(T.kind);
Ted Kremeneka5940822010-08-26 01:42:22 +0000776 printf(" [IBOutletCollection=%s]", clang_getCString(S));
777 clang_disposeString(S);
778 }
Ted Kremenekae9e2212010-08-27 21:34:58 +0000779
780 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
781 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
782 unsigned isVirtual = clang_isVirtualBase(Cursor);
783 const char *accessStr = 0;
784
785 switch (access) {
786 case CX_CXXInvalidAccessSpecifier:
787 accessStr = "invalid"; break;
788 case CX_CXXPublic:
789 accessStr = "public"; break;
790 case CX_CXXProtected:
791 accessStr = "protected"; break;
792 case CX_CXXPrivate:
793 accessStr = "private"; break;
794 }
795
796 printf(" [access=%s isVirtual=%s]", accessStr,
797 isVirtual ? "true" : "false");
798 }
Eli Benderskyc27a0c42014-10-10 20:01:05 +0000799
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000800 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
801 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000802 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
803 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000804 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Eli Benderskyc27a0c42014-10-10 20:01:05 +0000805 printf(" [Specialization of %s:%d:%d]",
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000806 clang_getCString(Name), line, column);
807 clang_disposeString(Name);
Eli Benderskyc27a0c42014-10-10 20:01:05 +0000808
809 if (Cursor.kind == CXCursor_FunctionDecl) {
810 /* Collect the template parameter kinds from the base template. */
811 unsigned NumTemplateArgs = clang_Cursor_getNumTemplateArguments(Cursor);
812 unsigned I;
813 for (I = 0; I < NumTemplateArgs; I++) {
814 enum CXTemplateArgumentKind TAK =
815 clang_Cursor_getTemplateArgumentKind(Cursor, I);
816 switch(TAK) {
817 case CXTemplateArgumentKind_Type:
818 {
819 CXType T = clang_Cursor_getTemplateArgumentType(Cursor, I);
820 CXString S = clang_getTypeSpelling(T);
821 printf(" [Template arg %d: kind: %d, type: %s]",
822 I, TAK, clang_getCString(S));
823 clang_disposeString(S);
824 }
825 break;
826 case CXTemplateArgumentKind_Integral:
Yaron Keren129dfbf2015-05-14 06:53:31 +0000827 printf(" [Template arg %d: kind: %d, intval: %lld]",
Eli Benderskyc27a0c42014-10-10 20:01:05 +0000828 I, TAK, clang_Cursor_getTemplateArgumentValue(Cursor, I));
829 break;
830 default:
831 printf(" [Template arg %d: kind: %d]\n", I, TAK);
832 }
833 }
834 }
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000835 }
Douglas Gregor99a26af2010-10-01 20:25:15 +0000836
837 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
838 if (num_overridden) {
839 unsigned I;
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000840 LineCol lineCols[50];
841 assert(num_overridden <= 50);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000842 printf(" [Overrides ");
843 for (I = 0; I != num_overridden; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000844 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000845 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000846 lineCols[I].line = line;
847 lineCols[I].col = column;
848 }
Michael Liaob94f47a2012-08-30 00:45:32 +0000849 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000850 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
851 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor99a26af2010-10-01 20:25:15 +0000852 if (I)
853 printf(", ");
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000854 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000855 }
856 printf("]");
857 clang_disposeOverriddenCursors(overridden);
858 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000859
860 if (Cursor.kind == CXCursor_InclusionDirective) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000861 CXFile File = clang_getIncludedFile(Cursor);
862 CXString Included = clang_getFileName(File);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000863 printf(" (%s)", clang_getCString(Included));
864 clang_disposeString(Included);
Douglas Gregor37aa4932011-05-04 00:14:37 +0000865
866 if (clang_isFileMultipleIncludeGuarded(TU, File))
867 printf(" [multi-include guarded]");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000868 }
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000869
870 CursorExtent = clang_getCursorExtent(Cursor);
871 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
872 CXNameRange_WantQualifier
873 | CXNameRange_WantSinglePiece
874 | CXNameRange_WantTemplateArgs,
875 0);
876 if (!clang_equalRanges(CursorExtent, RefNameRange))
877 PrintRange(RefNameRange, "SingleRefName");
878
879 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
880 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
881 CXNameRange_WantQualifier
882 | CXNameRange_WantTemplateArgs,
883 RefNameRangeNr);
884 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
885 break;
886 if (!clang_equalRanges(CursorExtent, RefNameRange))
887 PrintRange(RefNameRange, "RefName");
888 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000889
Chandler Carruthb2faa592014-05-02 23:30:59 +0000890 PrintCursorComments(Cursor, CommentSchemaFile);
Argyrios Kyrtzidis9adfd8a2013-04-18 22:15:49 +0000891
892 {
893 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
894 if (PropAttrs != CXObjCPropertyAttr_noattr) {
895 printf(" [");
896 #define PRINT_PROP_ATTR(A) \
897 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
898 PRINT_PROP_ATTR(readonly);
899 PRINT_PROP_ATTR(getter);
900 PRINT_PROP_ATTR(assign);
901 PRINT_PROP_ATTR(readwrite);
902 PRINT_PROP_ATTR(retain);
903 PRINT_PROP_ATTR(copy);
904 PRINT_PROP_ATTR(nonatomic);
905 PRINT_PROP_ATTR(setter);
906 PRINT_PROP_ATTR(atomic);
907 PRINT_PROP_ATTR(weak);
908 PRINT_PROP_ATTR(strong);
909 PRINT_PROP_ATTR(unsafe_unretained);
910 printf("]");
911 }
912 }
Argyrios Kyrtzidis9d9bc012013-04-18 23:29:12 +0000913
914 {
915 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
916 if (QT != CXObjCDeclQualifier_None) {
917 printf(" [");
918 #define PRINT_OBJC_QUAL(A) \
919 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
920 PRINT_OBJC_QUAL(In);
921 PRINT_OBJC_QUAL(Inout);
922 PRINT_OBJC_QUAL(Out);
923 PRINT_OBJC_QUAL(Bycopy);
924 PRINT_OBJC_QUAL(Byref);
925 PRINT_OBJC_QUAL(Oneway);
926 printf("]");
927 }
928 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000929 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000930}
Steve Naroff1054e602009-08-31 00:59:03 +0000931
Ted Kremenek29004672010-02-17 00:41:32 +0000932static const char* GetCursorSource(CXCursor Cursor) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000933 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekc560b682010-02-17 00:41:20 +0000934 CXString source;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000935 CXFile file;
Argyrios Kyrtzidis7ca77352011-11-03 02:20:36 +0000936 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000937 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +0000938 if (!clang_getCString(source)) {
Ted Kremenekc560b682010-02-17 00:41:20 +0000939 clang_disposeString(source);
940 return "<invalid loc>";
941 }
942 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000943 const char *b = basename(clang_getCString(source));
Ted Kremenekc560b682010-02-17 00:41:20 +0000944 clang_disposeString(source);
945 return b;
946 }
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000947}
948
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000949/******************************************************************************/
Ted Kremenekb478ff42010-01-26 17:59:48 +0000950/* Callbacks. */
951/******************************************************************************/
952
953typedef void (*PostVisitTU)(CXTranslationUnit);
954
Douglas Gregor33cdd812010-02-18 18:08:43 +0000955void PrintDiagnostic(CXDiagnostic Diagnostic) {
956 FILE *out = stderr;
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000957 CXFile file;
Douglas Gregord770f732010-02-22 23:17:23 +0000958 CXString Msg;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000959 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregora750e8e2010-11-19 16:18:16 +0000960 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
961 | CXDiagnostic_DisplayOption;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000962 unsigned i, num_fixits;
Ted Kremenek599d73a2010-03-25 02:00:39 +0000963
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000964 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000965 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000966
Douglas Gregord770f732010-02-22 23:17:23 +0000967 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
968 fprintf(stderr, "%s\n", clang_getCString(Msg));
969 clang_disposeString(Msg);
Ted Kremenek599d73a2010-03-25 02:00:39 +0000970
Douglas Gregor229bebd2010-11-09 06:24:54 +0000971 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
972 &file, 0, 0, 0);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000973 if (!file)
974 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000975
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000976 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek4a642302012-03-20 20:49:45 +0000977 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000978 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor836ec942010-02-19 18:16:06 +0000979 CXSourceRange range;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000980 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
981 CXSourceLocation start = clang_getRangeStart(range);
982 CXSourceLocation end = clang_getRangeEnd(range);
Douglas Gregor836ec942010-02-19 18:16:06 +0000983 unsigned start_line, start_column, end_line, end_column;
984 CXFile start_file, end_file;
Douglas Gregor229bebd2010-11-09 06:24:54 +0000985 clang_getSpellingLocation(start, &start_file, &start_line,
986 &start_column, 0);
987 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor836ec942010-02-19 18:16:06 +0000988 if (clang_equalLocations(start, end)) {
989 /* Insertion. */
990 if (start_file == file)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000991 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor836ec942010-02-19 18:16:06 +0000992 clang_getCString(insertion_text), start_line, start_column);
993 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
994 /* Removal. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000995 if (start_file == file && end_file == file) {
996 fprintf(out, "FIX-IT: Remove ");
997 PrintExtent(out, start_line, start_column, end_line, end_column);
998 fprintf(out, "\n");
Douglas Gregor60b11f62010-01-29 00:41:11 +0000999 }
Douglas Gregor836ec942010-02-19 18:16:06 +00001000 } else {
1001 /* Replacement. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001002 if (start_file == end_file) {
1003 fprintf(out, "FIX-IT: Replace ");
1004 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor836ec942010-02-19 18:16:06 +00001005 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor9773e3d2010-02-18 22:27:07 +00001006 }
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001007 }
Douglas Gregor836ec942010-02-19 18:16:06 +00001008 clang_disposeString(insertion_text);
Douglas Gregor60b11f62010-01-29 00:41:11 +00001009 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +00001010}
1011
Ted Kremenek914c7e62012-02-14 02:46:03 +00001012void PrintDiagnosticSet(CXDiagnosticSet Set) {
1013 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
1014 for ( ; i != n ; ++i) {
1015 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
1016 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001017 PrintDiagnostic(Diag);
Ted Kremenek914c7e62012-02-14 02:46:03 +00001018 if (ChildDiags)
1019 PrintDiagnosticSet(ChildDiags);
1020 }
1021}
1022
1023void PrintDiagnostics(CXTranslationUnit TU) {
1024 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
1025 PrintDiagnosticSet(TUSet);
1026 clang_disposeDiagnosticSet(TUSet);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001027}
1028
Ted Kremenek83f642e2011-04-18 22:47:10 +00001029void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayd6238f42011-08-29 16:37:29 +00001030 unsigned long total = 0;
Ted Kremenek11d1a422011-04-18 23:42:53 +00001031 unsigned i = 0;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001032 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet45cc5462011-04-18 23:33:22 +00001033 fprintf(stderr, "Memory usage:\n");
Ted Kremenek11d1a422011-04-18 23:42:53 +00001034 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenek23324122011-04-20 16:41:07 +00001035 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek83f642e2011-04-18 22:47:10 +00001036 unsigned long amount = usage.entries[i].amount;
1037 total += amount;
Ted Kremenek11d1a422011-04-18 23:42:53 +00001038 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek83f642e2011-04-18 22:47:10 +00001039 ((double) amount)/(1024*1024));
1040 }
Ted Kremenek11d1a422011-04-18 23:42:53 +00001041 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek83f642e2011-04-18 22:47:10 +00001042 ((double) total)/(1024*1024));
Ted Kremenek23324122011-04-20 16:41:07 +00001043 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek83f642e2011-04-18 22:47:10 +00001044}
1045
Ted Kremenekb478ff42010-01-26 17:59:48 +00001046/******************************************************************************/
Douglas Gregor720d0052010-01-20 21:32:04 +00001047/* Logic for testing traversal. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001048/******************************************************************************/
1049
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001050static void PrintCursorExtent(CXCursor C) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001051 CXSourceRange extent = clang_getCursorExtent(C);
1052 PrintRange(extent, "Extent");
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001053}
1054
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001055/* Data used by the visitors. */
1056typedef struct {
Douglas Gregor720d0052010-01-20 21:32:04 +00001057 CXTranslationUnit TU;
1058 enum CXCursorKind *Filter;
Chandler Carruthb2faa592014-05-02 23:30:59 +00001059 const char *CommentSchemaFile;
Douglas Gregor720d0052010-01-20 21:32:04 +00001060} VisitorData;
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001061
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001062
Ted Kremenek29004672010-02-17 00:41:32 +00001063enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001064 CXCursor Parent,
1065 CXClientData ClientData) {
1066 VisitorData *Data = (VisitorData *)ClientData;
1067 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001068 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001069 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001070 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001071 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor4f46e782010-01-19 21:36:55 +00001072 GetCursorSource(Cursor), line, column);
Chandler Carruthb2faa592014-05-02 23:30:59 +00001073 PrintCursor(Cursor, Data->CommentSchemaFile);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001074 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis1ab09cc2013-04-11 17:02:10 +00001075 if (clang_isDeclaration(Cursor.kind)) {
1076 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
1077 const char *accessStr = 0;
1078
1079 switch (access) {
1080 case CX_CXXInvalidAccessSpecifier: break;
1081 case CX_CXXPublic:
1082 accessStr = "public"; break;
1083 case CX_CXXProtected:
1084 accessStr = "protected"; break;
1085 case CX_CXXPrivate:
1086 accessStr = "private"; break;
1087 }
1088
1089 if (accessStr)
1090 printf(" [access=%s]", accessStr);
1091 }
Ted Kremenek29004672010-02-17 00:41:32 +00001092 printf("\n");
Douglas Gregor720d0052010-01-20 21:32:04 +00001093 return CXChildVisit_Recurse;
Steve Naroff772c1a42009-08-31 14:26:51 +00001094 }
Ted Kremenek29004672010-02-17 00:41:32 +00001095
Douglas Gregor720d0052010-01-20 21:32:04 +00001096 return CXChildVisit_Continue;
Steve Naroff1054e602009-08-31 00:59:03 +00001097}
Steve Naroffa1c72842009-08-28 15:28:48 +00001098
Ted Kremenek29004672010-02-17 00:41:32 +00001099static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001100 CXCursor Parent,
1101 CXClientData ClientData) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001102 const char *startBuf, *endBuf;
1103 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1104 CXCursor Ref;
Douglas Gregor720d0052010-01-20 21:32:04 +00001105 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001106
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001107 if (Cursor.kind != CXCursor_FunctionDecl ||
1108 !clang_isCursorDefinition(Cursor))
Douglas Gregor720d0052010-01-20 21:32:04 +00001109 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001110
1111 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1112 &startLine, &startColumn,
1113 &endLine, &endColumn);
1114 /* Probe the entire body, looking for both decls and refs. */
1115 curLine = startLine;
1116 curColumn = startColumn;
1117
1118 while (startBuf < endBuf) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001119 CXSourceLocation Loc;
Douglas Gregor4f46e782010-01-19 21:36:55 +00001120 CXFile file;
Ted Kremenekc560b682010-02-17 00:41:20 +00001121 CXString source;
Ted Kremenek29004672010-02-17 00:41:32 +00001122
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001123 if (*startBuf == '\n') {
1124 startBuf++;
1125 curLine++;
1126 curColumn = 1;
1127 } else if (*startBuf != '\t')
1128 curColumn++;
Ted Kremenek29004672010-02-17 00:41:32 +00001129
Douglas Gregor66a58812010-01-18 22:46:11 +00001130 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor229bebd2010-11-09 06:24:54 +00001131 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremenek29004672010-02-17 00:41:32 +00001132
Douglas Gregor4f46e782010-01-19 21:36:55 +00001133 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +00001134 if (clang_getCString(source)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001135 CXSourceLocation RefLoc
1136 = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregor816fd362010-01-22 21:44:22 +00001137 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor66a58812010-01-18 22:46:11 +00001138 if (Ref.kind == CXCursor_NoDeclFound) {
1139 /* Nothing found here; that's fine. */
1140 } else if (Ref.kind != CXCursor_FunctionDecl) {
1141 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1142 curLine, curColumn);
Chandler Carruthb2faa592014-05-02 23:30:59 +00001143 PrintCursor(Ref, Data->CommentSchemaFile);
Douglas Gregor66a58812010-01-18 22:46:11 +00001144 printf("\n");
1145 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001146 }
Ted Kremenekc560b682010-02-17 00:41:20 +00001147 clang_disposeString(source);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001148 startBuf++;
1149 }
Ted Kremenek29004672010-02-17 00:41:32 +00001150
Douglas Gregor720d0052010-01-20 21:32:04 +00001151 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001152}
1153
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001154/******************************************************************************/
1155/* USR testing. */
1156/******************************************************************************/
1157
Douglas Gregor720d0052010-01-20 21:32:04 +00001158enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1159 CXClientData ClientData) {
1160 VisitorData *Data = (VisitorData *)ClientData;
1161 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001162 CXString USR = clang_getCursorUSR(C);
1163 const char *cstr = clang_getCString(USR);
Ted Kremenek6d159c12010-04-20 23:15:40 +00001164 if (!cstr || cstr[0] == '\0') {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001165 clang_disposeString(USR);
Ted Kremenek7afa85b2010-04-16 21:31:52 +00001166 return CXChildVisit_Recurse;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001167 }
Ted Kremenek6d159c12010-04-20 23:15:40 +00001168 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1169
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001170 PrintCursorExtent(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001171 printf("\n");
1172 clang_disposeString(USR);
Ted Kremenek29004672010-02-17 00:41:32 +00001173
Douglas Gregor720d0052010-01-20 21:32:04 +00001174 return CXChildVisit_Recurse;
Ted Kremenek29004672010-02-17 00:41:32 +00001175 }
1176
Douglas Gregor720d0052010-01-20 21:32:04 +00001177 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001178}
1179
1180/******************************************************************************/
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001181/* Inclusion stack testing. */
1182/******************************************************************************/
1183
1184void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1185 unsigned includeStackLen, CXClientData data) {
Ted Kremenek29004672010-02-17 00:41:32 +00001186
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001187 unsigned i;
Ted Kremenekc560b682010-02-17 00:41:20 +00001188 CXString fname;
1189
1190 fname = clang_getFileName(includedFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001191 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenekc560b682010-02-17 00:41:20 +00001192 clang_disposeString(fname);
Ted Kremenek29004672010-02-17 00:41:32 +00001193
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001194 for (i = 0; i < includeStackLen; ++i) {
1195 CXFile includingFile;
1196 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001197 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1198 &column, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +00001199 fname = clang_getFileName(includingFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001200 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenekc560b682010-02-17 00:41:20 +00001201 clang_disposeString(fname);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001202 }
1203 printf("\n");
1204}
1205
1206void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremenek29004672010-02-17 00:41:32 +00001207 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001208}
1209
1210/******************************************************************************/
Ted Kremenek83b28a22010-03-03 06:37:58 +00001211/* Linkage testing. */
1212/******************************************************************************/
1213
1214static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1215 CXClientData d) {
1216 const char *linkage = 0;
1217
1218 if (clang_isInvalid(clang_getCursorKind(cursor)))
1219 return CXChildVisit_Recurse;
1220
1221 switch (clang_getCursorLinkage(cursor)) {
1222 case CXLinkage_Invalid: break;
Douglas Gregor0b466502010-03-04 19:36:27 +00001223 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1224 case CXLinkage_Internal: linkage = "Internal"; break;
1225 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1226 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek83b28a22010-03-03 06:37:58 +00001227 }
1228
1229 if (linkage) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001230 PrintCursor(cursor, NULL);
Ted Kremenek83b28a22010-03-03 06:37:58 +00001231 printf("linkage=%s\n", linkage);
1232 }
1233
1234 return CXChildVisit_Recurse;
1235}
1236
1237/******************************************************************************/
Ted Kremenek6bca9842010-05-14 21:29:26 +00001238/* Typekind testing. */
1239/******************************************************************************/
1240
Dmitri Gribenko00353722013-02-15 21:15:49 +00001241static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1242 CXString TypeSpelling, TypeKindSpelling;
1243
1244 TypeSpelling = clang_getTypeSpelling(T);
1245 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1246 printf(Format,
1247 clang_getCString(TypeSpelling),
1248 clang_getCString(TypeKindSpelling));
1249 clang_disposeString(TypeSpelling);
1250 clang_disposeString(TypeKindSpelling);
1251}
1252
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001253static enum CXVisitorResult FieldVisitor(CXCursor C,
1254 CXClientData client_data) {
1255 (*(int *) client_data)+=1;
1256 return CXVisit_Continue;
1257}
1258
Dmitri Gribenko00353722013-02-15 21:15:49 +00001259static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1260 CXClientData d) {
Ted Kremenek6bca9842010-05-14 21:29:26 +00001261 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001262 CXType T = clang_getCursorType(cursor);
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001263 enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001264 PrintCursor(cursor, NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00001265 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregor56a63802011-01-27 16:27:11 +00001266 if (clang_isConstQualifiedType(T))
1267 printf(" const");
1268 if (clang_isVolatileQualifiedType(T))
1269 printf(" volatile");
1270 if (clang_isRestrictQualifiedType(T))
1271 printf(" restrict");
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001272 if (RQ == CXRefQualifier_LValue)
1273 printf(" lvalue-ref-qualifier");
1274 if (RQ == CXRefQualifier_RValue)
1275 printf(" rvalue-ref-qualifier");
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001276 /* Print the canonical type if it is different. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001277 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001278 CXType CT = clang_getCanonicalType(T);
Ted Kremenekc1508872010-06-21 20:15:39 +00001279 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001280 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001281 }
1282 }
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001283 /* Print the return type if it exists. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001284 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001285 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenekc1508872010-06-21 20:15:39 +00001286 if (RT.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001287 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001288 }
1289 }
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001290 /* Print the argument types if they exist. */
1291 {
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001292 int NumArgs = clang_Cursor_getNumArguments(cursor);
1293 if (NumArgs != -1 && NumArgs != 0) {
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001294 int i;
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001295 printf(" [args=");
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001296 for (i = 0; i < NumArgs; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001297 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001298 if (T.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001299 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001300 }
1301 }
1302 printf("]");
1303 }
1304 }
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001305 /* Print the template argument types if they exist. */
1306 {
1307 int NumTArgs = clang_Type_getNumTemplateArguments(T);
1308 if (NumTArgs != -1 && NumTArgs != 0) {
1309 int i;
1310 printf(" [templateargs/%d=", NumTArgs);
1311 for (i = 0; i < NumTArgs; ++i) {
1312 CXType TArg = clang_Type_getTemplateArgumentAsType(T, i);
1313 if (TArg.kind != CXType_Invalid) {
1314 PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]");
1315 }
1316 }
1317 printf("]");
1318 }
1319 }
Ted Kremenek0c7476a2010-07-30 00:14:11 +00001320 /* Print if this is a non-POD type. */
1321 printf(" [isPOD=%d]", clang_isPODType(T));
Anders Waldenborgddce74f2014-04-09 19:16:08 +00001322 /* Print the pointee type. */
1323 {
1324 CXType PT = clang_getPointeeType(T);
1325 if (PT.kind != CXType_Invalid) {
1326 PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]");
1327 }
1328 }
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001329 /* Print the number of fields if they exist. */
1330 {
1331 int numFields = 0;
1332 if (clang_Type_visitFields(T, FieldVisitor, &numFields)){
1333 if (numFields != 0) {
1334 printf(" [nbFields=%d]", numFields);
1335 }
1336 /* Print if it is an anonymous record. */
1337 {
1338 unsigned isAnon = clang_Cursor_isAnonymous(cursor);
1339 if (isAnon != 0) {
1340 printf(" [isAnon=%d]", isAnon);
1341 }
1342 }
1343 }
1344 }
Ted Kremenekc1508872010-06-21 20:15:39 +00001345
Ted Kremenek6bca9842010-05-14 21:29:26 +00001346 printf("\n");
1347 }
1348 return CXChildVisit_Recurse;
1349}
1350
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001351static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1352 CXClientData d) {
1353 CXType T;
1354 enum CXCursorKind K = clang_getCursorKind(cursor);
1355 if (clang_isInvalid(K))
1356 return CXChildVisit_Recurse;
1357 T = clang_getCursorType(cursor);
1358 PrintCursor(cursor, NULL);
1359 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1360 /* Print the type sizeof if applicable. */
1361 {
1362 long long Size = clang_Type_getSizeOf(T);
1363 if (Size >= 0 || Size < -1 ) {
Yaron Keren129dfbf2015-05-14 06:53:31 +00001364 printf(" [sizeof=%lld]", Size);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001365 }
1366 }
1367 /* Print the type alignof if applicable. */
1368 {
1369 long long Align = clang_Type_getAlignOf(T);
1370 if (Align >= 0 || Align < -1) {
Yaron Keren129dfbf2015-05-14 06:53:31 +00001371 printf(" [alignof=%lld]", Align);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001372 }
1373 }
1374 /* Print the record field offset if applicable. */
1375 {
Nico Weber82098cb2014-04-24 04:14:12 +00001376 CXString FieldSpelling = clang_getCursorSpelling(cursor);
1377 const char *FieldName = clang_getCString(FieldSpelling);
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001378 /* recurse to get the first parent record that is not anonymous. */
1379 CXCursor Parent, Record;
1380 unsigned RecordIsAnonymous = 0;
Nico Weber82098cb2014-04-24 04:14:12 +00001381 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) {
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001382 Record = Parent = p;
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001383 do {
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001384 Record = Parent;
1385 Parent = clang_getCursorSemanticParent(Record);
1386 RecordIsAnonymous = clang_Cursor_isAnonymous(Record);
1387 /* Recurse as long as the parent is a CXType_Record and the Record
1388 is anonymous */
1389 } while ( clang_getCursorType(Parent).kind == CXType_Record &&
1390 RecordIsAnonymous > 0);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001391 {
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001392 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Record),
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001393 FieldName);
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001394 long long Offset2 = clang_Cursor_getOffsetOfField(cursor);
1395 if (Offset == Offset2){
Yaron Keren129dfbf2015-05-14 06:53:31 +00001396 printf(" [offsetof=%lld]", Offset);
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001397 } else {
1398 /* Offsets will be different in anonymous records. */
Yaron Keren129dfbf2015-05-14 06:53:31 +00001399 printf(" [offsetof=%lld/%lld]", Offset, Offset2);
Argyrios Kyrtzidis2bff5162015-04-13 16:55:04 +00001400 }
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001401 }
1402 }
Nico Weber82098cb2014-04-24 04:14:12 +00001403 clang_disposeString(FieldSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001404 }
1405 /* Print if its a bitfield */
1406 {
1407 int IsBitfield = clang_Cursor_isBitField(cursor);
1408 if (IsBitfield)
1409 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1410 }
1411 printf("\n");
1412 return CXChildVisit_Recurse;
1413}
1414
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001415/******************************************************************************/
Eli Bendersky44a206f2014-07-31 18:04:56 +00001416/* Mangling testing. */
1417/******************************************************************************/
1418
1419static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p,
1420 CXClientData d) {
1421 CXString MangledName;
1422 PrintCursor(cursor, NULL);
1423 MangledName = clang_Cursor_getMangling(cursor);
1424 printf(" [mangled=%s]\n", clang_getCString(MangledName));
Eli Bendersky78e83d82014-08-01 12:55:44 +00001425 clang_disposeString(MangledName);
Eli Bendersky44a206f2014-07-31 18:04:56 +00001426 return CXChildVisit_Continue;
1427}
1428
1429/******************************************************************************/
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001430/* Bitwidth testing. */
1431/******************************************************************************/
1432
1433static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1434 CXClientData d) {
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001435 int Bitwidth;
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001436 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1437 return CXChildVisit_Recurse;
1438
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001439 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001440 if (Bitwidth >= 0) {
1441 PrintCursor(cursor, NULL);
1442 printf(" bitwidth=%d\n", Bitwidth);
1443 }
1444
1445 return CXChildVisit_Recurse;
1446}
Ted Kremenek6bca9842010-05-14 21:29:26 +00001447
1448/******************************************************************************/
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001449/* Loading ASTs/source. */
1450/******************************************************************************/
1451
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001452static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek73eccd22010-01-12 18:53:15 +00001453 const char *filter, const char *prefix,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001454 CXCursorVisitor Visitor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001455 PostVisitTU PV,
1456 const char *CommentSchemaFile) {
Ted Kremenek29004672010-02-17 00:41:32 +00001457
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001458 if (prefix)
Ted Kremenek29004672010-02-17 00:41:32 +00001459 FileCheckPrefix = prefix;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001460
1461 if (Visitor) {
1462 enum CXCursorKind K = CXCursor_NotImplemented;
1463 enum CXCursorKind *ck = &K;
1464 VisitorData Data;
Ted Kremenek29004672010-02-17 00:41:32 +00001465
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001466 /* Perform some simple filtering. */
1467 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor97c75712010-10-02 22:49:11 +00001468 else if (!strcmp(filter, "all-display") ||
1469 !strcmp(filter, "local-display")) {
1470 ck = NULL;
1471 want_display_name = 1;
1472 }
Daniel Dunbard64ce7b2010-02-10 20:42:40 +00001473 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001474 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1475 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1476 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1477 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1478 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1479 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1480 else {
1481 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1482 return 1;
1483 }
Ted Kremenek29004672010-02-17 00:41:32 +00001484
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001485 Data.TU = TU;
1486 Data.Filter = ck;
Chandler Carruthb2faa592014-05-02 23:30:59 +00001487 Data.CommentSchemaFile = CommentSchemaFile;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001488 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001489 }
Ted Kremenek29004672010-02-17 00:41:32 +00001490
Ted Kremenekb478ff42010-01-26 17:59:48 +00001491 if (PV)
1492 PV(TU);
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001493
Douglas Gregor33cdd812010-02-18 18:08:43 +00001494 PrintDiagnostics(TU);
Argyrios Kyrtzidis70480492011-11-13 23:39:14 +00001495 if (checkForErrors(TU) != 0) {
1496 clang_disposeTranslationUnit(TU);
1497 return -1;
1498 }
1499
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001500 clang_disposeTranslationUnit(TU);
1501 return 0;
1502}
1503
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001504int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001505 const char *prefix, CXCursorVisitor Visitor,
1506 PostVisitTU PV) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001507 CXIndex Idx;
1508 CXTranslationUnit TU;
Ted Kremenek50228be2010-02-11 07:41:25 +00001509 int result;
Ted Kremenek29004672010-02-17 00:41:32 +00001510 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001511 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001512 /* displayDiagnostics=*/1);
Ted Kremenek29004672010-02-17 00:41:32 +00001513
Ted Kremenek50228be2010-02-11 07:41:25 +00001514 if (!CreateTranslationUnit(Idx, file, &TU)) {
1515 clang_disposeIndex(Idx);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001516 return 1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001517 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001518
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001519 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek50228be2010-02-11 07:41:25 +00001520 clang_disposeIndex(Idx);
1521 return result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001522}
1523
Ted Kremenekb478ff42010-01-26 17:59:48 +00001524int perform_test_load_source(int argc, const char **argv,
1525 const char *filter, CXCursorVisitor Visitor,
1526 PostVisitTU PV) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001527 CXIndex Idx;
1528 CXTranslationUnit TU;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001529 const char *CommentSchemaFile;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001530 struct CXUnsavedFile *unsaved_files = 0;
1531 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001532 enum CXErrorCode Err;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001533 int result;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001534
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001535 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor97c75712010-10-02 22:49:11 +00001536 (!strcmp(filter, "local") ||
1537 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001538 /* displayDiagnostics=*/1);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001539
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001540 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1541 argc--;
1542 argv++;
1543 }
1544
Ted Kremenek50228be2010-02-11 07:41:25 +00001545 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1546 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001547 return -1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001548 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001549
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001550 Err = clang_parseTranslationUnit2(Idx, 0,
1551 argv + num_unsaved_files,
1552 argc - num_unsaved_files,
1553 unsaved_files, num_unsaved_files,
1554 getDefaultParsingOptions(), &TU);
1555 if (Err != CXError_Success) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001556 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001557 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001558 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001559 clang_disposeIndex(Idx);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001560 return 1;
1561 }
1562
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001563 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1564 CommentSchemaFile);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001565 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001566 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001567 return result;
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001568}
1569
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001570int perform_test_reparse_source(int argc, const char **argv, int trials,
1571 const char *filter, CXCursorVisitor Visitor,
1572 PostVisitTU PV) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001573 CXIndex Idx;
1574 CXTranslationUnit TU;
1575 struct CXUnsavedFile *unsaved_files = 0;
1576 int num_unsaved_files = 0;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001577 int compiler_arg_idx = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001578 enum CXErrorCode Err;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001579 int result, i;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001580 int trial;
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001581 int remap_after_trial = 0;
1582 char *endptr = 0;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001583
1584 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1585 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001586 /* displayDiagnostics=*/1);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001587
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001588 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1589 clang_disposeIndex(Idx);
1590 return -1;
1591 }
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001592
1593 for (i = 0; i < argc; ++i) {
1594 if (strcmp(argv[i], "--") == 0)
1595 break;
1596 }
1597 if (i < argc)
1598 compiler_arg_idx = i+1;
1599 if (num_unsaved_files > compiler_arg_idx)
1600 compiler_arg_idx = num_unsaved_files;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001601
Daniel Dunbarec29d712010-08-18 23:09:16 +00001602 /* Load the initial translation unit -- we do this without honoring remapped
1603 * files, so that we have a way to test results after changing the source. */
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001604 Err = clang_parseTranslationUnit2(Idx, 0,
1605 argv + compiler_arg_idx,
1606 argc - compiler_arg_idx,
1607 0, 0, getDefaultParsingOptions(), &TU);
1608 if (Err != CXError_Success) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001609 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001610 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001611 free_remapped_files(unsaved_files, num_unsaved_files);
1612 clang_disposeIndex(Idx);
1613 return 1;
1614 }
1615
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001616 if (checkForErrors(TU) != 0)
1617 return -1;
1618
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001619 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1620 remap_after_trial =
1621 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1622 }
1623
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001624 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001625 free_remapped_files(unsaved_files, num_unsaved_files);
1626 if (parse_remapped_files_with_try(trial, argc, argv, 0,
1627 &unsaved_files, &num_unsaved_files)) {
1628 clang_disposeTranslationUnit(TU);
1629 clang_disposeIndex(Idx);
1630 return -1;
1631 }
1632
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001633 Err = clang_reparseTranslationUnit(
1634 TU,
1635 trial >= remap_after_trial ? num_unsaved_files : 0,
1636 trial >= remap_after_trial ? unsaved_files : 0,
1637 clang_defaultReparseOptions(TU));
1638 if (Err != CXError_Success) {
Daniel Dunbarec29d712010-08-18 23:09:16 +00001639 fprintf(stderr, "Unable to reparse translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001640 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001641 clang_disposeTranslationUnit(TU);
1642 free_remapped_files(unsaved_files, num_unsaved_files);
1643 clang_disposeIndex(Idx);
1644 return -1;
1645 }
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001646
1647 if (checkForErrors(TU) != 0)
1648 return -1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001649 }
1650
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001651 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001652
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001653 free_remapped_files(unsaved_files, num_unsaved_files);
1654 clang_disposeIndex(Idx);
1655 return result;
1656}
1657
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001658/******************************************************************************/
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001659/* Logic for testing clang_getCursor(). */
1660/******************************************************************************/
1661
Douglas Gregor37aa4932011-05-04 00:14:37 +00001662static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001663 unsigned start_line, unsigned start_col,
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001664 unsigned end_line, unsigned end_col,
1665 const char *prefix) {
Ted Kremenekb58514e2010-01-07 01:17:12 +00001666 printf("// %s: ", FileCheckPrefix);
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001667 if (prefix)
1668 printf("-%s", prefix);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00001669 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1670 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001671 PrintCursor(cursor, NULL);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001672 printf("\n");
1673}
1674
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001675static int perform_file_scan(const char *ast_file, const char *source_file,
1676 const char *prefix) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001677 CXIndex Idx;
1678 CXTranslationUnit TU;
1679 FILE *fp;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001680 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregor816fd362010-01-22 21:44:22 +00001681 CXFile file;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001682 unsigned line = 1, col = 1;
Daniel Dunbar6092d502010-02-14 08:32:51 +00001683 unsigned start_line = 1, start_col = 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001684
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001685 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001686 /* displayDiagnostics=*/1))) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001687 fprintf(stderr, "Could not create Index\n");
1688 return 1;
1689 }
Ted Kremenek29004672010-02-17 00:41:32 +00001690
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001691 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1692 return 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001693
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001694 if ((fp = fopen(source_file, "r")) == NULL) {
1695 fprintf(stderr, "Could not open '%s'\n", source_file);
Sylvestre Ledrub2482562014-08-18 15:18:56 +00001696 clang_disposeTranslationUnit(TU);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001697 return 1;
1698 }
Ted Kremenek29004672010-02-17 00:41:32 +00001699
Douglas Gregor816fd362010-01-22 21:44:22 +00001700 file = clang_getFile(TU, source_file);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001701 for (;;) {
1702 CXCursor cursor;
1703 int c = fgetc(fp);
Benjamin Kramer10d083172009-11-17 20:51:40 +00001704
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001705 if (c == '\n') {
1706 ++line;
1707 col = 1;
1708 } else
1709 ++col;
1710
1711 /* Check the cursor at this position, and dump the previous one if we have
1712 * found something new.
1713 */
1714 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1715 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1716 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregor37aa4932011-05-04 00:14:37 +00001717 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbar02968e52010-02-14 10:02:57 +00001718 line, col, prefix);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001719 start_line = line;
1720 start_col = col;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001721 }
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001722 if (c == EOF)
1723 break;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001724
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001725 prevCursor = cursor;
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001726 }
Ted Kremenek29004672010-02-17 00:41:32 +00001727
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001728 fclose(fp);
Douglas Gregor7a964ad2011-01-31 22:04:05 +00001729 clang_disposeTranslationUnit(TU);
1730 clang_disposeIndex(Idx);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001731 return 0;
1732}
1733
1734/******************************************************************************/
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001735/* Logic for testing clang code completion. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001736/******************************************************************************/
1737
Douglas Gregor9eb77012009-11-07 00:00:49 +00001738/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1739 on failure. If successful, the pointer *filename will contain newly-allocated
1740 memory (that will be owned by the caller) to store the file name. */
Ted Kremenek29004672010-02-17 00:41:32 +00001741int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001742 unsigned *column, unsigned *second_line,
1743 unsigned *second_column) {
Douglas Gregorf96ea292009-11-09 18:19:57 +00001744 /* Find the second colon. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001745 const char *last_colon = strrchr(input, ':');
1746 unsigned values[4], i;
1747 unsigned num_values = (second_line && second_column)? 4 : 2;
1748
Douglas Gregor9eb77012009-11-07 00:00:49 +00001749 char *endptr = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001750 if (!last_colon || last_colon == input) {
1751 if (num_values == 4)
1752 fprintf(stderr, "could not parse filename:line:column:line:column in "
1753 "'%s'\n", input);
1754 else
1755 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001756 return 1;
1757 }
1758
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001759 for (i = 0; i != num_values; ++i) {
1760 const char *prev_colon;
1761
1762 /* Parse the next line or column. */
1763 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1764 if (*endptr != 0 && *endptr != ':') {
Ted Kremenek29004672010-02-17 00:41:32 +00001765 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001766 (i % 2 ? "column" : "line"), input);
1767 return 1;
1768 }
Ted Kremenek29004672010-02-17 00:41:32 +00001769
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001770 if (i + 1 == num_values)
1771 break;
1772
1773 /* Find the previous colon. */
1774 prev_colon = last_colon - 1;
1775 while (prev_colon != input && *prev_colon != ':')
1776 --prev_colon;
1777 if (prev_colon == input) {
Ted Kremenek29004672010-02-17 00:41:32 +00001778 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001779 (i % 2 == 0? "column" : "line"), input);
Ted Kremenek29004672010-02-17 00:41:32 +00001780 return 1;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001781 }
1782
1783 last_colon = prev_colon;
Douglas Gregorf96ea292009-11-09 18:19:57 +00001784 }
1785
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001786 *line = values[0];
1787 *column = values[1];
Ted Kremenek29004672010-02-17 00:41:32 +00001788
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001789 if (second_line && second_column) {
1790 *second_line = values[2];
1791 *second_column = values[3];
1792 }
1793
Douglas Gregorf96ea292009-11-09 18:19:57 +00001794 /* Copy the file name. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001795 *filename = (char*)malloc(last_colon - input + 1);
1796 memcpy(*filename, input, last_colon - input);
1797 (*filename)[last_colon - input] = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001798 return 0;
1799}
1800
1801const char *
1802clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1803 switch (Kind) {
1804 case CXCompletionChunk_Optional: return "Optional";
1805 case CXCompletionChunk_TypedText: return "TypedText";
1806 case CXCompletionChunk_Text: return "Text";
1807 case CXCompletionChunk_Placeholder: return "Placeholder";
1808 case CXCompletionChunk_Informative: return "Informative";
1809 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1810 case CXCompletionChunk_LeftParen: return "LeftParen";
1811 case CXCompletionChunk_RightParen: return "RightParen";
1812 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1813 case CXCompletionChunk_RightBracket: return "RightBracket";
1814 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1815 case CXCompletionChunk_RightBrace: return "RightBrace";
1816 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1817 case CXCompletionChunk_RightAngle: return "RightAngle";
1818 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001819 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001820 case CXCompletionChunk_Colon: return "Colon";
1821 case CXCompletionChunk_SemiColon: return "SemiColon";
1822 case CXCompletionChunk_Equal: return "Equal";
1823 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1824 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor9eb77012009-11-07 00:00:49 +00001825 }
Ted Kremenek29004672010-02-17 00:41:32 +00001826
Douglas Gregor9eb77012009-11-07 00:00:49 +00001827 return "Unknown";
1828}
1829
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00001830static int checkForErrors(CXTranslationUnit TU) {
1831 unsigned Num, i;
1832 CXDiagnostic Diag;
1833 CXString DiagStr;
1834
1835 if (!getenv("CINDEXTEST_FAILONERROR"))
1836 return 0;
1837
1838 Num = clang_getNumDiagnostics(TU);
1839 for (i = 0; i != Num; ++i) {
1840 Diag = clang_getDiagnostic(TU, i);
1841 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1842 DiagStr = clang_formatDiagnostic(Diag,
1843 clang_defaultDiagnosticDisplayOptions());
1844 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1845 clang_disposeString(DiagStr);
1846 clang_disposeDiagnostic(Diag);
1847 return -1;
1848 }
1849 clang_disposeDiagnostic(Diag);
1850 }
1851
1852 return 0;
1853}
1854
Nico Weber8d19dff2014-05-07 21:05:22 +00001855static void print_completion_string(CXCompletionString completion_string,
1856 FILE *file) {
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001857 int I, N;
Ted Kremenek29004672010-02-17 00:41:32 +00001858
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001859 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001860 for (I = 0; I != N; ++I) {
Ted Kremenekf602f962010-02-17 01:42:24 +00001861 CXString text;
1862 const char *cstr;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001863 enum CXCompletionChunkKind Kind
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001864 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremenek29004672010-02-17 00:41:32 +00001865
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001866 if (Kind == CXCompletionChunk_Optional) {
1867 fprintf(file, "{Optional ");
1868 print_completion_string(
Ted Kremenek29004672010-02-17 00:41:32 +00001869 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001870 file);
1871 fprintf(file, "}");
1872 continue;
Douglas Gregor8ed5b772010-10-08 20:39:29 +00001873 }
1874
1875 if (Kind == CXCompletionChunk_VerticalSpace) {
1876 fprintf(file, "{VerticalSpace }");
1877 continue;
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001878 }
Ted Kremenek29004672010-02-17 00:41:32 +00001879
Douglas Gregorf81f5282009-11-09 17:05:28 +00001880 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenekf602f962010-02-17 01:42:24 +00001881 cstr = clang_getCString(text);
Ted Kremenek29004672010-02-17 00:41:32 +00001882 fprintf(file, "{%s %s}",
Douglas Gregor9eb77012009-11-07 00:00:49 +00001883 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenekf602f962010-02-17 01:42:24 +00001884 cstr ? cstr : "");
1885 clang_disposeString(text);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001886 }
Ted Kremenekf602f962010-02-17 01:42:24 +00001887
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001888}
1889
Nico Weber8d19dff2014-05-07 21:05:22 +00001890static void print_completion_result(CXCompletionResult *completion_result,
Nico Weberdf686022014-05-07 21:09:42 +00001891 FILE *file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001892 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001893 unsigned annotationCount;
Douglas Gregor78254c82012-03-27 23:34:16 +00001894 enum CXCursorKind ParentKind;
1895 CXString ParentName;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001896 CXString BriefComment;
1897 const char *BriefCommentCString;
Douglas Gregor78254c82012-03-27 23:34:16 +00001898
Ted Kremenek29004672010-02-17 00:41:32 +00001899 fprintf(file, "%s:", clang_getCString(ks));
1900 clang_disposeString(ks);
1901
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001902 print_completion_string(completion_result->CompletionString, file);
Douglas Gregorf757a122010-08-23 23:00:57 +00001903 fprintf(file, " (%u)",
Douglas Gregora2db7932010-05-26 22:00:08 +00001904 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregorf757a122010-08-23 23:00:57 +00001905 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1906 case CXAvailability_Available:
1907 break;
1908
1909 case CXAvailability_Deprecated:
1910 fprintf(file, " (deprecated)");
1911 break;
1912
1913 case CXAvailability_NotAvailable:
1914 fprintf(file, " (unavailable)");
1915 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001916
1917 case CXAvailability_NotAccessible:
1918 fprintf(file, " (inaccessible)");
1919 break;
Douglas Gregorf757a122010-08-23 23:00:57 +00001920 }
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001921
1922 annotationCount = clang_getCompletionNumAnnotations(
1923 completion_result->CompletionString);
1924 if (annotationCount) {
1925 unsigned i;
1926 fprintf(file, " (");
1927 for (i = 0; i < annotationCount; ++i) {
1928 if (i != 0)
1929 fprintf(file, ", ");
1930 fprintf(file, "\"%s\"",
1931 clang_getCString(clang_getCompletionAnnotation(
1932 completion_result->CompletionString, i)));
1933 }
1934 fprintf(file, ")");
1935 }
1936
Douglas Gregor78254c82012-03-27 23:34:16 +00001937 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1938 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1939 &ParentKind);
1940 if (ParentKind != CXCursor_NotImplemented) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001941 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregor78254c82012-03-27 23:34:16 +00001942 fprintf(file, " (parent: %s '%s')",
1943 clang_getCString(KindSpelling),
1944 clang_getCString(ParentName));
1945 clang_disposeString(KindSpelling);
1946 }
1947 clang_disposeString(ParentName);
1948 }
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001949
1950 BriefComment = clang_getCompletionBriefComment(
1951 completion_result->CompletionString);
1952 BriefCommentCString = clang_getCString(BriefComment);
1953 if (BriefCommentCString && *BriefCommentCString != '\0') {
1954 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1955 }
1956 clang_disposeString(BriefComment);
Douglas Gregor78254c82012-03-27 23:34:16 +00001957
Douglas Gregorf757a122010-08-23 23:00:57 +00001958 fprintf(file, "\n");
Douglas Gregor9eb77012009-11-07 00:00:49 +00001959}
1960
Douglas Gregor21325842011-07-07 16:03:39 +00001961void print_completion_contexts(unsigned long long contexts, FILE *file) {
1962 fprintf(file, "Completion contexts:\n");
1963 if (contexts == CXCompletionContext_Unknown) {
1964 fprintf(file, "Unknown\n");
1965 }
1966 if (contexts & CXCompletionContext_AnyType) {
1967 fprintf(file, "Any type\n");
1968 }
1969 if (contexts & CXCompletionContext_AnyValue) {
1970 fprintf(file, "Any value\n");
1971 }
1972 if (contexts & CXCompletionContext_ObjCObjectValue) {
1973 fprintf(file, "Objective-C object value\n");
1974 }
1975 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1976 fprintf(file, "Objective-C selector value\n");
1977 }
1978 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1979 fprintf(file, "C++ class type value\n");
1980 }
1981 if (contexts & CXCompletionContext_DotMemberAccess) {
1982 fprintf(file, "Dot member access\n");
1983 }
1984 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1985 fprintf(file, "Arrow member access\n");
1986 }
1987 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1988 fprintf(file, "Objective-C property access\n");
1989 }
1990 if (contexts & CXCompletionContext_EnumTag) {
1991 fprintf(file, "Enum tag\n");
1992 }
1993 if (contexts & CXCompletionContext_UnionTag) {
1994 fprintf(file, "Union tag\n");
1995 }
1996 if (contexts & CXCompletionContext_StructTag) {
1997 fprintf(file, "Struct tag\n");
1998 }
1999 if (contexts & CXCompletionContext_ClassTag) {
2000 fprintf(file, "Class name\n");
2001 }
2002 if (contexts & CXCompletionContext_Namespace) {
2003 fprintf(file, "Namespace or namespace alias\n");
2004 }
2005 if (contexts & CXCompletionContext_NestedNameSpecifier) {
2006 fprintf(file, "Nested name specifier\n");
2007 }
2008 if (contexts & CXCompletionContext_ObjCInterface) {
2009 fprintf(file, "Objective-C interface\n");
2010 }
2011 if (contexts & CXCompletionContext_ObjCProtocol) {
2012 fprintf(file, "Objective-C protocol\n");
2013 }
2014 if (contexts & CXCompletionContext_ObjCCategory) {
2015 fprintf(file, "Objective-C category\n");
2016 }
2017 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
2018 fprintf(file, "Objective-C instance method\n");
2019 }
2020 if (contexts & CXCompletionContext_ObjCClassMessage) {
2021 fprintf(file, "Objective-C class method\n");
2022 }
2023 if (contexts & CXCompletionContext_ObjCSelectorName) {
2024 fprintf(file, "Objective-C selector name\n");
2025 }
2026 if (contexts & CXCompletionContext_MacroName) {
2027 fprintf(file, "Macro name\n");
2028 }
2029 if (contexts & CXCompletionContext_NaturalLanguage) {
2030 fprintf(file, "Natural language\n");
2031 }
2032}
2033
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002034int my_stricmp(const char *s1, const char *s2) {
2035 while (*s1 && *s2) {
NAKAMURA Takumid3cc2202011-03-09 03:02:28 +00002036 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002037 if (c1 < c2)
2038 return -1;
2039 else if (c1 > c2)
2040 return 1;
2041
2042 ++s1;
2043 ++s2;
2044 }
2045
2046 if (*s1)
2047 return 1;
2048 else if (*s2)
2049 return -1;
2050 return 0;
2051}
2052
Douglas Gregor47815d52010-07-12 18:38:41 +00002053int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00002054 const char *input = argv[1];
2055 char *filename = 0;
2056 unsigned line;
2057 unsigned column;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00002058 CXIndex CIdx;
Ted Kremenekef3339b2009-11-17 18:09:14 +00002059 int errorCode;
Douglas Gregor9485bf92009-12-02 09:21:34 +00002060 struct CXUnsavedFile *unsaved_files = 0;
2061 int num_unsaved_files = 0;
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002062 CXCodeCompleteResults *results = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002063 enum CXErrorCode Err;
2064 CXTranslationUnit TU;
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002065 unsigned I, Repeats = 1;
2066 unsigned completionOptions = clang_defaultCodeCompleteOptions();
2067
2068 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
2069 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002070 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
2071 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregor028d3e42010-08-09 20:45:32 +00002072
Douglas Gregor47815d52010-07-12 18:38:41 +00002073 if (timing_only)
2074 input += strlen("-code-completion-timing=");
2075 else
2076 input += strlen("-code-completion-at=");
2077
Ted Kremenek29004672010-02-17 00:41:32 +00002078 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002079 0, 0)))
Ted Kremenekef3339b2009-11-17 18:09:14 +00002080 return errorCode;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002081
Douglas Gregor9485bf92009-12-02 09:21:34 +00002082 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2083 return -1;
2084
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002085 CIdx = clang_createIndex(0, 0);
2086
2087 if (getenv("CINDEXTEST_EDITING"))
2088 Repeats = 5;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002089
2090 Err = clang_parseTranslationUnit2(CIdx, 0,
2091 argv + num_unsaved_files + 2,
2092 argc - num_unsaved_files - 2,
2093 0, 0, getDefaultParsingOptions(), &TU);
2094 if (Err != CXError_Success) {
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002095 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002096 describeLibclangFailure(Err);
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002097 return 1;
2098 }
Douglas Gregorc6592922010-11-15 23:00:34 +00002099
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002100 Err = clang_reparseTranslationUnit(TU, 0, 0,
2101 clang_defaultReparseOptions(TU));
2102
2103 if (Err != CXError_Success) {
Douglas Gregorc6592922010-11-15 23:00:34 +00002104 fprintf(stderr, "Unable to reparse translation init!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002105 describeLibclangFailure(Err);
2106 clang_disposeTranslationUnit(TU);
Douglas Gregorc6592922010-11-15 23:00:34 +00002107 return 1;
2108 }
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002109
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002110 for (I = 0; I != Repeats; ++I) {
2111 results = clang_codeCompleteAt(TU, filename, line, column,
2112 unsaved_files, num_unsaved_files,
2113 completionOptions);
2114 if (!results) {
2115 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar186f7422010-08-19 23:44:06 +00002116 return 1;
2117 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002118 if (I != Repeats-1)
2119 clang_disposeCodeCompleteResults(results);
2120 }
Douglas Gregorba965fb2010-01-28 00:56:43 +00002121
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002122 if (results) {
Douglas Gregor63745d52011-07-21 01:05:26 +00002123 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor21325842011-07-07 16:03:39 +00002124 unsigned long long contexts;
Douglas Gregor63745d52011-07-21 01:05:26 +00002125 enum CXCursorKind containerKind;
Douglas Gregorea777402011-07-26 15:24:30 +00002126 CXString objCSelector;
2127 const char *selectorString;
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002128 if (!timing_only) {
2129 /* Sort the code-completion results based on the typed text. */
2130 clang_sortCodeCompletionResults(results->Results, results->NumResults);
2131
Douglas Gregor47815d52010-07-12 18:38:41 +00002132 for (i = 0; i != n; ++i)
2133 print_completion_result(results->Results + i, stdout);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002134 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00002135 n = clang_codeCompleteGetNumDiagnostics(results);
2136 for (i = 0; i != n; ++i) {
2137 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
2138 PrintDiagnostic(diag);
2139 clang_disposeDiagnostic(diag);
2140 }
Douglas Gregor21325842011-07-07 16:03:39 +00002141
2142 contexts = clang_codeCompleteGetContexts(results);
2143 print_completion_contexts(contexts, stdout);
2144
Douglas Gregorea777402011-07-26 15:24:30 +00002145 containerKind = clang_codeCompleteGetContainerKind(results,
2146 &containerIsIncomplete);
Douglas Gregor63745d52011-07-21 01:05:26 +00002147
2148 if (containerKind != CXCursor_InvalidCode) {
2149 /* We have found a container */
2150 CXString containerUSR, containerKindSpelling;
2151 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
2152 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
2153 clang_disposeString(containerKindSpelling);
2154
2155 if (containerIsIncomplete) {
2156 printf("Container is incomplete\n");
2157 }
2158 else {
2159 printf("Container is complete\n");
2160 }
2161
2162 containerUSR = clang_codeCompleteGetContainerUSR(results);
2163 printf("Container USR: %s\n", clang_getCString(containerUSR));
2164 clang_disposeString(containerUSR);
2165 }
2166
Douglas Gregorea777402011-07-26 15:24:30 +00002167 objCSelector = clang_codeCompleteGetObjCSelector(results);
2168 selectorString = clang_getCString(objCSelector);
2169 if (selectorString && strlen(selectorString) > 0) {
2170 printf("Objective-C selector: %s\n", selectorString);
2171 }
2172 clang_disposeString(objCSelector);
2173
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002174 clang_disposeCodeCompleteResults(results);
2175 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00002176 clang_disposeTranslationUnit(TU);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002177 clang_disposeIndex(CIdx);
2178 free(filename);
Ted Kremenek29004672010-02-17 00:41:32 +00002179
Douglas Gregor9485bf92009-12-02 09:21:34 +00002180 free_remapped_files(unsaved_files, num_unsaved_files);
2181
Ted Kremenekef3339b2009-11-17 18:09:14 +00002182 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002183}
2184
Douglas Gregor082c3e62010-01-15 19:40:17 +00002185typedef struct {
2186 char *filename;
2187 unsigned line;
2188 unsigned column;
2189} CursorSourceLocation;
2190
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002191static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002192 CXIndex CIdx;
2193 int errorCode;
2194 struct CXUnsavedFile *unsaved_files = 0;
2195 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002196 enum CXErrorCode Err;
Douglas Gregor082c3e62010-01-15 19:40:17 +00002197 CXTranslationUnit TU;
2198 CXCursor Cursor;
2199 CursorSourceLocation *Locations = 0;
2200 unsigned NumLocations = 0, Loc;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002201 unsigned Repeats = 1;
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002202 unsigned I;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002203
Ted Kremenek29004672010-02-17 00:41:32 +00002204 /* Count the number of locations. */
Douglas Gregor082c3e62010-01-15 19:40:17 +00002205 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2206 ++NumLocations;
Ted Kremenek29004672010-02-17 00:41:32 +00002207
Douglas Gregor082c3e62010-01-15 19:40:17 +00002208 /* Parse the locations. */
2209 assert(NumLocations > 0 && "Unable to count locations?");
2210 Locations = (CursorSourceLocation *)malloc(
2211 NumLocations * sizeof(CursorSourceLocation));
2212 for (Loc = 0; Loc < NumLocations; ++Loc) {
2213 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremenek29004672010-02-17 00:41:32 +00002214 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2215 &Locations[Loc].line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002216 &Locations[Loc].column, 0, 0)))
Douglas Gregor082c3e62010-01-15 19:40:17 +00002217 return errorCode;
2218 }
Ted Kremenek29004672010-02-17 00:41:32 +00002219
2220 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002221 &num_unsaved_files))
2222 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00002223
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002224 if (getenv("CINDEXTEST_EDITING"))
2225 Repeats = 5;
2226
2227 /* Parse the translation unit. When we're testing clang_getCursor() after
2228 reparsing, don't remap unsaved files until the second parse. */
2229 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002230 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2231 argv + num_unsaved_files + 1 + NumLocations,
2232 argc - num_unsaved_files - 2 - NumLocations,
2233 unsaved_files,
2234 Repeats > 1? 0 : num_unsaved_files,
2235 getDefaultParsingOptions(), &TU);
2236 if (Err != CXError_Success) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002237 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002238 describeLibclangFailure(Err);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002239 return -1;
2240 }
Ted Kremenek29004672010-02-17 00:41:32 +00002241
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002242 if (checkForErrors(TU) != 0)
2243 return -1;
2244
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002245 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002246 if (Repeats > 1) {
2247 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2248 clang_defaultReparseOptions(TU));
2249 if (Err != CXError_Success) {
2250 describeLibclangFailure(Err);
2251 clang_disposeTranslationUnit(TU);
2252 return 1;
2253 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002254 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002255
2256 if (checkForErrors(TU) != 0)
2257 return -1;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002258
2259 for (Loc = 0; Loc < NumLocations; ++Loc) {
2260 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2261 if (!file)
2262 continue;
Ted Kremenek29004672010-02-17 00:41:32 +00002263
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002264 Cursor = clang_getCursor(TU,
2265 clang_getLocation(TU, file, Locations[Loc].line,
2266 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002267
2268 if (checkForErrors(TU) != 0)
2269 return -1;
2270
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002271 if (I + 1 == Repeats) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002272 CXCompletionString completionString = clang_getCursorCompletionString(
2273 Cursor);
2274 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002275 CXString Spelling;
2276 const char *cspell;
2277 unsigned line, column;
2278 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2279 printf("%d:%d ", line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002280 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002281 PrintCursorExtent(Cursor);
2282 Spelling = clang_getCursorSpelling(Cursor);
2283 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002284 if (cspell && strlen(cspell) != 0) {
2285 unsigned pieceIndex;
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002286 printf(" Spelling=%s (", cspell);
2287 for (pieceIndex = 0; ; ++pieceIndex) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002288 CXSourceRange range =
2289 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002290 if (clang_Range_isNull(range))
2291 break;
2292 PrintRange(range, 0);
2293 }
2294 printf(")");
2295 }
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002296 clang_disposeString(Spelling);
Argyrios Kyrtzidis210f29f2012-03-30 22:15:48 +00002297 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
Nico Weber8d19dff2014-05-07 21:05:22 +00002298 printf(" Selector index=%d",
2299 clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002300 if (clang_Cursor_isDynamicCall(Cursor))
2301 printf(" Dynamic-call");
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002302 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002303 CXType T = clang_Cursor_getReceiverType(Cursor);
2304 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002305 printf(" Receiver-type=%s", clang_getCString(S));
2306 clang_disposeString(S);
2307 }
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002308
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002309 {
2310 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002311 CXFile astFile;
2312 CXString name, astFilename;
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002313 unsigned i, numHeaders;
2314 if (mod) {
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002315 astFile = clang_Module_getASTFile(mod);
2316 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002317 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002318 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidis884337f2014-05-15 04:44:25 +00002319 printf(" ModuleName=%s (%s) system=%d Headers(%d):",
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002320 clang_getCString(name), clang_getCString(astFilename),
Argyrios Kyrtzidis884337f2014-05-15 04:44:25 +00002321 clang_Module_isSystem(mod), numHeaders);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002322 clang_disposeString(name);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002323 clang_disposeString(astFilename);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002324 for (i = 0; i < numHeaders; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002325 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2326 CXString filename = clang_getFileName(file);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002327 printf("\n%s", clang_getCString(filename));
2328 clang_disposeString(filename);
2329 }
2330 }
2331 }
2332
Douglas Gregor3f35bb22011-08-04 20:04:59 +00002333 if (completionString != NULL) {
2334 printf("\nCompletion string: ");
2335 print_completion_string(completionString, stdout);
2336 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002337 printf("\n");
2338 free(Locations[Loc].filename);
2339 }
2340 }
Douglas Gregor082c3e62010-01-15 19:40:17 +00002341 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002342
Douglas Gregor33cdd812010-02-18 18:08:43 +00002343 PrintDiagnostics(TU);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002344 clang_disposeTranslationUnit(TU);
2345 clang_disposeIndex(CIdx);
2346 free(Locations);
2347 free_remapped_files(unsaved_files, num_unsaved_files);
2348 return 0;
2349}
2350
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002351static enum CXVisitorResult findFileRefsVisit(void *context,
2352 CXCursor cursor, CXSourceRange range) {
2353 if (clang_Range_isNull(range))
2354 return CXVisit_Continue;
2355
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002356 PrintCursor(cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002357 PrintRange(range, "");
2358 printf("\n");
2359 return CXVisit_Continue;
2360}
2361
2362static int find_file_refs_at(int argc, const char **argv) {
2363 CXIndex CIdx;
2364 int errorCode;
2365 struct CXUnsavedFile *unsaved_files = 0;
2366 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002367 enum CXErrorCode Err;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002368 CXTranslationUnit TU;
2369 CXCursor Cursor;
2370 CursorSourceLocation *Locations = 0;
2371 unsigned NumLocations = 0, Loc;
2372 unsigned Repeats = 1;
2373 unsigned I;
2374
2375 /* Count the number of locations. */
2376 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2377 ++NumLocations;
2378
2379 /* Parse the locations. */
2380 assert(NumLocations > 0 && "Unable to count locations?");
2381 Locations = (CursorSourceLocation *)malloc(
2382 NumLocations * sizeof(CursorSourceLocation));
2383 for (Loc = 0; Loc < NumLocations; ++Loc) {
2384 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2385 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2386 &Locations[Loc].line,
2387 &Locations[Loc].column, 0, 0)))
2388 return errorCode;
2389 }
2390
2391 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2392 &num_unsaved_files))
2393 return -1;
2394
2395 if (getenv("CINDEXTEST_EDITING"))
2396 Repeats = 5;
2397
2398 /* Parse the translation unit. When we're testing clang_getCursor() after
2399 reparsing, don't remap unsaved files until the second parse. */
2400 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002401 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2402 argv + num_unsaved_files + 1 + NumLocations,
2403 argc - num_unsaved_files - 2 - NumLocations,
2404 unsaved_files,
2405 Repeats > 1? 0 : num_unsaved_files,
2406 getDefaultParsingOptions(), &TU);
2407 if (Err != CXError_Success) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002408 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002409 describeLibclangFailure(Err);
2410 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002411 return -1;
2412 }
2413
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002414 if (checkForErrors(TU) != 0)
2415 return -1;
2416
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002417 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002418 if (Repeats > 1) {
2419 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2420 clang_defaultReparseOptions(TU));
2421 if (Err != CXError_Success) {
2422 describeLibclangFailure(Err);
2423 clang_disposeTranslationUnit(TU);
2424 return 1;
2425 }
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002426 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002427
2428 if (checkForErrors(TU) != 0)
2429 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002430
2431 for (Loc = 0; Loc < NumLocations; ++Loc) {
2432 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2433 if (!file)
2434 continue;
2435
2436 Cursor = clang_getCursor(TU,
2437 clang_getLocation(TU, file, Locations[Loc].line,
2438 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002439
2440 if (checkForErrors(TU) != 0)
2441 return -1;
2442
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002443 if (I + 1 == Repeats) {
Erik Verbruggen338b55c2011-10-06 11:38:08 +00002444 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002445 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002446 printf("\n");
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002447 clang_findReferencesInFile(Cursor, file, visitor);
2448 free(Locations[Loc].filename);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002449
2450 if (checkForErrors(TU) != 0)
2451 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002452 }
2453 }
2454 }
2455
2456 PrintDiagnostics(TU);
2457 clang_disposeTranslationUnit(TU);
2458 clang_disposeIndex(CIdx);
2459 free(Locations);
2460 free_remapped_files(unsaved_files, num_unsaved_files);
2461 return 0;
2462}
2463
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002464static enum CXVisitorResult findFileIncludesVisit(void *context,
2465 CXCursor cursor, CXSourceRange range) {
2466 PrintCursor(cursor, NULL);
2467 PrintRange(range, "");
2468 printf("\n");
2469 return CXVisit_Continue;
2470}
2471
2472static int find_file_includes_in(int argc, const char **argv) {
2473 CXIndex CIdx;
2474 struct CXUnsavedFile *unsaved_files = 0;
2475 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002476 enum CXErrorCode Err;
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002477 CXTranslationUnit TU;
2478 const char **Filenames = 0;
2479 unsigned NumFilenames = 0;
2480 unsigned Repeats = 1;
2481 unsigned I, FI;
2482
2483 /* Count the number of locations. */
2484 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2485 ++NumFilenames;
2486
2487 /* Parse the locations. */
2488 assert(NumFilenames > 0 && "Unable to count filenames?");
2489 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2490 for (I = 0; I < NumFilenames; ++I) {
2491 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2492 /* Copy the file name. */
2493 Filenames[I] = input;
2494 }
2495
2496 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2497 &num_unsaved_files))
2498 return -1;
2499
2500 if (getenv("CINDEXTEST_EDITING"))
2501 Repeats = 2;
2502
2503 /* Parse the translation unit. When we're testing clang_getCursor() after
2504 reparsing, don't remap unsaved files until the second parse. */
2505 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002506 Err = clang_parseTranslationUnit2(
2507 CIdx, argv[argc - 1],
2508 argv + num_unsaved_files + 1 + NumFilenames,
2509 argc - num_unsaved_files - 2 - NumFilenames,
2510 unsaved_files,
2511 Repeats > 1 ? 0 : num_unsaved_files, getDefaultParsingOptions(), &TU);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002512
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002513 if (Err != CXError_Success) {
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002514 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002515 describeLibclangFailure(Err);
2516 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002517 return -1;
2518 }
2519
2520 if (checkForErrors(TU) != 0)
2521 return -1;
2522
2523 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002524 if (Repeats > 1) {
2525 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2526 clang_defaultReparseOptions(TU));
2527 if (Err != CXError_Success) {
2528 describeLibclangFailure(Err);
2529 clang_disposeTranslationUnit(TU);
2530 return 1;
2531 }
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002532 }
2533
2534 if (checkForErrors(TU) != 0)
2535 return -1;
2536
2537 for (FI = 0; FI < NumFilenames; ++FI) {
2538 CXFile file = clang_getFile(TU, Filenames[FI]);
2539 if (!file)
2540 continue;
2541
2542 if (checkForErrors(TU) != 0)
2543 return -1;
2544
2545 if (I + 1 == Repeats) {
2546 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2547 clang_findIncludesInFile(TU, file, visitor);
2548
2549 if (checkForErrors(TU) != 0)
2550 return -1;
2551 }
2552 }
2553 }
2554
2555 PrintDiagnostics(TU);
2556 clang_disposeTranslationUnit(TU);
2557 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis1b5b1ce2013-03-11 16:03:17 +00002558 free((void *)Filenames);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002559 free_remapped_files(unsaved_files, num_unsaved_files);
2560 return 0;
2561}
2562
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002563#define MAX_IMPORTED_ASTFILES 200
2564
2565typedef struct {
2566 char **filenames;
2567 unsigned num_files;
2568} ImportedASTFilesData;
2569
2570static ImportedASTFilesData *importedASTs_create() {
2571 ImportedASTFilesData *p;
2572 p = malloc(sizeof(ImportedASTFilesData));
2573 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2574 p->num_files = 0;
2575 return p;
2576}
2577
2578static void importedASTs_dispose(ImportedASTFilesData *p) {
2579 unsigned i;
2580 if (!p)
2581 return;
2582
2583 for (i = 0; i < p->num_files; ++i)
2584 free(p->filenames[i]);
2585 free(p->filenames);
2586 free(p);
2587}
2588
2589static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2590 unsigned i;
2591 assert(p && file);
2592 for (i = 0; i < p->num_files; ++i)
2593 if (strcmp(file, p->filenames[i]) == 0)
2594 return;
2595 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2596 p->filenames[p->num_files++] = strdup(file);
2597}
2598
Nico Weberdf686022014-05-07 21:09:42 +00002599typedef struct IndexDataStringList_ {
2600 struct IndexDataStringList_ *next;
2601 char data[1]; /* Dynamically sized. */
2602} IndexDataStringList;
2603
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002604typedef struct {
2605 const char *check_prefix;
2606 int first_check_printed;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002607 int fail_for_error;
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002608 int abort;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002609 const char *main_filename;
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002610 ImportedASTFilesData *importedASTs;
Nico Weberdf686022014-05-07 21:09:42 +00002611 IndexDataStringList *strings;
Argyrios Kyrtzidisf6d49c32014-05-14 23:14:37 +00002612 CXTranslationUnit TU;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002613} IndexData;
2614
Nico Weberdf686022014-05-07 21:09:42 +00002615static void free_client_data(IndexData *index_data) {
2616 IndexDataStringList *node = index_data->strings;
2617 while (node) {
2618 IndexDataStringList *next = node->next;
2619 free(node);
2620 node = next;
2621 }
2622 index_data->strings = NULL;
2623}
2624
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002625static void printCheck(IndexData *data) {
2626 if (data->check_prefix) {
2627 if (data->first_check_printed) {
2628 printf("// %s-NEXT: ", data->check_prefix);
2629 } else {
2630 printf("// %s : ", data->check_prefix);
2631 data->first_check_printed = 1;
2632 }
2633 }
2634}
2635
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002636static void printCXIndexFile(CXIdxClientFile file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002637 CXString filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002638 printf("%s", clang_getCString(filename));
2639 clang_disposeString(filename);
2640}
2641
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002642static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2643 IndexData *index_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002644 CXString filename;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002645 const char *cname;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002646 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002647 unsigned line, column;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002648 int isMainFile;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002649
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002650 index_data = (IndexData *)client_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002651 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2652 if (line == 0) {
Argyrios Kyrtzidis9f571862012-10-11 19:00:44 +00002653 printf("<invalid>");
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002654 return;
2655 }
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002656 if (!file) {
2657 printf("<no idxfile>");
2658 return;
2659 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002660 filename = clang_getFileName((CXFile)file);
2661 cname = clang_getCString(filename);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002662 if (strcmp(cname, index_data->main_filename) == 0)
2663 isMainFile = 1;
2664 else
2665 isMainFile = 0;
2666 clang_disposeString(filename);
2667
2668 if (!isMainFile) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002669 printCXIndexFile(file);
2670 printf(":");
2671 }
2672 printf("%d:%d", line, column);
2673}
2674
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002675static unsigned digitCount(unsigned val) {
2676 unsigned c = 1;
2677 while (1) {
2678 if (val < 10)
2679 return c;
2680 ++c;
2681 val /= 10;
2682 }
2683}
2684
Nico Weberdf686022014-05-07 21:09:42 +00002685static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
2686 const CXIdxEntityInfo *info,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002687 CXIdxLoc loc) {
Nico Weberdf686022014-05-07 21:09:42 +00002688 IndexData *index_data;
2689 IndexDataStringList *node;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002690 const char *name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002691 char *newStr;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002692 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002693 unsigned line, column;
2694
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002695 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002696 if (!name)
2697 name = "<anon-tag>";
2698
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002699 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Nico Weberdf686022014-05-07 21:09:42 +00002700
2701 node =
2702 (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
2703 digitCount(line) + digitCount(column) + 2);
2704 newStr = node->data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002705 sprintf(newStr, "%s:%d:%d", name, line, column);
Nico Weberdf686022014-05-07 21:09:42 +00002706
2707 /* Remember string so it can be freed later. */
2708 index_data = (IndexData *)client_data;
2709 node->next = index_data->strings;
2710 index_data->strings = node;
2711
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002712 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002713}
2714
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002715static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2716 CXIdxClientContainer container;
2717 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidisdf15c202011-11-16 02:35:05 +00002718 if (!container)
2719 printf("[<<NULL>>]");
2720 else
2721 printf("[%s]", (const char *)container);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002722}
2723
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002724static const char *getEntityKindString(CXIdxEntityKind kind) {
2725 switch (kind) {
2726 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2727 case CXIdxEntity_Typedef: return "typedef";
2728 case CXIdxEntity_Function: return "function";
2729 case CXIdxEntity_Variable: return "variable";
2730 case CXIdxEntity_Field: return "field";
2731 case CXIdxEntity_EnumConstant: return "enumerator";
2732 case CXIdxEntity_ObjCClass: return "objc-class";
2733 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2734 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002735 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2736 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002737 case CXIdxEntity_ObjCProperty: return "objc-property";
2738 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2739 case CXIdxEntity_Enum: return "enum";
2740 case CXIdxEntity_Struct: return "struct";
2741 case CXIdxEntity_Union: return "union";
2742 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002743 case CXIdxEntity_CXXNamespace: return "namespace";
2744 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2745 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2746 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2747 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2748 case CXIdxEntity_CXXConstructor: return "constructor";
2749 case CXIdxEntity_CXXDestructor: return "destructor";
2750 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2751 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikiedcefd952012-08-31 21:55:26 +00002752 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002753 }
2754 assert(0 && "Garbage entity kind");
2755 return 0;
2756}
2757
2758static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2759 switch (kind) {
2760 case CXIdxEntity_NonTemplate: return "";
2761 case CXIdxEntity_Template: return "-template";
2762 case CXIdxEntity_TemplatePartialSpecialization:
2763 return "-template-partial-spec";
2764 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002765 }
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002766 assert(0 && "Garbage entity kind");
2767 return 0;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002768}
2769
Argyrios Kyrtzidis52002882011-12-07 20:44:12 +00002770static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2771 switch (kind) {
2772 case CXIdxEntityLang_None: return "<none>";
2773 case CXIdxEntityLang_C: return "C";
2774 case CXIdxEntityLang_ObjC: return "ObjC";
2775 case CXIdxEntityLang_CXX: return "C++";
2776 }
2777 assert(0 && "Garbage language kind");
2778 return 0;
2779}
2780
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002781static void printEntityInfo(const char *cb,
2782 CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002783 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002784 const char *name;
2785 IndexData *index_data;
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002786 unsigned i;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002787 index_data = (IndexData *)client_data;
2788 printCheck(index_data);
2789
Argyrios Kyrtzidise4acd232011-11-16 02:34:59 +00002790 if (!info) {
2791 printf("%s: <<NULL>>", cb);
2792 return;
2793 }
2794
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002795 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002796 if (!name)
2797 name = "<anon-tag>";
2798
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002799 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2800 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002801 printf(" | name: %s", name);
2802 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002803 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002804
2805 for (i = 0; i != info->numAttributes; ++i) {
2806 const CXIdxAttrInfo *Attr = info->attributes[i];
2807 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002808 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002809 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002810}
2811
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002812static void printBaseClassInfo(CXClientData client_data,
2813 const CXIdxBaseClassInfo *info) {
2814 printEntityInfo(" <base>", client_data, info->base);
2815 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002816 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002817 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002818 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002819}
2820
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002821static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2822 CXClientData client_data) {
2823 unsigned i;
2824 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2825 printEntityInfo(" <protocol>", client_data,
2826 ProtoInfo->protocols[i]->protocol);
2827 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002828 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002829 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002830 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002831 printf("\n");
2832 }
2833}
2834
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002835static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002836 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002837 CXString str;
2838 const char *cstr;
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002839 unsigned numDiags, i;
2840 CXDiagnostic diag;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002841 IndexData *index_data;
2842 index_data = (IndexData *)client_data;
2843 printCheck(index_data);
2844
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002845 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2846 for (i = 0; i != numDiags; ++i) {
2847 diag = clang_getDiagnosticInSet(diagSet, i);
2848 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2849 cstr = clang_getCString(str);
2850 printf("[diagnostic]: %s\n", cstr);
2851 clang_disposeString(str);
2852
2853 if (getenv("CINDEXTEST_FAILONERROR") &&
2854 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2855 index_data->fail_for_error = 1;
2856 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002857 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002858}
2859
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002860static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2861 CXFile file, void *reserved) {
2862 IndexData *index_data;
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002863 CXString filename;
2864
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002865 index_data = (IndexData *)client_data;
2866 printCheck(index_data);
2867
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002868 filename = clang_getFileName(file);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002869 index_data->main_filename = clang_getCString(filename);
2870 clang_disposeString(filename);
2871
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002872 printf("[enteredMainFile]: ");
2873 printCXIndexFile((CXIdxClientFile)file);
2874 printf("\n");
2875
2876 return (CXIdxClientFile)file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002877}
2878
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002879static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002880 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002881 IndexData *index_data;
Argyrios Kyrtzidisf6d49c32014-05-14 23:14:37 +00002882 CXModule Mod;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002883 index_data = (IndexData *)client_data;
2884 printCheck(index_data);
2885
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002886 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002887 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002888 printf(" | name: \"%s\"", info->filename);
2889 printf(" | hash loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002890 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidisf6d49c32014-05-14 23:14:37 +00002891 printf(" | isImport: %d | isAngled: %d | isModule: %d",
Argyrios Kyrtzidis5e2ec482012-10-18 00:17:05 +00002892 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidisf6d49c32014-05-14 23:14:37 +00002893
2894 Mod = clang_getModuleForFile(index_data->TU, (CXFile)info->file);
2895 if (Mod) {
2896 CXString str = clang_Module_getFullName(Mod);
2897 const char *cstr = clang_getCString(str);
2898 printf(" | module: %s", cstr);
2899 clang_disposeString(str);
2900 }
2901
2902 printf("\n");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002903
2904 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002905}
2906
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002907static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2908 const CXIdxImportedASTFileInfo *info) {
2909 IndexData *index_data;
2910 index_data = (IndexData *)client_data;
2911 printCheck(index_data);
2912
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002913 if (index_data->importedASTs) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002914 CXString filename = clang_getFileName(info->file);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002915 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2916 clang_disposeString(filename);
2917 }
2918
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002919 printf("[importedASTFile]: ");
2920 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002921 if (info->module) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002922 CXString name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002923 printf(" | loc: ");
2924 printCXIndexLoc(info->loc, client_data);
2925 printf(" | name: \"%s\"", clang_getCString(name));
2926 printf(" | isImplicit: %d\n", info->isImplicit);
2927 clang_disposeString(name);
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002928 } else {
NAKAMURA Takumie259d912012-10-12 14:25:52 +00002929 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002930 printf("\n");
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002931 }
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002932
2933 return (CXIdxClientFile)info->file;
2934}
2935
Nico Weber8d19dff2014-05-07 21:05:22 +00002936static CXIdxClientContainer
2937index_startedTranslationUnit(CXClientData client_data, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002938 IndexData *index_data;
2939 index_data = (IndexData *)client_data;
2940 printCheck(index_data);
2941
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002942 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002943 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002944}
2945
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002946static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002947 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002948 IndexData *index_data;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002949 const CXIdxObjCCategoryDeclInfo *CatInfo;
2950 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002951 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002952 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002953 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002954 unsigned i;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002955 index_data = (IndexData *)client_data;
2956
2957 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2958 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002959 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002960 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002961 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis663c8ec2011-12-07 20:44:19 +00002962 printf(" | semantic-container: ");
2963 printCXIndexContainer(info->semanticContainer);
2964 printf(" | lexical-container: ");
2965 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002966 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002967 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002968 if (info->flags & CXIdxDeclFlag_Skipped) {
2969 assert(!info->isContainer);
2970 printf(" | isContainer: skipped");
2971 } else {
2972 printf(" | isContainer: %d", info->isContainer);
2973 }
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002974 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002975
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002976 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi2a4859a2011-11-18 00:51:03 +00002977 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002978 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002979 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002980 printf("\n");
2981 }
2982
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002983 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2984 const char *kindName = 0;
2985 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2986 switch (K) {
2987 case CXIdxObjCContainer_ForwardRef:
2988 kindName = "forward-ref"; break;
2989 case CXIdxObjCContainer_Interface:
2990 kindName = "interface"; break;
2991 case CXIdxObjCContainer_Implementation:
2992 kindName = "implementation"; break;
2993 }
2994 printCheck(index_data);
2995 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2996 }
2997
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002998 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002999 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
3000 CatInfo->objcClass);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003001 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003002 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003003 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00003004 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00003005 printf("\n");
3006 }
3007
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00003008 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
3009 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00003010 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00003011 printf("\n");
3012 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003013 }
3014
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00003015 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
3016 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00003017 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003018
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00003019 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
3020 if (PropInfo->getter) {
3021 printEntityInfo(" <getter>", client_data, PropInfo->getter);
3022 printf("\n");
3023 }
3024 if (PropInfo->setter) {
3025 printEntityInfo(" <setter>", client_data, PropInfo->setter);
3026 printf("\n");
3027 }
3028 }
3029
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00003030 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
3031 for (i = 0; i != CXXClassInfo->numBases; ++i) {
3032 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
3033 printf("\n");
3034 }
3035 }
3036
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003037 if (info->declAsContainer)
Nico Weber8d19dff2014-05-07 21:05:22 +00003038 clang_index_setClientContainer(
3039 info->declAsContainer,
Nico Weberdf686022014-05-07 21:09:42 +00003040 makeClientContainer(client_data, info->entityInfo, info->loc));
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003041}
3042
3043static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00003044 const CXIdxEntityRefInfo *info) {
Nico Weber8d19dff2014-05-07 21:05:22 +00003045 printEntityInfo("[indexEntityReference]", client_data,
3046 info->referencedEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003047 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003048 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003049 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00003050 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00003051 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003052 printf(" | container: ");
3053 printCXIndexContainer(info->container);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00003054 printf(" | refkind: ");
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00003055 switch (info->kind) {
3056 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00003057 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00003058 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003059 printf("\n");
3060}
3061
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00003062static int index_abortQuery(CXClientData client_data, void *reserved) {
3063 IndexData *index_data;
3064 index_data = (IndexData *)client_data;
3065 return index_data->abort;
3066}
3067
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003068static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00003069 index_abortQuery,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003070 index_diagnostic,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00003071 index_enteredMainFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003072 index_ppIncludedFile,
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00003073 index_importedASTFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003074 index_startedTranslationUnit,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00003075 index_indexDeclaration,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003076 index_indexEntityReference
3077};
3078
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00003079static unsigned getIndexOptions(void) {
3080 unsigned index_opts;
3081 index_opts = 0;
3082 if (getenv("CINDEXTEST_SUPPRESSREFS"))
3083 index_opts |= CXIndexOpt_SuppressRedundantRefs;
3084 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
3085 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00003086 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
3087 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00003088
3089 return index_opts;
3090}
3091
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003092static int index_compile_args(int num_args, const char **args,
3093 CXIndexAction idxAction,
3094 ImportedASTFilesData *importedASTs,
3095 const char *check_prefix) {
3096 IndexData index_data;
3097 unsigned index_opts;
3098 int result;
3099
3100 if (num_args == 0) {
3101 fprintf(stderr, "no compiler arguments\n");
3102 return -1;
3103 }
3104
3105 index_data.check_prefix = check_prefix;
3106 index_data.first_check_printed = 0;
3107 index_data.fail_for_error = 0;
3108 index_data.abort = 0;
3109 index_data.main_filename = "";
3110 index_data.importedASTs = importedASTs;
Nico Weberdf686022014-05-07 21:09:42 +00003111 index_data.strings = NULL;
Argyrios Kyrtzidisf6d49c32014-05-14 23:14:37 +00003112 index_data.TU = NULL;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003113
3114 index_opts = getIndexOptions();
3115 result = clang_indexSourceFile(idxAction, &index_data,
3116 &IndexCB,sizeof(IndexCB), index_opts,
3117 0, args, num_args, 0, 0, 0,
3118 getDefaultParsingOptions());
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003119 if (result != CXError_Success)
3120 describeLibclangFailure(result);
3121
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003122 if (index_data.fail_for_error)
3123 result = -1;
3124
Nico Weberdf686022014-05-07 21:09:42 +00003125 free_client_data(&index_data);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003126 return result;
3127}
3128
3129static int index_ast_file(const char *ast_file,
3130 CXIndex Idx,
3131 CXIndexAction idxAction,
3132 ImportedASTFilesData *importedASTs,
3133 const char *check_prefix) {
3134 CXTranslationUnit TU;
3135 IndexData index_data;
3136 unsigned index_opts;
3137 int result;
3138
3139 if (!CreateTranslationUnit(Idx, ast_file, &TU))
3140 return -1;
3141
3142 index_data.check_prefix = check_prefix;
3143 index_data.first_check_printed = 0;
3144 index_data.fail_for_error = 0;
3145 index_data.abort = 0;
3146 index_data.main_filename = "";
3147 index_data.importedASTs = importedASTs;
Nico Weberdf686022014-05-07 21:09:42 +00003148 index_data.strings = NULL;
Argyrios Kyrtzidisf6d49c32014-05-14 23:14:37 +00003149 index_data.TU = TU;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003150
3151 index_opts = getIndexOptions();
3152 result = clang_indexTranslationUnit(idxAction, &index_data,
3153 &IndexCB,sizeof(IndexCB),
3154 index_opts, TU);
3155 if (index_data.fail_for_error)
3156 result = -1;
3157
3158 clang_disposeTranslationUnit(TU);
Nico Weberdf686022014-05-07 21:09:42 +00003159 free_client_data(&index_data);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003160 return result;
3161}
3162
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003163static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003164 const char *check_prefix;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003165 CXIndex Idx;
3166 CXIndexAction idxAction;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003167 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003168 int result;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003169
3170 check_prefix = 0;
3171 if (argc > 0) {
3172 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3173 check_prefix = argv[0] + strlen("-check-prefix=");
3174 ++argv;
3175 --argc;
3176 }
3177 }
3178
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003179 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003180 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003181 fprintf(stderr, "Could not create Index\n");
3182 return 1;
3183 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003184 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003185 importedASTs = 0;
3186 if (full)
3187 importedASTs = importedASTs_create();
3188
3189 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
3190 if (result != 0)
3191 goto finished;
3192
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003193 if (full) {
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003194 unsigned i;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003195 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
3196 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
3197 importedASTs, check_prefix);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003198 }
3199 }
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003200
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003201finished:
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003202 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003203 clang_IndexAction_dispose(idxAction);
3204 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003205 return result;
3206}
3207
3208static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003209 const char *check_prefix;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003210 CXIndex Idx;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003211 CXIndexAction idxAction;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003212 int result;
3213
3214 check_prefix = 0;
3215 if (argc > 0) {
3216 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3217 check_prefix = argv[0] + strlen("-check-prefix=");
3218 ++argv;
3219 --argc;
3220 }
3221 }
3222
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003223 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003224 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003225 fprintf(stderr, "Could not create Index\n");
3226 return 1;
3227 }
3228 idxAction = clang_IndexAction_create(Idx);
3229
3230 result = index_ast_file(argv[0], Idx, idxAction,
3231 /*importedASTs=*/0, check_prefix);
3232
3233 clang_IndexAction_dispose(idxAction);
3234 clang_disposeIndex(Idx);
3235 return result;
3236}
3237
3238static int index_compile_db(int argc, const char **argv) {
3239 const char *check_prefix;
3240 CXIndex Idx;
3241 CXIndexAction idxAction;
3242 int errorCode = 0;
3243
3244 check_prefix = 0;
3245 if (argc > 0) {
3246 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3247 check_prefix = argv[0] + strlen("-check-prefix=");
3248 ++argv;
3249 --argc;
3250 }
3251 }
3252
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003253 if (argc == 0) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003254 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003255 return -1;
3256 }
3257
3258 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003259 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003260 fprintf(stderr, "Could not create Index\n");
3261 return 1;
3262 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003263 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003264
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003265 {
3266 const char *database = argv[0];
3267 CXCompilationDatabase db = 0;
3268 CXCompileCommands CCmds = 0;
3269 CXCompileCommand CCmd;
3270 CXCompilationDatabase_Error ec;
3271 CXString wd;
3272#define MAX_COMPILE_ARGS 512
3273 CXString cxargs[MAX_COMPILE_ARGS];
3274 const char *args[MAX_COMPILE_ARGS];
3275 char *tmp;
3276 unsigned len;
3277 char *buildDir;
3278 int i, a, numCmds, numArgs;
3279
3280 len = strlen(database);
3281 tmp = (char *) malloc(len+1);
3282 memcpy(tmp, database, len+1);
3283 buildDir = dirname(tmp);
3284
3285 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3286
3287 if (db) {
3288
3289 if (ec!=CXCompilationDatabase_NoError) {
3290 printf("unexpected error %d code while loading compilation database\n", ec);
3291 errorCode = -1;
3292 goto cdb_end;
3293 }
3294
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003295 if (chdir(buildDir) != 0) {
3296 printf("Could not chdir to %s\n", buildDir);
3297 errorCode = -1;
3298 goto cdb_end;
3299 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003300
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003301 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003302 if (!CCmds) {
3303 printf("compilation db is empty\n");
3304 errorCode = -1;
3305 goto cdb_end;
3306 }
3307
3308 numCmds = clang_CompileCommands_getSize(CCmds);
3309
3310 if (numCmds==0) {
3311 fprintf(stderr, "should not get an empty compileCommand set\n");
3312 errorCode = -1;
3313 goto cdb_end;
3314 }
3315
3316 for (i=0; i<numCmds && errorCode == 0; ++i) {
3317 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3318
3319 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003320 if (chdir(clang_getCString(wd)) != 0) {
3321 printf("Could not chdir to %s\n", clang_getCString(wd));
3322 errorCode = -1;
3323 goto cdb_end;
3324 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003325 clang_disposeString(wd);
3326
3327 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3328 if (numArgs > MAX_COMPILE_ARGS){
3329 fprintf(stderr, "got more compile arguments than maximum\n");
3330 errorCode = -1;
3331 goto cdb_end;
3332 }
3333 for (a=0; a<numArgs; ++a) {
3334 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3335 args[a] = clang_getCString(cxargs[a]);
3336 }
3337
3338 errorCode = index_compile_args(numArgs, args, idxAction,
3339 /*importedASTs=*/0, check_prefix);
3340
3341 for (a=0; a<numArgs; ++a)
3342 clang_disposeString(cxargs[a]);
3343 }
3344 } else {
3345 printf("database loading failed with error code %d.\n", ec);
3346 errorCode = -1;
3347 }
3348
3349 cdb_end:
3350 clang_CompileCommands_dispose(CCmds);
3351 clang_CompilationDatabase_dispose(db);
3352 free(tmp);
3353
3354 }
3355
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003356 clang_IndexAction_dispose(idxAction);
3357 clang_disposeIndex(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003358 return errorCode;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003359}
3360
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003361int perform_token_annotation(int argc, const char **argv) {
3362 const char *input = argv[1];
3363 char *filename = 0;
3364 unsigned line, second_line;
3365 unsigned column, second_column;
3366 CXIndex CIdx;
3367 CXTranslationUnit TU = 0;
3368 int errorCode;
3369 struct CXUnsavedFile *unsaved_files = 0;
3370 int num_unsaved_files = 0;
3371 CXToken *tokens;
3372 unsigned num_tokens;
3373 CXSourceRange range;
3374 CXSourceLocation startLoc, endLoc;
3375 CXFile file = 0;
3376 CXCursor *cursors = 0;
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003377 CXSourceRangeList *skipped_ranges = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003378 enum CXErrorCode Err;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003379 unsigned i;
3380
3381 input += strlen("-test-annotate-tokens=");
3382 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3383 &second_line, &second_column)))
3384 return errorCode;
3385
Richard Smith1ea42eb2012-07-05 08:20:49 +00003386 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3387 free(filename);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003388 return -1;
Richard Smith1ea42eb2012-07-05 08:20:49 +00003389 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003390
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003391 CIdx = clang_createIndex(0, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003392 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
3393 argv + num_unsaved_files + 2,
3394 argc - num_unsaved_files - 3,
3395 unsaved_files,
3396 num_unsaved_files,
3397 getDefaultParsingOptions(), &TU);
3398 if (Err != CXError_Success) {
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003399 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003400 describeLibclangFailure(Err);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003401 clang_disposeIndex(CIdx);
3402 free(filename);
3403 free_remapped_files(unsaved_files, num_unsaved_files);
3404 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003405 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003406 errorCode = 0;
3407
Richard Smith1ea42eb2012-07-05 08:20:49 +00003408 if (checkForErrors(TU) != 0) {
3409 errorCode = -1;
3410 goto teardown;
3411 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003412
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003413 if (getenv("CINDEXTEST_EDITING")) {
3414 for (i = 0; i < 5; ++i) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003415 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3416 clang_defaultReparseOptions(TU));
3417 if (Err != CXError_Success) {
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003418 fprintf(stderr, "Unable to reparse translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003419 describeLibclangFailure(Err);
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003420 errorCode = -1;
3421 goto teardown;
3422 }
3423 }
3424 }
3425
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003426 if (checkForErrors(TU) != 0) {
3427 errorCode = -1;
3428 goto teardown;
3429 }
3430
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003431 file = clang_getFile(TU, filename);
3432 if (!file) {
3433 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3434 errorCode = -1;
3435 goto teardown;
3436 }
3437
3438 startLoc = clang_getLocation(TU, file, line, column);
3439 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003440 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003441 column);
3442 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003443 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003444 }
3445
3446 endLoc = clang_getLocation(TU, file, second_line, second_column);
3447 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003448 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003449 second_line, second_column);
3450 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003451 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003452 }
3453
3454 range = clang_getRange(startLoc, endLoc);
3455 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003456
3457 if (checkForErrors(TU) != 0) {
3458 errorCode = -1;
3459 goto teardown;
3460 }
3461
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003462 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3463 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003464
3465 if (checkForErrors(TU) != 0) {
3466 errorCode = -1;
3467 goto teardown;
3468 }
3469
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003470 skipped_ranges = clang_getSkippedRanges(TU, file);
3471 for (i = 0; i != skipped_ranges->count; ++i) {
3472 unsigned start_line, start_column, end_line, end_column;
3473 clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
3474 0, &start_line, &start_column, 0);
3475 clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
3476 0, &end_line, &end_column, 0);
3477 printf("Skipping: ");
3478 PrintExtent(stdout, start_line, start_column, end_line, end_column);
3479 printf("\n");
3480 }
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003481 clang_disposeSourceRangeList(skipped_ranges);
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003482
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003483 for (i = 0; i != num_tokens; ++i) {
3484 const char *kind = "<unknown>";
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003485 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3486 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003487 unsigned start_line, start_column, end_line, end_column;
3488
3489 switch (clang_getTokenKind(tokens[i])) {
3490 case CXToken_Punctuation: kind = "Punctuation"; break;
3491 case CXToken_Keyword: kind = "Keyword"; break;
3492 case CXToken_Identifier: kind = "Identifier"; break;
3493 case CXToken_Literal: kind = "Literal"; break;
3494 case CXToken_Comment: kind = "Comment"; break;
3495 }
Douglas Gregor229bebd2010-11-09 06:24:54 +00003496 clang_getSpellingLocation(clang_getRangeStart(extent),
3497 0, &start_line, &start_column, 0);
3498 clang_getSpellingLocation(clang_getRangeEnd(extent),
3499 0, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003500 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Krameraf7ae312012-04-14 09:11:51 +00003501 clang_disposeString(spelling);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003502 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor61656112010-01-26 18:31:56 +00003503 if (!clang_isInvalid(cursors[i].kind)) {
3504 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003505 PrintCursor(cursors[i], NULL);
Douglas Gregor61656112010-01-26 18:31:56 +00003506 }
3507 printf("\n");
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003508 }
3509 free(cursors);
Ted Kremenek983fb5de2010-10-20 21:22:15 +00003510 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003511
3512 teardown:
Douglas Gregor33cdd812010-02-18 18:08:43 +00003513 PrintDiagnostics(TU);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003514 clang_disposeTranslationUnit(TU);
3515 clang_disposeIndex(CIdx);
3516 free(filename);
3517 free_remapped_files(unsaved_files, num_unsaved_files);
3518 return errorCode;
3519}
3520
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003521static int
3522perform_test_compilation_db(const char *database, int argc, const char **argv) {
3523 CXCompilationDatabase db;
3524 CXCompileCommands CCmds;
3525 CXCompileCommand CCmd;
3526 CXCompilationDatabase_Error ec;
3527 CXString wd;
3528 CXString arg;
3529 int errorCode = 0;
3530 char *tmp;
3531 unsigned len;
3532 char *buildDir;
3533 int i, j, a, numCmds, numArgs;
3534
3535 len = strlen(database);
3536 tmp = (char *) malloc(len+1);
3537 memcpy(tmp, database, len+1);
3538 buildDir = dirname(tmp);
3539
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003540 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003541
3542 if (db) {
3543
3544 if (ec!=CXCompilationDatabase_NoError) {
3545 printf("unexpected error %d code while loading compilation database\n", ec);
3546 errorCode = -1;
3547 goto cdb_end;
3548 }
3549
3550 for (i=0; i<argc && errorCode==0; ) {
3551 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003552 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003553
3554 if (!CCmds) {
3555 printf("file %s not found in compilation db\n", argv[i+1]);
3556 errorCode = -1;
3557 break;
3558 }
3559
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003560 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003561
3562 if (numCmds==0) {
3563 fprintf(stderr, "should not get an empty compileCommand set for file"
3564 " '%s'\n", argv[i+1]);
3565 errorCode = -1;
3566 break;
3567 }
3568
3569 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003570 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003571
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003572 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003573 printf("workdir:'%s'", clang_getCString(wd));
3574 clang_disposeString(wd);
3575
3576 printf(" cmdline:'");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003577 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003578 for (a=0; a<numArgs; ++a) {
3579 if (a) printf(" ");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003580 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003581 printf("%s", clang_getCString(arg));
3582 clang_disposeString(arg);
3583 }
3584 printf("'\n");
3585 }
3586
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003587 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003588
3589 i += 2;
3590 }
3591 }
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003592 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003593 } else {
3594 printf("database loading failed with error code %d.\n", ec);
3595 errorCode = -1;
3596 }
3597
3598cdb_end:
3599 free(tmp);
3600
3601 return errorCode;
3602}
3603
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003604/******************************************************************************/
Ted Kremenek599d73a2010-03-25 02:00:39 +00003605/* USR printing. */
3606/******************************************************************************/
3607
3608static int insufficient_usr(const char *kind, const char *usage) {
3609 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3610 return 1;
3611}
3612
3613static unsigned isUSR(const char *s) {
3614 return s[0] == 'c' && s[1] == ':';
3615}
3616
3617static int not_usr(const char *s, const char *arg) {
3618 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3619 return 1;
3620}
3621
3622static void print_usr(CXString usr) {
3623 const char *s = clang_getCString(usr);
3624 printf("%s\n", s);
3625 clang_disposeString(usr);
3626}
3627
3628static void display_usrs() {
3629 fprintf(stderr, "-print-usrs options:\n"
3630 " ObjCCategory <class name> <category name>\n"
3631 " ObjCClass <class name>\n"
3632 " ObjCIvar <ivar name> <class USR>\n"
3633 " ObjCMethod <selector> [0=class method|1=instance method] "
3634 "<class USR>\n"
3635 " ObjCProperty <property name> <class USR>\n"
3636 " ObjCProtocol <protocol name>\n");
3637}
3638
3639int print_usrs(const char **I, const char **E) {
3640 while (I != E) {
3641 const char *kind = *I;
3642 unsigned len = strlen(kind);
3643 switch (len) {
3644 case 8:
3645 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3646 if (I + 2 >= E)
3647 return insufficient_usr(kind, "<ivar name> <class USR>");
3648 if (!isUSR(I[2]))
3649 return not_usr("<class USR>", I[2]);
3650 else {
3651 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003652 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003653 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003654 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3655 }
3656
3657 I += 3;
3658 continue;
3659 }
3660 break;
3661 case 9:
3662 if (memcmp(kind, "ObjCClass", 9) == 0) {
3663 if (I + 1 >= E)
3664 return insufficient_usr(kind, "<class name>");
3665 print_usr(clang_constructUSR_ObjCClass(I[1]));
3666 I += 2;
3667 continue;
3668 }
3669 break;
3670 case 10:
3671 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3672 if (I + 3 >= E)
3673 return insufficient_usr(kind, "<method selector> "
3674 "[0=class method|1=instance method] <class USR>");
3675 if (!isUSR(I[3]))
3676 return not_usr("<class USR>", I[3]);
3677 else {
3678 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003679 x.data = (void*) I[3];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003680 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003681 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3682 }
3683 I += 4;
3684 continue;
3685 }
3686 break;
3687 case 12:
3688 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3689 if (I + 2 >= E)
3690 return insufficient_usr(kind, "<class name> <category name>");
3691 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3692 I += 3;
3693 continue;
3694 }
3695 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3696 if (I + 1 >= E)
3697 return insufficient_usr(kind, "<protocol name>");
3698 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3699 I += 2;
3700 continue;
3701 }
3702 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3703 if (I + 2 >= E)
3704 return insufficient_usr(kind, "<property name> <class USR>");
3705 if (!isUSR(I[2]))
3706 return not_usr("<class USR>", I[2]);
3707 else {
3708 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003709 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003710 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003711 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3712 }
3713 I += 3;
3714 continue;
3715 }
3716 break;
3717 default:
3718 break;
3719 }
3720 break;
3721 }
3722
3723 if (I != E) {
3724 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3725 display_usrs();
3726 return 1;
3727 }
3728 return 0;
3729}
3730
3731int print_usrs_file(const char *file_name) {
3732 char line[2048];
3733 const char *args[128];
3734 unsigned numChars = 0;
3735
3736 FILE *fp = fopen(file_name, "r");
3737 if (!fp) {
3738 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3739 return 1;
3740 }
3741
3742 /* This code is not really all that safe, but it works fine for testing. */
3743 while (!feof(fp)) {
3744 char c = fgetc(fp);
3745 if (c == '\n') {
3746 unsigned i = 0;
3747 const char *s = 0;
3748
3749 if (numChars == 0)
3750 continue;
3751
3752 line[numChars] = '\0';
3753 numChars = 0;
3754
3755 if (line[0] == '/' && line[1] == '/')
3756 continue;
3757
3758 s = strtok(line, " ");
3759 while (s) {
3760 args[i] = s;
3761 ++i;
3762 s = strtok(0, " ");
3763 }
3764 if (print_usrs(&args[0], &args[i]))
3765 return 1;
3766 }
3767 else
3768 line[numChars++] = c;
3769 }
3770
3771 fclose(fp);
3772 return 0;
3773}
3774
3775/******************************************************************************/
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003776/* Command line processing. */
3777/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003778int write_pch_file(const char *filename, int argc, const char *argv[]) {
3779 CXIndex Idx;
3780 CXTranslationUnit TU;
3781 struct CXUnsavedFile *unsaved_files = 0;
3782 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003783 enum CXErrorCode Err;
Francois Pichetabcfbec2011-07-06 22:09:44 +00003784 int result = 0;
Douglas Gregore9386682010-08-13 05:36:37 +00003785
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003786 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregore9386682010-08-13 05:36:37 +00003787
3788 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3789 clang_disposeIndex(Idx);
3790 return -1;
3791 }
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003792
3793 Err = clang_parseTranslationUnit2(
3794 Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files,
3795 unsaved_files, num_unsaved_files,
3796 CXTranslationUnit_Incomplete |
3797 CXTranslationUnit_DetailedPreprocessingRecord |
3798 CXTranslationUnit_ForSerialization,
3799 &TU);
3800 if (Err != CXError_Success) {
Douglas Gregore9386682010-08-13 05:36:37 +00003801 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003802 describeLibclangFailure(Err);
Douglas Gregore9386682010-08-13 05:36:37 +00003803 free_remapped_files(unsaved_files, num_unsaved_files);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003804 clang_disposeTranslationUnit(TU);
Douglas Gregore9386682010-08-13 05:36:37 +00003805 clang_disposeIndex(Idx);
3806 return 1;
3807 }
3808
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003809 switch (clang_saveTranslationUnit(TU, filename,
3810 clang_defaultSaveOptions(TU))) {
3811 case CXSaveError_None:
3812 break;
3813
3814 case CXSaveError_TranslationErrors:
3815 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3816 filename);
3817 result = 2;
3818 break;
3819
3820 case CXSaveError_InvalidTU:
3821 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3822 filename);
3823 result = 3;
3824 break;
3825
3826 case CXSaveError_Unknown:
3827 default:
3828 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3829 result = 1;
3830 break;
3831 }
3832
Douglas Gregore9386682010-08-13 05:36:37 +00003833 clang_disposeTranslationUnit(TU);
3834 free_remapped_files(unsaved_files, num_unsaved_files);
3835 clang_disposeIndex(Idx);
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003836 return result;
Douglas Gregore9386682010-08-13 05:36:37 +00003837}
3838
3839/******************************************************************************/
Ted Kremenekd010ba42011-11-10 08:43:12 +00003840/* Serialized diagnostics. */
3841/******************************************************************************/
3842
3843static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3844 switch (error) {
3845 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3846 case CXLoadDiag_None: break;
3847 case CXLoadDiag_Unknown: return "Unknown";
3848 case CXLoadDiag_InvalidFile: return "Invalid File";
3849 }
3850 return "None";
3851}
3852
3853static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3854 switch (severity) {
3855 case CXDiagnostic_Note: return "note";
3856 case CXDiagnostic_Error: return "error";
3857 case CXDiagnostic_Fatal: return "fatal";
3858 case CXDiagnostic_Ignored: return "ignored";
3859 case CXDiagnostic_Warning: return "warning";
3860 }
3861 return "unknown";
3862}
3863
3864static void printIndent(unsigned indent) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003865 if (indent == 0)
3866 return;
3867 fprintf(stderr, "+");
3868 --indent;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003869 while (indent > 0) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003870 fprintf(stderr, "-");
Ted Kremenekd010ba42011-11-10 08:43:12 +00003871 --indent;
3872 }
3873}
3874
3875static void printLocation(CXSourceLocation L) {
3876 CXFile File;
3877 CXString FileName;
3878 unsigned line, column, offset;
3879
3880 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3881 FileName = clang_getFileName(File);
3882
3883 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3884 clang_disposeString(FileName);
3885}
3886
3887static void printRanges(CXDiagnostic D, unsigned indent) {
3888 unsigned i, n = clang_getDiagnosticNumRanges(D);
3889
3890 for (i = 0; i < n; ++i) {
3891 CXSourceLocation Start, End;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003892 CXSourceRange SR = clang_getDiagnosticRange(D, i);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003893 Start = clang_getRangeStart(SR);
3894 End = clang_getRangeEnd(SR);
3895
3896 printIndent(indent);
3897 fprintf(stderr, "Range: ");
3898 printLocation(Start);
3899 fprintf(stderr, " ");
3900 printLocation(End);
3901 fprintf(stderr, "\n");
3902 }
3903}
3904
3905static void printFixIts(CXDiagnostic D, unsigned indent) {
3906 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek4a642302012-03-20 20:49:45 +00003907 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003908 for (i = 0 ; i < n; ++i) {
3909 CXSourceRange ReplacementRange;
3910 CXString text;
3911 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3912
3913 printIndent(indent);
3914 fprintf(stderr, "FIXIT: (");
3915 printLocation(clang_getRangeStart(ReplacementRange));
3916 fprintf(stderr, " - ");
3917 printLocation(clang_getRangeEnd(ReplacementRange));
3918 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3919 clang_disposeString(text);
3920 }
3921}
3922
3923static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003924 unsigned i, n;
3925
Ted Kremenekd010ba42011-11-10 08:43:12 +00003926 if (!Diags)
3927 return;
3928
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003929 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003930 for (i = 0; i < n; ++i) {
3931 CXSourceLocation DiagLoc;
3932 CXDiagnostic D;
3933 CXFile File;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003934 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003935 unsigned line, column, offset;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003936 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003937
3938 D = clang_getDiagnosticInSet(Diags, i);
3939 DiagLoc = clang_getDiagnosticLocation(D);
3940 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3941 FileName = clang_getFileName(File);
3942 DiagSpelling = clang_getDiagnosticSpelling(D);
3943
3944 printIndent(indent);
3945
3946 fprintf(stderr, "%s:%d:%d: %s: %s",
3947 clang_getCString(FileName),
3948 line,
3949 column,
3950 getSeverityString(clang_getDiagnosticSeverity(D)),
3951 clang_getCString(DiagSpelling));
3952
3953 DiagOption = clang_getDiagnosticOption(D, 0);
3954 DiagOptionStr = clang_getCString(DiagOption);
3955 if (DiagOptionStr) {
3956 fprintf(stderr, " [%s]", DiagOptionStr);
3957 }
3958
Ted Kremenek26a6d492012-04-12 00:03:31 +00003959 DiagCat = clang_getDiagnosticCategoryText(D);
3960 DiagCatStr = clang_getCString(DiagCat);
3961 if (DiagCatStr) {
3962 fprintf(stderr, " [%s]", DiagCatStr);
3963 }
3964
Ted Kremenekd010ba42011-11-10 08:43:12 +00003965 fprintf(stderr, "\n");
3966
3967 printRanges(D, indent);
3968 printFixIts(D, indent);
3969
NAKAMURA Takumi27dd3962011-11-10 10:07:57 +00003970 /* Print subdiagnostics. */
Ted Kremenekd010ba42011-11-10 08:43:12 +00003971 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3972
3973 clang_disposeString(FileName);
3974 clang_disposeString(DiagSpelling);
3975 clang_disposeString(DiagOption);
Nico Weberce5528a2014-05-11 17:16:59 +00003976 clang_disposeString(DiagCat);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003977 }
3978}
3979
3980static int read_diagnostics(const char *filename) {
3981 enum CXLoadDiag_Error error;
3982 CXString errorString;
3983 CXDiagnosticSet Diags = 0;
3984
3985 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3986 if (!Diags) {
3987 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3988 getDiagnosticCodeStr(error),
3989 clang_getCString(errorString));
3990 clang_disposeString(errorString);
3991 return 1;
3992 }
3993
3994 printDiagnosticSet(Diags, 0);
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003995 fprintf(stderr, "Number of diagnostics: %d\n",
3996 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenekd010ba42011-11-10 08:43:12 +00003997 clang_disposeDiagnosticSet(Diags);
3998 return 0;
3999}
4000
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004001static int perform_print_build_session_timestamp(void) {
Yaron Keren129dfbf2015-05-14 06:53:31 +00004002 printf("%lld\n", clang_getBuildSessionTimestamp());
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004003 return 0;
4004}
4005
Ted Kremenekd010ba42011-11-10 08:43:12 +00004006/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00004007/* Command line processing. */
4008/******************************************************************************/
Ted Kremenekef3339b2009-11-17 18:09:14 +00004009
Douglas Gregor720d0052010-01-20 21:32:04 +00004010static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004011 if (s[0] == '\0')
Douglas Gregor720d0052010-01-20 21:32:04 +00004012 return FilteredPrintingVisitor;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004013 if (strcmp(s, "-usrs") == 0)
4014 return USRVisitor;
Ted Kremenek83f642e2011-04-18 22:47:10 +00004015 if (strncmp(s, "-memory-usage", 13) == 0)
4016 return GetVisitor(s + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004017 return NULL;
4018}
4019
Ted Kremenekef3339b2009-11-17 18:09:14 +00004020static void print_usage(void) {
4021 fprintf(stderr,
Ted Kremenek1cd27d52009-11-17 18:13:31 +00004022 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00004023 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregor082c3e62010-01-15 19:40:17 +00004024 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004025 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
4026 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi4deb9a92012-10-24 22:52:04 +00004027 fprintf(stderr,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00004028 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00004029 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00004030 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00004031 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek0469b7e2009-11-18 02:02:52 +00004032 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen338b55c2011-10-06 11:38:08 +00004033 "[FileCheck prefix]\n");
4034 fprintf(stderr,
Ted Kremeneka44d99c2010-01-05 23:18:49 +00004035 " c-index-test -test-load-tu <AST file> <symbol filter> "
4036 "[FileCheck prefix]\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004037 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
4038 "[FileCheck prefix]\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00004039 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregor082c3e62010-01-15 19:40:17 +00004040 fprintf(stderr,
Ted Kremenek83f642e2011-04-18 22:47:10 +00004041 " c-index-test -test-load-source-memory-usage "
4042 "<symbol filter> {<args>}*\n"
Douglas Gregoraa21cc42010-07-19 21:46:24 +00004043 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
4044 " {<args>}*\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00004045 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00004046 " c-index-test -test-load-source-usrs-memory-usage "
4047 "<symbol filter> {<args>}*\n"
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00004048 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
4049 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek11d1a422011-04-18 23:42:53 +00004050 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth718df592010-07-22 06:29:13 +00004051 fprintf(stderr,
Ted Kremenek11d1a422011-04-18 23:42:53 +00004052 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenko00353722013-02-15 21:15:49 +00004053 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00004054 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00004055 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00004056 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregore9386682010-08-13 05:36:37 +00004057 " c-index-test -print-usr-file <file>\n"
Ted Kremenekd010ba42011-11-10 08:43:12 +00004058 " c-index-test -write-pch <file> <compiler arguments>\n");
4059 fprintf(stderr,
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004060 " c-index-test -compilation-db [lookup <filename>] database\n");
4061 fprintf(stderr,
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004062 " c-index-test -print-build-session-timestamp\n");
4063 fprintf(stderr,
Ted Kremenekd010ba42011-11-10 08:43:12 +00004064 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregor73a18fd2010-07-20 14:34:35 +00004065 fprintf(stderr,
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004066 " <symbol filter> values:\n%s",
Ted Kremenek1cd27d52009-11-17 18:13:31 +00004067 " all - load all symbols, including those from PCH\n"
4068 " local - load all symbols except those in PCH\n"
4069 " category - only load ObjC categories (non-PCH)\n"
4070 " interface - only load ObjC interfaces (non-PCH)\n"
4071 " protocol - only load ObjC protocols (non-PCH)\n"
4072 " function - only load functions (non-PCH)\n"
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00004073 " typedef - only load typdefs (non-PCH)\n"
4074 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekef3339b2009-11-17 18:09:14 +00004075}
4076
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004077/***/
4078
4079int cindextest_main(int argc, const char **argv) {
Douglas Gregor1e21cc72010-02-18 23:07:20 +00004080 clang_enableStackTraces();
Ted Kremenekd010ba42011-11-10 08:43:12 +00004081 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
4082 return read_diagnostics(argv[2]);
Ted Kremenekef3339b2009-11-17 18:09:14 +00004083 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor47815d52010-07-12 18:38:41 +00004084 return perform_code_completion(argc, argv, 0);
4085 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
4086 return perform_code_completion(argc, argv, 1);
Douglas Gregor082c3e62010-01-15 19:40:17 +00004087 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
4088 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00004089 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
4090 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004091 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
4092 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00004093 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00004094 return index_file(argc - 2, argv + 2, /*full=*/0);
4095 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
4096 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00004097 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
4098 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00004099 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
4100 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004101 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00004102 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004103 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00004104 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
4105 NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004106 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00004107 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
4108 CXCursorVisitor I = GetVisitor(argv[1] + 25);
4109 if (I) {
4110 int trials = atoi(argv[2]);
4111 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
4112 NULL);
4113 }
4114 }
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004115 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00004116 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek83f642e2011-04-18 22:47:10 +00004117
4118 PostVisitTU postVisit = 0;
4119 if (strstr(argv[1], "-memory-usage"))
4120 postVisit = PrintMemoryUsage;
4121
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004122 if (I)
Ted Kremenek83f642e2011-04-18 22:47:10 +00004123 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
4124 postVisit);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004125 }
4126 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek0469b7e2009-11-18 02:02:52 +00004127 return perform_file_scan(argv[2], argv[3],
4128 argc >= 5 ? argv[4] : 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00004129 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
4130 return perform_token_annotation(argc, argv);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00004131 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
4132 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
4133 PrintInclusionStack);
4134 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
4135 return perform_test_load_tu(argv[2], "all", NULL, NULL,
4136 PrintInclusionStack);
Ted Kremenek83b28a22010-03-03 06:37:58 +00004137 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
4138 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
4139 NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00004140 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek6bca9842010-05-14 21:29:26 +00004141 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenko00353722013-02-15 21:15:49 +00004142 PrintType, 0);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00004143 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
4144 return perform_test_load_source(argc - 2, argv + 2, "all",
4145 PrintTypeSize, 0);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00004146 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
4147 return perform_test_load_source(argc - 2, argv + 2, "all",
4148 PrintBitWidth, 0);
Eli Bendersky44a206f2014-07-31 18:04:56 +00004149 else if (argc > 2 && strcmp(argv[1], "-test-print-mangle") == 0)
4150 return perform_test_load_tu(argv[2], "all", NULL, PrintMangledName, NULL);
Ted Kremenek599d73a2010-03-25 02:00:39 +00004151 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
4152 if (argc > 2)
4153 return print_usrs(argv + 2, argv + argc);
4154 else {
4155 display_usrs();
4156 return 1;
4157 }
4158 }
4159 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
4160 return print_usrs_file(argv[2]);
Douglas Gregore9386682010-08-13 05:36:37 +00004161 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
4162 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004163 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
4164 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004165 else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0)
4166 return perform_print_build_session_timestamp();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004167
Ted Kremenekef3339b2009-11-17 18:09:14 +00004168 print_usage();
4169 return 1;
Steve Naroffa1c72842009-08-28 15:28:48 +00004170}
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004171
4172/***/
4173
4174/* We intentionally run in a separate thread to ensure we at least minimal
4175 * testing of a multithreaded environment (for example, having a reduced stack
4176 * size). */
4177
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004178typedef struct thread_info {
4179 int argc;
4180 const char **argv;
4181 int result;
4182} thread_info;
Benjamin Kramer112fc6c2010-11-04 19:11:31 +00004183void thread_runner(void *client_data_v) {
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004184 thread_info *client_data = client_data_v;
4185 client_data->result = cindextest_main(client_data->argc, client_data->argv);
Reid Klecknere931c062014-06-05 00:13:43 +00004186}
4187
4188static void flush_atexit(void) {
Timur Iskhodzhanoveae19462014-06-06 11:04:46 +00004189 /* stdout, and surprisingly even stderr, are not always flushed on process
4190 * and thread exit, particularly when the system is under heavy load. */
Reid Klecknere931c062014-06-05 00:13:43 +00004191 fflush(stdout);
4192 fflush(stderr);
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004193}
4194
4195int main(int argc, const char **argv) {
Benjamin Kramer3a913ed2012-08-10 10:06:13 +00004196 thread_info client_data;
4197
Reid Klecknere931c062014-06-05 00:13:43 +00004198 atexit(flush_atexit);
4199
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00004200#ifdef CLANG_HAVE_LIBXML
4201 LIBXML_TEST_VERSION
4202#endif
4203
Douglas Gregorf428bf82010-10-27 16:00:01 +00004204 if (getenv("CINDEXTEST_NOTHREADS"))
4205 return cindextest_main(argc, argv);
4206
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004207 client_data.argc = argc;
4208 client_data.argv = argv;
Daniel Dunbar23397c32010-11-04 01:26:31 +00004209 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004210 return client_data.result;
4211}