blob: 7ed88dee66d05e252a79bd354e6316d66dd748fe [file] [log] [blame]
Steve Naroff69b10fd2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroffa1c72842009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004#include "clang-c/CXCompilationDatabase.h"
Dmitri Gribenkof430da42014-02-12 10:33:14 +00005#include "clang-c/BuildSystem.h"
Alp Toker59c6bc52014-04-28 02:39:27 +00006#include "clang-c/Documentation.h"
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00007#include "llvm/Config/config.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 }
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000799
800 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);
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000805 printf(" [Specialization of %s:%d:%d]",
806 clang_getCString(Name), line, column);
807 clang_disposeString(Name);
808 }
Douglas Gregor99a26af2010-10-01 20:25:15 +0000809
810 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
811 if (num_overridden) {
812 unsigned I;
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000813 LineCol lineCols[50];
814 assert(num_overridden <= 50);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000815 printf(" [Overrides ");
816 for (I = 0; I != num_overridden; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000817 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000818 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000819 lineCols[I].line = line;
820 lineCols[I].col = column;
821 }
Michael Liaob94f47a2012-08-30 00:45:32 +0000822 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000823 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
824 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor99a26af2010-10-01 20:25:15 +0000825 if (I)
826 printf(", ");
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000827 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000828 }
829 printf("]");
830 clang_disposeOverriddenCursors(overridden);
831 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000832
833 if (Cursor.kind == CXCursor_InclusionDirective) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000834 CXFile File = clang_getIncludedFile(Cursor);
835 CXString Included = clang_getFileName(File);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000836 printf(" (%s)", clang_getCString(Included));
837 clang_disposeString(Included);
Douglas Gregor37aa4932011-05-04 00:14:37 +0000838
839 if (clang_isFileMultipleIncludeGuarded(TU, File))
840 printf(" [multi-include guarded]");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000841 }
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000842
843 CursorExtent = clang_getCursorExtent(Cursor);
844 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
845 CXNameRange_WantQualifier
846 | CXNameRange_WantSinglePiece
847 | CXNameRange_WantTemplateArgs,
848 0);
849 if (!clang_equalRanges(CursorExtent, RefNameRange))
850 PrintRange(RefNameRange, "SingleRefName");
851
852 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
853 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
854 CXNameRange_WantQualifier
855 | CXNameRange_WantTemplateArgs,
856 RefNameRangeNr);
857 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
858 break;
859 if (!clang_equalRanges(CursorExtent, RefNameRange))
860 PrintRange(RefNameRange, "RefName");
861 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000862
Chandler Carruthb2faa592014-05-02 23:30:59 +0000863 PrintCursorComments(Cursor, CommentSchemaFile);
Argyrios Kyrtzidis9adfd8a2013-04-18 22:15:49 +0000864
865 {
866 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
867 if (PropAttrs != CXObjCPropertyAttr_noattr) {
868 printf(" [");
869 #define PRINT_PROP_ATTR(A) \
870 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
871 PRINT_PROP_ATTR(readonly);
872 PRINT_PROP_ATTR(getter);
873 PRINT_PROP_ATTR(assign);
874 PRINT_PROP_ATTR(readwrite);
875 PRINT_PROP_ATTR(retain);
876 PRINT_PROP_ATTR(copy);
877 PRINT_PROP_ATTR(nonatomic);
878 PRINT_PROP_ATTR(setter);
879 PRINT_PROP_ATTR(atomic);
880 PRINT_PROP_ATTR(weak);
881 PRINT_PROP_ATTR(strong);
882 PRINT_PROP_ATTR(unsafe_unretained);
883 printf("]");
884 }
885 }
Argyrios Kyrtzidis9d9bc012013-04-18 23:29:12 +0000886
887 {
888 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
889 if (QT != CXObjCDeclQualifier_None) {
890 printf(" [");
891 #define PRINT_OBJC_QUAL(A) \
892 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
893 PRINT_OBJC_QUAL(In);
894 PRINT_OBJC_QUAL(Inout);
895 PRINT_OBJC_QUAL(Out);
896 PRINT_OBJC_QUAL(Bycopy);
897 PRINT_OBJC_QUAL(Byref);
898 PRINT_OBJC_QUAL(Oneway);
899 printf("]");
900 }
901 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000902 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000903}
Steve Naroff1054e602009-08-31 00:59:03 +0000904
Ted Kremenek29004672010-02-17 00:41:32 +0000905static const char* GetCursorSource(CXCursor Cursor) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000906 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekc560b682010-02-17 00:41:20 +0000907 CXString source;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000908 CXFile file;
Argyrios Kyrtzidis7ca77352011-11-03 02:20:36 +0000909 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000910 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +0000911 if (!clang_getCString(source)) {
Ted Kremenekc560b682010-02-17 00:41:20 +0000912 clang_disposeString(source);
913 return "<invalid loc>";
914 }
915 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000916 const char *b = basename(clang_getCString(source));
Ted Kremenekc560b682010-02-17 00:41:20 +0000917 clang_disposeString(source);
918 return b;
919 }
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000920}
921
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000922/******************************************************************************/
Ted Kremenekb478ff42010-01-26 17:59:48 +0000923/* Callbacks. */
924/******************************************************************************/
925
926typedef void (*PostVisitTU)(CXTranslationUnit);
927
Douglas Gregor33cdd812010-02-18 18:08:43 +0000928void PrintDiagnostic(CXDiagnostic Diagnostic) {
929 FILE *out = stderr;
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000930 CXFile file;
Douglas Gregord770f732010-02-22 23:17:23 +0000931 CXString Msg;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000932 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregora750e8e2010-11-19 16:18:16 +0000933 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
934 | CXDiagnostic_DisplayOption;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000935 unsigned i, num_fixits;
Ted Kremenek599d73a2010-03-25 02:00:39 +0000936
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000937 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000938 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000939
Douglas Gregord770f732010-02-22 23:17:23 +0000940 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
941 fprintf(stderr, "%s\n", clang_getCString(Msg));
942 clang_disposeString(Msg);
Ted Kremenek599d73a2010-03-25 02:00:39 +0000943
Douglas Gregor229bebd2010-11-09 06:24:54 +0000944 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
945 &file, 0, 0, 0);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000946 if (!file)
947 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000948
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000949 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek4a642302012-03-20 20:49:45 +0000950 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000951 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor836ec942010-02-19 18:16:06 +0000952 CXSourceRange range;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000953 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
954 CXSourceLocation start = clang_getRangeStart(range);
955 CXSourceLocation end = clang_getRangeEnd(range);
Douglas Gregor836ec942010-02-19 18:16:06 +0000956 unsigned start_line, start_column, end_line, end_column;
957 CXFile start_file, end_file;
Douglas Gregor229bebd2010-11-09 06:24:54 +0000958 clang_getSpellingLocation(start, &start_file, &start_line,
959 &start_column, 0);
960 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor836ec942010-02-19 18:16:06 +0000961 if (clang_equalLocations(start, end)) {
962 /* Insertion. */
963 if (start_file == file)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000964 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor836ec942010-02-19 18:16:06 +0000965 clang_getCString(insertion_text), start_line, start_column);
966 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
967 /* Removal. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000968 if (start_file == file && end_file == file) {
969 fprintf(out, "FIX-IT: Remove ");
970 PrintExtent(out, start_line, start_column, end_line, end_column);
971 fprintf(out, "\n");
Douglas Gregor60b11f62010-01-29 00:41:11 +0000972 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000973 } else {
974 /* Replacement. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000975 if (start_file == end_file) {
976 fprintf(out, "FIX-IT: Replace ");
977 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor836ec942010-02-19 18:16:06 +0000978 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor9773e3d2010-02-18 22:27:07 +0000979 }
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000980 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000981 clang_disposeString(insertion_text);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000982 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000983}
984
Ted Kremenek914c7e62012-02-14 02:46:03 +0000985void PrintDiagnosticSet(CXDiagnosticSet Set) {
986 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
987 for ( ; i != n ; ++i) {
988 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
989 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregor33cdd812010-02-18 18:08:43 +0000990 PrintDiagnostic(Diag);
Ted Kremenek914c7e62012-02-14 02:46:03 +0000991 if (ChildDiags)
992 PrintDiagnosticSet(ChildDiags);
993 }
994}
995
996void PrintDiagnostics(CXTranslationUnit TU) {
997 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
998 PrintDiagnosticSet(TUSet);
999 clang_disposeDiagnosticSet(TUSet);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001000}
1001
Ted Kremenek83f642e2011-04-18 22:47:10 +00001002void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayd6238f42011-08-29 16:37:29 +00001003 unsigned long total = 0;
Ted Kremenek11d1a422011-04-18 23:42:53 +00001004 unsigned i = 0;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001005 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet45cc5462011-04-18 23:33:22 +00001006 fprintf(stderr, "Memory usage:\n");
Ted Kremenek11d1a422011-04-18 23:42:53 +00001007 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenek23324122011-04-20 16:41:07 +00001008 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek83f642e2011-04-18 22:47:10 +00001009 unsigned long amount = usage.entries[i].amount;
1010 total += amount;
Ted Kremenek11d1a422011-04-18 23:42:53 +00001011 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek83f642e2011-04-18 22:47:10 +00001012 ((double) amount)/(1024*1024));
1013 }
Ted Kremenek11d1a422011-04-18 23:42:53 +00001014 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek83f642e2011-04-18 22:47:10 +00001015 ((double) total)/(1024*1024));
Ted Kremenek23324122011-04-20 16:41:07 +00001016 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek83f642e2011-04-18 22:47:10 +00001017}
1018
Ted Kremenekb478ff42010-01-26 17:59:48 +00001019/******************************************************************************/
Douglas Gregor720d0052010-01-20 21:32:04 +00001020/* Logic for testing traversal. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001021/******************************************************************************/
1022
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001023static void PrintCursorExtent(CXCursor C) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001024 CXSourceRange extent = clang_getCursorExtent(C);
1025 PrintRange(extent, "Extent");
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001026}
1027
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001028/* Data used by the visitors. */
1029typedef struct {
Douglas Gregor720d0052010-01-20 21:32:04 +00001030 CXTranslationUnit TU;
1031 enum CXCursorKind *Filter;
Chandler Carruthb2faa592014-05-02 23:30:59 +00001032 const char *CommentSchemaFile;
Douglas Gregor720d0052010-01-20 21:32:04 +00001033} VisitorData;
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001034
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001035
Ted Kremenek29004672010-02-17 00:41:32 +00001036enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001037 CXCursor Parent,
1038 CXClientData ClientData) {
1039 VisitorData *Data = (VisitorData *)ClientData;
1040 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001041 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001042 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001043 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001044 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor4f46e782010-01-19 21:36:55 +00001045 GetCursorSource(Cursor), line, column);
Chandler Carruthb2faa592014-05-02 23:30:59 +00001046 PrintCursor(Cursor, Data->CommentSchemaFile);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001047 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis1ab09cc2013-04-11 17:02:10 +00001048 if (clang_isDeclaration(Cursor.kind)) {
1049 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
1050 const char *accessStr = 0;
1051
1052 switch (access) {
1053 case CX_CXXInvalidAccessSpecifier: break;
1054 case CX_CXXPublic:
1055 accessStr = "public"; break;
1056 case CX_CXXProtected:
1057 accessStr = "protected"; break;
1058 case CX_CXXPrivate:
1059 accessStr = "private"; break;
1060 }
1061
1062 if (accessStr)
1063 printf(" [access=%s]", accessStr);
1064 }
Ted Kremenek29004672010-02-17 00:41:32 +00001065 printf("\n");
Douglas Gregor720d0052010-01-20 21:32:04 +00001066 return CXChildVisit_Recurse;
Steve Naroff772c1a42009-08-31 14:26:51 +00001067 }
Ted Kremenek29004672010-02-17 00:41:32 +00001068
Douglas Gregor720d0052010-01-20 21:32:04 +00001069 return CXChildVisit_Continue;
Steve Naroff1054e602009-08-31 00:59:03 +00001070}
Steve Naroffa1c72842009-08-28 15:28:48 +00001071
Ted Kremenek29004672010-02-17 00:41:32 +00001072static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001073 CXCursor Parent,
1074 CXClientData ClientData) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001075 const char *startBuf, *endBuf;
1076 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1077 CXCursor Ref;
Douglas Gregor720d0052010-01-20 21:32:04 +00001078 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001079
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001080 if (Cursor.kind != CXCursor_FunctionDecl ||
1081 !clang_isCursorDefinition(Cursor))
Douglas Gregor720d0052010-01-20 21:32:04 +00001082 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001083
1084 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1085 &startLine, &startColumn,
1086 &endLine, &endColumn);
1087 /* Probe the entire body, looking for both decls and refs. */
1088 curLine = startLine;
1089 curColumn = startColumn;
1090
1091 while (startBuf < endBuf) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001092 CXSourceLocation Loc;
Douglas Gregor4f46e782010-01-19 21:36:55 +00001093 CXFile file;
Ted Kremenekc560b682010-02-17 00:41:20 +00001094 CXString source;
Ted Kremenek29004672010-02-17 00:41:32 +00001095
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001096 if (*startBuf == '\n') {
1097 startBuf++;
1098 curLine++;
1099 curColumn = 1;
1100 } else if (*startBuf != '\t')
1101 curColumn++;
Ted Kremenek29004672010-02-17 00:41:32 +00001102
Douglas Gregor66a58812010-01-18 22:46:11 +00001103 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor229bebd2010-11-09 06:24:54 +00001104 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremenek29004672010-02-17 00:41:32 +00001105
Douglas Gregor4f46e782010-01-19 21:36:55 +00001106 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +00001107 if (clang_getCString(source)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001108 CXSourceLocation RefLoc
1109 = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregor816fd362010-01-22 21:44:22 +00001110 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor66a58812010-01-18 22:46:11 +00001111 if (Ref.kind == CXCursor_NoDeclFound) {
1112 /* Nothing found here; that's fine. */
1113 } else if (Ref.kind != CXCursor_FunctionDecl) {
1114 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1115 curLine, curColumn);
Chandler Carruthb2faa592014-05-02 23:30:59 +00001116 PrintCursor(Ref, Data->CommentSchemaFile);
Douglas Gregor66a58812010-01-18 22:46:11 +00001117 printf("\n");
1118 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001119 }
Ted Kremenekc560b682010-02-17 00:41:20 +00001120 clang_disposeString(source);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001121 startBuf++;
1122 }
Ted Kremenek29004672010-02-17 00:41:32 +00001123
Douglas Gregor720d0052010-01-20 21:32:04 +00001124 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001125}
1126
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001127/******************************************************************************/
1128/* USR testing. */
1129/******************************************************************************/
1130
Douglas Gregor720d0052010-01-20 21:32:04 +00001131enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1132 CXClientData ClientData) {
1133 VisitorData *Data = (VisitorData *)ClientData;
1134 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001135 CXString USR = clang_getCursorUSR(C);
1136 const char *cstr = clang_getCString(USR);
Ted Kremenek6d159c12010-04-20 23:15:40 +00001137 if (!cstr || cstr[0] == '\0') {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001138 clang_disposeString(USR);
Ted Kremenek7afa85b2010-04-16 21:31:52 +00001139 return CXChildVisit_Recurse;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001140 }
Ted Kremenek6d159c12010-04-20 23:15:40 +00001141 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1142
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001143 PrintCursorExtent(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001144 printf("\n");
1145 clang_disposeString(USR);
Ted Kremenek29004672010-02-17 00:41:32 +00001146
Douglas Gregor720d0052010-01-20 21:32:04 +00001147 return CXChildVisit_Recurse;
Ted Kremenek29004672010-02-17 00:41:32 +00001148 }
1149
Douglas Gregor720d0052010-01-20 21:32:04 +00001150 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001151}
1152
1153/******************************************************************************/
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001154/* Inclusion stack testing. */
1155/******************************************************************************/
1156
1157void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1158 unsigned includeStackLen, CXClientData data) {
Ted Kremenek29004672010-02-17 00:41:32 +00001159
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001160 unsigned i;
Ted Kremenekc560b682010-02-17 00:41:20 +00001161 CXString fname;
1162
1163 fname = clang_getFileName(includedFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001164 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenekc560b682010-02-17 00:41:20 +00001165 clang_disposeString(fname);
Ted Kremenek29004672010-02-17 00:41:32 +00001166
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001167 for (i = 0; i < includeStackLen; ++i) {
1168 CXFile includingFile;
1169 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001170 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1171 &column, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +00001172 fname = clang_getFileName(includingFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001173 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenekc560b682010-02-17 00:41:20 +00001174 clang_disposeString(fname);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001175 }
1176 printf("\n");
1177}
1178
1179void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremenek29004672010-02-17 00:41:32 +00001180 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001181}
1182
1183/******************************************************************************/
Ted Kremenek83b28a22010-03-03 06:37:58 +00001184/* Linkage testing. */
1185/******************************************************************************/
1186
1187static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1188 CXClientData d) {
1189 const char *linkage = 0;
1190
1191 if (clang_isInvalid(clang_getCursorKind(cursor)))
1192 return CXChildVisit_Recurse;
1193
1194 switch (clang_getCursorLinkage(cursor)) {
1195 case CXLinkage_Invalid: break;
Douglas Gregor0b466502010-03-04 19:36:27 +00001196 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1197 case CXLinkage_Internal: linkage = "Internal"; break;
1198 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1199 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek83b28a22010-03-03 06:37:58 +00001200 }
1201
1202 if (linkage) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001203 PrintCursor(cursor, NULL);
Ted Kremenek83b28a22010-03-03 06:37:58 +00001204 printf("linkage=%s\n", linkage);
1205 }
1206
1207 return CXChildVisit_Recurse;
1208}
1209
1210/******************************************************************************/
Ted Kremenek6bca9842010-05-14 21:29:26 +00001211/* Typekind testing. */
1212/******************************************************************************/
1213
Dmitri Gribenko00353722013-02-15 21:15:49 +00001214static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1215 CXString TypeSpelling, TypeKindSpelling;
1216
1217 TypeSpelling = clang_getTypeSpelling(T);
1218 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1219 printf(Format,
1220 clang_getCString(TypeSpelling),
1221 clang_getCString(TypeKindSpelling));
1222 clang_disposeString(TypeSpelling);
1223 clang_disposeString(TypeKindSpelling);
1224}
1225
1226static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1227 CXClientData d) {
Ted Kremenek6bca9842010-05-14 21:29:26 +00001228 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001229 CXType T = clang_getCursorType(cursor);
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001230 enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001231 PrintCursor(cursor, NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00001232 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregor56a63802011-01-27 16:27:11 +00001233 if (clang_isConstQualifiedType(T))
1234 printf(" const");
1235 if (clang_isVolatileQualifiedType(T))
1236 printf(" volatile");
1237 if (clang_isRestrictQualifiedType(T))
1238 printf(" restrict");
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001239 if (RQ == CXRefQualifier_LValue)
1240 printf(" lvalue-ref-qualifier");
1241 if (RQ == CXRefQualifier_RValue)
1242 printf(" rvalue-ref-qualifier");
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001243 /* Print the canonical type if it is different. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001244 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001245 CXType CT = clang_getCanonicalType(T);
Ted Kremenekc1508872010-06-21 20:15:39 +00001246 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001247 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001248 }
1249 }
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001250 /* Print the return type if it exists. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001251 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001252 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenekc1508872010-06-21 20:15:39 +00001253 if (RT.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001254 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001255 }
1256 }
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001257 /* Print the argument types if they exist. */
1258 {
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001259 int NumArgs = clang_Cursor_getNumArguments(cursor);
1260 if (NumArgs != -1 && NumArgs != 0) {
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001261 int i;
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001262 printf(" [args=");
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001263 for (i = 0; i < NumArgs; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001264 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001265 if (T.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001266 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001267 }
1268 }
1269 printf("]");
1270 }
1271 }
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001272 /* Print the template argument types if they exist. */
1273 {
1274 int NumTArgs = clang_Type_getNumTemplateArguments(T);
1275 if (NumTArgs != -1 && NumTArgs != 0) {
1276 int i;
1277 printf(" [templateargs/%d=", NumTArgs);
1278 for (i = 0; i < NumTArgs; ++i) {
1279 CXType TArg = clang_Type_getTemplateArgumentAsType(T, i);
1280 if (TArg.kind != CXType_Invalid) {
1281 PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]");
1282 }
1283 }
1284 printf("]");
1285 }
1286 }
Ted Kremenek0c7476a2010-07-30 00:14:11 +00001287 /* Print if this is a non-POD type. */
1288 printf(" [isPOD=%d]", clang_isPODType(T));
Anders Waldenborgddce74f2014-04-09 19:16:08 +00001289 /* Print the pointee type. */
1290 {
1291 CXType PT = clang_getPointeeType(T);
1292 if (PT.kind != CXType_Invalid) {
1293 PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]");
1294 }
1295 }
Ted Kremenekc1508872010-06-21 20:15:39 +00001296
Ted Kremenek6bca9842010-05-14 21:29:26 +00001297 printf("\n");
1298 }
1299 return CXChildVisit_Recurse;
1300}
1301
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001302static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1303 CXClientData d) {
1304 CXType T;
1305 enum CXCursorKind K = clang_getCursorKind(cursor);
1306 if (clang_isInvalid(K))
1307 return CXChildVisit_Recurse;
1308 T = clang_getCursorType(cursor);
1309 PrintCursor(cursor, NULL);
1310 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1311 /* Print the type sizeof if applicable. */
1312 {
1313 long long Size = clang_Type_getSizeOf(T);
1314 if (Size >= 0 || Size < -1 ) {
1315 printf(" [sizeof=%lld]", Size);
1316 }
1317 }
1318 /* Print the type alignof if applicable. */
1319 {
1320 long long Align = clang_Type_getAlignOf(T);
1321 if (Align >= 0 || Align < -1) {
1322 printf(" [alignof=%lld]", Align);
1323 }
1324 }
1325 /* Print the record field offset if applicable. */
1326 {
Nico Weber82098cb2014-04-24 04:14:12 +00001327 CXString FieldSpelling = clang_getCursorSpelling(cursor);
1328 const char *FieldName = clang_getCString(FieldSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001329 /* recurse to get the root anonymous record parent */
1330 CXCursor Parent, Root;
Nico Weber82098cb2014-04-24 04:14:12 +00001331 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) {
1332 CXString RootParentSpelling;
1333 const char *RootParentName = 0;
Richard Smith131daca2014-03-06 21:59:38 +00001334 Parent = p;
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001335 do {
Nico Weber82098cb2014-04-24 04:14:12 +00001336 if (RootParentName != 0)
1337 clang_disposeString(RootParentSpelling);
1338
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001339 Root = Parent;
Nico Weber82098cb2014-04-24 04:14:12 +00001340 RootParentSpelling = clang_getCursorSpelling(Root);
1341 RootParentName = clang_getCString(RootParentSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001342 Parent = clang_getCursorSemanticParent(Root);
Nico Weber82098cb2014-04-24 04:14:12 +00001343 } while (clang_getCursorType(Parent).kind == CXType_Record &&
1344 !strcmp(RootParentName, ""));
1345 clang_disposeString(RootParentSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001346 /* if RootParentName is "", record is anonymous. */
1347 {
1348 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1349 FieldName);
1350 printf(" [offsetof=%lld]", Offset);
1351 }
1352 }
Nico Weber82098cb2014-04-24 04:14:12 +00001353 clang_disposeString(FieldSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001354 }
1355 /* Print if its a bitfield */
1356 {
1357 int IsBitfield = clang_Cursor_isBitField(cursor);
1358 if (IsBitfield)
1359 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1360 }
1361 printf("\n");
1362 return CXChildVisit_Recurse;
1363}
1364
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001365/******************************************************************************/
1366/* Bitwidth testing. */
1367/******************************************************************************/
1368
1369static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1370 CXClientData d) {
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001371 int Bitwidth;
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001372 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1373 return CXChildVisit_Recurse;
1374
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001375 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001376 if (Bitwidth >= 0) {
1377 PrintCursor(cursor, NULL);
1378 printf(" bitwidth=%d\n", Bitwidth);
1379 }
1380
1381 return CXChildVisit_Recurse;
1382}
Ted Kremenek6bca9842010-05-14 21:29:26 +00001383
1384/******************************************************************************/
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001385/* Loading ASTs/source. */
1386/******************************************************************************/
1387
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001388static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek73eccd22010-01-12 18:53:15 +00001389 const char *filter, const char *prefix,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001390 CXCursorVisitor Visitor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001391 PostVisitTU PV,
1392 const char *CommentSchemaFile) {
Ted Kremenek29004672010-02-17 00:41:32 +00001393
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001394 if (prefix)
Ted Kremenek29004672010-02-17 00:41:32 +00001395 FileCheckPrefix = prefix;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001396
1397 if (Visitor) {
1398 enum CXCursorKind K = CXCursor_NotImplemented;
1399 enum CXCursorKind *ck = &K;
1400 VisitorData Data;
Ted Kremenek29004672010-02-17 00:41:32 +00001401
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001402 /* Perform some simple filtering. */
1403 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor97c75712010-10-02 22:49:11 +00001404 else if (!strcmp(filter, "all-display") ||
1405 !strcmp(filter, "local-display")) {
1406 ck = NULL;
1407 want_display_name = 1;
1408 }
Daniel Dunbard64ce7b2010-02-10 20:42:40 +00001409 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001410 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1411 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1412 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1413 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1414 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1415 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1416 else {
1417 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1418 return 1;
1419 }
Ted Kremenek29004672010-02-17 00:41:32 +00001420
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001421 Data.TU = TU;
1422 Data.Filter = ck;
Chandler Carruthb2faa592014-05-02 23:30:59 +00001423 Data.CommentSchemaFile = CommentSchemaFile;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001424 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001425 }
Ted Kremenek29004672010-02-17 00:41:32 +00001426
Ted Kremenekb478ff42010-01-26 17:59:48 +00001427 if (PV)
1428 PV(TU);
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001429
Douglas Gregor33cdd812010-02-18 18:08:43 +00001430 PrintDiagnostics(TU);
Argyrios Kyrtzidis70480492011-11-13 23:39:14 +00001431 if (checkForErrors(TU) != 0) {
1432 clang_disposeTranslationUnit(TU);
1433 return -1;
1434 }
1435
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001436 clang_disposeTranslationUnit(TU);
1437 return 0;
1438}
1439
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001440int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001441 const char *prefix, CXCursorVisitor Visitor,
1442 PostVisitTU PV) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001443 CXIndex Idx;
1444 CXTranslationUnit TU;
Ted Kremenek50228be2010-02-11 07:41:25 +00001445 int result;
Ted Kremenek29004672010-02-17 00:41:32 +00001446 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001447 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001448 /* displayDiagnostics=*/1);
Ted Kremenek29004672010-02-17 00:41:32 +00001449
Ted Kremenek50228be2010-02-11 07:41:25 +00001450 if (!CreateTranslationUnit(Idx, file, &TU)) {
1451 clang_disposeIndex(Idx);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001452 return 1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001453 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001454
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001455 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek50228be2010-02-11 07:41:25 +00001456 clang_disposeIndex(Idx);
1457 return result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001458}
1459
Ted Kremenekb478ff42010-01-26 17:59:48 +00001460int perform_test_load_source(int argc, const char **argv,
1461 const char *filter, CXCursorVisitor Visitor,
1462 PostVisitTU PV) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001463 CXIndex Idx;
1464 CXTranslationUnit TU;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001465 const char *CommentSchemaFile;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001466 struct CXUnsavedFile *unsaved_files = 0;
1467 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001468 enum CXErrorCode Err;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001469 int result;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001470
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001471 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor97c75712010-10-02 22:49:11 +00001472 (!strcmp(filter, "local") ||
1473 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001474 /* displayDiagnostics=*/1);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001475
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001476 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1477 argc--;
1478 argv++;
1479 }
1480
Ted Kremenek50228be2010-02-11 07:41:25 +00001481 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1482 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001483 return -1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001484 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001485
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001486 Err = clang_parseTranslationUnit2(Idx, 0,
1487 argv + num_unsaved_files,
1488 argc - num_unsaved_files,
1489 unsaved_files, num_unsaved_files,
1490 getDefaultParsingOptions(), &TU);
1491 if (Err != CXError_Success) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001492 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001493 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001494 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001495 clang_disposeIndex(Idx);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001496 return 1;
1497 }
1498
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001499 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1500 CommentSchemaFile);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001501 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001502 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001503 return result;
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001504}
1505
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001506int perform_test_reparse_source(int argc, const char **argv, int trials,
1507 const char *filter, CXCursorVisitor Visitor,
1508 PostVisitTU PV) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001509 CXIndex Idx;
1510 CXTranslationUnit TU;
1511 struct CXUnsavedFile *unsaved_files = 0;
1512 int num_unsaved_files = 0;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001513 int compiler_arg_idx = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001514 enum CXErrorCode Err;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001515 int result, i;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001516 int trial;
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001517 int remap_after_trial = 0;
1518 char *endptr = 0;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001519
1520 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1521 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001522 /* displayDiagnostics=*/1);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001523
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001524 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1525 clang_disposeIndex(Idx);
1526 return -1;
1527 }
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001528
1529 for (i = 0; i < argc; ++i) {
1530 if (strcmp(argv[i], "--") == 0)
1531 break;
1532 }
1533 if (i < argc)
1534 compiler_arg_idx = i+1;
1535 if (num_unsaved_files > compiler_arg_idx)
1536 compiler_arg_idx = num_unsaved_files;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001537
Daniel Dunbarec29d712010-08-18 23:09:16 +00001538 /* Load the initial translation unit -- we do this without honoring remapped
1539 * files, so that we have a way to test results after changing the source. */
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001540 Err = clang_parseTranslationUnit2(Idx, 0,
1541 argv + compiler_arg_idx,
1542 argc - compiler_arg_idx,
1543 0, 0, getDefaultParsingOptions(), &TU);
1544 if (Err != CXError_Success) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001545 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001546 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001547 free_remapped_files(unsaved_files, num_unsaved_files);
1548 clang_disposeIndex(Idx);
1549 return 1;
1550 }
1551
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001552 if (checkForErrors(TU) != 0)
1553 return -1;
1554
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001555 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1556 remap_after_trial =
1557 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1558 }
1559
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001560 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001561 free_remapped_files(unsaved_files, num_unsaved_files);
1562 if (parse_remapped_files_with_try(trial, argc, argv, 0,
1563 &unsaved_files, &num_unsaved_files)) {
1564 clang_disposeTranslationUnit(TU);
1565 clang_disposeIndex(Idx);
1566 return -1;
1567 }
1568
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001569 Err = clang_reparseTranslationUnit(
1570 TU,
1571 trial >= remap_after_trial ? num_unsaved_files : 0,
1572 trial >= remap_after_trial ? unsaved_files : 0,
1573 clang_defaultReparseOptions(TU));
1574 if (Err != CXError_Success) {
Daniel Dunbarec29d712010-08-18 23:09:16 +00001575 fprintf(stderr, "Unable to reparse translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001576 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001577 clang_disposeTranslationUnit(TU);
1578 free_remapped_files(unsaved_files, num_unsaved_files);
1579 clang_disposeIndex(Idx);
1580 return -1;
1581 }
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001582
1583 if (checkForErrors(TU) != 0)
1584 return -1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001585 }
1586
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001587 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001588
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001589 free_remapped_files(unsaved_files, num_unsaved_files);
1590 clang_disposeIndex(Idx);
1591 return result;
1592}
1593
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001594/******************************************************************************/
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001595/* Logic for testing clang_getCursor(). */
1596/******************************************************************************/
1597
Douglas Gregor37aa4932011-05-04 00:14:37 +00001598static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001599 unsigned start_line, unsigned start_col,
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001600 unsigned end_line, unsigned end_col,
1601 const char *prefix) {
Ted Kremenekb58514e2010-01-07 01:17:12 +00001602 printf("// %s: ", FileCheckPrefix);
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001603 if (prefix)
1604 printf("-%s", prefix);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00001605 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1606 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001607 PrintCursor(cursor, NULL);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001608 printf("\n");
1609}
1610
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001611static int perform_file_scan(const char *ast_file, const char *source_file,
1612 const char *prefix) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001613 CXIndex Idx;
1614 CXTranslationUnit TU;
1615 FILE *fp;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001616 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregor816fd362010-01-22 21:44:22 +00001617 CXFile file;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001618 unsigned line = 1, col = 1;
Daniel Dunbar6092d502010-02-14 08:32:51 +00001619 unsigned start_line = 1, start_col = 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001620
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001621 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001622 /* displayDiagnostics=*/1))) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001623 fprintf(stderr, "Could not create Index\n");
1624 return 1;
1625 }
Ted Kremenek29004672010-02-17 00:41:32 +00001626
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001627 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1628 return 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001629
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001630 if ((fp = fopen(source_file, "r")) == NULL) {
1631 fprintf(stderr, "Could not open '%s'\n", source_file);
1632 return 1;
1633 }
Ted Kremenek29004672010-02-17 00:41:32 +00001634
Douglas Gregor816fd362010-01-22 21:44:22 +00001635 file = clang_getFile(TU, source_file);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001636 for (;;) {
1637 CXCursor cursor;
1638 int c = fgetc(fp);
Benjamin Kramer10d083172009-11-17 20:51:40 +00001639
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001640 if (c == '\n') {
1641 ++line;
1642 col = 1;
1643 } else
1644 ++col;
1645
1646 /* Check the cursor at this position, and dump the previous one if we have
1647 * found something new.
1648 */
1649 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1650 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1651 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregor37aa4932011-05-04 00:14:37 +00001652 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbar02968e52010-02-14 10:02:57 +00001653 line, col, prefix);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001654 start_line = line;
1655 start_col = col;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001656 }
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001657 if (c == EOF)
1658 break;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001659
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001660 prevCursor = cursor;
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001661 }
Ted Kremenek29004672010-02-17 00:41:32 +00001662
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001663 fclose(fp);
Douglas Gregor7a964ad2011-01-31 22:04:05 +00001664 clang_disposeTranslationUnit(TU);
1665 clang_disposeIndex(Idx);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001666 return 0;
1667}
1668
1669/******************************************************************************/
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001670/* Logic for testing clang code completion. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001671/******************************************************************************/
1672
Douglas Gregor9eb77012009-11-07 00:00:49 +00001673/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1674 on failure. If successful, the pointer *filename will contain newly-allocated
1675 memory (that will be owned by the caller) to store the file name. */
Ted Kremenek29004672010-02-17 00:41:32 +00001676int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001677 unsigned *column, unsigned *second_line,
1678 unsigned *second_column) {
Douglas Gregorf96ea292009-11-09 18:19:57 +00001679 /* Find the second colon. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001680 const char *last_colon = strrchr(input, ':');
1681 unsigned values[4], i;
1682 unsigned num_values = (second_line && second_column)? 4 : 2;
1683
Douglas Gregor9eb77012009-11-07 00:00:49 +00001684 char *endptr = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001685 if (!last_colon || last_colon == input) {
1686 if (num_values == 4)
1687 fprintf(stderr, "could not parse filename:line:column:line:column in "
1688 "'%s'\n", input);
1689 else
1690 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001691 return 1;
1692 }
1693
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001694 for (i = 0; i != num_values; ++i) {
1695 const char *prev_colon;
1696
1697 /* Parse the next line or column. */
1698 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1699 if (*endptr != 0 && *endptr != ':') {
Ted Kremenek29004672010-02-17 00:41:32 +00001700 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001701 (i % 2 ? "column" : "line"), input);
1702 return 1;
1703 }
Ted Kremenek29004672010-02-17 00:41:32 +00001704
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001705 if (i + 1 == num_values)
1706 break;
1707
1708 /* Find the previous colon. */
1709 prev_colon = last_colon - 1;
1710 while (prev_colon != input && *prev_colon != ':')
1711 --prev_colon;
1712 if (prev_colon == input) {
Ted Kremenek29004672010-02-17 00:41:32 +00001713 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001714 (i % 2 == 0? "column" : "line"), input);
Ted Kremenek29004672010-02-17 00:41:32 +00001715 return 1;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001716 }
1717
1718 last_colon = prev_colon;
Douglas Gregorf96ea292009-11-09 18:19:57 +00001719 }
1720
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001721 *line = values[0];
1722 *column = values[1];
Ted Kremenek29004672010-02-17 00:41:32 +00001723
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001724 if (second_line && second_column) {
1725 *second_line = values[2];
1726 *second_column = values[3];
1727 }
1728
Douglas Gregorf96ea292009-11-09 18:19:57 +00001729 /* Copy the file name. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001730 *filename = (char*)malloc(last_colon - input + 1);
1731 memcpy(*filename, input, last_colon - input);
1732 (*filename)[last_colon - input] = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001733 return 0;
1734}
1735
1736const char *
1737clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1738 switch (Kind) {
1739 case CXCompletionChunk_Optional: return "Optional";
1740 case CXCompletionChunk_TypedText: return "TypedText";
1741 case CXCompletionChunk_Text: return "Text";
1742 case CXCompletionChunk_Placeholder: return "Placeholder";
1743 case CXCompletionChunk_Informative: return "Informative";
1744 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1745 case CXCompletionChunk_LeftParen: return "LeftParen";
1746 case CXCompletionChunk_RightParen: return "RightParen";
1747 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1748 case CXCompletionChunk_RightBracket: return "RightBracket";
1749 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1750 case CXCompletionChunk_RightBrace: return "RightBrace";
1751 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1752 case CXCompletionChunk_RightAngle: return "RightAngle";
1753 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001754 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001755 case CXCompletionChunk_Colon: return "Colon";
1756 case CXCompletionChunk_SemiColon: return "SemiColon";
1757 case CXCompletionChunk_Equal: return "Equal";
1758 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1759 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor9eb77012009-11-07 00:00:49 +00001760 }
Ted Kremenek29004672010-02-17 00:41:32 +00001761
Douglas Gregor9eb77012009-11-07 00:00:49 +00001762 return "Unknown";
1763}
1764
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00001765static int checkForErrors(CXTranslationUnit TU) {
1766 unsigned Num, i;
1767 CXDiagnostic Diag;
1768 CXString DiagStr;
1769
1770 if (!getenv("CINDEXTEST_FAILONERROR"))
1771 return 0;
1772
1773 Num = clang_getNumDiagnostics(TU);
1774 for (i = 0; i != Num; ++i) {
1775 Diag = clang_getDiagnostic(TU, i);
1776 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1777 DiagStr = clang_formatDiagnostic(Diag,
1778 clang_defaultDiagnosticDisplayOptions());
1779 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1780 clang_disposeString(DiagStr);
1781 clang_disposeDiagnostic(Diag);
1782 return -1;
1783 }
1784 clang_disposeDiagnostic(Diag);
1785 }
1786
1787 return 0;
1788}
1789
Nico Weber8d19dff2014-05-07 21:05:22 +00001790static void print_completion_string(CXCompletionString completion_string,
1791 FILE *file) {
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001792 int I, N;
Ted Kremenek29004672010-02-17 00:41:32 +00001793
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001794 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001795 for (I = 0; I != N; ++I) {
Ted Kremenekf602f962010-02-17 01:42:24 +00001796 CXString text;
1797 const char *cstr;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001798 enum CXCompletionChunkKind Kind
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001799 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremenek29004672010-02-17 00:41:32 +00001800
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001801 if (Kind == CXCompletionChunk_Optional) {
1802 fprintf(file, "{Optional ");
1803 print_completion_string(
Ted Kremenek29004672010-02-17 00:41:32 +00001804 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001805 file);
1806 fprintf(file, "}");
1807 continue;
Douglas Gregor8ed5b772010-10-08 20:39:29 +00001808 }
1809
1810 if (Kind == CXCompletionChunk_VerticalSpace) {
1811 fprintf(file, "{VerticalSpace }");
1812 continue;
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001813 }
Ted Kremenek29004672010-02-17 00:41:32 +00001814
Douglas Gregorf81f5282009-11-09 17:05:28 +00001815 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenekf602f962010-02-17 01:42:24 +00001816 cstr = clang_getCString(text);
Ted Kremenek29004672010-02-17 00:41:32 +00001817 fprintf(file, "{%s %s}",
Douglas Gregor9eb77012009-11-07 00:00:49 +00001818 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenekf602f962010-02-17 01:42:24 +00001819 cstr ? cstr : "");
1820 clang_disposeString(text);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001821 }
Ted Kremenekf602f962010-02-17 01:42:24 +00001822
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001823}
1824
Nico Weber8d19dff2014-05-07 21:05:22 +00001825static void print_completion_result(CXCompletionResult *completion_result,
Nico Weberdf686022014-05-07 21:09:42 +00001826 FILE *file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001827 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001828 unsigned annotationCount;
Douglas Gregor78254c82012-03-27 23:34:16 +00001829 enum CXCursorKind ParentKind;
1830 CXString ParentName;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001831 CXString BriefComment;
1832 const char *BriefCommentCString;
Douglas Gregor78254c82012-03-27 23:34:16 +00001833
Ted Kremenek29004672010-02-17 00:41:32 +00001834 fprintf(file, "%s:", clang_getCString(ks));
1835 clang_disposeString(ks);
1836
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001837 print_completion_string(completion_result->CompletionString, file);
Douglas Gregorf757a122010-08-23 23:00:57 +00001838 fprintf(file, " (%u)",
Douglas Gregora2db7932010-05-26 22:00:08 +00001839 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregorf757a122010-08-23 23:00:57 +00001840 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1841 case CXAvailability_Available:
1842 break;
1843
1844 case CXAvailability_Deprecated:
1845 fprintf(file, " (deprecated)");
1846 break;
1847
1848 case CXAvailability_NotAvailable:
1849 fprintf(file, " (unavailable)");
1850 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001851
1852 case CXAvailability_NotAccessible:
1853 fprintf(file, " (inaccessible)");
1854 break;
Douglas Gregorf757a122010-08-23 23:00:57 +00001855 }
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001856
1857 annotationCount = clang_getCompletionNumAnnotations(
1858 completion_result->CompletionString);
1859 if (annotationCount) {
1860 unsigned i;
1861 fprintf(file, " (");
1862 for (i = 0; i < annotationCount; ++i) {
1863 if (i != 0)
1864 fprintf(file, ", ");
1865 fprintf(file, "\"%s\"",
1866 clang_getCString(clang_getCompletionAnnotation(
1867 completion_result->CompletionString, i)));
1868 }
1869 fprintf(file, ")");
1870 }
1871
Douglas Gregor78254c82012-03-27 23:34:16 +00001872 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1873 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1874 &ParentKind);
1875 if (ParentKind != CXCursor_NotImplemented) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001876 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregor78254c82012-03-27 23:34:16 +00001877 fprintf(file, " (parent: %s '%s')",
1878 clang_getCString(KindSpelling),
1879 clang_getCString(ParentName));
1880 clang_disposeString(KindSpelling);
1881 }
1882 clang_disposeString(ParentName);
1883 }
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001884
1885 BriefComment = clang_getCompletionBriefComment(
1886 completion_result->CompletionString);
1887 BriefCommentCString = clang_getCString(BriefComment);
1888 if (BriefCommentCString && *BriefCommentCString != '\0') {
1889 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1890 }
1891 clang_disposeString(BriefComment);
Douglas Gregor78254c82012-03-27 23:34:16 +00001892
Douglas Gregorf757a122010-08-23 23:00:57 +00001893 fprintf(file, "\n");
Douglas Gregor9eb77012009-11-07 00:00:49 +00001894}
1895
Douglas Gregor21325842011-07-07 16:03:39 +00001896void print_completion_contexts(unsigned long long contexts, FILE *file) {
1897 fprintf(file, "Completion contexts:\n");
1898 if (contexts == CXCompletionContext_Unknown) {
1899 fprintf(file, "Unknown\n");
1900 }
1901 if (contexts & CXCompletionContext_AnyType) {
1902 fprintf(file, "Any type\n");
1903 }
1904 if (contexts & CXCompletionContext_AnyValue) {
1905 fprintf(file, "Any value\n");
1906 }
1907 if (contexts & CXCompletionContext_ObjCObjectValue) {
1908 fprintf(file, "Objective-C object value\n");
1909 }
1910 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1911 fprintf(file, "Objective-C selector value\n");
1912 }
1913 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1914 fprintf(file, "C++ class type value\n");
1915 }
1916 if (contexts & CXCompletionContext_DotMemberAccess) {
1917 fprintf(file, "Dot member access\n");
1918 }
1919 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1920 fprintf(file, "Arrow member access\n");
1921 }
1922 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1923 fprintf(file, "Objective-C property access\n");
1924 }
1925 if (contexts & CXCompletionContext_EnumTag) {
1926 fprintf(file, "Enum tag\n");
1927 }
1928 if (contexts & CXCompletionContext_UnionTag) {
1929 fprintf(file, "Union tag\n");
1930 }
1931 if (contexts & CXCompletionContext_StructTag) {
1932 fprintf(file, "Struct tag\n");
1933 }
1934 if (contexts & CXCompletionContext_ClassTag) {
1935 fprintf(file, "Class name\n");
1936 }
1937 if (contexts & CXCompletionContext_Namespace) {
1938 fprintf(file, "Namespace or namespace alias\n");
1939 }
1940 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1941 fprintf(file, "Nested name specifier\n");
1942 }
1943 if (contexts & CXCompletionContext_ObjCInterface) {
1944 fprintf(file, "Objective-C interface\n");
1945 }
1946 if (contexts & CXCompletionContext_ObjCProtocol) {
1947 fprintf(file, "Objective-C protocol\n");
1948 }
1949 if (contexts & CXCompletionContext_ObjCCategory) {
1950 fprintf(file, "Objective-C category\n");
1951 }
1952 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1953 fprintf(file, "Objective-C instance method\n");
1954 }
1955 if (contexts & CXCompletionContext_ObjCClassMessage) {
1956 fprintf(file, "Objective-C class method\n");
1957 }
1958 if (contexts & CXCompletionContext_ObjCSelectorName) {
1959 fprintf(file, "Objective-C selector name\n");
1960 }
1961 if (contexts & CXCompletionContext_MacroName) {
1962 fprintf(file, "Macro name\n");
1963 }
1964 if (contexts & CXCompletionContext_NaturalLanguage) {
1965 fprintf(file, "Natural language\n");
1966 }
1967}
1968
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001969int my_stricmp(const char *s1, const char *s2) {
1970 while (*s1 && *s2) {
NAKAMURA Takumid3cc2202011-03-09 03:02:28 +00001971 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001972 if (c1 < c2)
1973 return -1;
1974 else if (c1 > c2)
1975 return 1;
1976
1977 ++s1;
1978 ++s2;
1979 }
1980
1981 if (*s1)
1982 return 1;
1983 else if (*s2)
1984 return -1;
1985 return 0;
1986}
1987
Douglas Gregor47815d52010-07-12 18:38:41 +00001988int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00001989 const char *input = argv[1];
1990 char *filename = 0;
1991 unsigned line;
1992 unsigned column;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001993 CXIndex CIdx;
Ted Kremenekef3339b2009-11-17 18:09:14 +00001994 int errorCode;
Douglas Gregor9485bf92009-12-02 09:21:34 +00001995 struct CXUnsavedFile *unsaved_files = 0;
1996 int num_unsaved_files = 0;
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00001997 CXCodeCompleteResults *results = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001998 enum CXErrorCode Err;
1999 CXTranslationUnit TU;
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002000 unsigned I, Repeats = 1;
2001 unsigned completionOptions = clang_defaultCodeCompleteOptions();
2002
2003 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
2004 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002005 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
2006 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregor028d3e42010-08-09 20:45:32 +00002007
Douglas Gregor47815d52010-07-12 18:38:41 +00002008 if (timing_only)
2009 input += strlen("-code-completion-timing=");
2010 else
2011 input += strlen("-code-completion-at=");
2012
Ted Kremenek29004672010-02-17 00:41:32 +00002013 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002014 0, 0)))
Ted Kremenekef3339b2009-11-17 18:09:14 +00002015 return errorCode;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002016
Douglas Gregor9485bf92009-12-02 09:21:34 +00002017 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2018 return -1;
2019
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002020 CIdx = clang_createIndex(0, 0);
2021
2022 if (getenv("CINDEXTEST_EDITING"))
2023 Repeats = 5;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002024
2025 Err = clang_parseTranslationUnit2(CIdx, 0,
2026 argv + num_unsaved_files + 2,
2027 argc - num_unsaved_files - 2,
2028 0, 0, getDefaultParsingOptions(), &TU);
2029 if (Err != CXError_Success) {
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002030 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002031 describeLibclangFailure(Err);
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002032 return 1;
2033 }
Douglas Gregorc6592922010-11-15 23:00:34 +00002034
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002035 Err = clang_reparseTranslationUnit(TU, 0, 0,
2036 clang_defaultReparseOptions(TU));
2037
2038 if (Err != CXError_Success) {
Douglas Gregorc6592922010-11-15 23:00:34 +00002039 fprintf(stderr, "Unable to reparse translation init!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002040 describeLibclangFailure(Err);
2041 clang_disposeTranslationUnit(TU);
Douglas Gregorc6592922010-11-15 23:00:34 +00002042 return 1;
2043 }
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002044
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002045 for (I = 0; I != Repeats; ++I) {
2046 results = clang_codeCompleteAt(TU, filename, line, column,
2047 unsaved_files, num_unsaved_files,
2048 completionOptions);
2049 if (!results) {
2050 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar186f7422010-08-19 23:44:06 +00002051 return 1;
2052 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002053 if (I != Repeats-1)
2054 clang_disposeCodeCompleteResults(results);
2055 }
Douglas Gregorba965fb2010-01-28 00:56:43 +00002056
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002057 if (results) {
Douglas Gregor63745d52011-07-21 01:05:26 +00002058 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor21325842011-07-07 16:03:39 +00002059 unsigned long long contexts;
Douglas Gregor63745d52011-07-21 01:05:26 +00002060 enum CXCursorKind containerKind;
Douglas Gregorea777402011-07-26 15:24:30 +00002061 CXString objCSelector;
2062 const char *selectorString;
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002063 if (!timing_only) {
2064 /* Sort the code-completion results based on the typed text. */
2065 clang_sortCodeCompletionResults(results->Results, results->NumResults);
2066
Douglas Gregor47815d52010-07-12 18:38:41 +00002067 for (i = 0; i != n; ++i)
2068 print_completion_result(results->Results + i, stdout);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002069 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00002070 n = clang_codeCompleteGetNumDiagnostics(results);
2071 for (i = 0; i != n; ++i) {
2072 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
2073 PrintDiagnostic(diag);
2074 clang_disposeDiagnostic(diag);
2075 }
Douglas Gregor21325842011-07-07 16:03:39 +00002076
2077 contexts = clang_codeCompleteGetContexts(results);
2078 print_completion_contexts(contexts, stdout);
2079
Douglas Gregorea777402011-07-26 15:24:30 +00002080 containerKind = clang_codeCompleteGetContainerKind(results,
2081 &containerIsIncomplete);
Douglas Gregor63745d52011-07-21 01:05:26 +00002082
2083 if (containerKind != CXCursor_InvalidCode) {
2084 /* We have found a container */
2085 CXString containerUSR, containerKindSpelling;
2086 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
2087 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
2088 clang_disposeString(containerKindSpelling);
2089
2090 if (containerIsIncomplete) {
2091 printf("Container is incomplete\n");
2092 }
2093 else {
2094 printf("Container is complete\n");
2095 }
2096
2097 containerUSR = clang_codeCompleteGetContainerUSR(results);
2098 printf("Container USR: %s\n", clang_getCString(containerUSR));
2099 clang_disposeString(containerUSR);
2100 }
2101
Douglas Gregorea777402011-07-26 15:24:30 +00002102 objCSelector = clang_codeCompleteGetObjCSelector(results);
2103 selectorString = clang_getCString(objCSelector);
2104 if (selectorString && strlen(selectorString) > 0) {
2105 printf("Objective-C selector: %s\n", selectorString);
2106 }
2107 clang_disposeString(objCSelector);
2108
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002109 clang_disposeCodeCompleteResults(results);
2110 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00002111 clang_disposeTranslationUnit(TU);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002112 clang_disposeIndex(CIdx);
2113 free(filename);
Ted Kremenek29004672010-02-17 00:41:32 +00002114
Douglas Gregor9485bf92009-12-02 09:21:34 +00002115 free_remapped_files(unsaved_files, num_unsaved_files);
2116
Ted Kremenekef3339b2009-11-17 18:09:14 +00002117 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002118}
2119
Douglas Gregor082c3e62010-01-15 19:40:17 +00002120typedef struct {
2121 char *filename;
2122 unsigned line;
2123 unsigned column;
2124} CursorSourceLocation;
2125
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002126static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002127 CXIndex CIdx;
2128 int errorCode;
2129 struct CXUnsavedFile *unsaved_files = 0;
2130 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002131 enum CXErrorCode Err;
Douglas Gregor082c3e62010-01-15 19:40:17 +00002132 CXTranslationUnit TU;
2133 CXCursor Cursor;
2134 CursorSourceLocation *Locations = 0;
2135 unsigned NumLocations = 0, Loc;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002136 unsigned Repeats = 1;
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002137 unsigned I;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002138
Ted Kremenek29004672010-02-17 00:41:32 +00002139 /* Count the number of locations. */
Douglas Gregor082c3e62010-01-15 19:40:17 +00002140 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2141 ++NumLocations;
Ted Kremenek29004672010-02-17 00:41:32 +00002142
Douglas Gregor082c3e62010-01-15 19:40:17 +00002143 /* Parse the locations. */
2144 assert(NumLocations > 0 && "Unable to count locations?");
2145 Locations = (CursorSourceLocation *)malloc(
2146 NumLocations * sizeof(CursorSourceLocation));
2147 for (Loc = 0; Loc < NumLocations; ++Loc) {
2148 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremenek29004672010-02-17 00:41:32 +00002149 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2150 &Locations[Loc].line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002151 &Locations[Loc].column, 0, 0)))
Douglas Gregor082c3e62010-01-15 19:40:17 +00002152 return errorCode;
2153 }
Ted Kremenek29004672010-02-17 00:41:32 +00002154
2155 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002156 &num_unsaved_files))
2157 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00002158
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002159 if (getenv("CINDEXTEST_EDITING"))
2160 Repeats = 5;
2161
2162 /* Parse the translation unit. When we're testing clang_getCursor() after
2163 reparsing, don't remap unsaved files until the second parse. */
2164 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002165 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2166 argv + num_unsaved_files + 1 + NumLocations,
2167 argc - num_unsaved_files - 2 - NumLocations,
2168 unsaved_files,
2169 Repeats > 1? 0 : num_unsaved_files,
2170 getDefaultParsingOptions(), &TU);
2171 if (Err != CXError_Success) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002172 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002173 describeLibclangFailure(Err);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002174 return -1;
2175 }
Ted Kremenek29004672010-02-17 00:41:32 +00002176
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002177 if (checkForErrors(TU) != 0)
2178 return -1;
2179
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002180 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002181 if (Repeats > 1) {
2182 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2183 clang_defaultReparseOptions(TU));
2184 if (Err != CXError_Success) {
2185 describeLibclangFailure(Err);
2186 clang_disposeTranslationUnit(TU);
2187 return 1;
2188 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002189 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002190
2191 if (checkForErrors(TU) != 0)
2192 return -1;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002193
2194 for (Loc = 0; Loc < NumLocations; ++Loc) {
2195 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2196 if (!file)
2197 continue;
Ted Kremenek29004672010-02-17 00:41:32 +00002198
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002199 Cursor = clang_getCursor(TU,
2200 clang_getLocation(TU, file, Locations[Loc].line,
2201 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002202
2203 if (checkForErrors(TU) != 0)
2204 return -1;
2205
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002206 if (I + 1 == Repeats) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002207 CXCompletionString completionString = clang_getCursorCompletionString(
2208 Cursor);
2209 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002210 CXString Spelling;
2211 const char *cspell;
2212 unsigned line, column;
2213 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2214 printf("%d:%d ", line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002215 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002216 PrintCursorExtent(Cursor);
2217 Spelling = clang_getCursorSpelling(Cursor);
2218 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002219 if (cspell && strlen(cspell) != 0) {
2220 unsigned pieceIndex;
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002221 printf(" Spelling=%s (", cspell);
2222 for (pieceIndex = 0; ; ++pieceIndex) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002223 CXSourceRange range =
2224 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002225 if (clang_Range_isNull(range))
2226 break;
2227 PrintRange(range, 0);
2228 }
2229 printf(")");
2230 }
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002231 clang_disposeString(Spelling);
Argyrios Kyrtzidis210f29f2012-03-30 22:15:48 +00002232 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
Nico Weber8d19dff2014-05-07 21:05:22 +00002233 printf(" Selector index=%d",
2234 clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002235 if (clang_Cursor_isDynamicCall(Cursor))
2236 printf(" Dynamic-call");
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002237 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002238 CXType T = clang_Cursor_getReceiverType(Cursor);
2239 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002240 printf(" Receiver-type=%s", clang_getCString(S));
2241 clang_disposeString(S);
2242 }
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002243
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002244 {
2245 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002246 CXFile astFile;
2247 CXString name, astFilename;
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002248 unsigned i, numHeaders;
2249 if (mod) {
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002250 astFile = clang_Module_getASTFile(mod);
2251 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002252 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002253 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002254 printf(" ModuleName=%s (%s) Headers(%d):",
2255 clang_getCString(name), clang_getCString(astFilename),
2256 numHeaders);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002257 clang_disposeString(name);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002258 clang_disposeString(astFilename);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002259 for (i = 0; i < numHeaders; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002260 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2261 CXString filename = clang_getFileName(file);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002262 printf("\n%s", clang_getCString(filename));
2263 clang_disposeString(filename);
2264 }
2265 }
2266 }
2267
Douglas Gregor3f35bb22011-08-04 20:04:59 +00002268 if (completionString != NULL) {
2269 printf("\nCompletion string: ");
2270 print_completion_string(completionString, stdout);
2271 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002272 printf("\n");
2273 free(Locations[Loc].filename);
2274 }
2275 }
Douglas Gregor082c3e62010-01-15 19:40:17 +00002276 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002277
Douglas Gregor33cdd812010-02-18 18:08:43 +00002278 PrintDiagnostics(TU);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002279 clang_disposeTranslationUnit(TU);
2280 clang_disposeIndex(CIdx);
2281 free(Locations);
2282 free_remapped_files(unsaved_files, num_unsaved_files);
2283 return 0;
2284}
2285
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002286static enum CXVisitorResult findFileRefsVisit(void *context,
2287 CXCursor cursor, CXSourceRange range) {
2288 if (clang_Range_isNull(range))
2289 return CXVisit_Continue;
2290
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002291 PrintCursor(cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002292 PrintRange(range, "");
2293 printf("\n");
2294 return CXVisit_Continue;
2295}
2296
2297static int find_file_refs_at(int argc, const char **argv) {
2298 CXIndex CIdx;
2299 int errorCode;
2300 struct CXUnsavedFile *unsaved_files = 0;
2301 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002302 enum CXErrorCode Err;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002303 CXTranslationUnit TU;
2304 CXCursor Cursor;
2305 CursorSourceLocation *Locations = 0;
2306 unsigned NumLocations = 0, Loc;
2307 unsigned Repeats = 1;
2308 unsigned I;
2309
2310 /* Count the number of locations. */
2311 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2312 ++NumLocations;
2313
2314 /* Parse the locations. */
2315 assert(NumLocations > 0 && "Unable to count locations?");
2316 Locations = (CursorSourceLocation *)malloc(
2317 NumLocations * sizeof(CursorSourceLocation));
2318 for (Loc = 0; Loc < NumLocations; ++Loc) {
2319 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2320 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2321 &Locations[Loc].line,
2322 &Locations[Loc].column, 0, 0)))
2323 return errorCode;
2324 }
2325
2326 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2327 &num_unsaved_files))
2328 return -1;
2329
2330 if (getenv("CINDEXTEST_EDITING"))
2331 Repeats = 5;
2332
2333 /* Parse the translation unit. When we're testing clang_getCursor() after
2334 reparsing, don't remap unsaved files until the second parse. */
2335 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002336 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2337 argv + num_unsaved_files + 1 + NumLocations,
2338 argc - num_unsaved_files - 2 - NumLocations,
2339 unsaved_files,
2340 Repeats > 1? 0 : num_unsaved_files,
2341 getDefaultParsingOptions(), &TU);
2342 if (Err != CXError_Success) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002343 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002344 describeLibclangFailure(Err);
2345 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002346 return -1;
2347 }
2348
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002349 if (checkForErrors(TU) != 0)
2350 return -1;
2351
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002352 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002353 if (Repeats > 1) {
2354 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2355 clang_defaultReparseOptions(TU));
2356 if (Err != CXError_Success) {
2357 describeLibclangFailure(Err);
2358 clang_disposeTranslationUnit(TU);
2359 return 1;
2360 }
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002361 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002362
2363 if (checkForErrors(TU) != 0)
2364 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002365
2366 for (Loc = 0; Loc < NumLocations; ++Loc) {
2367 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2368 if (!file)
2369 continue;
2370
2371 Cursor = clang_getCursor(TU,
2372 clang_getLocation(TU, file, Locations[Loc].line,
2373 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002374
2375 if (checkForErrors(TU) != 0)
2376 return -1;
2377
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002378 if (I + 1 == Repeats) {
Erik Verbruggen338b55c2011-10-06 11:38:08 +00002379 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002380 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002381 printf("\n");
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002382 clang_findReferencesInFile(Cursor, file, visitor);
2383 free(Locations[Loc].filename);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002384
2385 if (checkForErrors(TU) != 0)
2386 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002387 }
2388 }
2389 }
2390
2391 PrintDiagnostics(TU);
2392 clang_disposeTranslationUnit(TU);
2393 clang_disposeIndex(CIdx);
2394 free(Locations);
2395 free_remapped_files(unsaved_files, num_unsaved_files);
2396 return 0;
2397}
2398
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002399static enum CXVisitorResult findFileIncludesVisit(void *context,
2400 CXCursor cursor, CXSourceRange range) {
2401 PrintCursor(cursor, NULL);
2402 PrintRange(range, "");
2403 printf("\n");
2404 return CXVisit_Continue;
2405}
2406
2407static int find_file_includes_in(int argc, const char **argv) {
2408 CXIndex CIdx;
2409 struct CXUnsavedFile *unsaved_files = 0;
2410 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002411 enum CXErrorCode Err;
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002412 CXTranslationUnit TU;
2413 const char **Filenames = 0;
2414 unsigned NumFilenames = 0;
2415 unsigned Repeats = 1;
2416 unsigned I, FI;
2417
2418 /* Count the number of locations. */
2419 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2420 ++NumFilenames;
2421
2422 /* Parse the locations. */
2423 assert(NumFilenames > 0 && "Unable to count filenames?");
2424 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2425 for (I = 0; I < NumFilenames; ++I) {
2426 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2427 /* Copy the file name. */
2428 Filenames[I] = input;
2429 }
2430
2431 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2432 &num_unsaved_files))
2433 return -1;
2434
2435 if (getenv("CINDEXTEST_EDITING"))
2436 Repeats = 2;
2437
2438 /* Parse the translation unit. When we're testing clang_getCursor() after
2439 reparsing, don't remap unsaved files until the second parse. */
2440 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002441 Err = clang_parseTranslationUnit2(
2442 CIdx, argv[argc - 1],
2443 argv + num_unsaved_files + 1 + NumFilenames,
2444 argc - num_unsaved_files - 2 - NumFilenames,
2445 unsaved_files,
2446 Repeats > 1 ? 0 : num_unsaved_files, getDefaultParsingOptions(), &TU);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002447
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002448 if (Err != CXError_Success) {
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002449 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002450 describeLibclangFailure(Err);
2451 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002452 return -1;
2453 }
2454
2455 if (checkForErrors(TU) != 0)
2456 return -1;
2457
2458 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002459 if (Repeats > 1) {
2460 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2461 clang_defaultReparseOptions(TU));
2462 if (Err != CXError_Success) {
2463 describeLibclangFailure(Err);
2464 clang_disposeTranslationUnit(TU);
2465 return 1;
2466 }
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002467 }
2468
2469 if (checkForErrors(TU) != 0)
2470 return -1;
2471
2472 for (FI = 0; FI < NumFilenames; ++FI) {
2473 CXFile file = clang_getFile(TU, Filenames[FI]);
2474 if (!file)
2475 continue;
2476
2477 if (checkForErrors(TU) != 0)
2478 return -1;
2479
2480 if (I + 1 == Repeats) {
2481 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2482 clang_findIncludesInFile(TU, file, visitor);
2483
2484 if (checkForErrors(TU) != 0)
2485 return -1;
2486 }
2487 }
2488 }
2489
2490 PrintDiagnostics(TU);
2491 clang_disposeTranslationUnit(TU);
2492 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis1b5b1ce2013-03-11 16:03:17 +00002493 free((void *)Filenames);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002494 free_remapped_files(unsaved_files, num_unsaved_files);
2495 return 0;
2496}
2497
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002498#define MAX_IMPORTED_ASTFILES 200
2499
2500typedef struct {
2501 char **filenames;
2502 unsigned num_files;
2503} ImportedASTFilesData;
2504
2505static ImportedASTFilesData *importedASTs_create() {
2506 ImportedASTFilesData *p;
2507 p = malloc(sizeof(ImportedASTFilesData));
2508 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2509 p->num_files = 0;
2510 return p;
2511}
2512
2513static void importedASTs_dispose(ImportedASTFilesData *p) {
2514 unsigned i;
2515 if (!p)
2516 return;
2517
2518 for (i = 0; i < p->num_files; ++i)
2519 free(p->filenames[i]);
2520 free(p->filenames);
2521 free(p);
2522}
2523
2524static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2525 unsigned i;
2526 assert(p && file);
2527 for (i = 0; i < p->num_files; ++i)
2528 if (strcmp(file, p->filenames[i]) == 0)
2529 return;
2530 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2531 p->filenames[p->num_files++] = strdup(file);
2532}
2533
Nico Weberdf686022014-05-07 21:09:42 +00002534typedef struct IndexDataStringList_ {
2535 struct IndexDataStringList_ *next;
2536 char data[1]; /* Dynamically sized. */
2537} IndexDataStringList;
2538
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002539typedef struct {
2540 const char *check_prefix;
2541 int first_check_printed;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002542 int fail_for_error;
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002543 int abort;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002544 const char *main_filename;
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002545 ImportedASTFilesData *importedASTs;
Nico Weberdf686022014-05-07 21:09:42 +00002546 IndexDataStringList *strings;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002547} IndexData;
2548
Nico Weberdf686022014-05-07 21:09:42 +00002549static void free_client_data(IndexData *index_data) {
2550 IndexDataStringList *node = index_data->strings;
2551 while (node) {
2552 IndexDataStringList *next = node->next;
2553 free(node);
2554 node = next;
2555 }
2556 index_data->strings = NULL;
2557}
2558
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002559static void printCheck(IndexData *data) {
2560 if (data->check_prefix) {
2561 if (data->first_check_printed) {
2562 printf("// %s-NEXT: ", data->check_prefix);
2563 } else {
2564 printf("// %s : ", data->check_prefix);
2565 data->first_check_printed = 1;
2566 }
2567 }
2568}
2569
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002570static void printCXIndexFile(CXIdxClientFile file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002571 CXString filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002572 printf("%s", clang_getCString(filename));
2573 clang_disposeString(filename);
2574}
2575
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002576static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2577 IndexData *index_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002578 CXString filename;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002579 const char *cname;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002580 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002581 unsigned line, column;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002582 int isMainFile;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002583
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002584 index_data = (IndexData *)client_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002585 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2586 if (line == 0) {
Argyrios Kyrtzidis9f571862012-10-11 19:00:44 +00002587 printf("<invalid>");
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002588 return;
2589 }
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002590 if (!file) {
2591 printf("<no idxfile>");
2592 return;
2593 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002594 filename = clang_getFileName((CXFile)file);
2595 cname = clang_getCString(filename);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002596 if (strcmp(cname, index_data->main_filename) == 0)
2597 isMainFile = 1;
2598 else
2599 isMainFile = 0;
2600 clang_disposeString(filename);
2601
2602 if (!isMainFile) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002603 printCXIndexFile(file);
2604 printf(":");
2605 }
2606 printf("%d:%d", line, column);
2607}
2608
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002609static unsigned digitCount(unsigned val) {
2610 unsigned c = 1;
2611 while (1) {
2612 if (val < 10)
2613 return c;
2614 ++c;
2615 val /= 10;
2616 }
2617}
2618
Nico Weberdf686022014-05-07 21:09:42 +00002619static CXIdxClientContainer makeClientContainer(CXClientData *client_data,
2620 const CXIdxEntityInfo *info,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002621 CXIdxLoc loc) {
Nico Weberdf686022014-05-07 21:09:42 +00002622 IndexData *index_data;
2623 IndexDataStringList *node;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002624 const char *name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002625 char *newStr;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002626 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002627 unsigned line, column;
2628
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002629 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002630 if (!name)
2631 name = "<anon-tag>";
2632
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002633 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Nico Weberdf686022014-05-07 21:09:42 +00002634
2635 node =
2636 (IndexDataStringList *)malloc(sizeof(IndexDataStringList) + strlen(name) +
2637 digitCount(line) + digitCount(column) + 2);
2638 newStr = node->data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002639 sprintf(newStr, "%s:%d:%d", name, line, column);
Nico Weberdf686022014-05-07 21:09:42 +00002640
2641 /* Remember string so it can be freed later. */
2642 index_data = (IndexData *)client_data;
2643 node->next = index_data->strings;
2644 index_data->strings = node;
2645
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002646 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002647}
2648
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002649static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2650 CXIdxClientContainer container;
2651 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidisdf15c202011-11-16 02:35:05 +00002652 if (!container)
2653 printf("[<<NULL>>]");
2654 else
2655 printf("[%s]", (const char *)container);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002656}
2657
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002658static const char *getEntityKindString(CXIdxEntityKind kind) {
2659 switch (kind) {
2660 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2661 case CXIdxEntity_Typedef: return "typedef";
2662 case CXIdxEntity_Function: return "function";
2663 case CXIdxEntity_Variable: return "variable";
2664 case CXIdxEntity_Field: return "field";
2665 case CXIdxEntity_EnumConstant: return "enumerator";
2666 case CXIdxEntity_ObjCClass: return "objc-class";
2667 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2668 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002669 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2670 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002671 case CXIdxEntity_ObjCProperty: return "objc-property";
2672 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2673 case CXIdxEntity_Enum: return "enum";
2674 case CXIdxEntity_Struct: return "struct";
2675 case CXIdxEntity_Union: return "union";
2676 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002677 case CXIdxEntity_CXXNamespace: return "namespace";
2678 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2679 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2680 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2681 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2682 case CXIdxEntity_CXXConstructor: return "constructor";
2683 case CXIdxEntity_CXXDestructor: return "destructor";
2684 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2685 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikiedcefd952012-08-31 21:55:26 +00002686 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002687 }
2688 assert(0 && "Garbage entity kind");
2689 return 0;
2690}
2691
2692static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2693 switch (kind) {
2694 case CXIdxEntity_NonTemplate: return "";
2695 case CXIdxEntity_Template: return "-template";
2696 case CXIdxEntity_TemplatePartialSpecialization:
2697 return "-template-partial-spec";
2698 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002699 }
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002700 assert(0 && "Garbage entity kind");
2701 return 0;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002702}
2703
Argyrios Kyrtzidis52002882011-12-07 20:44:12 +00002704static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2705 switch (kind) {
2706 case CXIdxEntityLang_None: return "<none>";
2707 case CXIdxEntityLang_C: return "C";
2708 case CXIdxEntityLang_ObjC: return "ObjC";
2709 case CXIdxEntityLang_CXX: return "C++";
2710 }
2711 assert(0 && "Garbage language kind");
2712 return 0;
2713}
2714
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002715static void printEntityInfo(const char *cb,
2716 CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002717 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002718 const char *name;
2719 IndexData *index_data;
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002720 unsigned i;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002721 index_data = (IndexData *)client_data;
2722 printCheck(index_data);
2723
Argyrios Kyrtzidise4acd232011-11-16 02:34:59 +00002724 if (!info) {
2725 printf("%s: <<NULL>>", cb);
2726 return;
2727 }
2728
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002729 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002730 if (!name)
2731 name = "<anon-tag>";
2732
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002733 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2734 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002735 printf(" | name: %s", name);
2736 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002737 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002738
2739 for (i = 0; i != info->numAttributes; ++i) {
2740 const CXIdxAttrInfo *Attr = info->attributes[i];
2741 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002742 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002743 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002744}
2745
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002746static void printBaseClassInfo(CXClientData client_data,
2747 const CXIdxBaseClassInfo *info) {
2748 printEntityInfo(" <base>", client_data, info->base);
2749 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002750 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002751 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002752 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002753}
2754
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002755static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2756 CXClientData client_data) {
2757 unsigned i;
2758 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2759 printEntityInfo(" <protocol>", client_data,
2760 ProtoInfo->protocols[i]->protocol);
2761 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002762 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002763 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002764 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002765 printf("\n");
2766 }
2767}
2768
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002769static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002770 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002771 CXString str;
2772 const char *cstr;
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002773 unsigned numDiags, i;
2774 CXDiagnostic diag;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002775 IndexData *index_data;
2776 index_data = (IndexData *)client_data;
2777 printCheck(index_data);
2778
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002779 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2780 for (i = 0; i != numDiags; ++i) {
2781 diag = clang_getDiagnosticInSet(diagSet, i);
2782 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2783 cstr = clang_getCString(str);
2784 printf("[diagnostic]: %s\n", cstr);
2785 clang_disposeString(str);
2786
2787 if (getenv("CINDEXTEST_FAILONERROR") &&
2788 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2789 index_data->fail_for_error = 1;
2790 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002791 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002792}
2793
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002794static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2795 CXFile file, void *reserved) {
2796 IndexData *index_data;
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002797 CXString filename;
2798
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002799 index_data = (IndexData *)client_data;
2800 printCheck(index_data);
2801
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002802 filename = clang_getFileName(file);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002803 index_data->main_filename = clang_getCString(filename);
2804 clang_disposeString(filename);
2805
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002806 printf("[enteredMainFile]: ");
2807 printCXIndexFile((CXIdxClientFile)file);
2808 printf("\n");
2809
2810 return (CXIdxClientFile)file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002811}
2812
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002813static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002814 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002815 IndexData *index_data;
2816 index_data = (IndexData *)client_data;
2817 printCheck(index_data);
2818
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002819 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002820 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002821 printf(" | name: \"%s\"", info->filename);
2822 printf(" | hash loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002823 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis5e2ec482012-10-18 00:17:05 +00002824 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2825 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002826
2827 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002828}
2829
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002830static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2831 const CXIdxImportedASTFileInfo *info) {
2832 IndexData *index_data;
2833 index_data = (IndexData *)client_data;
2834 printCheck(index_data);
2835
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002836 if (index_data->importedASTs) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002837 CXString filename = clang_getFileName(info->file);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002838 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2839 clang_disposeString(filename);
2840 }
2841
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002842 printf("[importedASTFile]: ");
2843 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002844 if (info->module) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002845 CXString name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002846 printf(" | loc: ");
2847 printCXIndexLoc(info->loc, client_data);
2848 printf(" | name: \"%s\"", clang_getCString(name));
2849 printf(" | isImplicit: %d\n", info->isImplicit);
2850 clang_disposeString(name);
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002851 } else {
NAKAMURA Takumie259d912012-10-12 14:25:52 +00002852 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002853 printf("\n");
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002854 }
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002855
2856 return (CXIdxClientFile)info->file;
2857}
2858
Nico Weber8d19dff2014-05-07 21:05:22 +00002859static CXIdxClientContainer
2860index_startedTranslationUnit(CXClientData client_data, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002861 IndexData *index_data;
2862 index_data = (IndexData *)client_data;
2863 printCheck(index_data);
2864
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002865 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002866 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002867}
2868
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002869static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002870 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002871 IndexData *index_data;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002872 const CXIdxObjCCategoryDeclInfo *CatInfo;
2873 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002874 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002875 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002876 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002877 unsigned i;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002878 index_data = (IndexData *)client_data;
2879
2880 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2881 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002882 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002883 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002884 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis663c8ec2011-12-07 20:44:19 +00002885 printf(" | semantic-container: ");
2886 printCXIndexContainer(info->semanticContainer);
2887 printf(" | lexical-container: ");
2888 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002889 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002890 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002891 if (info->flags & CXIdxDeclFlag_Skipped) {
2892 assert(!info->isContainer);
2893 printf(" | isContainer: skipped");
2894 } else {
2895 printf(" | isContainer: %d", info->isContainer);
2896 }
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002897 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002898
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002899 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi2a4859a2011-11-18 00:51:03 +00002900 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002901 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002902 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002903 printf("\n");
2904 }
2905
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002906 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2907 const char *kindName = 0;
2908 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2909 switch (K) {
2910 case CXIdxObjCContainer_ForwardRef:
2911 kindName = "forward-ref"; break;
2912 case CXIdxObjCContainer_Interface:
2913 kindName = "interface"; break;
2914 case CXIdxObjCContainer_Implementation:
2915 kindName = "implementation"; break;
2916 }
2917 printCheck(index_data);
2918 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2919 }
2920
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002921 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002922 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2923 CatInfo->objcClass);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002924 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002925 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002926 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002927 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002928 printf("\n");
2929 }
2930
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002931 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2932 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002933 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002934 printf("\n");
2935 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002936 }
2937
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002938 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2939 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002940 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002941
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002942 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2943 if (PropInfo->getter) {
2944 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2945 printf("\n");
2946 }
2947 if (PropInfo->setter) {
2948 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2949 printf("\n");
2950 }
2951 }
2952
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002953 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2954 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2955 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2956 printf("\n");
2957 }
2958 }
2959
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002960 if (info->declAsContainer)
Nico Weber8d19dff2014-05-07 21:05:22 +00002961 clang_index_setClientContainer(
2962 info->declAsContainer,
Nico Weberdf686022014-05-07 21:09:42 +00002963 makeClientContainer(client_data, info->entityInfo, info->loc));
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002964}
2965
2966static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002967 const CXIdxEntityRefInfo *info) {
Nico Weber8d19dff2014-05-07 21:05:22 +00002968 printEntityInfo("[indexEntityReference]", client_data,
2969 info->referencedEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002970 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002971 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002972 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002973 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002974 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002975 printf(" | container: ");
2976 printCXIndexContainer(info->container);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002977 printf(" | refkind: ");
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002978 switch (info->kind) {
2979 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002980 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002981 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002982 printf("\n");
2983}
2984
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002985static int index_abortQuery(CXClientData client_data, void *reserved) {
2986 IndexData *index_data;
2987 index_data = (IndexData *)client_data;
2988 return index_data->abort;
2989}
2990
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002991static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002992 index_abortQuery,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002993 index_diagnostic,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002994 index_enteredMainFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002995 index_ppIncludedFile,
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002996 index_importedASTFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002997 index_startedTranslationUnit,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002998 index_indexDeclaration,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002999 index_indexEntityReference
3000};
3001
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00003002static unsigned getIndexOptions(void) {
3003 unsigned index_opts;
3004 index_opts = 0;
3005 if (getenv("CINDEXTEST_SUPPRESSREFS"))
3006 index_opts |= CXIndexOpt_SuppressRedundantRefs;
3007 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
3008 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00003009 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
3010 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00003011
3012 return index_opts;
3013}
3014
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003015static int index_compile_args(int num_args, const char **args,
3016 CXIndexAction idxAction,
3017 ImportedASTFilesData *importedASTs,
3018 const char *check_prefix) {
3019 IndexData index_data;
3020 unsigned index_opts;
3021 int result;
3022
3023 if (num_args == 0) {
3024 fprintf(stderr, "no compiler arguments\n");
3025 return -1;
3026 }
3027
3028 index_data.check_prefix = check_prefix;
3029 index_data.first_check_printed = 0;
3030 index_data.fail_for_error = 0;
3031 index_data.abort = 0;
3032 index_data.main_filename = "";
3033 index_data.importedASTs = importedASTs;
Nico Weberdf686022014-05-07 21:09:42 +00003034 index_data.strings = NULL;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003035
3036 index_opts = getIndexOptions();
3037 result = clang_indexSourceFile(idxAction, &index_data,
3038 &IndexCB,sizeof(IndexCB), index_opts,
3039 0, args, num_args, 0, 0, 0,
3040 getDefaultParsingOptions());
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003041 if (result != CXError_Success)
3042 describeLibclangFailure(result);
3043
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003044 if (index_data.fail_for_error)
3045 result = -1;
3046
Nico Weberdf686022014-05-07 21:09:42 +00003047 free_client_data(&index_data);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003048 return result;
3049}
3050
3051static int index_ast_file(const char *ast_file,
3052 CXIndex Idx,
3053 CXIndexAction idxAction,
3054 ImportedASTFilesData *importedASTs,
3055 const char *check_prefix) {
3056 CXTranslationUnit TU;
3057 IndexData index_data;
3058 unsigned index_opts;
3059 int result;
3060
3061 if (!CreateTranslationUnit(Idx, ast_file, &TU))
3062 return -1;
3063
3064 index_data.check_prefix = check_prefix;
3065 index_data.first_check_printed = 0;
3066 index_data.fail_for_error = 0;
3067 index_data.abort = 0;
3068 index_data.main_filename = "";
3069 index_data.importedASTs = importedASTs;
Nico Weberdf686022014-05-07 21:09:42 +00003070 index_data.strings = NULL;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003071
3072 index_opts = getIndexOptions();
3073 result = clang_indexTranslationUnit(idxAction, &index_data,
3074 &IndexCB,sizeof(IndexCB),
3075 index_opts, TU);
3076 if (index_data.fail_for_error)
3077 result = -1;
3078
3079 clang_disposeTranslationUnit(TU);
Nico Weberdf686022014-05-07 21:09:42 +00003080 free_client_data(&index_data);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003081 return result;
3082}
3083
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003084static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003085 const char *check_prefix;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003086 CXIndex Idx;
3087 CXIndexAction idxAction;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003088 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003089 int result;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003090
3091 check_prefix = 0;
3092 if (argc > 0) {
3093 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3094 check_prefix = argv[0] + strlen("-check-prefix=");
3095 ++argv;
3096 --argc;
3097 }
3098 }
3099
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003100 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003101 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003102 fprintf(stderr, "Could not create Index\n");
3103 return 1;
3104 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003105 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003106 importedASTs = 0;
3107 if (full)
3108 importedASTs = importedASTs_create();
3109
3110 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
3111 if (result != 0)
3112 goto finished;
3113
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003114 if (full) {
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003115 unsigned i;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003116 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
3117 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
3118 importedASTs, check_prefix);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003119 }
3120 }
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003121
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003122finished:
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003123 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003124 clang_IndexAction_dispose(idxAction);
3125 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003126 return result;
3127}
3128
3129static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003130 const char *check_prefix;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003131 CXIndex Idx;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003132 CXIndexAction idxAction;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003133 int result;
3134
3135 check_prefix = 0;
3136 if (argc > 0) {
3137 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3138 check_prefix = argv[0] + strlen("-check-prefix=");
3139 ++argv;
3140 --argc;
3141 }
3142 }
3143
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003144 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003145 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003146 fprintf(stderr, "Could not create Index\n");
3147 return 1;
3148 }
3149 idxAction = clang_IndexAction_create(Idx);
3150
3151 result = index_ast_file(argv[0], Idx, idxAction,
3152 /*importedASTs=*/0, check_prefix);
3153
3154 clang_IndexAction_dispose(idxAction);
3155 clang_disposeIndex(Idx);
3156 return result;
3157}
3158
3159static int index_compile_db(int argc, const char **argv) {
3160 const char *check_prefix;
3161 CXIndex Idx;
3162 CXIndexAction idxAction;
3163 int errorCode = 0;
3164
3165 check_prefix = 0;
3166 if (argc > 0) {
3167 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3168 check_prefix = argv[0] + strlen("-check-prefix=");
3169 ++argv;
3170 --argc;
3171 }
3172 }
3173
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003174 if (argc == 0) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003175 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003176 return -1;
3177 }
3178
3179 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003180 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +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 Kyrtzidis4c910b12011-11-22 07:24:51 +00003185
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003186 {
3187 const char *database = argv[0];
3188 CXCompilationDatabase db = 0;
3189 CXCompileCommands CCmds = 0;
3190 CXCompileCommand CCmd;
3191 CXCompilationDatabase_Error ec;
3192 CXString wd;
3193#define MAX_COMPILE_ARGS 512
3194 CXString cxargs[MAX_COMPILE_ARGS];
3195 const char *args[MAX_COMPILE_ARGS];
3196 char *tmp;
3197 unsigned len;
3198 char *buildDir;
3199 int i, a, numCmds, numArgs;
3200
3201 len = strlen(database);
3202 tmp = (char *) malloc(len+1);
3203 memcpy(tmp, database, len+1);
3204 buildDir = dirname(tmp);
3205
3206 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3207
3208 if (db) {
3209
3210 if (ec!=CXCompilationDatabase_NoError) {
3211 printf("unexpected error %d code while loading compilation database\n", ec);
3212 errorCode = -1;
3213 goto cdb_end;
3214 }
3215
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003216 if (chdir(buildDir) != 0) {
3217 printf("Could not chdir to %s\n", buildDir);
3218 errorCode = -1;
3219 goto cdb_end;
3220 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003221
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003222 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003223 if (!CCmds) {
3224 printf("compilation db is empty\n");
3225 errorCode = -1;
3226 goto cdb_end;
3227 }
3228
3229 numCmds = clang_CompileCommands_getSize(CCmds);
3230
3231 if (numCmds==0) {
3232 fprintf(stderr, "should not get an empty compileCommand set\n");
3233 errorCode = -1;
3234 goto cdb_end;
3235 }
3236
3237 for (i=0; i<numCmds && errorCode == 0; ++i) {
3238 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3239
3240 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003241 if (chdir(clang_getCString(wd)) != 0) {
3242 printf("Could not chdir to %s\n", clang_getCString(wd));
3243 errorCode = -1;
3244 goto cdb_end;
3245 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003246 clang_disposeString(wd);
3247
3248 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3249 if (numArgs > MAX_COMPILE_ARGS){
3250 fprintf(stderr, "got more compile arguments than maximum\n");
3251 errorCode = -1;
3252 goto cdb_end;
3253 }
3254 for (a=0; a<numArgs; ++a) {
3255 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3256 args[a] = clang_getCString(cxargs[a]);
3257 }
3258
3259 errorCode = index_compile_args(numArgs, args, idxAction,
3260 /*importedASTs=*/0, check_prefix);
3261
3262 for (a=0; a<numArgs; ++a)
3263 clang_disposeString(cxargs[a]);
3264 }
3265 } else {
3266 printf("database loading failed with error code %d.\n", ec);
3267 errorCode = -1;
3268 }
3269
3270 cdb_end:
3271 clang_CompileCommands_dispose(CCmds);
3272 clang_CompilationDatabase_dispose(db);
3273 free(tmp);
3274
3275 }
3276
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003277 clang_IndexAction_dispose(idxAction);
3278 clang_disposeIndex(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003279 return errorCode;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003280}
3281
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003282int perform_token_annotation(int argc, const char **argv) {
3283 const char *input = argv[1];
3284 char *filename = 0;
3285 unsigned line, second_line;
3286 unsigned column, second_column;
3287 CXIndex CIdx;
3288 CXTranslationUnit TU = 0;
3289 int errorCode;
3290 struct CXUnsavedFile *unsaved_files = 0;
3291 int num_unsaved_files = 0;
3292 CXToken *tokens;
3293 unsigned num_tokens;
3294 CXSourceRange range;
3295 CXSourceLocation startLoc, endLoc;
3296 CXFile file = 0;
3297 CXCursor *cursors = 0;
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003298 CXSourceRangeList *skipped_ranges = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003299 enum CXErrorCode Err;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003300 unsigned i;
3301
3302 input += strlen("-test-annotate-tokens=");
3303 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3304 &second_line, &second_column)))
3305 return errorCode;
3306
Richard Smith1ea42eb2012-07-05 08:20:49 +00003307 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3308 free(filename);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003309 return -1;
Richard Smith1ea42eb2012-07-05 08:20:49 +00003310 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003311
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003312 CIdx = clang_createIndex(0, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003313 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
3314 argv + num_unsaved_files + 2,
3315 argc - num_unsaved_files - 3,
3316 unsaved_files,
3317 num_unsaved_files,
3318 getDefaultParsingOptions(), &TU);
3319 if (Err != CXError_Success) {
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003320 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003321 describeLibclangFailure(Err);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003322 clang_disposeIndex(CIdx);
3323 free(filename);
3324 free_remapped_files(unsaved_files, num_unsaved_files);
3325 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003326 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003327 errorCode = 0;
3328
Richard Smith1ea42eb2012-07-05 08:20:49 +00003329 if (checkForErrors(TU) != 0) {
3330 errorCode = -1;
3331 goto teardown;
3332 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003333
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003334 if (getenv("CINDEXTEST_EDITING")) {
3335 for (i = 0; i < 5; ++i) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003336 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3337 clang_defaultReparseOptions(TU));
3338 if (Err != CXError_Success) {
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003339 fprintf(stderr, "Unable to reparse translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003340 describeLibclangFailure(Err);
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003341 errorCode = -1;
3342 goto teardown;
3343 }
3344 }
3345 }
3346
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003347 if (checkForErrors(TU) != 0) {
3348 errorCode = -1;
3349 goto teardown;
3350 }
3351
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003352 file = clang_getFile(TU, filename);
3353 if (!file) {
3354 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3355 errorCode = -1;
3356 goto teardown;
3357 }
3358
3359 startLoc = clang_getLocation(TU, file, line, column);
3360 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003361 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003362 column);
3363 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003364 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003365 }
3366
3367 endLoc = clang_getLocation(TU, file, second_line, second_column);
3368 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003369 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003370 second_line, second_column);
3371 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003372 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003373 }
3374
3375 range = clang_getRange(startLoc, endLoc);
3376 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003377
3378 if (checkForErrors(TU) != 0) {
3379 errorCode = -1;
3380 goto teardown;
3381 }
3382
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003383 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3384 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003385
3386 if (checkForErrors(TU) != 0) {
3387 errorCode = -1;
3388 goto teardown;
3389 }
3390
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003391 skipped_ranges = clang_getSkippedRanges(TU, file);
3392 for (i = 0; i != skipped_ranges->count; ++i) {
3393 unsigned start_line, start_column, end_line, end_column;
3394 clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
3395 0, &start_line, &start_column, 0);
3396 clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
3397 0, &end_line, &end_column, 0);
3398 printf("Skipping: ");
3399 PrintExtent(stdout, start_line, start_column, end_line, end_column);
3400 printf("\n");
3401 }
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003402 clang_disposeSourceRangeList(skipped_ranges);
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003403
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003404 for (i = 0; i != num_tokens; ++i) {
3405 const char *kind = "<unknown>";
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003406 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3407 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003408 unsigned start_line, start_column, end_line, end_column;
3409
3410 switch (clang_getTokenKind(tokens[i])) {
3411 case CXToken_Punctuation: kind = "Punctuation"; break;
3412 case CXToken_Keyword: kind = "Keyword"; break;
3413 case CXToken_Identifier: kind = "Identifier"; break;
3414 case CXToken_Literal: kind = "Literal"; break;
3415 case CXToken_Comment: kind = "Comment"; break;
3416 }
Douglas Gregor229bebd2010-11-09 06:24:54 +00003417 clang_getSpellingLocation(clang_getRangeStart(extent),
3418 0, &start_line, &start_column, 0);
3419 clang_getSpellingLocation(clang_getRangeEnd(extent),
3420 0, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003421 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Krameraf7ae312012-04-14 09:11:51 +00003422 clang_disposeString(spelling);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003423 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor61656112010-01-26 18:31:56 +00003424 if (!clang_isInvalid(cursors[i].kind)) {
3425 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003426 PrintCursor(cursors[i], NULL);
Douglas Gregor61656112010-01-26 18:31:56 +00003427 }
3428 printf("\n");
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003429 }
3430 free(cursors);
Ted Kremenek983fb5de2010-10-20 21:22:15 +00003431 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003432
3433 teardown:
Douglas Gregor33cdd812010-02-18 18:08:43 +00003434 PrintDiagnostics(TU);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003435 clang_disposeTranslationUnit(TU);
3436 clang_disposeIndex(CIdx);
3437 free(filename);
3438 free_remapped_files(unsaved_files, num_unsaved_files);
3439 return errorCode;
3440}
3441
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003442static int
3443perform_test_compilation_db(const char *database, int argc, const char **argv) {
3444 CXCompilationDatabase db;
3445 CXCompileCommands CCmds;
3446 CXCompileCommand CCmd;
3447 CXCompilationDatabase_Error ec;
3448 CXString wd;
3449 CXString arg;
3450 int errorCode = 0;
3451 char *tmp;
3452 unsigned len;
3453 char *buildDir;
3454 int i, j, a, numCmds, numArgs;
3455
3456 len = strlen(database);
3457 tmp = (char *) malloc(len+1);
3458 memcpy(tmp, database, len+1);
3459 buildDir = dirname(tmp);
3460
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003461 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003462
3463 if (db) {
3464
3465 if (ec!=CXCompilationDatabase_NoError) {
3466 printf("unexpected error %d code while loading compilation database\n", ec);
3467 errorCode = -1;
3468 goto cdb_end;
3469 }
3470
3471 for (i=0; i<argc && errorCode==0; ) {
3472 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003473 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003474
3475 if (!CCmds) {
3476 printf("file %s not found in compilation db\n", argv[i+1]);
3477 errorCode = -1;
3478 break;
3479 }
3480
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003481 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003482
3483 if (numCmds==0) {
3484 fprintf(stderr, "should not get an empty compileCommand set for file"
3485 " '%s'\n", argv[i+1]);
3486 errorCode = -1;
3487 break;
3488 }
3489
3490 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003491 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003492
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003493 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003494 printf("workdir:'%s'", clang_getCString(wd));
3495 clang_disposeString(wd);
3496
3497 printf(" cmdline:'");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003498 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003499 for (a=0; a<numArgs; ++a) {
3500 if (a) printf(" ");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003501 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003502 printf("%s", clang_getCString(arg));
3503 clang_disposeString(arg);
3504 }
3505 printf("'\n");
3506 }
3507
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003508 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003509
3510 i += 2;
3511 }
3512 }
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003513 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003514 } else {
3515 printf("database loading failed with error code %d.\n", ec);
3516 errorCode = -1;
3517 }
3518
3519cdb_end:
3520 free(tmp);
3521
3522 return errorCode;
3523}
3524
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003525/******************************************************************************/
Ted Kremenek599d73a2010-03-25 02:00:39 +00003526/* USR printing. */
3527/******************************************************************************/
3528
3529static int insufficient_usr(const char *kind, const char *usage) {
3530 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3531 return 1;
3532}
3533
3534static unsigned isUSR(const char *s) {
3535 return s[0] == 'c' && s[1] == ':';
3536}
3537
3538static int not_usr(const char *s, const char *arg) {
3539 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3540 return 1;
3541}
3542
3543static void print_usr(CXString usr) {
3544 const char *s = clang_getCString(usr);
3545 printf("%s\n", s);
3546 clang_disposeString(usr);
3547}
3548
3549static void display_usrs() {
3550 fprintf(stderr, "-print-usrs options:\n"
3551 " ObjCCategory <class name> <category name>\n"
3552 " ObjCClass <class name>\n"
3553 " ObjCIvar <ivar name> <class USR>\n"
3554 " ObjCMethod <selector> [0=class method|1=instance method] "
3555 "<class USR>\n"
3556 " ObjCProperty <property name> <class USR>\n"
3557 " ObjCProtocol <protocol name>\n");
3558}
3559
3560int print_usrs(const char **I, const char **E) {
3561 while (I != E) {
3562 const char *kind = *I;
3563 unsigned len = strlen(kind);
3564 switch (len) {
3565 case 8:
3566 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3567 if (I + 2 >= E)
3568 return insufficient_usr(kind, "<ivar name> <class USR>");
3569 if (!isUSR(I[2]))
3570 return not_usr("<class USR>", I[2]);
3571 else {
3572 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003573 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003574 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003575 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3576 }
3577
3578 I += 3;
3579 continue;
3580 }
3581 break;
3582 case 9:
3583 if (memcmp(kind, "ObjCClass", 9) == 0) {
3584 if (I + 1 >= E)
3585 return insufficient_usr(kind, "<class name>");
3586 print_usr(clang_constructUSR_ObjCClass(I[1]));
3587 I += 2;
3588 continue;
3589 }
3590 break;
3591 case 10:
3592 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3593 if (I + 3 >= E)
3594 return insufficient_usr(kind, "<method selector> "
3595 "[0=class method|1=instance method] <class USR>");
3596 if (!isUSR(I[3]))
3597 return not_usr("<class USR>", I[3]);
3598 else {
3599 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003600 x.data = (void*) I[3];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003601 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003602 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3603 }
3604 I += 4;
3605 continue;
3606 }
3607 break;
3608 case 12:
3609 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3610 if (I + 2 >= E)
3611 return insufficient_usr(kind, "<class name> <category name>");
3612 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3613 I += 3;
3614 continue;
3615 }
3616 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3617 if (I + 1 >= E)
3618 return insufficient_usr(kind, "<protocol name>");
3619 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3620 I += 2;
3621 continue;
3622 }
3623 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3624 if (I + 2 >= E)
3625 return insufficient_usr(kind, "<property name> <class USR>");
3626 if (!isUSR(I[2]))
3627 return not_usr("<class USR>", I[2]);
3628 else {
3629 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003630 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003631 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003632 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3633 }
3634 I += 3;
3635 continue;
3636 }
3637 break;
3638 default:
3639 break;
3640 }
3641 break;
3642 }
3643
3644 if (I != E) {
3645 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3646 display_usrs();
3647 return 1;
3648 }
3649 return 0;
3650}
3651
3652int print_usrs_file(const char *file_name) {
3653 char line[2048];
3654 const char *args[128];
3655 unsigned numChars = 0;
3656
3657 FILE *fp = fopen(file_name, "r");
3658 if (!fp) {
3659 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3660 return 1;
3661 }
3662
3663 /* This code is not really all that safe, but it works fine for testing. */
3664 while (!feof(fp)) {
3665 char c = fgetc(fp);
3666 if (c == '\n') {
3667 unsigned i = 0;
3668 const char *s = 0;
3669
3670 if (numChars == 0)
3671 continue;
3672
3673 line[numChars] = '\0';
3674 numChars = 0;
3675
3676 if (line[0] == '/' && line[1] == '/')
3677 continue;
3678
3679 s = strtok(line, " ");
3680 while (s) {
3681 args[i] = s;
3682 ++i;
3683 s = strtok(0, " ");
3684 }
3685 if (print_usrs(&args[0], &args[i]))
3686 return 1;
3687 }
3688 else
3689 line[numChars++] = c;
3690 }
3691
3692 fclose(fp);
3693 return 0;
3694}
3695
3696/******************************************************************************/
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003697/* Command line processing. */
3698/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003699int write_pch_file(const char *filename, int argc, const char *argv[]) {
3700 CXIndex Idx;
3701 CXTranslationUnit TU;
3702 struct CXUnsavedFile *unsaved_files = 0;
3703 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003704 enum CXErrorCode Err;
Francois Pichetabcfbec2011-07-06 22:09:44 +00003705 int result = 0;
Douglas Gregore9386682010-08-13 05:36:37 +00003706
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003707 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregore9386682010-08-13 05:36:37 +00003708
3709 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3710 clang_disposeIndex(Idx);
3711 return -1;
3712 }
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003713
3714 Err = clang_parseTranslationUnit2(
3715 Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files,
3716 unsaved_files, num_unsaved_files,
3717 CXTranslationUnit_Incomplete |
3718 CXTranslationUnit_DetailedPreprocessingRecord |
3719 CXTranslationUnit_ForSerialization,
3720 &TU);
3721 if (Err != CXError_Success) {
Douglas Gregore9386682010-08-13 05:36:37 +00003722 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003723 describeLibclangFailure(Err);
Douglas Gregore9386682010-08-13 05:36:37 +00003724 free_remapped_files(unsaved_files, num_unsaved_files);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003725 clang_disposeTranslationUnit(TU);
Douglas Gregore9386682010-08-13 05:36:37 +00003726 clang_disposeIndex(Idx);
3727 return 1;
3728 }
3729
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003730 switch (clang_saveTranslationUnit(TU, filename,
3731 clang_defaultSaveOptions(TU))) {
3732 case CXSaveError_None:
3733 break;
3734
3735 case CXSaveError_TranslationErrors:
3736 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3737 filename);
3738 result = 2;
3739 break;
3740
3741 case CXSaveError_InvalidTU:
3742 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3743 filename);
3744 result = 3;
3745 break;
3746
3747 case CXSaveError_Unknown:
3748 default:
3749 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3750 result = 1;
3751 break;
3752 }
3753
Douglas Gregore9386682010-08-13 05:36:37 +00003754 clang_disposeTranslationUnit(TU);
3755 free_remapped_files(unsaved_files, num_unsaved_files);
3756 clang_disposeIndex(Idx);
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003757 return result;
Douglas Gregore9386682010-08-13 05:36:37 +00003758}
3759
3760/******************************************************************************/
Ted Kremenekd010ba42011-11-10 08:43:12 +00003761/* Serialized diagnostics. */
3762/******************************************************************************/
3763
3764static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3765 switch (error) {
3766 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3767 case CXLoadDiag_None: break;
3768 case CXLoadDiag_Unknown: return "Unknown";
3769 case CXLoadDiag_InvalidFile: return "Invalid File";
3770 }
3771 return "None";
3772}
3773
3774static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3775 switch (severity) {
3776 case CXDiagnostic_Note: return "note";
3777 case CXDiagnostic_Error: return "error";
3778 case CXDiagnostic_Fatal: return "fatal";
3779 case CXDiagnostic_Ignored: return "ignored";
3780 case CXDiagnostic_Warning: return "warning";
3781 }
3782 return "unknown";
3783}
3784
3785static void printIndent(unsigned indent) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003786 if (indent == 0)
3787 return;
3788 fprintf(stderr, "+");
3789 --indent;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003790 while (indent > 0) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003791 fprintf(stderr, "-");
Ted Kremenekd010ba42011-11-10 08:43:12 +00003792 --indent;
3793 }
3794}
3795
3796static void printLocation(CXSourceLocation L) {
3797 CXFile File;
3798 CXString FileName;
3799 unsigned line, column, offset;
3800
3801 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3802 FileName = clang_getFileName(File);
3803
3804 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3805 clang_disposeString(FileName);
3806}
3807
3808static void printRanges(CXDiagnostic D, unsigned indent) {
3809 unsigned i, n = clang_getDiagnosticNumRanges(D);
3810
3811 for (i = 0; i < n; ++i) {
3812 CXSourceLocation Start, End;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003813 CXSourceRange SR = clang_getDiagnosticRange(D, i);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003814 Start = clang_getRangeStart(SR);
3815 End = clang_getRangeEnd(SR);
3816
3817 printIndent(indent);
3818 fprintf(stderr, "Range: ");
3819 printLocation(Start);
3820 fprintf(stderr, " ");
3821 printLocation(End);
3822 fprintf(stderr, "\n");
3823 }
3824}
3825
3826static void printFixIts(CXDiagnostic D, unsigned indent) {
3827 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek4a642302012-03-20 20:49:45 +00003828 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003829 for (i = 0 ; i < n; ++i) {
3830 CXSourceRange ReplacementRange;
3831 CXString text;
3832 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3833
3834 printIndent(indent);
3835 fprintf(stderr, "FIXIT: (");
3836 printLocation(clang_getRangeStart(ReplacementRange));
3837 fprintf(stderr, " - ");
3838 printLocation(clang_getRangeEnd(ReplacementRange));
3839 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3840 clang_disposeString(text);
3841 }
3842}
3843
3844static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003845 unsigned i, n;
3846
Ted Kremenekd010ba42011-11-10 08:43:12 +00003847 if (!Diags)
3848 return;
3849
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003850 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003851 for (i = 0; i < n; ++i) {
3852 CXSourceLocation DiagLoc;
3853 CXDiagnostic D;
3854 CXFile File;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003855 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003856 unsigned line, column, offset;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003857 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003858
3859 D = clang_getDiagnosticInSet(Diags, i);
3860 DiagLoc = clang_getDiagnosticLocation(D);
3861 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3862 FileName = clang_getFileName(File);
3863 DiagSpelling = clang_getDiagnosticSpelling(D);
3864
3865 printIndent(indent);
3866
3867 fprintf(stderr, "%s:%d:%d: %s: %s",
3868 clang_getCString(FileName),
3869 line,
3870 column,
3871 getSeverityString(clang_getDiagnosticSeverity(D)),
3872 clang_getCString(DiagSpelling));
3873
3874 DiagOption = clang_getDiagnosticOption(D, 0);
3875 DiagOptionStr = clang_getCString(DiagOption);
3876 if (DiagOptionStr) {
3877 fprintf(stderr, " [%s]", DiagOptionStr);
3878 }
3879
Ted Kremenek26a6d492012-04-12 00:03:31 +00003880 DiagCat = clang_getDiagnosticCategoryText(D);
3881 DiagCatStr = clang_getCString(DiagCat);
3882 if (DiagCatStr) {
3883 fprintf(stderr, " [%s]", DiagCatStr);
3884 }
3885
Ted Kremenekd010ba42011-11-10 08:43:12 +00003886 fprintf(stderr, "\n");
3887
3888 printRanges(D, indent);
3889 printFixIts(D, indent);
3890
NAKAMURA Takumi27dd3962011-11-10 10:07:57 +00003891 /* Print subdiagnostics. */
Ted Kremenekd010ba42011-11-10 08:43:12 +00003892 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3893
3894 clang_disposeString(FileName);
3895 clang_disposeString(DiagSpelling);
3896 clang_disposeString(DiagOption);
Nico Weberce5528a2014-05-11 17:16:59 +00003897 clang_disposeString(DiagCat);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003898 }
3899}
3900
3901static int read_diagnostics(const char *filename) {
3902 enum CXLoadDiag_Error error;
3903 CXString errorString;
3904 CXDiagnosticSet Diags = 0;
3905
3906 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3907 if (!Diags) {
3908 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3909 getDiagnosticCodeStr(error),
3910 clang_getCString(errorString));
3911 clang_disposeString(errorString);
3912 return 1;
3913 }
3914
3915 printDiagnosticSet(Diags, 0);
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003916 fprintf(stderr, "Number of diagnostics: %d\n",
3917 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenekd010ba42011-11-10 08:43:12 +00003918 clang_disposeDiagnosticSet(Diags);
3919 return 0;
3920}
3921
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003922static int perform_print_build_session_timestamp(void) {
3923 printf("%lld\n", clang_getBuildSessionTimestamp());
3924 return 0;
3925}
3926
Ted Kremenekd010ba42011-11-10 08:43:12 +00003927/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003928/* Command line processing. */
3929/******************************************************************************/
Ted Kremenekef3339b2009-11-17 18:09:14 +00003930
Douglas Gregor720d0052010-01-20 21:32:04 +00003931static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003932 if (s[0] == '\0')
Douglas Gregor720d0052010-01-20 21:32:04 +00003933 return FilteredPrintingVisitor;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003934 if (strcmp(s, "-usrs") == 0)
3935 return USRVisitor;
Ted Kremenek83f642e2011-04-18 22:47:10 +00003936 if (strncmp(s, "-memory-usage", 13) == 0)
3937 return GetVisitor(s + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003938 return NULL;
3939}
3940
Ted Kremenekef3339b2009-11-17 18:09:14 +00003941static void print_usage(void) {
3942 fprintf(stderr,
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003943 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003944 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregor082c3e62010-01-15 19:40:17 +00003945 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003946 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3947 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi4deb9a92012-10-24 22:52:04 +00003948 fprintf(stderr,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003949 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003950 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003951 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003952 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek0469b7e2009-11-18 02:02:52 +00003953 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen338b55c2011-10-06 11:38:08 +00003954 "[FileCheck prefix]\n");
3955 fprintf(stderr,
Ted Kremeneka44d99c2010-01-05 23:18:49 +00003956 " c-index-test -test-load-tu <AST file> <symbol filter> "
3957 "[FileCheck prefix]\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003958 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3959 "[FileCheck prefix]\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003960 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregor082c3e62010-01-15 19:40:17 +00003961 fprintf(stderr,
Ted Kremenek83f642e2011-04-18 22:47:10 +00003962 " c-index-test -test-load-source-memory-usage "
3963 "<symbol filter> {<args>}*\n"
Douglas Gregoraa21cc42010-07-19 21:46:24 +00003964 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3965 " {<args>}*\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003966 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003967 " c-index-test -test-load-source-usrs-memory-usage "
3968 "<symbol filter> {<args>}*\n"
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00003969 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3970 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek11d1a422011-04-18 23:42:53 +00003971 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth718df592010-07-22 06:29:13 +00003972 fprintf(stderr,
Ted Kremenek11d1a422011-04-18 23:42:53 +00003973 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenko00353722013-02-15 21:15:49 +00003974 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00003975 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00003976 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003977 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregore9386682010-08-13 05:36:37 +00003978 " c-index-test -print-usr-file <file>\n"
Ted Kremenekd010ba42011-11-10 08:43:12 +00003979 " c-index-test -write-pch <file> <compiler arguments>\n");
3980 fprintf(stderr,
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003981 " c-index-test -compilation-db [lookup <filename>] database\n");
3982 fprintf(stderr,
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003983 " c-index-test -print-build-session-timestamp\n");
3984 fprintf(stderr,
Ted Kremenekd010ba42011-11-10 08:43:12 +00003985 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregor73a18fd2010-07-20 14:34:35 +00003986 fprintf(stderr,
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003987 " <symbol filter> values:\n%s",
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003988 " all - load all symbols, including those from PCH\n"
3989 " local - load all symbols except those in PCH\n"
3990 " category - only load ObjC categories (non-PCH)\n"
3991 " interface - only load ObjC interfaces (non-PCH)\n"
3992 " protocol - only load ObjC protocols (non-PCH)\n"
3993 " function - only load functions (non-PCH)\n"
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00003994 " typedef - only load typdefs (non-PCH)\n"
3995 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekef3339b2009-11-17 18:09:14 +00003996}
3997
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003998/***/
3999
4000int cindextest_main(int argc, const char **argv) {
Douglas Gregor1e21cc72010-02-18 23:07:20 +00004001 clang_enableStackTraces();
Ted Kremenekd010ba42011-11-10 08:43:12 +00004002 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
4003 return read_diagnostics(argv[2]);
Ted Kremenekef3339b2009-11-17 18:09:14 +00004004 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor47815d52010-07-12 18:38:41 +00004005 return perform_code_completion(argc, argv, 0);
4006 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
4007 return perform_code_completion(argc, argv, 1);
Douglas Gregor082c3e62010-01-15 19:40:17 +00004008 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
4009 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00004010 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
4011 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00004012 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
4013 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00004014 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00004015 return index_file(argc - 2, argv + 2, /*full=*/0);
4016 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
4017 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00004018 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
4019 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00004020 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
4021 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004022 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00004023 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004024 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00004025 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
4026 NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004027 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00004028 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
4029 CXCursorVisitor I = GetVisitor(argv[1] + 25);
4030 if (I) {
4031 int trials = atoi(argv[2]);
4032 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
4033 NULL);
4034 }
4035 }
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004036 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00004037 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek83f642e2011-04-18 22:47:10 +00004038
4039 PostVisitTU postVisit = 0;
4040 if (strstr(argv[1], "-memory-usage"))
4041 postVisit = PrintMemoryUsage;
4042
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004043 if (I)
Ted Kremenek83f642e2011-04-18 22:47:10 +00004044 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
4045 postVisit);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004046 }
4047 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek0469b7e2009-11-18 02:02:52 +00004048 return perform_file_scan(argv[2], argv[3],
4049 argc >= 5 ? argv[4] : 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00004050 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
4051 return perform_token_annotation(argc, argv);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00004052 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
4053 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
4054 PrintInclusionStack);
4055 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
4056 return perform_test_load_tu(argv[2], "all", NULL, NULL,
4057 PrintInclusionStack);
Ted Kremenek83b28a22010-03-03 06:37:58 +00004058 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
4059 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
4060 NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00004061 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek6bca9842010-05-14 21:29:26 +00004062 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenko00353722013-02-15 21:15:49 +00004063 PrintType, 0);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00004064 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
4065 return perform_test_load_source(argc - 2, argv + 2, "all",
4066 PrintTypeSize, 0);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00004067 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
4068 return perform_test_load_source(argc - 2, argv + 2, "all",
4069 PrintBitWidth, 0);
Ted Kremenek599d73a2010-03-25 02:00:39 +00004070 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
4071 if (argc > 2)
4072 return print_usrs(argv + 2, argv + argc);
4073 else {
4074 display_usrs();
4075 return 1;
4076 }
4077 }
4078 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
4079 return print_usrs_file(argv[2]);
Douglas Gregore9386682010-08-13 05:36:37 +00004080 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
4081 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004082 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
4083 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004084 else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0)
4085 return perform_print_build_session_timestamp();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004086
Ted Kremenekef3339b2009-11-17 18:09:14 +00004087 print_usage();
4088 return 1;
Steve Naroffa1c72842009-08-28 15:28:48 +00004089}
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004090
4091/***/
4092
4093/* We intentionally run in a separate thread to ensure we at least minimal
4094 * testing of a multithreaded environment (for example, having a reduced stack
4095 * size). */
4096
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004097typedef struct thread_info {
4098 int argc;
4099 const char **argv;
4100 int result;
4101} thread_info;
Benjamin Kramer112fc6c2010-11-04 19:11:31 +00004102void thread_runner(void *client_data_v) {
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004103 thread_info *client_data = client_data_v;
4104 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumia7d49882012-04-07 06:59:28 +00004105#ifdef __CYGWIN__
4106 fflush(stdout); /* stdout is not flushed on Cygwin. */
4107#endif
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004108}
4109
4110int main(int argc, const char **argv) {
Benjamin Kramer3a913ed2012-08-10 10:06:13 +00004111 thread_info client_data;
4112
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00004113#ifdef CLANG_HAVE_LIBXML
4114 LIBXML_TEST_VERSION
4115#endif
4116
Douglas Gregorf428bf82010-10-27 16:00:01 +00004117 if (getenv("CINDEXTEST_NOTHREADS"))
4118 return cindextest_main(argc, argv);
4119
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004120 client_data.argc = argc;
4121 client_data.argv = argv;
Daniel Dunbar23397c32010-11-04 01:26:31 +00004122 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004123 return client_data.result;
4124}