blob: ae2b81b2c54973a03b229f6a33621350225cce1f [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
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000545typedef struct {
546 const char *CommentSchemaFile;
547#ifdef CLANG_HAVE_LIBXML
548 xmlRelaxNGParserCtxtPtr RNGParser;
549 xmlRelaxNGPtr Schema;
550#endif
551} CommentXMLValidationData;
552
553static void ValidateCommentXML(const char *Str,
554 CommentXMLValidationData *ValidationData) {
555#ifdef CLANG_HAVE_LIBXML
556 xmlDocPtr Doc;
557 xmlRelaxNGValidCtxtPtr ValidationCtxt;
558 int status;
559
560 if (!ValidationData || !ValidationData->CommentSchemaFile)
561 return;
562
563 if (!ValidationData->RNGParser) {
564 ValidationData->RNGParser =
565 xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile);
566 ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser);
567 }
568 if (!ValidationData->RNGParser) {
569 printf(" libXMLError");
570 return;
571 }
572
573 Doc = xmlParseDoc((const xmlChar *) Str);
574
575 if (!Doc) {
576 xmlErrorPtr Error = xmlGetLastError();
577 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
578 return;
579 }
580
581 ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->Schema);
582 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
583 if (!status)
584 printf(" CommentXMLValid");
585 else if (status > 0) {
586 xmlErrorPtr Error = xmlGetLastError();
587 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
588 } else
589 printf(" libXMLError");
590
591 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
592 xmlFreeDoc(Doc);
593#endif
594}
595
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000596static void PrintCursorComments(CXCursor Cursor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000597 CommentXMLValidationData *ValidationData) {
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000598 {
599 CXString RawComment;
600 const char *RawCommentCString;
601 CXString BriefComment;
602 const char *BriefCommentCString;
603
604 RawComment = clang_Cursor_getRawCommentText(Cursor);
605 RawCommentCString = clang_getCString(RawComment);
606 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
607 PrintCStringWithPrefix("RawComment", RawCommentCString);
608 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
609
610 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
611 BriefCommentCString = clang_getCString(BriefComment);
612 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
613 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
614 clang_disposeString(BriefComment);
615 }
616 clang_disposeString(RawComment);
617 }
618
619 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000620 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000621 if (clang_Comment_getKind(Comment) != CXComment_Null) {
622 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
623 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000624 {
625 CXString XML;
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000626 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000627 PrintCXStringWithPrefix("FullCommentAsXML", XML);
628 ValidateCommentXML(clang_getCString(XML), ValidationData);
629 clang_disposeString(XML);
630 }
631
Dmitri Gribenko5e4fe002012-07-20 21:34:34 +0000632 DumpCXComment(Comment);
633 }
634 }
635}
636
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000637typedef struct {
638 unsigned line;
639 unsigned col;
640} LineCol;
641
642static int lineCol_cmp(const void *p1, const void *p2) {
643 const LineCol *lhs = p1;
644 const LineCol *rhs = p2;
645 if (lhs->line != rhs->line)
646 return (int)lhs->line - (int)rhs->line;
647 return (int)lhs->col - (int)rhs->col;
648}
649
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +0000650static void PrintCursor(CXCursor Cursor,
651 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +0000652 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000653 if (clang_isInvalid(Cursor.kind)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000654 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
Ted Kremenek29004672010-02-17 00:41:32 +0000655 printf("Invalid Cursor => %s", clang_getCString(ks));
656 clang_disposeString(ks);
657 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000658 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000659 CXString string, ks;
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000660 CXCursor Referenced;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000661 unsigned line, column;
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000662 CXCursor SpecializationOf;
Douglas Gregor99a26af2010-10-01 20:25:15 +0000663 CXCursor *overridden;
664 unsigned num_overridden;
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000665 unsigned RefNameRangeNr;
666 CXSourceRange CursorExtent;
667 CXSourceRange RefNameRange;
Douglas Gregord6225d32012-05-08 00:14:45 +0000668 int AlwaysUnavailable;
669 int AlwaysDeprecated;
670 CXString UnavailableMessage;
671 CXString DeprecatedMessage;
672 CXPlatformAvailability PlatformAvailability[2];
673 int NumPlatformAvailability;
674 int I;
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000675
Ted Kremenek29004672010-02-17 00:41:32 +0000676 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor97c75712010-10-02 22:49:11 +0000677 string = want_display_name? clang_getCursorDisplayName(Cursor)
678 : clang_getCursorSpelling(Cursor);
Ted Kremenek29004672010-02-17 00:41:32 +0000679 printf("%s=%s", clang_getCString(ks),
680 clang_getCString(string));
681 clang_disposeString(ks);
Steve Naroff8675d5c2009-11-09 17:45:52 +0000682 clang_disposeString(string);
Ted Kremenek29004672010-02-17 00:41:32 +0000683
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000684 Referenced = clang_getCursorReferenced(Cursor);
685 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000686 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
687 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
688 printf("[");
689 for (I = 0; I != N; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000690 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor2967e282010-09-14 00:20:32 +0000691 CXSourceLocation Loc;
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000692 if (I)
693 printf(", ");
694
Douglas Gregor2967e282010-09-14 00:20:32 +0000695 Loc = clang_getCursorLocation(Ovl);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000696 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000697 printf("%d:%d", line, column);
698 }
699 printf("]");
700 } else {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000701 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000702 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor16a2bdd2010-09-13 22:52:57 +0000703 printf(":%d:%d", line, column);
704 }
Douglas Gregorad27e8b2010-01-19 01:20:04 +0000705 }
Douglas Gregor6b8232f2010-01-19 19:34:47 +0000706
707 if (clang_isCursorDefinition(Cursor))
708 printf(" (Definition)");
Douglas Gregorf757a122010-08-23 23:00:57 +0000709
710 switch (clang_getCursorAvailability(Cursor)) {
711 case CXAvailability_Available:
712 break;
713
714 case CXAvailability_Deprecated:
715 printf(" (deprecated)");
716 break;
717
718 case CXAvailability_NotAvailable:
719 printf(" (unavailable)");
720 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +0000721
722 case CXAvailability_NotAccessible:
723 printf(" (inaccessible)");
724 break;
Douglas Gregorf757a122010-08-23 23:00:57 +0000725 }
Ted Kremeneka5940822010-08-26 01:42:22 +0000726
Douglas Gregord6225d32012-05-08 00:14:45 +0000727 NumPlatformAvailability
728 = clang_getCursorPlatformAvailability(Cursor,
729 &AlwaysDeprecated,
730 &DeprecatedMessage,
731 &AlwaysUnavailable,
732 &UnavailableMessage,
733 PlatformAvailability, 2);
734 if (AlwaysUnavailable) {
735 printf(" (always unavailable: \"%s\")",
736 clang_getCString(UnavailableMessage));
737 } else if (AlwaysDeprecated) {
738 printf(" (always deprecated: \"%s\")",
739 clang_getCString(DeprecatedMessage));
740 } else {
741 for (I = 0; I != NumPlatformAvailability; ++I) {
742 if (I >= 2)
743 break;
744
745 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
746 if (PlatformAvailability[I].Unavailable)
747 printf(", unavailable");
748 else {
749 printVersion(", introduced=", PlatformAvailability[I].Introduced);
750 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
751 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
752 }
753 if (clang_getCString(PlatformAvailability[I].Message)[0])
754 printf(", message=\"%s\"",
755 clang_getCString(PlatformAvailability[I].Message));
756 printf(")");
757 }
758 }
759 for (I = 0; I != NumPlatformAvailability; ++I) {
760 if (I >= 2)
761 break;
762 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
763 }
764
765 clang_disposeString(DeprecatedMessage);
766 clang_disposeString(UnavailableMessage);
767
Douglas Gregora8d0c772011-05-13 15:54:42 +0000768 if (clang_CXXMethod_isStatic(Cursor))
769 printf(" (static)");
770 if (clang_CXXMethod_isVirtual(Cursor))
771 printf(" (virtual)");
Dmitri Gribenkoe570ede2014-04-07 14:59:13 +0000772 if (clang_CXXMethod_isConst(Cursor))
773 printf(" (const)");
Dmitri Gribenko62770be2013-05-17 18:38:35 +0000774 if (clang_CXXMethod_isPureVirtual(Cursor))
775 printf(" (pure)");
Argyrios Kyrtzidis23814e42013-04-18 23:53:05 +0000776 if (clang_Cursor_isVariadic(Cursor))
777 printf(" (variadic)");
Argyrios Kyrtzidis7b50fc52013-07-05 20:44:37 +0000778 if (clang_Cursor_isObjCOptional(Cursor))
779 printf(" (@optional)");
780
Ted Kremeneka5940822010-08-26 01:42:22 +0000781 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000782 CXType T =
783 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
784 CXString S = clang_getTypeKindSpelling(T.kind);
Ted Kremeneka5940822010-08-26 01:42:22 +0000785 printf(" [IBOutletCollection=%s]", clang_getCString(S));
786 clang_disposeString(S);
787 }
Ted Kremenekae9e2212010-08-27 21:34:58 +0000788
789 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
790 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
791 unsigned isVirtual = clang_isVirtualBase(Cursor);
792 const char *accessStr = 0;
793
794 switch (access) {
795 case CX_CXXInvalidAccessSpecifier:
796 accessStr = "invalid"; break;
797 case CX_CXXPublic:
798 accessStr = "public"; break;
799 case CX_CXXProtected:
800 accessStr = "protected"; break;
801 case CX_CXXPrivate:
802 accessStr = "private"; break;
803 }
804
805 printf(" [access=%s isVirtual=%s]", accessStr,
806 isVirtual ? "true" : "false");
807 }
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000808
809 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
810 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000811 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
812 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000813 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregord3f48bd2010-09-02 00:07:54 +0000814 printf(" [Specialization of %s:%d:%d]",
815 clang_getCString(Name), line, column);
816 clang_disposeString(Name);
817 }
Douglas Gregor99a26af2010-10-01 20:25:15 +0000818
819 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
820 if (num_overridden) {
821 unsigned I;
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000822 LineCol lineCols[50];
823 assert(num_overridden <= 50);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000824 printf(" [Overrides ");
825 for (I = 0; I != num_overridden; ++I) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000826 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregor229bebd2010-11-09 06:24:54 +0000827 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000828 lineCols[I].line = line;
829 lineCols[I].col = column;
830 }
Michael Liaob94f47a2012-08-30 00:45:32 +0000831 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000832 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
833 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor99a26af2010-10-01 20:25:15 +0000834 if (I)
835 printf(", ");
Argyrios Kyrtzidis079ff5c2012-08-22 23:15:52 +0000836 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor99a26af2010-10-01 20:25:15 +0000837 }
838 printf("]");
839 clang_disposeOverriddenCursors(overridden);
840 }
Douglas Gregor796d76a2010-10-20 22:00:55 +0000841
842 if (Cursor.kind == CXCursor_InclusionDirective) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000843 CXFile File = clang_getIncludedFile(Cursor);
844 CXString Included = clang_getFileName(File);
Douglas Gregor796d76a2010-10-20 22:00:55 +0000845 printf(" (%s)", clang_getCString(Included));
846 clang_disposeString(Included);
Douglas Gregor37aa4932011-05-04 00:14:37 +0000847
848 if (clang_isFileMultipleIncludeGuarded(TU, File))
849 printf(" [multi-include guarded]");
Douglas Gregor796d76a2010-10-20 22:00:55 +0000850 }
Douglas Gregorc1679ec2011-07-25 17:48:11 +0000851
852 CursorExtent = clang_getCursorExtent(Cursor);
853 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
854 CXNameRange_WantQualifier
855 | CXNameRange_WantSinglePiece
856 | CXNameRange_WantTemplateArgs,
857 0);
858 if (!clang_equalRanges(CursorExtent, RefNameRange))
859 PrintRange(RefNameRange, "SingleRefName");
860
861 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
862 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
863 CXNameRange_WantQualifier
864 | CXNameRange_WantTemplateArgs,
865 RefNameRangeNr);
866 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
867 break;
868 if (!clang_equalRanges(CursorExtent, RefNameRange))
869 PrintRange(RefNameRange, "RefName");
870 }
Dmitri Gribenkoaab83832012-06-20 00:34:58 +0000871
Dmitri Gribenko7acbf002012-09-10 20:32:42 +0000872 PrintCursorComments(Cursor, ValidationData);
Argyrios Kyrtzidis9adfd8a2013-04-18 22:15:49 +0000873
874 {
875 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
876 if (PropAttrs != CXObjCPropertyAttr_noattr) {
877 printf(" [");
878 #define PRINT_PROP_ATTR(A) \
879 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
880 PRINT_PROP_ATTR(readonly);
881 PRINT_PROP_ATTR(getter);
882 PRINT_PROP_ATTR(assign);
883 PRINT_PROP_ATTR(readwrite);
884 PRINT_PROP_ATTR(retain);
885 PRINT_PROP_ATTR(copy);
886 PRINT_PROP_ATTR(nonatomic);
887 PRINT_PROP_ATTR(setter);
888 PRINT_PROP_ATTR(atomic);
889 PRINT_PROP_ATTR(weak);
890 PRINT_PROP_ATTR(strong);
891 PRINT_PROP_ATTR(unsafe_unretained);
892 printf("]");
893 }
894 }
Argyrios Kyrtzidis9d9bc012013-04-18 23:29:12 +0000895
896 {
897 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
898 if (QT != CXObjCDeclQualifier_None) {
899 printf(" [");
900 #define PRINT_OBJC_QUAL(A) \
901 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
902 PRINT_OBJC_QUAL(In);
903 PRINT_OBJC_QUAL(Inout);
904 PRINT_OBJC_QUAL(Out);
905 PRINT_OBJC_QUAL(Bycopy);
906 PRINT_OBJC_QUAL(Byref);
907 PRINT_OBJC_QUAL(Oneway);
908 printf("]");
909 }
910 }
Steve Naroff63f475a2009-09-25 21:32:34 +0000911 }
Steve Naroff38c1a7b2009-09-03 15:49:00 +0000912}
Steve Naroff1054e602009-08-31 00:59:03 +0000913
Ted Kremenek29004672010-02-17 00:41:32 +0000914static const char* GetCursorSource(CXCursor Cursor) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000915 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenekc560b682010-02-17 00:41:20 +0000916 CXString source;
Douglas Gregor4f46e782010-01-19 21:36:55 +0000917 CXFile file;
Argyrios Kyrtzidis7ca77352011-11-03 02:20:36 +0000918 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor4f46e782010-01-19 21:36:55 +0000919 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +0000920 if (!clang_getCString(source)) {
Ted Kremenekc560b682010-02-17 00:41:20 +0000921 clang_disposeString(source);
922 return "<invalid loc>";
923 }
924 else {
Ted Kremenek29004672010-02-17 00:41:32 +0000925 const char *b = basename(clang_getCString(source));
Ted Kremenekc560b682010-02-17 00:41:20 +0000926 clang_disposeString(source);
927 return b;
928 }
Ted Kremenek4c4d6432009-11-17 05:31:58 +0000929}
930
Ted Kremenek1cd27d52009-11-17 18:13:31 +0000931/******************************************************************************/
Ted Kremenekb478ff42010-01-26 17:59:48 +0000932/* Callbacks. */
933/******************************************************************************/
934
935typedef void (*PostVisitTU)(CXTranslationUnit);
936
Douglas Gregor33cdd812010-02-18 18:08:43 +0000937void PrintDiagnostic(CXDiagnostic Diagnostic) {
938 FILE *out = stderr;
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000939 CXFile file;
Douglas Gregord770f732010-02-22 23:17:23 +0000940 CXString Msg;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000941 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregora750e8e2010-11-19 16:18:16 +0000942 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
943 | CXDiagnostic_DisplayOption;
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000944 unsigned i, num_fixits;
Ted Kremenek599d73a2010-03-25 02:00:39 +0000945
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000946 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000947 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000948
Douglas Gregord770f732010-02-22 23:17:23 +0000949 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
950 fprintf(stderr, "%s\n", clang_getCString(Msg));
951 clang_disposeString(Msg);
Ted Kremenek599d73a2010-03-25 02:00:39 +0000952
Douglas Gregor229bebd2010-11-09 06:24:54 +0000953 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
954 &file, 0, 0, 0);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000955 if (!file)
956 return;
Ted Kremenek29004672010-02-17 00:41:32 +0000957
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000958 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek4a642302012-03-20 20:49:45 +0000959 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000960 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor836ec942010-02-19 18:16:06 +0000961 CXSourceRange range;
Enea Zaffanella476f38a2013-07-22 20:58:30 +0000962 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
963 CXSourceLocation start = clang_getRangeStart(range);
964 CXSourceLocation end = clang_getRangeEnd(range);
Douglas Gregor836ec942010-02-19 18:16:06 +0000965 unsigned start_line, start_column, end_line, end_column;
966 CXFile start_file, end_file;
Douglas Gregor229bebd2010-11-09 06:24:54 +0000967 clang_getSpellingLocation(start, &start_file, &start_line,
968 &start_column, 0);
969 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor836ec942010-02-19 18:16:06 +0000970 if (clang_equalLocations(start, end)) {
971 /* Insertion. */
972 if (start_file == file)
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000973 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor836ec942010-02-19 18:16:06 +0000974 clang_getCString(insertion_text), start_line, start_column);
975 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
976 /* Removal. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000977 if (start_file == file && end_file == file) {
978 fprintf(out, "FIX-IT: Remove ");
979 PrintExtent(out, start_line, start_column, end_line, end_column);
980 fprintf(out, "\n");
Douglas Gregor60b11f62010-01-29 00:41:11 +0000981 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000982 } else {
983 /* Replacement. */
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000984 if (start_file == end_file) {
985 fprintf(out, "FIX-IT: Replace ");
986 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor836ec942010-02-19 18:16:06 +0000987 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor9773e3d2010-02-18 22:27:07 +0000988 }
Douglas Gregor1e21cc72010-02-18 23:07:20 +0000989 break;
990 }
Douglas Gregor836ec942010-02-19 18:16:06 +0000991 clang_disposeString(insertion_text);
Douglas Gregor60b11f62010-01-29 00:41:11 +0000992 }
Douglas Gregor4f9c3762010-01-28 00:27:43 +0000993}
994
Ted Kremenek914c7e62012-02-14 02:46:03 +0000995void PrintDiagnosticSet(CXDiagnosticSet Set) {
996 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
997 for ( ; i != n ; ++i) {
998 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
999 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001000 PrintDiagnostic(Diag);
Ted Kremenek914c7e62012-02-14 02:46:03 +00001001 if (ChildDiags)
1002 PrintDiagnosticSet(ChildDiags);
1003 }
1004}
1005
1006void PrintDiagnostics(CXTranslationUnit TU) {
1007 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
1008 PrintDiagnosticSet(TUSet);
1009 clang_disposeDiagnosticSet(TUSet);
Douglas Gregor33cdd812010-02-18 18:08:43 +00001010}
1011
Ted Kremenek83f642e2011-04-18 22:47:10 +00001012void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayd6238f42011-08-29 16:37:29 +00001013 unsigned long total = 0;
Ted Kremenek11d1a422011-04-18 23:42:53 +00001014 unsigned i = 0;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001015 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet45cc5462011-04-18 23:33:22 +00001016 fprintf(stderr, "Memory usage:\n");
Ted Kremenek11d1a422011-04-18 23:42:53 +00001017 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenek23324122011-04-20 16:41:07 +00001018 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek83f642e2011-04-18 22:47:10 +00001019 unsigned long amount = usage.entries[i].amount;
1020 total += amount;
Ted Kremenek11d1a422011-04-18 23:42:53 +00001021 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek83f642e2011-04-18 22:47:10 +00001022 ((double) amount)/(1024*1024));
1023 }
Ted Kremenek11d1a422011-04-18 23:42:53 +00001024 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek83f642e2011-04-18 22:47:10 +00001025 ((double) total)/(1024*1024));
Ted Kremenek23324122011-04-20 16:41:07 +00001026 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek83f642e2011-04-18 22:47:10 +00001027}
1028
Ted Kremenekb478ff42010-01-26 17:59:48 +00001029/******************************************************************************/
Douglas Gregor720d0052010-01-20 21:32:04 +00001030/* Logic for testing traversal. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001031/******************************************************************************/
1032
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001033static void PrintCursorExtent(CXCursor C) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001034 CXSourceRange extent = clang_getCursorExtent(C);
1035 PrintRange(extent, "Extent");
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001036}
1037
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001038/* Data used by the visitors. */
1039typedef struct {
Douglas Gregor720d0052010-01-20 21:32:04 +00001040 CXTranslationUnit TU;
1041 enum CXCursorKind *Filter;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001042 CommentXMLValidationData ValidationData;
Douglas Gregor720d0052010-01-20 21:32:04 +00001043} VisitorData;
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001044
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001045
Ted Kremenek29004672010-02-17 00:41:32 +00001046enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001047 CXCursor Parent,
1048 CXClientData ClientData) {
1049 VisitorData *Data = (VisitorData *)ClientData;
1050 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001051 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor4f46e782010-01-19 21:36:55 +00001052 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001053 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001054 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor4f46e782010-01-19 21:36:55 +00001055 GetCursorSource(Cursor), line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001056 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001057 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis1ab09cc2013-04-11 17:02:10 +00001058 if (clang_isDeclaration(Cursor.kind)) {
1059 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
1060 const char *accessStr = 0;
1061
1062 switch (access) {
1063 case CX_CXXInvalidAccessSpecifier: break;
1064 case CX_CXXPublic:
1065 accessStr = "public"; break;
1066 case CX_CXXProtected:
1067 accessStr = "protected"; break;
1068 case CX_CXXPrivate:
1069 accessStr = "private"; break;
1070 }
1071
1072 if (accessStr)
1073 printf(" [access=%s]", accessStr);
1074 }
Ted Kremenek29004672010-02-17 00:41:32 +00001075 printf("\n");
Douglas Gregor720d0052010-01-20 21:32:04 +00001076 return CXChildVisit_Recurse;
Steve Naroff772c1a42009-08-31 14:26:51 +00001077 }
Ted Kremenek29004672010-02-17 00:41:32 +00001078
Douglas Gregor720d0052010-01-20 21:32:04 +00001079 return CXChildVisit_Continue;
Steve Naroff1054e602009-08-31 00:59:03 +00001080}
Steve Naroffa1c72842009-08-28 15:28:48 +00001081
Ted Kremenek29004672010-02-17 00:41:32 +00001082static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregor720d0052010-01-20 21:32:04 +00001083 CXCursor Parent,
1084 CXClientData ClientData) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001085 const char *startBuf, *endBuf;
1086 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1087 CXCursor Ref;
Douglas Gregor720d0052010-01-20 21:32:04 +00001088 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001089
Douglas Gregor6b8232f2010-01-19 19:34:47 +00001090 if (Cursor.kind != CXCursor_FunctionDecl ||
1091 !clang_isCursorDefinition(Cursor))
Douglas Gregor720d0052010-01-20 21:32:04 +00001092 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001093
1094 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1095 &startLine, &startColumn,
1096 &endLine, &endColumn);
1097 /* Probe the entire body, looking for both decls and refs. */
1098 curLine = startLine;
1099 curColumn = startColumn;
1100
1101 while (startBuf < endBuf) {
Douglas Gregor66a58812010-01-18 22:46:11 +00001102 CXSourceLocation Loc;
Douglas Gregor4f46e782010-01-19 21:36:55 +00001103 CXFile file;
Ted Kremenekc560b682010-02-17 00:41:20 +00001104 CXString source;
Ted Kremenek29004672010-02-17 00:41:32 +00001105
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001106 if (*startBuf == '\n') {
1107 startBuf++;
1108 curLine++;
1109 curColumn = 1;
1110 } else if (*startBuf != '\t')
1111 curColumn++;
Ted Kremenek29004672010-02-17 00:41:32 +00001112
Douglas Gregor66a58812010-01-18 22:46:11 +00001113 Loc = clang_getCursorLocation(Cursor);
Douglas Gregor229bebd2010-11-09 06:24:54 +00001114 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremenek29004672010-02-17 00:41:32 +00001115
Douglas Gregor4f46e782010-01-19 21:36:55 +00001116 source = clang_getFileName(file);
Ted Kremenek29004672010-02-17 00:41:32 +00001117 if (clang_getCString(source)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001118 CXSourceLocation RefLoc
1119 = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregor816fd362010-01-22 21:44:22 +00001120 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor66a58812010-01-18 22:46:11 +00001121 if (Ref.kind == CXCursor_NoDeclFound) {
1122 /* Nothing found here; that's fine. */
1123 } else if (Ref.kind != CXCursor_FunctionDecl) {
1124 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1125 curLine, curColumn);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001126 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor66a58812010-01-18 22:46:11 +00001127 printf("\n");
1128 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001129 }
Ted Kremenekc560b682010-02-17 00:41:20 +00001130 clang_disposeString(source);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001131 startBuf++;
1132 }
Ted Kremenek29004672010-02-17 00:41:32 +00001133
Douglas Gregor720d0052010-01-20 21:32:04 +00001134 return CXChildVisit_Continue;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001135}
1136
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001137/******************************************************************************/
1138/* USR testing. */
1139/******************************************************************************/
1140
Douglas Gregor720d0052010-01-20 21:32:04 +00001141enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1142 CXClientData ClientData) {
1143 VisitorData *Data = (VisitorData *)ClientData;
1144 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001145 CXString USR = clang_getCursorUSR(C);
1146 const char *cstr = clang_getCString(USR);
Ted Kremenek6d159c12010-04-20 23:15:40 +00001147 if (!cstr || cstr[0] == '\0') {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001148 clang_disposeString(USR);
Ted Kremenek7afa85b2010-04-16 21:31:52 +00001149 return CXChildVisit_Recurse;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001150 }
Ted Kremenek6d159c12010-04-20 23:15:40 +00001151 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1152
Douglas Gregor33c34ac2010-01-19 00:34:46 +00001153 PrintCursorExtent(C);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001154 printf("\n");
1155 clang_disposeString(USR);
Ted Kremenek29004672010-02-17 00:41:32 +00001156
Douglas Gregor720d0052010-01-20 21:32:04 +00001157 return CXChildVisit_Recurse;
Ted Kremenek29004672010-02-17 00:41:32 +00001158 }
1159
Douglas Gregor720d0052010-01-20 21:32:04 +00001160 return CXChildVisit_Continue;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001161}
1162
1163/******************************************************************************/
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001164/* Inclusion stack testing. */
1165/******************************************************************************/
1166
1167void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1168 unsigned includeStackLen, CXClientData data) {
Ted Kremenek29004672010-02-17 00:41:32 +00001169
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001170 unsigned i;
Ted Kremenekc560b682010-02-17 00:41:20 +00001171 CXString fname;
1172
1173 fname = clang_getFileName(includedFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001174 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenekc560b682010-02-17 00:41:20 +00001175 clang_disposeString(fname);
Ted Kremenek29004672010-02-17 00:41:32 +00001176
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001177 for (i = 0; i < includeStackLen; ++i) {
1178 CXFile includingFile;
1179 unsigned line, column;
Douglas Gregor229bebd2010-11-09 06:24:54 +00001180 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1181 &column, 0);
Ted Kremenekc560b682010-02-17 00:41:20 +00001182 fname = clang_getFileName(includingFile);
Ted Kremenek29004672010-02-17 00:41:32 +00001183 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenekc560b682010-02-17 00:41:20 +00001184 clang_disposeString(fname);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001185 }
1186 printf("\n");
1187}
1188
1189void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremenek29004672010-02-17 00:41:32 +00001190 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00001191}
1192
1193/******************************************************************************/
Ted Kremenek83b28a22010-03-03 06:37:58 +00001194/* Linkage testing. */
1195/******************************************************************************/
1196
1197static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1198 CXClientData d) {
1199 const char *linkage = 0;
1200
1201 if (clang_isInvalid(clang_getCursorKind(cursor)))
1202 return CXChildVisit_Recurse;
1203
1204 switch (clang_getCursorLinkage(cursor)) {
1205 case CXLinkage_Invalid: break;
Douglas Gregor0b466502010-03-04 19:36:27 +00001206 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1207 case CXLinkage_Internal: linkage = "Internal"; break;
1208 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1209 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek83b28a22010-03-03 06:37:58 +00001210 }
1211
1212 if (linkage) {
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001213 PrintCursor(cursor, NULL);
Ted Kremenek83b28a22010-03-03 06:37:58 +00001214 printf("linkage=%s\n", linkage);
1215 }
1216
1217 return CXChildVisit_Recurse;
1218}
1219
1220/******************************************************************************/
Ted Kremenek6bca9842010-05-14 21:29:26 +00001221/* Typekind testing. */
1222/******************************************************************************/
1223
Dmitri Gribenko00353722013-02-15 21:15:49 +00001224static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1225 CXString TypeSpelling, TypeKindSpelling;
1226
1227 TypeSpelling = clang_getTypeSpelling(T);
1228 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1229 printf(Format,
1230 clang_getCString(TypeSpelling),
1231 clang_getCString(TypeKindSpelling));
1232 clang_disposeString(TypeSpelling);
1233 clang_disposeString(TypeKindSpelling);
1234}
1235
1236static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1237 CXClientData d) {
Ted Kremenek6bca9842010-05-14 21:29:26 +00001238 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001239 CXType T = clang_getCursorType(cursor);
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001240 enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001241 PrintCursor(cursor, NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00001242 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregor56a63802011-01-27 16:27:11 +00001243 if (clang_isConstQualifiedType(T))
1244 printf(" const");
1245 if (clang_isVolatileQualifiedType(T))
1246 printf(" volatile");
1247 if (clang_isRestrictQualifiedType(T))
1248 printf(" restrict");
Argyrios Kyrtzidisadff3ae2013-10-11 19:58:38 +00001249 if (RQ == CXRefQualifier_LValue)
1250 printf(" lvalue-ref-qualifier");
1251 if (RQ == CXRefQualifier_RValue)
1252 printf(" rvalue-ref-qualifier");
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001253 /* Print the canonical type if it is different. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001254 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001255 CXType CT = clang_getCanonicalType(T);
Ted Kremenekc1508872010-06-21 20:15:39 +00001256 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001257 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001258 }
1259 }
Benjamin Kramer1e63c742010-06-22 09:29:44 +00001260 /* Print the return type if it exists. */
Ted Kremenekc1508872010-06-21 20:15:39 +00001261 {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001262 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenekc1508872010-06-21 20:15:39 +00001263 if (RT.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001264 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenekc1508872010-06-21 20:15:39 +00001265 }
1266 }
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001267 /* Print the argument types if they exist. */
1268 {
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001269 int NumArgs = clang_Cursor_getNumArguments(cursor);
1270 if (NumArgs != -1 && NumArgs != 0) {
Argyrios Kyrtzidis08804172012-04-11 19:54:09 +00001271 int i;
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001272 printf(" [args=");
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001273 for (i = 0; i < NumArgs; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001274 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001275 if (T.kind != CXType_Invalid) {
Dmitri Gribenko00353722013-02-15 21:15:49 +00001276 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidis0c27e4b2012-04-11 19:32:19 +00001277 }
1278 }
1279 printf("]");
1280 }
1281 }
Dmitri Gribenko6ede6ab2014-02-27 16:05:05 +00001282 /* Print the template argument types if they exist. */
1283 {
1284 int NumTArgs = clang_Type_getNumTemplateArguments(T);
1285 if (NumTArgs != -1 && NumTArgs != 0) {
1286 int i;
1287 printf(" [templateargs/%d=", NumTArgs);
1288 for (i = 0; i < NumTArgs; ++i) {
1289 CXType TArg = clang_Type_getTemplateArgumentAsType(T, i);
1290 if (TArg.kind != CXType_Invalid) {
1291 PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]");
1292 }
1293 }
1294 printf("]");
1295 }
1296 }
Ted Kremenek0c7476a2010-07-30 00:14:11 +00001297 /* Print if this is a non-POD type. */
1298 printf(" [isPOD=%d]", clang_isPODType(T));
Anders Waldenborgddce74f2014-04-09 19:16:08 +00001299 /* Print the pointee type. */
1300 {
1301 CXType PT = clang_getPointeeType(T);
1302 if (PT.kind != CXType_Invalid) {
1303 PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]");
1304 }
1305 }
Ted Kremenekc1508872010-06-21 20:15:39 +00001306
Ted Kremenek6bca9842010-05-14 21:29:26 +00001307 printf("\n");
1308 }
1309 return CXChildVisit_Recurse;
1310}
1311
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001312static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1313 CXClientData d) {
1314 CXType T;
1315 enum CXCursorKind K = clang_getCursorKind(cursor);
1316 if (clang_isInvalid(K))
1317 return CXChildVisit_Recurse;
1318 T = clang_getCursorType(cursor);
1319 PrintCursor(cursor, NULL);
1320 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1321 /* Print the type sizeof if applicable. */
1322 {
1323 long long Size = clang_Type_getSizeOf(T);
1324 if (Size >= 0 || Size < -1 ) {
1325 printf(" [sizeof=%lld]", Size);
1326 }
1327 }
1328 /* Print the type alignof if applicable. */
1329 {
1330 long long Align = clang_Type_getAlignOf(T);
1331 if (Align >= 0 || Align < -1) {
1332 printf(" [alignof=%lld]", Align);
1333 }
1334 }
1335 /* Print the record field offset if applicable. */
1336 {
Nico Weber82098cb2014-04-24 04:14:12 +00001337 CXString FieldSpelling = clang_getCursorSpelling(cursor);
1338 const char *FieldName = clang_getCString(FieldSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001339 /* recurse to get the root anonymous record parent */
1340 CXCursor Parent, Root;
Nico Weber82098cb2014-04-24 04:14:12 +00001341 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) {
1342 CXString RootParentSpelling;
1343 const char *RootParentName = 0;
Richard Smith131daca2014-03-06 21:59:38 +00001344 Parent = p;
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001345 do {
Nico Weber82098cb2014-04-24 04:14:12 +00001346 if (RootParentName != 0)
1347 clang_disposeString(RootParentSpelling);
1348
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001349 Root = Parent;
Nico Weber82098cb2014-04-24 04:14:12 +00001350 RootParentSpelling = clang_getCursorSpelling(Root);
1351 RootParentName = clang_getCString(RootParentSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001352 Parent = clang_getCursorSemanticParent(Root);
Nico Weber82098cb2014-04-24 04:14:12 +00001353 } while (clang_getCursorType(Parent).kind == CXType_Record &&
1354 !strcmp(RootParentName, ""));
1355 clang_disposeString(RootParentSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001356 /* if RootParentName is "", record is anonymous. */
1357 {
1358 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1359 FieldName);
1360 printf(" [offsetof=%lld]", Offset);
1361 }
1362 }
Nico Weber82098cb2014-04-24 04:14:12 +00001363 clang_disposeString(FieldSpelling);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00001364 }
1365 /* Print if its a bitfield */
1366 {
1367 int IsBitfield = clang_Cursor_isBitField(cursor);
1368 if (IsBitfield)
1369 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1370 }
1371 printf("\n");
1372 return CXChildVisit_Recurse;
1373}
1374
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001375/******************************************************************************/
1376/* Bitwidth testing. */
1377/******************************************************************************/
1378
1379static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1380 CXClientData d) {
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001381 int Bitwidth;
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001382 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1383 return CXChildVisit_Recurse;
1384
NAKAMURA Takumidfaed1b2012-12-04 15:32:03 +00001385 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00001386 if (Bitwidth >= 0) {
1387 PrintCursor(cursor, NULL);
1388 printf(" bitwidth=%d\n", Bitwidth);
1389 }
1390
1391 return CXChildVisit_Recurse;
1392}
Ted Kremenek6bca9842010-05-14 21:29:26 +00001393
1394/******************************************************************************/
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00001395/* Loading ASTs/source. */
1396/******************************************************************************/
1397
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001398static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek73eccd22010-01-12 18:53:15 +00001399 const char *filter, const char *prefix,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001400 CXCursorVisitor Visitor,
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001401 PostVisitTU PV,
1402 const char *CommentSchemaFile) {
Ted Kremenek29004672010-02-17 00:41:32 +00001403
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001404 if (prefix)
Ted Kremenek29004672010-02-17 00:41:32 +00001405 FileCheckPrefix = prefix;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001406
1407 if (Visitor) {
1408 enum CXCursorKind K = CXCursor_NotImplemented;
1409 enum CXCursorKind *ck = &K;
1410 VisitorData Data;
Ted Kremenek29004672010-02-17 00:41:32 +00001411
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001412 /* Perform some simple filtering. */
1413 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor97c75712010-10-02 22:49:11 +00001414 else if (!strcmp(filter, "all-display") ||
1415 !strcmp(filter, "local-display")) {
1416 ck = NULL;
1417 want_display_name = 1;
1418 }
Daniel Dunbard64ce7b2010-02-10 20:42:40 +00001419 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001420 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1421 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1422 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1423 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1424 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1425 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1426 else {
1427 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1428 return 1;
1429 }
Ted Kremenek29004672010-02-17 00:41:32 +00001430
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001431 Data.TU = TU;
1432 Data.Filter = ck;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001433 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1434#ifdef CLANG_HAVE_LIBXML
1435 Data.ValidationData.RNGParser = NULL;
1436 Data.ValidationData.Schema = NULL;
1437#endif
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001438 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001439 }
Ted Kremenek29004672010-02-17 00:41:32 +00001440
Ted Kremenekb478ff42010-01-26 17:59:48 +00001441 if (PV)
1442 PV(TU);
Ted Kremeneka97a5cd2010-01-26 17:55:33 +00001443
Douglas Gregor33cdd812010-02-18 18:08:43 +00001444 PrintDiagnostics(TU);
Argyrios Kyrtzidis70480492011-11-13 23:39:14 +00001445 if (checkForErrors(TU) != 0) {
1446 clang_disposeTranslationUnit(TU);
1447 return -1;
1448 }
1449
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001450 clang_disposeTranslationUnit(TU);
1451 return 0;
1452}
1453
Ted Kremeneka44d99c2010-01-05 23:18:49 +00001454int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekb478ff42010-01-26 17:59:48 +00001455 const char *prefix, CXCursorVisitor Visitor,
1456 PostVisitTU PV) {
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001457 CXIndex Idx;
1458 CXTranslationUnit TU;
Ted Kremenek50228be2010-02-11 07:41:25 +00001459 int result;
Ted Kremenek29004672010-02-17 00:41:32 +00001460 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001461 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001462 /* displayDiagnostics=*/1);
Ted Kremenek29004672010-02-17 00:41:32 +00001463
Ted Kremenek50228be2010-02-11 07:41:25 +00001464 if (!CreateTranslationUnit(Idx, file, &TU)) {
1465 clang_disposeIndex(Idx);
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001466 return 1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001467 }
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001468
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001469 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek50228be2010-02-11 07:41:25 +00001470 clang_disposeIndex(Idx);
1471 return result;
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00001472}
1473
Ted Kremenekb478ff42010-01-26 17:59:48 +00001474int perform_test_load_source(int argc, const char **argv,
1475 const char *filter, CXCursorVisitor Visitor,
1476 PostVisitTU PV) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001477 CXIndex Idx;
1478 CXTranslationUnit TU;
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001479 const char *CommentSchemaFile;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001480 struct CXUnsavedFile *unsaved_files = 0;
1481 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001482 enum CXErrorCode Err;
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001483 int result;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001484
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001485 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor97c75712010-10-02 22:49:11 +00001486 (!strcmp(filter, "local") ||
1487 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001488 /* displayDiagnostics=*/1);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001489
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001490 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1491 argc--;
1492 argv++;
1493 }
1494
Ted Kremenek50228be2010-02-11 07:41:25 +00001495 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1496 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001497 return -1;
Ted Kremenek50228be2010-02-11 07:41:25 +00001498 }
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001499
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001500 Err = clang_parseTranslationUnit2(Idx, 0,
1501 argv + num_unsaved_files,
1502 argc - num_unsaved_files,
1503 unsaved_files, num_unsaved_files,
1504 getDefaultParsingOptions(), &TU);
1505 if (Err != CXError_Success) {
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001506 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001507 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001508 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001509 clang_disposeIndex(Idx);
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001510 return 1;
1511 }
1512
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001513 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1514 CommentSchemaFile);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001515 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek50228be2010-02-11 07:41:25 +00001516 clang_disposeIndex(Idx);
Douglas Gregoraa98ed92010-01-23 00:14:00 +00001517 return result;
Daniel Dunbar3e535d72009-12-01 02:03:10 +00001518}
1519
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001520int perform_test_reparse_source(int argc, const char **argv, int trials,
1521 const char *filter, CXCursorVisitor Visitor,
1522 PostVisitTU PV) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001523 CXIndex Idx;
1524 CXTranslationUnit TU;
1525 struct CXUnsavedFile *unsaved_files = 0;
1526 int num_unsaved_files = 0;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001527 int compiler_arg_idx = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001528 enum CXErrorCode Err;
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001529 int result, i;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001530 int trial;
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001531 int remap_after_trial = 0;
1532 char *endptr = 0;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001533
1534 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1535 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidisbcc8a5a2013-04-09 20:29:24 +00001536 /* displayDiagnostics=*/1);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001537
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001538 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1539 clang_disposeIndex(Idx);
1540 return -1;
1541 }
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001542
1543 for (i = 0; i < argc; ++i) {
1544 if (strcmp(argv[i], "--") == 0)
1545 break;
1546 }
1547 if (i < argc)
1548 compiler_arg_idx = i+1;
1549 if (num_unsaved_files > compiler_arg_idx)
1550 compiler_arg_idx = num_unsaved_files;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001551
Daniel Dunbarec29d712010-08-18 23:09:16 +00001552 /* Load the initial translation unit -- we do this without honoring remapped
1553 * files, so that we have a way to test results after changing the source. */
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001554 Err = clang_parseTranslationUnit2(Idx, 0,
1555 argv + compiler_arg_idx,
1556 argc - compiler_arg_idx,
1557 0, 0, getDefaultParsingOptions(), &TU);
1558 if (Err != CXError_Success) {
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001559 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001560 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001561 free_remapped_files(unsaved_files, num_unsaved_files);
1562 clang_disposeIndex(Idx);
1563 return 1;
1564 }
1565
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001566 if (checkForErrors(TU) != 0)
1567 return -1;
1568
Argyrios Kyrtzidis3405baa2011-09-12 18:09:31 +00001569 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1570 remap_after_trial =
1571 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1572 }
1573
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001574 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis011e6a52013-12-05 08:19:23 +00001575 free_remapped_files(unsaved_files, num_unsaved_files);
1576 if (parse_remapped_files_with_try(trial, argc, argv, 0,
1577 &unsaved_files, &num_unsaved_files)) {
1578 clang_disposeTranslationUnit(TU);
1579 clang_disposeIndex(Idx);
1580 return -1;
1581 }
1582
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001583 Err = clang_reparseTranslationUnit(
1584 TU,
1585 trial >= remap_after_trial ? num_unsaved_files : 0,
1586 trial >= remap_after_trial ? unsaved_files : 0,
1587 clang_defaultReparseOptions(TU));
1588 if (Err != CXError_Success) {
Daniel Dunbarec29d712010-08-18 23:09:16 +00001589 fprintf(stderr, "Unable to reparse translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00001590 describeLibclangFailure(Err);
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001591 clang_disposeTranslationUnit(TU);
1592 free_remapped_files(unsaved_files, num_unsaved_files);
1593 clang_disposeIndex(Idx);
1594 return -1;
1595 }
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001596
1597 if (checkForErrors(TU) != 0)
1598 return -1;
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001599 }
1600
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001601 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidise74e8222011-11-13 22:08:33 +00001602
Douglas Gregoraa21cc42010-07-19 21:46:24 +00001603 free_remapped_files(unsaved_files, num_unsaved_files);
1604 clang_disposeIndex(Idx);
1605 return result;
1606}
1607
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001608/******************************************************************************/
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001609/* Logic for testing clang_getCursor(). */
1610/******************************************************************************/
1611
Douglas Gregor37aa4932011-05-04 00:14:37 +00001612static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001613 unsigned start_line, unsigned start_col,
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001614 unsigned end_line, unsigned end_col,
1615 const char *prefix) {
Ted Kremenekb58514e2010-01-07 01:17:12 +00001616 printf("// %s: ", FileCheckPrefix);
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001617 if (prefix)
1618 printf("-%s", prefix);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00001619 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1620 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00001621 PrintCursor(cursor, NULL);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001622 printf("\n");
1623}
1624
Ted Kremenek0469b7e2009-11-18 02:02:52 +00001625static int perform_file_scan(const char *ast_file, const char *source_file,
1626 const char *prefix) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001627 CXIndex Idx;
1628 CXTranslationUnit TU;
1629 FILE *fp;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001630 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregor816fd362010-01-22 21:44:22 +00001631 CXFile file;
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001632 unsigned line = 1, col = 1;
Daniel Dunbar6092d502010-02-14 08:32:51 +00001633 unsigned start_line = 1, start_col = 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001634
Douglas Gregor1e21cc72010-02-18 23:07:20 +00001635 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00001636 /* displayDiagnostics=*/1))) {
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001637 fprintf(stderr, "Could not create Index\n");
1638 return 1;
1639 }
Ted Kremenek29004672010-02-17 00:41:32 +00001640
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001641 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1642 return 1;
Ted Kremenek29004672010-02-17 00:41:32 +00001643
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001644 if ((fp = fopen(source_file, "r")) == NULL) {
1645 fprintf(stderr, "Could not open '%s'\n", source_file);
1646 return 1;
1647 }
Ted Kremenek29004672010-02-17 00:41:32 +00001648
Douglas Gregor816fd362010-01-22 21:44:22 +00001649 file = clang_getFile(TU, source_file);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001650 for (;;) {
1651 CXCursor cursor;
1652 int c = fgetc(fp);
Benjamin Kramer10d083172009-11-17 20:51:40 +00001653
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001654 if (c == '\n') {
1655 ++line;
1656 col = 1;
1657 } else
1658 ++col;
1659
1660 /* Check the cursor at this position, and dump the previous one if we have
1661 * found something new.
1662 */
1663 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1664 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1665 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregor37aa4932011-05-04 00:14:37 +00001666 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbar02968e52010-02-14 10:02:57 +00001667 line, col, prefix);
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001668 start_line = line;
1669 start_col = col;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001670 }
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001671 if (c == EOF)
1672 break;
Benjamin Kramer10d083172009-11-17 20:51:40 +00001673
Daniel Dunbareb27e7d2010-02-14 08:32:32 +00001674 prevCursor = cursor;
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001675 }
Ted Kremenek29004672010-02-17 00:41:32 +00001676
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001677 fclose(fp);
Douglas Gregor7a964ad2011-01-31 22:04:05 +00001678 clang_disposeTranslationUnit(TU);
1679 clang_disposeIndex(Idx);
Ted Kremenek2df52dc2009-11-17 19:37:36 +00001680 return 0;
1681}
1682
1683/******************************************************************************/
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00001684/* Logic for testing clang code completion. */
Ted Kremenek1cd27d52009-11-17 18:13:31 +00001685/******************************************************************************/
1686
Douglas Gregor9eb77012009-11-07 00:00:49 +00001687/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1688 on failure. If successful, the pointer *filename will contain newly-allocated
1689 memory (that will be owned by the caller) to store the file name. */
Ted Kremenek29004672010-02-17 00:41:32 +00001690int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001691 unsigned *column, unsigned *second_line,
1692 unsigned *second_column) {
Douglas Gregorf96ea292009-11-09 18:19:57 +00001693 /* Find the second colon. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001694 const char *last_colon = strrchr(input, ':');
1695 unsigned values[4], i;
1696 unsigned num_values = (second_line && second_column)? 4 : 2;
1697
Douglas Gregor9eb77012009-11-07 00:00:49 +00001698 char *endptr = 0;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001699 if (!last_colon || last_colon == input) {
1700 if (num_values == 4)
1701 fprintf(stderr, "could not parse filename:line:column:line:column in "
1702 "'%s'\n", input);
1703 else
1704 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001705 return 1;
1706 }
1707
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001708 for (i = 0; i != num_values; ++i) {
1709 const char *prev_colon;
1710
1711 /* Parse the next line or column. */
1712 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1713 if (*endptr != 0 && *endptr != ':') {
Ted Kremenek29004672010-02-17 00:41:32 +00001714 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001715 (i % 2 ? "column" : "line"), input);
1716 return 1;
1717 }
Ted Kremenek29004672010-02-17 00:41:32 +00001718
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001719 if (i + 1 == num_values)
1720 break;
1721
1722 /* Find the previous colon. */
1723 prev_colon = last_colon - 1;
1724 while (prev_colon != input && *prev_colon != ':')
1725 --prev_colon;
1726 if (prev_colon == input) {
Ted Kremenek29004672010-02-17 00:41:32 +00001727 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001728 (i % 2 == 0? "column" : "line"), input);
Ted Kremenek29004672010-02-17 00:41:32 +00001729 return 1;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001730 }
1731
1732 last_colon = prev_colon;
Douglas Gregorf96ea292009-11-09 18:19:57 +00001733 }
1734
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001735 *line = values[0];
1736 *column = values[1];
Ted Kremenek29004672010-02-17 00:41:32 +00001737
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001738 if (second_line && second_column) {
1739 *second_line = values[2];
1740 *second_column = values[3];
1741 }
1742
Douglas Gregorf96ea292009-11-09 18:19:57 +00001743 /* Copy the file name. */
Douglas Gregor27b4fa92010-01-26 17:06:03 +00001744 *filename = (char*)malloc(last_colon - input + 1);
1745 memcpy(*filename, input, last_colon - input);
1746 (*filename)[last_colon - input] = 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001747 return 0;
1748}
1749
1750const char *
1751clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1752 switch (Kind) {
1753 case CXCompletionChunk_Optional: return "Optional";
1754 case CXCompletionChunk_TypedText: return "TypedText";
1755 case CXCompletionChunk_Text: return "Text";
1756 case CXCompletionChunk_Placeholder: return "Placeholder";
1757 case CXCompletionChunk_Informative: return "Informative";
1758 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1759 case CXCompletionChunk_LeftParen: return "LeftParen";
1760 case CXCompletionChunk_RightParen: return "RightParen";
1761 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1762 case CXCompletionChunk_RightBracket: return "RightBracket";
1763 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1764 case CXCompletionChunk_RightBrace: return "RightBrace";
1765 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1766 case CXCompletionChunk_RightAngle: return "RightAngle";
1767 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorb3fa9192009-12-18 18:53:37 +00001768 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor504a6ae2010-01-10 23:08:15 +00001769 case CXCompletionChunk_Colon: return "Colon";
1770 case CXCompletionChunk_SemiColon: return "SemiColon";
1771 case CXCompletionChunk_Equal: return "Equal";
1772 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1773 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor9eb77012009-11-07 00:00:49 +00001774 }
Ted Kremenek29004672010-02-17 00:41:32 +00001775
Douglas Gregor9eb77012009-11-07 00:00:49 +00001776 return "Unknown";
1777}
1778
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00001779static int checkForErrors(CXTranslationUnit TU) {
1780 unsigned Num, i;
1781 CXDiagnostic Diag;
1782 CXString DiagStr;
1783
1784 if (!getenv("CINDEXTEST_FAILONERROR"))
1785 return 0;
1786
1787 Num = clang_getNumDiagnostics(TU);
1788 for (i = 0; i != Num; ++i) {
1789 Diag = clang_getDiagnostic(TU, i);
1790 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1791 DiagStr = clang_formatDiagnostic(Diag,
1792 clang_defaultDiagnosticDisplayOptions());
1793 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1794 clang_disposeString(DiagStr);
1795 clang_disposeDiagnostic(Diag);
1796 return -1;
1797 }
1798 clang_disposeDiagnostic(Diag);
1799 }
1800
1801 return 0;
1802}
1803
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001804void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00001805 int I, N;
Ted Kremenek29004672010-02-17 00:41:32 +00001806
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001807 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001808 for (I = 0; I != N; ++I) {
Ted Kremenekf602f962010-02-17 01:42:24 +00001809 CXString text;
1810 const char *cstr;
Douglas Gregor9eb77012009-11-07 00:00:49 +00001811 enum CXCompletionChunkKind Kind
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001812 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremenek29004672010-02-17 00:41:32 +00001813
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001814 if (Kind == CXCompletionChunk_Optional) {
1815 fprintf(file, "{Optional ");
1816 print_completion_string(
Ted Kremenek29004672010-02-17 00:41:32 +00001817 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001818 file);
1819 fprintf(file, "}");
1820 continue;
Douglas Gregor8ed5b772010-10-08 20:39:29 +00001821 }
1822
1823 if (Kind == CXCompletionChunk_VerticalSpace) {
1824 fprintf(file, "{VerticalSpace }");
1825 continue;
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001826 }
Ted Kremenek29004672010-02-17 00:41:32 +00001827
Douglas Gregorf81f5282009-11-09 17:05:28 +00001828 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenekf602f962010-02-17 01:42:24 +00001829 cstr = clang_getCString(text);
Ted Kremenek29004672010-02-17 00:41:32 +00001830 fprintf(file, "{%s %s}",
Douglas Gregor9eb77012009-11-07 00:00:49 +00001831 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenekf602f962010-02-17 01:42:24 +00001832 cstr ? cstr : "");
1833 clang_disposeString(text);
Douglas Gregor9eb77012009-11-07 00:00:49 +00001834 }
Ted Kremenekf602f962010-02-17 01:42:24 +00001835
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001836}
1837
1838void print_completion_result(CXCompletionResult *completion_result,
1839 CXClientData client_data) {
1840 FILE *file = (FILE *)client_data;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001841 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001842 unsigned annotationCount;
Douglas Gregor78254c82012-03-27 23:34:16 +00001843 enum CXCursorKind ParentKind;
1844 CXString ParentName;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001845 CXString BriefComment;
1846 const char *BriefCommentCString;
Douglas Gregor78254c82012-03-27 23:34:16 +00001847
Ted Kremenek29004672010-02-17 00:41:32 +00001848 fprintf(file, "%s:", clang_getCString(ks));
1849 clang_disposeString(ks);
1850
Douglas Gregor8b14f8f2009-11-09 16:04:45 +00001851 print_completion_string(completion_result->CompletionString, file);
Douglas Gregorf757a122010-08-23 23:00:57 +00001852 fprintf(file, " (%u)",
Douglas Gregora2db7932010-05-26 22:00:08 +00001853 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregorf757a122010-08-23 23:00:57 +00001854 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1855 case CXAvailability_Available:
1856 break;
1857
1858 case CXAvailability_Deprecated:
1859 fprintf(file, " (deprecated)");
1860 break;
1861
1862 case CXAvailability_NotAvailable:
1863 fprintf(file, " (unavailable)");
1864 break;
Erik Verbruggen2e657ff2011-10-06 07:27:49 +00001865
1866 case CXAvailability_NotAccessible:
1867 fprintf(file, " (inaccessible)");
1868 break;
Douglas Gregorf757a122010-08-23 23:00:57 +00001869 }
Erik Verbruggen98ea7f62011-10-14 15:31:08 +00001870
1871 annotationCount = clang_getCompletionNumAnnotations(
1872 completion_result->CompletionString);
1873 if (annotationCount) {
1874 unsigned i;
1875 fprintf(file, " (");
1876 for (i = 0; i < annotationCount; ++i) {
1877 if (i != 0)
1878 fprintf(file, ", ");
1879 fprintf(file, "\"%s\"",
1880 clang_getCString(clang_getCompletionAnnotation(
1881 completion_result->CompletionString, i)));
1882 }
1883 fprintf(file, ")");
1884 }
1885
Douglas Gregor78254c82012-03-27 23:34:16 +00001886 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1887 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1888 &ParentKind);
1889 if (ParentKind != CXCursor_NotImplemented) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00001890 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregor78254c82012-03-27 23:34:16 +00001891 fprintf(file, " (parent: %s '%s')",
1892 clang_getCString(KindSpelling),
1893 clang_getCString(ParentName));
1894 clang_disposeString(KindSpelling);
1895 }
1896 clang_disposeString(ParentName);
1897 }
Dmitri Gribenko3292d062012-07-02 17:35:10 +00001898
1899 BriefComment = clang_getCompletionBriefComment(
1900 completion_result->CompletionString);
1901 BriefCommentCString = clang_getCString(BriefComment);
1902 if (BriefCommentCString && *BriefCommentCString != '\0') {
1903 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1904 }
1905 clang_disposeString(BriefComment);
Douglas Gregor78254c82012-03-27 23:34:16 +00001906
Douglas Gregorf757a122010-08-23 23:00:57 +00001907 fprintf(file, "\n");
Douglas Gregor9eb77012009-11-07 00:00:49 +00001908}
1909
Douglas Gregor21325842011-07-07 16:03:39 +00001910void print_completion_contexts(unsigned long long contexts, FILE *file) {
1911 fprintf(file, "Completion contexts:\n");
1912 if (contexts == CXCompletionContext_Unknown) {
1913 fprintf(file, "Unknown\n");
1914 }
1915 if (contexts & CXCompletionContext_AnyType) {
1916 fprintf(file, "Any type\n");
1917 }
1918 if (contexts & CXCompletionContext_AnyValue) {
1919 fprintf(file, "Any value\n");
1920 }
1921 if (contexts & CXCompletionContext_ObjCObjectValue) {
1922 fprintf(file, "Objective-C object value\n");
1923 }
1924 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1925 fprintf(file, "Objective-C selector value\n");
1926 }
1927 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1928 fprintf(file, "C++ class type value\n");
1929 }
1930 if (contexts & CXCompletionContext_DotMemberAccess) {
1931 fprintf(file, "Dot member access\n");
1932 }
1933 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1934 fprintf(file, "Arrow member access\n");
1935 }
1936 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1937 fprintf(file, "Objective-C property access\n");
1938 }
1939 if (contexts & CXCompletionContext_EnumTag) {
1940 fprintf(file, "Enum tag\n");
1941 }
1942 if (contexts & CXCompletionContext_UnionTag) {
1943 fprintf(file, "Union tag\n");
1944 }
1945 if (contexts & CXCompletionContext_StructTag) {
1946 fprintf(file, "Struct tag\n");
1947 }
1948 if (contexts & CXCompletionContext_ClassTag) {
1949 fprintf(file, "Class name\n");
1950 }
1951 if (contexts & CXCompletionContext_Namespace) {
1952 fprintf(file, "Namespace or namespace alias\n");
1953 }
1954 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1955 fprintf(file, "Nested name specifier\n");
1956 }
1957 if (contexts & CXCompletionContext_ObjCInterface) {
1958 fprintf(file, "Objective-C interface\n");
1959 }
1960 if (contexts & CXCompletionContext_ObjCProtocol) {
1961 fprintf(file, "Objective-C protocol\n");
1962 }
1963 if (contexts & CXCompletionContext_ObjCCategory) {
1964 fprintf(file, "Objective-C category\n");
1965 }
1966 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1967 fprintf(file, "Objective-C instance method\n");
1968 }
1969 if (contexts & CXCompletionContext_ObjCClassMessage) {
1970 fprintf(file, "Objective-C class method\n");
1971 }
1972 if (contexts & CXCompletionContext_ObjCSelectorName) {
1973 fprintf(file, "Objective-C selector name\n");
1974 }
1975 if (contexts & CXCompletionContext_MacroName) {
1976 fprintf(file, "Macro name\n");
1977 }
1978 if (contexts & CXCompletionContext_NaturalLanguage) {
1979 fprintf(file, "Natural language\n");
1980 }
1981}
1982
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001983int my_stricmp(const char *s1, const char *s2) {
1984 while (*s1 && *s2) {
NAKAMURA Takumid3cc2202011-03-09 03:02:28 +00001985 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00001986 if (c1 < c2)
1987 return -1;
1988 else if (c1 > c2)
1989 return 1;
1990
1991 ++s1;
1992 ++s2;
1993 }
1994
1995 if (*s1)
1996 return 1;
1997 else if (*s2)
1998 return -1;
1999 return 0;
2000}
2001
Douglas Gregor47815d52010-07-12 18:38:41 +00002002int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor9eb77012009-11-07 00:00:49 +00002003 const char *input = argv[1];
2004 char *filename = 0;
2005 unsigned line;
2006 unsigned column;
Daniel Dunbar4ba3b292009-11-07 18:34:24 +00002007 CXIndex CIdx;
Ted Kremenekef3339b2009-11-17 18:09:14 +00002008 int errorCode;
Douglas Gregor9485bf92009-12-02 09:21:34 +00002009 struct CXUnsavedFile *unsaved_files = 0;
2010 int num_unsaved_files = 0;
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002011 CXCodeCompleteResults *results = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002012 enum CXErrorCode Err;
2013 CXTranslationUnit TU;
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002014 unsigned I, Repeats = 1;
2015 unsigned completionOptions = clang_defaultCodeCompleteOptions();
2016
2017 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
2018 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenko3292d062012-07-02 17:35:10 +00002019 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
2020 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregor028d3e42010-08-09 20:45:32 +00002021
Douglas Gregor47815d52010-07-12 18:38:41 +00002022 if (timing_only)
2023 input += strlen("-code-completion-timing=");
2024 else
2025 input += strlen("-code-completion-at=");
2026
Ted Kremenek29004672010-02-17 00:41:32 +00002027 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002028 0, 0)))
Ted Kremenekef3339b2009-11-17 18:09:14 +00002029 return errorCode;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002030
Douglas Gregor9485bf92009-12-02 09:21:34 +00002031 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
2032 return -1;
2033
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002034 CIdx = clang_createIndex(0, 0);
2035
2036 if (getenv("CINDEXTEST_EDITING"))
2037 Repeats = 5;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002038
2039 Err = clang_parseTranslationUnit2(CIdx, 0,
2040 argv + num_unsaved_files + 2,
2041 argc - num_unsaved_files - 2,
2042 0, 0, getDefaultParsingOptions(), &TU);
2043 if (Err != CXError_Success) {
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002044 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002045 describeLibclangFailure(Err);
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002046 return 1;
2047 }
Douglas Gregorc6592922010-11-15 23:00:34 +00002048
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002049 Err = clang_reparseTranslationUnit(TU, 0, 0,
2050 clang_defaultReparseOptions(TU));
2051
2052 if (Err != CXError_Success) {
Douglas Gregorc6592922010-11-15 23:00:34 +00002053 fprintf(stderr, "Unable to reparse translation init!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002054 describeLibclangFailure(Err);
2055 clang_disposeTranslationUnit(TU);
Douglas Gregorc6592922010-11-15 23:00:34 +00002056 return 1;
2057 }
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002058
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002059 for (I = 0; I != Repeats; ++I) {
2060 results = clang_codeCompleteAt(TU, filename, line, column,
2061 unsaved_files, num_unsaved_files,
2062 completionOptions);
2063 if (!results) {
2064 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar186f7422010-08-19 23:44:06 +00002065 return 1;
2066 }
Douglas Gregor36e3b5c2010-10-11 21:37:58 +00002067 if (I != Repeats-1)
2068 clang_disposeCodeCompleteResults(results);
2069 }
Douglas Gregorba965fb2010-01-28 00:56:43 +00002070
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002071 if (results) {
Douglas Gregor63745d52011-07-21 01:05:26 +00002072 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor21325842011-07-07 16:03:39 +00002073 unsigned long long contexts;
Douglas Gregor63745d52011-07-21 01:05:26 +00002074 enum CXCursorKind containerKind;
Douglas Gregorea777402011-07-26 15:24:30 +00002075 CXString objCSelector;
2076 const char *selectorString;
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002077 if (!timing_only) {
2078 /* Sort the code-completion results based on the typed text. */
2079 clang_sortCodeCompletionResults(results->Results, results->NumResults);
2080
Douglas Gregor47815d52010-07-12 18:38:41 +00002081 for (i = 0; i != n; ++i)
2082 print_completion_result(results->Results + i, stdout);
Douglas Gregor49f67ce2010-08-26 13:48:20 +00002083 }
Douglas Gregor33cdd812010-02-18 18:08:43 +00002084 n = clang_codeCompleteGetNumDiagnostics(results);
2085 for (i = 0; i != n; ++i) {
2086 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
2087 PrintDiagnostic(diag);
2088 clang_disposeDiagnostic(diag);
2089 }
Douglas Gregor21325842011-07-07 16:03:39 +00002090
2091 contexts = clang_codeCompleteGetContexts(results);
2092 print_completion_contexts(contexts, stdout);
2093
Douglas Gregorea777402011-07-26 15:24:30 +00002094 containerKind = clang_codeCompleteGetContainerKind(results,
2095 &containerIsIncomplete);
Douglas Gregor63745d52011-07-21 01:05:26 +00002096
2097 if (containerKind != CXCursor_InvalidCode) {
2098 /* We have found a container */
2099 CXString containerUSR, containerKindSpelling;
2100 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
2101 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
2102 clang_disposeString(containerKindSpelling);
2103
2104 if (containerIsIncomplete) {
2105 printf("Container is incomplete\n");
2106 }
2107 else {
2108 printf("Container is complete\n");
2109 }
2110
2111 containerUSR = clang_codeCompleteGetContainerUSR(results);
2112 printf("Container USR: %s\n", clang_getCString(containerUSR));
2113 clang_disposeString(containerUSR);
2114 }
2115
Douglas Gregorea777402011-07-26 15:24:30 +00002116 objCSelector = clang_codeCompleteGetObjCSelector(results);
2117 selectorString = clang_getCString(objCSelector);
2118 if (selectorString && strlen(selectorString) > 0) {
2119 printf("Objective-C selector: %s\n", selectorString);
2120 }
2121 clang_disposeString(objCSelector);
2122
Douglas Gregorf72b6ac2009-12-18 16:20:58 +00002123 clang_disposeCodeCompleteResults(results);
2124 }
Douglas Gregor028d3e42010-08-09 20:45:32 +00002125 clang_disposeTranslationUnit(TU);
Douglas Gregor9eb77012009-11-07 00:00:49 +00002126 clang_disposeIndex(CIdx);
2127 free(filename);
Ted Kremenek29004672010-02-17 00:41:32 +00002128
Douglas Gregor9485bf92009-12-02 09:21:34 +00002129 free_remapped_files(unsaved_files, num_unsaved_files);
2130
Ted Kremenekef3339b2009-11-17 18:09:14 +00002131 return 0;
Douglas Gregor9eb77012009-11-07 00:00:49 +00002132}
2133
Douglas Gregor082c3e62010-01-15 19:40:17 +00002134typedef struct {
2135 char *filename;
2136 unsigned line;
2137 unsigned column;
2138} CursorSourceLocation;
2139
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002140static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002141 CXIndex CIdx;
2142 int errorCode;
2143 struct CXUnsavedFile *unsaved_files = 0;
2144 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002145 enum CXErrorCode Err;
Douglas Gregor082c3e62010-01-15 19:40:17 +00002146 CXTranslationUnit TU;
2147 CXCursor Cursor;
2148 CursorSourceLocation *Locations = 0;
2149 unsigned NumLocations = 0, Loc;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002150 unsigned Repeats = 1;
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002151 unsigned I;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002152
Ted Kremenek29004672010-02-17 00:41:32 +00002153 /* Count the number of locations. */
Douglas Gregor082c3e62010-01-15 19:40:17 +00002154 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2155 ++NumLocations;
Ted Kremenek29004672010-02-17 00:41:32 +00002156
Douglas Gregor082c3e62010-01-15 19:40:17 +00002157 /* Parse the locations. */
2158 assert(NumLocations > 0 && "Unable to count locations?");
2159 Locations = (CursorSourceLocation *)malloc(
2160 NumLocations * sizeof(CursorSourceLocation));
2161 for (Loc = 0; Loc < NumLocations; ++Loc) {
2162 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremenek29004672010-02-17 00:41:32 +00002163 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2164 &Locations[Loc].line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00002165 &Locations[Loc].column, 0, 0)))
Douglas Gregor082c3e62010-01-15 19:40:17 +00002166 return errorCode;
2167 }
Ted Kremenek29004672010-02-17 00:41:32 +00002168
2169 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregor082c3e62010-01-15 19:40:17 +00002170 &num_unsaved_files))
2171 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00002172
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002173 if (getenv("CINDEXTEST_EDITING"))
2174 Repeats = 5;
2175
2176 /* Parse the translation unit. When we're testing clang_getCursor() after
2177 reparsing, don't remap unsaved files until the second parse. */
2178 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002179 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2180 argv + num_unsaved_files + 1 + NumLocations,
2181 argc - num_unsaved_files - 2 - NumLocations,
2182 unsaved_files,
2183 Repeats > 1? 0 : num_unsaved_files,
2184 getDefaultParsingOptions(), &TU);
2185 if (Err != CXError_Success) {
Douglas Gregor082c3e62010-01-15 19:40:17 +00002186 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002187 describeLibclangFailure(Err);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002188 return -1;
2189 }
Ted Kremenek29004672010-02-17 00:41:32 +00002190
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002191 if (checkForErrors(TU) != 0)
2192 return -1;
2193
Douglas Gregorb42f34b2010-11-30 06:04:54 +00002194 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002195 if (Repeats > 1) {
2196 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2197 clang_defaultReparseOptions(TU));
2198 if (Err != CXError_Success) {
2199 describeLibclangFailure(Err);
2200 clang_disposeTranslationUnit(TU);
2201 return 1;
2202 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002203 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002204
2205 if (checkForErrors(TU) != 0)
2206 return -1;
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002207
2208 for (Loc = 0; Loc < NumLocations; ++Loc) {
2209 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2210 if (!file)
2211 continue;
Ted Kremenek29004672010-02-17 00:41:32 +00002212
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002213 Cursor = clang_getCursor(TU,
2214 clang_getLocation(TU, file, Locations[Loc].line,
2215 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002216
2217 if (checkForErrors(TU) != 0)
2218 return -1;
2219
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002220 if (I + 1 == Repeats) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002221 CXCompletionString completionString = clang_getCursorCompletionString(
2222 Cursor);
2223 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002224 CXString Spelling;
2225 const char *cspell;
2226 unsigned line, column;
2227 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2228 printf("%d:%d ", line, column);
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002229 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002230 PrintCursorExtent(Cursor);
2231 Spelling = clang_getCursorSpelling(Cursor);
2232 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002233 if (cspell && strlen(cspell) != 0) {
2234 unsigned pieceIndex;
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002235 printf(" Spelling=%s (", cspell);
2236 for (pieceIndex = 0; ; ++pieceIndex) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002237 CXSourceRange range =
2238 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidis191a6a82012-03-30 20:58:35 +00002239 if (clang_Range_isNull(range))
2240 break;
2241 PrintRange(range, 0);
2242 }
2243 printf(")");
2244 }
Argyrios Kyrtzidis7aa274f2012-03-30 00:19:05 +00002245 clang_disposeString(Spelling);
Argyrios Kyrtzidis210f29f2012-03-30 22:15:48 +00002246 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2247 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002248 if (clang_Cursor_isDynamicCall(Cursor))
2249 printf(" Dynamic-call");
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002250 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002251 CXType T = clang_Cursor_getReceiverType(Cursor);
2252 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisb26a24c2012-11-01 02:01:34 +00002253 printf(" Receiver-type=%s", clang_getCString(S));
2254 clang_disposeString(S);
2255 }
Argyrios Kyrtzidisb6df68212012-07-02 23:54:36 +00002256
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002257 {
2258 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002259 CXFile astFile;
2260 CXString name, astFilename;
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002261 unsigned i, numHeaders;
2262 if (mod) {
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002263 astFile = clang_Module_getASTFile(mod);
2264 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002265 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidis3c5305c2013-03-13 21:13:43 +00002266 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002267 printf(" ModuleName=%s (%s) Headers(%d):",
2268 clang_getCString(name), clang_getCString(astFilename),
2269 numHeaders);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002270 clang_disposeString(name);
Argyrios Kyrtzidis12fdb9e2013-04-26 22:47:49 +00002271 clang_disposeString(astFilename);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002272 for (i = 0; i < numHeaders; ++i) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002273 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2274 CXString filename = clang_getFileName(file);
Argyrios Kyrtzidis2b9b5bb2012-10-05 00:22:37 +00002275 printf("\n%s", clang_getCString(filename));
2276 clang_disposeString(filename);
2277 }
2278 }
2279 }
2280
Douglas Gregor3f35bb22011-08-04 20:04:59 +00002281 if (completionString != NULL) {
2282 printf("\nCompletion string: ");
2283 print_completion_string(completionString, stdout);
2284 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002285 printf("\n");
2286 free(Locations[Loc].filename);
2287 }
2288 }
Douglas Gregor082c3e62010-01-15 19:40:17 +00002289 }
Douglas Gregor2f6358b2010-11-30 05:52:55 +00002290
Douglas Gregor33cdd812010-02-18 18:08:43 +00002291 PrintDiagnostics(TU);
Douglas Gregor082c3e62010-01-15 19:40:17 +00002292 clang_disposeTranslationUnit(TU);
2293 clang_disposeIndex(CIdx);
2294 free(Locations);
2295 free_remapped_files(unsaved_files, num_unsaved_files);
2296 return 0;
2297}
2298
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002299static enum CXVisitorResult findFileRefsVisit(void *context,
2300 CXCursor cursor, CXSourceRange range) {
2301 if (clang_Range_isNull(range))
2302 return CXVisit_Continue;
2303
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002304 PrintCursor(cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002305 PrintRange(range, "");
2306 printf("\n");
2307 return CXVisit_Continue;
2308}
2309
2310static int find_file_refs_at(int argc, const char **argv) {
2311 CXIndex CIdx;
2312 int errorCode;
2313 struct CXUnsavedFile *unsaved_files = 0;
2314 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002315 enum CXErrorCode Err;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002316 CXTranslationUnit TU;
2317 CXCursor Cursor;
2318 CursorSourceLocation *Locations = 0;
2319 unsigned NumLocations = 0, Loc;
2320 unsigned Repeats = 1;
2321 unsigned I;
2322
2323 /* Count the number of locations. */
2324 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2325 ++NumLocations;
2326
2327 /* Parse the locations. */
2328 assert(NumLocations > 0 && "Unable to count locations?");
2329 Locations = (CursorSourceLocation *)malloc(
2330 NumLocations * sizeof(CursorSourceLocation));
2331 for (Loc = 0; Loc < NumLocations; ++Loc) {
2332 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2333 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2334 &Locations[Loc].line,
2335 &Locations[Loc].column, 0, 0)))
2336 return errorCode;
2337 }
2338
2339 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2340 &num_unsaved_files))
2341 return -1;
2342
2343 if (getenv("CINDEXTEST_EDITING"))
2344 Repeats = 5;
2345
2346 /* Parse the translation unit. When we're testing clang_getCursor() after
2347 reparsing, don't remap unsaved files until the second parse. */
2348 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002349 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
2350 argv + num_unsaved_files + 1 + NumLocations,
2351 argc - num_unsaved_files - 2 - NumLocations,
2352 unsaved_files,
2353 Repeats > 1? 0 : num_unsaved_files,
2354 getDefaultParsingOptions(), &TU);
2355 if (Err != CXError_Success) {
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002356 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002357 describeLibclangFailure(Err);
2358 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002359 return -1;
2360 }
2361
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002362 if (checkForErrors(TU) != 0)
2363 return -1;
2364
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002365 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002366 if (Repeats > 1) {
2367 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2368 clang_defaultReparseOptions(TU));
2369 if (Err != CXError_Success) {
2370 describeLibclangFailure(Err);
2371 clang_disposeTranslationUnit(TU);
2372 return 1;
2373 }
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002374 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002375
2376 if (checkForErrors(TU) != 0)
2377 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002378
2379 for (Loc = 0; Loc < NumLocations; ++Loc) {
2380 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2381 if (!file)
2382 continue;
2383
2384 Cursor = clang_getCursor(TU,
2385 clang_getLocation(TU, file, Locations[Loc].line,
2386 Locations[Loc].column));
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002387
2388 if (checkForErrors(TU) != 0)
2389 return -1;
2390
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002391 if (I + 1 == Repeats) {
Erik Verbruggen338b55c2011-10-06 11:38:08 +00002392 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002393 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002394 printf("\n");
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002395 clang_findReferencesInFile(Cursor, file, visitor);
2396 free(Locations[Loc].filename);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002397
2398 if (checkForErrors(TU) != 0)
2399 return -1;
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00002400 }
2401 }
2402 }
2403
2404 PrintDiagnostics(TU);
2405 clang_disposeTranslationUnit(TU);
2406 clang_disposeIndex(CIdx);
2407 free(Locations);
2408 free_remapped_files(unsaved_files, num_unsaved_files);
2409 return 0;
2410}
2411
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002412static enum CXVisitorResult findFileIncludesVisit(void *context,
2413 CXCursor cursor, CXSourceRange range) {
2414 PrintCursor(cursor, NULL);
2415 PrintRange(range, "");
2416 printf("\n");
2417 return CXVisit_Continue;
2418}
2419
2420static int find_file_includes_in(int argc, const char **argv) {
2421 CXIndex CIdx;
2422 struct CXUnsavedFile *unsaved_files = 0;
2423 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002424 enum CXErrorCode Err;
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002425 CXTranslationUnit TU;
2426 const char **Filenames = 0;
2427 unsigned NumFilenames = 0;
2428 unsigned Repeats = 1;
2429 unsigned I, FI;
2430
2431 /* Count the number of locations. */
2432 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2433 ++NumFilenames;
2434
2435 /* Parse the locations. */
2436 assert(NumFilenames > 0 && "Unable to count filenames?");
2437 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2438 for (I = 0; I < NumFilenames; ++I) {
2439 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2440 /* Copy the file name. */
2441 Filenames[I] = input;
2442 }
2443
2444 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2445 &num_unsaved_files))
2446 return -1;
2447
2448 if (getenv("CINDEXTEST_EDITING"))
2449 Repeats = 2;
2450
2451 /* Parse the translation unit. When we're testing clang_getCursor() after
2452 reparsing, don't remap unsaved files until the second parse. */
2453 CIdx = clang_createIndex(1, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002454 Err = clang_parseTranslationUnit2(
2455 CIdx, argv[argc - 1],
2456 argv + num_unsaved_files + 1 + NumFilenames,
2457 argc - num_unsaved_files - 2 - NumFilenames,
2458 unsaved_files,
2459 Repeats > 1 ? 0 : num_unsaved_files, getDefaultParsingOptions(), &TU);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002460
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002461 if (Err != CXError_Success) {
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002462 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002463 describeLibclangFailure(Err);
2464 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002465 return -1;
2466 }
2467
2468 if (checkForErrors(TU) != 0)
2469 return -1;
2470
2471 for (I = 0; I != Repeats; ++I) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00002472 if (Repeats > 1) {
2473 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2474 clang_defaultReparseOptions(TU));
2475 if (Err != CXError_Success) {
2476 describeLibclangFailure(Err);
2477 clang_disposeTranslationUnit(TU);
2478 return 1;
2479 }
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002480 }
2481
2482 if (checkForErrors(TU) != 0)
2483 return -1;
2484
2485 for (FI = 0; FI < NumFilenames; ++FI) {
2486 CXFile file = clang_getFile(TU, Filenames[FI]);
2487 if (!file)
2488 continue;
2489
2490 if (checkForErrors(TU) != 0)
2491 return -1;
2492
2493 if (I + 1 == Repeats) {
2494 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2495 clang_findIncludesInFile(TU, file, visitor);
2496
2497 if (checkForErrors(TU) != 0)
2498 return -1;
2499 }
2500 }
2501 }
2502
2503 PrintDiagnostics(TU);
2504 clang_disposeTranslationUnit(TU);
2505 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis1b5b1ce2013-03-11 16:03:17 +00002506 free((void *)Filenames);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00002507 free_remapped_files(unsaved_files, num_unsaved_files);
2508 return 0;
2509}
2510
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002511#define MAX_IMPORTED_ASTFILES 200
2512
2513typedef struct {
2514 char **filenames;
2515 unsigned num_files;
2516} ImportedASTFilesData;
2517
2518static ImportedASTFilesData *importedASTs_create() {
2519 ImportedASTFilesData *p;
2520 p = malloc(sizeof(ImportedASTFilesData));
2521 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2522 p->num_files = 0;
2523 return p;
2524}
2525
2526static void importedASTs_dispose(ImportedASTFilesData *p) {
2527 unsigned i;
2528 if (!p)
2529 return;
2530
2531 for (i = 0; i < p->num_files; ++i)
2532 free(p->filenames[i]);
2533 free(p->filenames);
2534 free(p);
2535}
2536
2537static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2538 unsigned i;
2539 assert(p && file);
2540 for (i = 0; i < p->num_files; ++i)
2541 if (strcmp(file, p->filenames[i]) == 0)
2542 return;
2543 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2544 p->filenames[p->num_files++] = strdup(file);
2545}
2546
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002547typedef struct {
2548 const char *check_prefix;
2549 int first_check_printed;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002550 int fail_for_error;
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002551 int abort;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002552 const char *main_filename;
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002553 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002554} IndexData;
2555
2556static void printCheck(IndexData *data) {
2557 if (data->check_prefix) {
2558 if (data->first_check_printed) {
2559 printf("// %s-NEXT: ", data->check_prefix);
2560 } else {
2561 printf("// %s : ", data->check_prefix);
2562 data->first_check_printed = 1;
2563 }
2564 }
2565}
2566
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002567static void printCXIndexFile(CXIdxClientFile file) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002568 CXString filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002569 printf("%s", clang_getCString(filename));
2570 clang_disposeString(filename);
2571}
2572
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002573static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2574 IndexData *index_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002575 CXString filename;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002576 const char *cname;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002577 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002578 unsigned line, column;
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002579 int isMainFile;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002580
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002581 index_data = (IndexData *)client_data;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002582 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2583 if (line == 0) {
Argyrios Kyrtzidis9f571862012-10-11 19:00:44 +00002584 printf("<invalid>");
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002585 return;
2586 }
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002587 if (!file) {
2588 printf("<no idxfile>");
2589 return;
2590 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002591 filename = clang_getFileName((CXFile)file);
2592 cname = clang_getCString(filename);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002593 if (strcmp(cname, index_data->main_filename) == 0)
2594 isMainFile = 1;
2595 else
2596 isMainFile = 0;
2597 clang_disposeString(filename);
2598
2599 if (!isMainFile) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002600 printCXIndexFile(file);
2601 printf(":");
2602 }
2603 printf("%d:%d", line, column);
2604}
2605
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002606static unsigned digitCount(unsigned val) {
2607 unsigned c = 1;
2608 while (1) {
2609 if (val < 10)
2610 return c;
2611 ++c;
2612 val /= 10;
2613 }
2614}
2615
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002616static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2617 CXIdxLoc loc) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002618 const char *name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002619 char *newStr;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002620 CXIdxClientFile file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002621 unsigned line, column;
2622
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002623 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002624 if (!name)
2625 name = "<anon-tag>";
2626
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002627 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidis90068072011-10-20 17:21:46 +00002628 /* FIXME: free these.*/
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002629 newStr = (char *)malloc(strlen(name) +
2630 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002631 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002632 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002633}
2634
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002635static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2636 CXIdxClientContainer container;
2637 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidisdf15c202011-11-16 02:35:05 +00002638 if (!container)
2639 printf("[<<NULL>>]");
2640 else
2641 printf("[%s]", (const char *)container);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002642}
2643
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002644static const char *getEntityKindString(CXIdxEntityKind kind) {
2645 switch (kind) {
2646 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2647 case CXIdxEntity_Typedef: return "typedef";
2648 case CXIdxEntity_Function: return "function";
2649 case CXIdxEntity_Variable: return "variable";
2650 case CXIdxEntity_Field: return "field";
2651 case CXIdxEntity_EnumConstant: return "enumerator";
2652 case CXIdxEntity_ObjCClass: return "objc-class";
2653 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2654 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002655 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2656 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002657 case CXIdxEntity_ObjCProperty: return "objc-property";
2658 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2659 case CXIdxEntity_Enum: return "enum";
2660 case CXIdxEntity_Struct: return "struct";
2661 case CXIdxEntity_Union: return "union";
2662 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002663 case CXIdxEntity_CXXNamespace: return "namespace";
2664 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2665 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2666 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2667 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2668 case CXIdxEntity_CXXConstructor: return "constructor";
2669 case CXIdxEntity_CXXDestructor: return "destructor";
2670 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2671 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikiedcefd952012-08-31 21:55:26 +00002672 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002673 }
2674 assert(0 && "Garbage entity kind");
2675 return 0;
2676}
2677
2678static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2679 switch (kind) {
2680 case CXIdxEntity_NonTemplate: return "";
2681 case CXIdxEntity_Template: return "-template";
2682 case CXIdxEntity_TemplatePartialSpecialization:
2683 return "-template-partial-spec";
2684 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002685 }
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002686 assert(0 && "Garbage entity kind");
2687 return 0;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002688}
2689
Argyrios Kyrtzidis52002882011-12-07 20:44:12 +00002690static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2691 switch (kind) {
2692 case CXIdxEntityLang_None: return "<none>";
2693 case CXIdxEntityLang_C: return "C";
2694 case CXIdxEntityLang_ObjC: return "ObjC";
2695 case CXIdxEntityLang_CXX: return "C++";
2696 }
2697 assert(0 && "Garbage language kind");
2698 return 0;
2699}
2700
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002701static void printEntityInfo(const char *cb,
2702 CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002703 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002704 const char *name;
2705 IndexData *index_data;
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002706 unsigned i;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002707 index_data = (IndexData *)client_data;
2708 printCheck(index_data);
2709
Argyrios Kyrtzidise4acd232011-11-16 02:34:59 +00002710 if (!info) {
2711 printf("%s: <<NULL>>", cb);
2712 return;
2713 }
2714
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002715 name = info->name;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002716 if (!name)
2717 name = "<anon-tag>";
2718
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002719 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2720 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002721 printf(" | name: %s", name);
2722 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisccdf8272011-12-13 18:47:35 +00002723 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002724
2725 for (i = 0; i != info->numAttributes; ++i) {
2726 const CXIdxAttrInfo *Attr = info->attributes[i];
2727 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002728 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis4d873b72011-12-15 00:05:00 +00002729 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002730}
2731
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002732static void printBaseClassInfo(CXClientData client_data,
2733 const CXIdxBaseClassInfo *info) {
2734 printEntityInfo(" <base>", client_data, info->base);
2735 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002736 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002737 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002738 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002739}
2740
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002741static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2742 CXClientData client_data) {
2743 unsigned i;
2744 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2745 printEntityInfo(" <protocol>", client_data,
2746 ProtoInfo->protocols[i]->protocol);
2747 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002748 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002749 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002750 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002751 printf("\n");
2752 }
2753}
2754
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002755static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002756 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002757 CXString str;
2758 const char *cstr;
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002759 unsigned numDiags, i;
2760 CXDiagnostic diag;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002761 IndexData *index_data;
2762 index_data = (IndexData *)client_data;
2763 printCheck(index_data);
2764
Argyrios Kyrtzidisf2d99b02011-12-01 02:42:50 +00002765 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2766 for (i = 0; i != numDiags; ++i) {
2767 diag = clang_getDiagnosticInSet(diagSet, i);
2768 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2769 cstr = clang_getCString(str);
2770 printf("[diagnostic]: %s\n", cstr);
2771 clang_disposeString(str);
2772
2773 if (getenv("CINDEXTEST_FAILONERROR") &&
2774 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2775 index_data->fail_for_error = 1;
2776 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00002777 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002778}
2779
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002780static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2781 CXFile file, void *reserved) {
2782 IndexData *index_data;
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002783 CXString filename;
2784
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002785 index_data = (IndexData *)client_data;
2786 printCheck(index_data);
2787
Argyrios Kyrtzidisa15f8162012-03-15 18:48:52 +00002788 filename = clang_getFileName(file);
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002789 index_data->main_filename = clang_getCString(filename);
2790 clang_disposeString(filename);
2791
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002792 printf("[enteredMainFile]: ");
2793 printCXIndexFile((CXIdxClientFile)file);
2794 printf("\n");
2795
2796 return (CXIdxClientFile)file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002797}
2798
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002799static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002800 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002801 IndexData *index_data;
2802 index_data = (IndexData *)client_data;
2803 printCheck(index_data);
2804
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002805 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002806 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002807 printf(" | name: \"%s\"", info->filename);
2808 printf(" | hash loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002809 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis5e2ec482012-10-18 00:17:05 +00002810 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2811 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002812
2813 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002814}
2815
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002816static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2817 const CXIdxImportedASTFileInfo *info) {
2818 IndexData *index_data;
2819 index_data = (IndexData *)client_data;
2820 printCheck(index_data);
2821
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002822 if (index_data->importedASTs) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002823 CXString filename = clang_getFileName(info->file);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00002824 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2825 clang_disposeString(filename);
2826 }
2827
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002828 printf("[importedASTFile]: ");
2829 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002830 if (info->module) {
Enea Zaffanella476f38a2013-07-22 20:58:30 +00002831 CXString name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002832 printf(" | loc: ");
2833 printCXIndexLoc(info->loc, client_data);
2834 printf(" | name: \"%s\"", clang_getCString(name));
2835 printf(" | isImplicit: %d\n", info->isImplicit);
2836 clang_disposeString(name);
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002837 } else {
NAKAMURA Takumie259d912012-10-12 14:25:52 +00002838 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis0db720f2012-10-11 16:05:00 +00002839 printf("\n");
Argyrios Kyrtzidisdc78f3e2012-10-05 00:22:40 +00002840 }
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002841
2842 return (CXIdxClientFile)info->file;
2843}
2844
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002845static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002846 void *reserved) {
2847 IndexData *index_data;
2848 index_data = (IndexData *)client_data;
2849 printCheck(index_data);
2850
Argyrios Kyrtzidis8c258042011-11-05 04:03:35 +00002851 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002852 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002853}
2854
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002855static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002856 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002857 IndexData *index_data;
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002858 const CXIdxObjCCategoryDeclInfo *CatInfo;
2859 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002860 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002861 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002862 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002863 unsigned i;
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002864 index_data = (IndexData *)client_data;
2865
2866 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2867 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002868 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002869 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002870 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis663c8ec2011-12-07 20:44:19 +00002871 printf(" | semantic-container: ");
2872 printCXIndexContainer(info->semanticContainer);
2873 printf(" | lexical-container: ");
2874 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002875 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002876 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002877 if (info->flags & CXIdxDeclFlag_Skipped) {
2878 assert(!info->isContainer);
2879 printf(" | isContainer: skipped");
2880 } else {
2881 printf(" | isContainer: %d", info->isContainer);
2882 }
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002883 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002884
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002885 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi2a4859a2011-11-18 00:51:03 +00002886 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002887 printf(" <attribute>: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002888 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002889 printf("\n");
2890 }
2891
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002892 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2893 const char *kindName = 0;
2894 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2895 switch (K) {
2896 case CXIdxObjCContainer_ForwardRef:
2897 kindName = "forward-ref"; break;
2898 case CXIdxObjCContainer_Interface:
2899 kindName = "interface"; break;
2900 case CXIdxObjCContainer_Implementation:
2901 kindName = "implementation"; break;
2902 }
2903 printCheck(index_data);
2904 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2905 }
2906
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002907 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002908 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2909 CatInfo->objcClass);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002910 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002911 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00002912 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002913 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002914 printf("\n");
2915 }
2916
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002917 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2918 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002919 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002920 printf("\n");
2921 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002922 }
2923
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002924 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2925 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002926 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002927
Argyrios Kyrtzidis93db2922012-02-28 17:50:33 +00002928 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2929 if (PropInfo->getter) {
2930 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2931 printf("\n");
2932 }
2933 if (PropInfo->setter) {
2934 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2935 printf("\n");
2936 }
2937 }
2938
Argyrios Kyrtzidisb3c16ba2011-12-07 20:44:15 +00002939 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2940 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2941 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2942 printf("\n");
2943 }
2944 }
2945
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00002946 if (info->declAsContainer)
2947 clang_index_setClientContainer(info->declAsContainer,
2948 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002949}
2950
2951static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis3e429e72011-11-12 02:16:30 +00002952 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002953 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002954 printf(" | cursor: ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00002955 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002956 printf(" | loc: ");
Argyrios Kyrtzidis0abc5eb2012-03-15 18:07:22 +00002957 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002958 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002959 printf(" | container: ");
2960 printCXIndexContainer(info->container);
Argyrios Kyrtzidis86acd722011-11-14 22:39:19 +00002961 printf(" | refkind: ");
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002962 switch (info->kind) {
2963 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidiseffdbf52011-11-18 00:26:51 +00002964 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidis0c7735e52011-10-18 15:50:50 +00002965 }
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002966 printf("\n");
2967}
2968
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002969static int index_abortQuery(CXClientData client_data, void *reserved) {
2970 IndexData *index_data;
2971 index_data = (IndexData *)client_data;
2972 return index_data->abort;
2973}
2974
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002975static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidisb11f5a42011-11-28 04:56:00 +00002976 index_abortQuery,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002977 index_diagnostic,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002978 index_enteredMainFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002979 index_ppIncludedFile,
Argyrios Kyrtzidis472eda02012-10-02 16:10:38 +00002980 index_importedASTFile,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002981 index_startedTranslationUnit,
Argyrios Kyrtzidis7519c5e2011-11-11 00:23:36 +00002982 index_indexDeclaration,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00002983 index_indexEntityReference
2984};
2985
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00002986static unsigned getIndexOptions(void) {
2987 unsigned index_opts;
2988 index_opts = 0;
2989 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2990 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2991 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2992 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis8b71bc72012-12-06 19:41:16 +00002993 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
2994 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidisfb7d1452012-01-14 00:11:49 +00002995
2996 return index_opts;
2997}
2998
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00002999static int index_compile_args(int num_args, const char **args,
3000 CXIndexAction idxAction,
3001 ImportedASTFilesData *importedASTs,
3002 const char *check_prefix) {
3003 IndexData index_data;
3004 unsigned index_opts;
3005 int result;
3006
3007 if (num_args == 0) {
3008 fprintf(stderr, "no compiler arguments\n");
3009 return -1;
3010 }
3011
3012 index_data.check_prefix = check_prefix;
3013 index_data.first_check_printed = 0;
3014 index_data.fail_for_error = 0;
3015 index_data.abort = 0;
3016 index_data.main_filename = "";
3017 index_data.importedASTs = importedASTs;
3018
3019 index_opts = getIndexOptions();
3020 result = clang_indexSourceFile(idxAction, &index_data,
3021 &IndexCB,sizeof(IndexCB), index_opts,
3022 0, args, num_args, 0, 0, 0,
3023 getDefaultParsingOptions());
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003024 if (result != CXError_Success)
3025 describeLibclangFailure(result);
3026
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003027 if (index_data.fail_for_error)
3028 result = -1;
3029
3030 return result;
3031}
3032
3033static int index_ast_file(const char *ast_file,
3034 CXIndex Idx,
3035 CXIndexAction idxAction,
3036 ImportedASTFilesData *importedASTs,
3037 const char *check_prefix) {
3038 CXTranslationUnit TU;
3039 IndexData index_data;
3040 unsigned index_opts;
3041 int result;
3042
3043 if (!CreateTranslationUnit(Idx, ast_file, &TU))
3044 return -1;
3045
3046 index_data.check_prefix = check_prefix;
3047 index_data.first_check_printed = 0;
3048 index_data.fail_for_error = 0;
3049 index_data.abort = 0;
3050 index_data.main_filename = "";
3051 index_data.importedASTs = importedASTs;
3052
3053 index_opts = getIndexOptions();
3054 result = clang_indexTranslationUnit(idxAction, &index_data,
3055 &IndexCB,sizeof(IndexCB),
3056 index_opts, TU);
3057 if (index_data.fail_for_error)
3058 result = -1;
3059
3060 clang_disposeTranslationUnit(TU);
3061 return result;
3062}
3063
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003064static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003065 const char *check_prefix;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003066 CXIndex Idx;
3067 CXIndexAction idxAction;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003068 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003069 int result;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003070
3071 check_prefix = 0;
3072 if (argc > 0) {
3073 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3074 check_prefix = argv[0] + strlen("-check-prefix=");
3075 ++argv;
3076 --argc;
3077 }
3078 }
3079
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003080 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003081 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003082 fprintf(stderr, "Could not create Index\n");
3083 return 1;
3084 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003085 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003086 importedASTs = 0;
3087 if (full)
3088 importedASTs = importedASTs_create();
3089
3090 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
3091 if (result != 0)
3092 goto finished;
3093
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003094 if (full) {
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003095 unsigned i;
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003096 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
3097 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
3098 importedASTs, check_prefix);
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003099 }
3100 }
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003101
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003102finished:
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003103 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003104 clang_IndexAction_dispose(idxAction);
3105 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003106 return result;
3107}
3108
3109static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003110 const char *check_prefix;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003111 CXIndex Idx;
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003112 CXIndexAction idxAction;
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003113 int result;
3114
3115 check_prefix = 0;
3116 if (argc > 0) {
3117 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3118 check_prefix = argv[0] + strlen("-check-prefix=");
3119 ++argv;
3120 --argc;
3121 }
3122 }
3123
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003124 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003125 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003126 fprintf(stderr, "Could not create Index\n");
3127 return 1;
3128 }
3129 idxAction = clang_IndexAction_create(Idx);
3130
3131 result = index_ast_file(argv[0], Idx, idxAction,
3132 /*importedASTs=*/0, check_prefix);
3133
3134 clang_IndexAction_dispose(idxAction);
3135 clang_disposeIndex(Idx);
3136 return result;
3137}
3138
3139static int index_compile_db(int argc, const char **argv) {
3140 const char *check_prefix;
3141 CXIndex Idx;
3142 CXIndexAction idxAction;
3143 int errorCode = 0;
3144
3145 check_prefix = 0;
3146 if (argc > 0) {
3147 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3148 check_prefix = argv[0] + strlen("-check-prefix=");
3149 ++argv;
3150 --argc;
3151 }
3152 }
3153
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003154 if (argc == 0) {
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003155 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003156 return -1;
3157 }
3158
3159 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003160 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003161 fprintf(stderr, "Could not create Index\n");
3162 return 1;
3163 }
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003164 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003165
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003166 {
3167 const char *database = argv[0];
3168 CXCompilationDatabase db = 0;
3169 CXCompileCommands CCmds = 0;
3170 CXCompileCommand CCmd;
3171 CXCompilationDatabase_Error ec;
3172 CXString wd;
3173#define MAX_COMPILE_ARGS 512
3174 CXString cxargs[MAX_COMPILE_ARGS];
3175 const char *args[MAX_COMPILE_ARGS];
3176 char *tmp;
3177 unsigned len;
3178 char *buildDir;
3179 int i, a, numCmds, numArgs;
3180
3181 len = strlen(database);
3182 tmp = (char *) malloc(len+1);
3183 memcpy(tmp, database, len+1);
3184 buildDir = dirname(tmp);
3185
3186 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3187
3188 if (db) {
3189
3190 if (ec!=CXCompilationDatabase_NoError) {
3191 printf("unexpected error %d code while loading compilation database\n", ec);
3192 errorCode = -1;
3193 goto cdb_end;
3194 }
3195
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003196 if (chdir(buildDir) != 0) {
3197 printf("Could not chdir to %s\n", buildDir);
3198 errorCode = -1;
3199 goto cdb_end;
3200 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003201
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003202 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003203 if (!CCmds) {
3204 printf("compilation db is empty\n");
3205 errorCode = -1;
3206 goto cdb_end;
3207 }
3208
3209 numCmds = clang_CompileCommands_getSize(CCmds);
3210
3211 if (numCmds==0) {
3212 fprintf(stderr, "should not get an empty compileCommand set\n");
3213 errorCode = -1;
3214 goto cdb_end;
3215 }
3216
3217 for (i=0; i<numCmds && errorCode == 0; ++i) {
3218 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3219
3220 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidisfdea8132012-12-17 20:19:56 +00003221 if (chdir(clang_getCString(wd)) != 0) {
3222 printf("Could not chdir to %s\n", clang_getCString(wd));
3223 errorCode = -1;
3224 goto cdb_end;
3225 }
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003226 clang_disposeString(wd);
3227
3228 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3229 if (numArgs > MAX_COMPILE_ARGS){
3230 fprintf(stderr, "got more compile arguments than maximum\n");
3231 errorCode = -1;
3232 goto cdb_end;
3233 }
3234 for (a=0; a<numArgs; ++a) {
3235 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3236 args[a] = clang_getCString(cxargs[a]);
3237 }
3238
3239 errorCode = index_compile_args(numArgs, args, idxAction,
3240 /*importedASTs=*/0, check_prefix);
3241
3242 for (a=0; a<numArgs; ++a)
3243 clang_disposeString(cxargs[a]);
3244 }
3245 } else {
3246 printf("database loading failed with error code %d.\n", ec);
3247 errorCode = -1;
3248 }
3249
3250 cdb_end:
3251 clang_CompileCommands_dispose(CCmds);
3252 clang_CompilationDatabase_dispose(db);
3253 free(tmp);
3254
3255 }
3256
Argyrios Kyrtzidis4c910b12011-11-22 07:24:51 +00003257 clang_IndexAction_dispose(idxAction);
3258 clang_disposeIndex(Idx);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003259 return errorCode;
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003260}
3261
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003262int perform_token_annotation(int argc, const char **argv) {
3263 const char *input = argv[1];
3264 char *filename = 0;
3265 unsigned line, second_line;
3266 unsigned column, second_column;
3267 CXIndex CIdx;
3268 CXTranslationUnit TU = 0;
3269 int errorCode;
3270 struct CXUnsavedFile *unsaved_files = 0;
3271 int num_unsaved_files = 0;
3272 CXToken *tokens;
3273 unsigned num_tokens;
3274 CXSourceRange range;
3275 CXSourceLocation startLoc, endLoc;
3276 CXFile file = 0;
3277 CXCursor *cursors = 0;
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003278 CXSourceRangeList *skipped_ranges = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003279 enum CXErrorCode Err;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003280 unsigned i;
3281
3282 input += strlen("-test-annotate-tokens=");
3283 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3284 &second_line, &second_column)))
3285 return errorCode;
3286
Richard Smith1ea42eb2012-07-05 08:20:49 +00003287 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3288 free(filename);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003289 return -1;
Richard Smith1ea42eb2012-07-05 08:20:49 +00003290 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003291
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003292 CIdx = clang_createIndex(0, 1);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003293 Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1],
3294 argv + num_unsaved_files + 2,
3295 argc - num_unsaved_files - 3,
3296 unsaved_files,
3297 num_unsaved_files,
3298 getDefaultParsingOptions(), &TU);
3299 if (Err != CXError_Success) {
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003300 fprintf(stderr, "unable to parse input\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003301 describeLibclangFailure(Err);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003302 clang_disposeIndex(CIdx);
3303 free(filename);
3304 free_remapped_files(unsaved_files, num_unsaved_files);
3305 return -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003306 }
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003307 errorCode = 0;
3308
Richard Smith1ea42eb2012-07-05 08:20:49 +00003309 if (checkForErrors(TU) != 0) {
3310 errorCode = -1;
3311 goto teardown;
3312 }
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003313
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003314 if (getenv("CINDEXTEST_EDITING")) {
3315 for (i = 0; i < 5; ++i) {
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003316 Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3317 clang_defaultReparseOptions(TU));
3318 if (Err != CXError_Success) {
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003319 fprintf(stderr, "Unable to reparse translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003320 describeLibclangFailure(Err);
Argyrios Kyrtzidis4cdfcae2011-09-26 08:01:41 +00003321 errorCode = -1;
3322 goto teardown;
3323 }
3324 }
3325 }
3326
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003327 if (checkForErrors(TU) != 0) {
3328 errorCode = -1;
3329 goto teardown;
3330 }
3331
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003332 file = clang_getFile(TU, filename);
3333 if (!file) {
3334 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3335 errorCode = -1;
3336 goto teardown;
3337 }
3338
3339 startLoc = clang_getLocation(TU, file, line, column);
3340 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003341 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003342 column);
3343 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003344 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003345 }
3346
3347 endLoc = clang_getLocation(TU, file, second_line, second_column);
3348 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremenek29004672010-02-17 00:41:32 +00003349 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003350 second_line, second_column);
3351 errorCode = -1;
Ted Kremenek29004672010-02-17 00:41:32 +00003352 goto teardown;
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003353 }
3354
3355 range = clang_getRange(startLoc, endLoc);
3356 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003357
3358 if (checkForErrors(TU) != 0) {
3359 errorCode = -1;
3360 goto teardown;
3361 }
3362
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003363 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3364 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisa109e002011-10-28 22:54:36 +00003365
3366 if (checkForErrors(TU) != 0) {
3367 errorCode = -1;
3368 goto teardown;
3369 }
3370
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003371 skipped_ranges = clang_getSkippedRanges(TU, file);
3372 for (i = 0; i != skipped_ranges->count; ++i) {
3373 unsigned start_line, start_column, end_line, end_column;
3374 clang_getSpellingLocation(clang_getRangeStart(skipped_ranges->ranges[i]),
3375 0, &start_line, &start_column, 0);
3376 clang_getSpellingLocation(clang_getRangeEnd(skipped_ranges->ranges[i]),
3377 0, &end_line, &end_column, 0);
3378 printf("Skipping: ");
3379 PrintExtent(stdout, start_line, start_column, end_line, end_column);
3380 printf("\n");
3381 }
Argyrios Kyrtzidis0e282ef2013-12-06 18:55:45 +00003382 clang_disposeSourceRangeList(skipped_ranges);
Argyrios Kyrtzidis9ef57752013-12-05 08:19:32 +00003383
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003384 for (i = 0; i != num_tokens; ++i) {
3385 const char *kind = "<unknown>";
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003386 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3387 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003388 unsigned start_line, start_column, end_line, end_column;
3389
3390 switch (clang_getTokenKind(tokens[i])) {
3391 case CXToken_Punctuation: kind = "Punctuation"; break;
3392 case CXToken_Keyword: kind = "Keyword"; break;
3393 case CXToken_Identifier: kind = "Identifier"; break;
3394 case CXToken_Literal: kind = "Literal"; break;
3395 case CXToken_Comment: kind = "Comment"; break;
3396 }
Douglas Gregor229bebd2010-11-09 06:24:54 +00003397 clang_getSpellingLocation(clang_getRangeStart(extent),
3398 0, &start_line, &start_column, 0);
3399 clang_getSpellingLocation(clang_getRangeEnd(extent),
3400 0, &end_line, &end_column, 0);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003401 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Krameraf7ae312012-04-14 09:11:51 +00003402 clang_disposeString(spelling);
Daniel Dunbar98c07e02010-02-14 08:32:24 +00003403 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor61656112010-01-26 18:31:56 +00003404 if (!clang_isInvalid(cursors[i].kind)) {
3405 printf(" ");
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00003406 PrintCursor(cursors[i], NULL);
Douglas Gregor61656112010-01-26 18:31:56 +00003407 }
3408 printf("\n");
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003409 }
3410 free(cursors);
Ted Kremenek983fb5de2010-10-20 21:22:15 +00003411 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003412
3413 teardown:
Douglas Gregor33cdd812010-02-18 18:08:43 +00003414 PrintDiagnostics(TU);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00003415 clang_disposeTranslationUnit(TU);
3416 clang_disposeIndex(CIdx);
3417 free(filename);
3418 free_remapped_files(unsaved_files, num_unsaved_files);
3419 return errorCode;
3420}
3421
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003422static int
3423perform_test_compilation_db(const char *database, int argc, const char **argv) {
3424 CXCompilationDatabase db;
3425 CXCompileCommands CCmds;
3426 CXCompileCommand CCmd;
3427 CXCompilationDatabase_Error ec;
3428 CXString wd;
3429 CXString arg;
3430 int errorCode = 0;
3431 char *tmp;
3432 unsigned len;
3433 char *buildDir;
3434 int i, j, a, numCmds, numArgs;
3435
3436 len = strlen(database);
3437 tmp = (char *) malloc(len+1);
3438 memcpy(tmp, database, len+1);
3439 buildDir = dirname(tmp);
3440
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003441 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003442
3443 if (db) {
3444
3445 if (ec!=CXCompilationDatabase_NoError) {
3446 printf("unexpected error %d code while loading compilation database\n", ec);
3447 errorCode = -1;
3448 goto cdb_end;
3449 }
3450
3451 for (i=0; i<argc && errorCode==0; ) {
3452 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003453 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003454
3455 if (!CCmds) {
3456 printf("file %s not found in compilation db\n", argv[i+1]);
3457 errorCode = -1;
3458 break;
3459 }
3460
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003461 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003462
3463 if (numCmds==0) {
3464 fprintf(stderr, "should not get an empty compileCommand set for file"
3465 " '%s'\n", argv[i+1]);
3466 errorCode = -1;
3467 break;
3468 }
3469
3470 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003471 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003472
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003473 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003474 printf("workdir:'%s'", clang_getCString(wd));
3475 clang_disposeString(wd);
3476
3477 printf(" cmdline:'");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003478 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003479 for (a=0; a<numArgs; ++a) {
3480 if (a) printf(" ");
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003481 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003482 printf("%s", clang_getCString(arg));
3483 clang_disposeString(arg);
3484 }
3485 printf("'\n");
3486 }
3487
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003488 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003489
3490 i += 2;
3491 }
3492 }
Arnaud A. de Grandmaisonfa6d73c2012-07-03 20:38:12 +00003493 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003494 } else {
3495 printf("database loading failed with error code %d.\n", ec);
3496 errorCode = -1;
3497 }
3498
3499cdb_end:
3500 free(tmp);
3501
3502 return errorCode;
3503}
3504
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003505/******************************************************************************/
Ted Kremenek599d73a2010-03-25 02:00:39 +00003506/* USR printing. */
3507/******************************************************************************/
3508
3509static int insufficient_usr(const char *kind, const char *usage) {
3510 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3511 return 1;
3512}
3513
3514static unsigned isUSR(const char *s) {
3515 return s[0] == 'c' && s[1] == ':';
3516}
3517
3518static int not_usr(const char *s, const char *arg) {
3519 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3520 return 1;
3521}
3522
3523static void print_usr(CXString usr) {
3524 const char *s = clang_getCString(usr);
3525 printf("%s\n", s);
3526 clang_disposeString(usr);
3527}
3528
3529static void display_usrs() {
3530 fprintf(stderr, "-print-usrs options:\n"
3531 " ObjCCategory <class name> <category name>\n"
3532 " ObjCClass <class name>\n"
3533 " ObjCIvar <ivar name> <class USR>\n"
3534 " ObjCMethod <selector> [0=class method|1=instance method] "
3535 "<class USR>\n"
3536 " ObjCProperty <property name> <class USR>\n"
3537 " ObjCProtocol <protocol name>\n");
3538}
3539
3540int print_usrs(const char **I, const char **E) {
3541 while (I != E) {
3542 const char *kind = *I;
3543 unsigned len = strlen(kind);
3544 switch (len) {
3545 case 8:
3546 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3547 if (I + 2 >= E)
3548 return insufficient_usr(kind, "<ivar name> <class USR>");
3549 if (!isUSR(I[2]))
3550 return not_usr("<class USR>", I[2]);
3551 else {
3552 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003553 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003554 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003555 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3556 }
3557
3558 I += 3;
3559 continue;
3560 }
3561 break;
3562 case 9:
3563 if (memcmp(kind, "ObjCClass", 9) == 0) {
3564 if (I + 1 >= E)
3565 return insufficient_usr(kind, "<class name>");
3566 print_usr(clang_constructUSR_ObjCClass(I[1]));
3567 I += 2;
3568 continue;
3569 }
3570 break;
3571 case 10:
3572 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3573 if (I + 3 >= E)
3574 return insufficient_usr(kind, "<method selector> "
3575 "[0=class method|1=instance method] <class USR>");
3576 if (!isUSR(I[3]))
3577 return not_usr("<class USR>", I[3]);
3578 else {
3579 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003580 x.data = (void*) I[3];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003581 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003582 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3583 }
3584 I += 4;
3585 continue;
3586 }
3587 break;
3588 case 12:
3589 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3590 if (I + 2 >= E)
3591 return insufficient_usr(kind, "<class name> <category name>");
3592 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3593 I += 3;
3594 continue;
3595 }
3596 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3597 if (I + 1 >= E)
3598 return insufficient_usr(kind, "<protocol name>");
3599 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3600 I += 2;
3601 continue;
3602 }
3603 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3604 if (I + 2 >= E)
3605 return insufficient_usr(kind, "<property name> <class USR>");
3606 if (!isUSR(I[2]))
3607 return not_usr("<class USR>", I[2]);
3608 else {
3609 CXString x;
Ted Kremenek91554282010-11-16 08:15:36 +00003610 x.data = (void*) I[2];
Ted Kremenek4b4f3692010-11-16 01:56:27 +00003611 x.private_flags = 0;
Ted Kremenek599d73a2010-03-25 02:00:39 +00003612 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3613 }
3614 I += 3;
3615 continue;
3616 }
3617 break;
3618 default:
3619 break;
3620 }
3621 break;
3622 }
3623
3624 if (I != E) {
3625 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3626 display_usrs();
3627 return 1;
3628 }
3629 return 0;
3630}
3631
3632int print_usrs_file(const char *file_name) {
3633 char line[2048];
3634 const char *args[128];
3635 unsigned numChars = 0;
3636
3637 FILE *fp = fopen(file_name, "r");
3638 if (!fp) {
3639 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3640 return 1;
3641 }
3642
3643 /* This code is not really all that safe, but it works fine for testing. */
3644 while (!feof(fp)) {
3645 char c = fgetc(fp);
3646 if (c == '\n') {
3647 unsigned i = 0;
3648 const char *s = 0;
3649
3650 if (numChars == 0)
3651 continue;
3652
3653 line[numChars] = '\0';
3654 numChars = 0;
3655
3656 if (line[0] == '/' && line[1] == '/')
3657 continue;
3658
3659 s = strtok(line, " ");
3660 while (s) {
3661 args[i] = s;
3662 ++i;
3663 s = strtok(0, " ");
3664 }
3665 if (print_usrs(&args[0], &args[i]))
3666 return 1;
3667 }
3668 else
3669 line[numChars++] = c;
3670 }
3671
3672 fclose(fp);
3673 return 0;
3674}
3675
3676/******************************************************************************/
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003677/* Command line processing. */
3678/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003679int write_pch_file(const char *filename, int argc, const char *argv[]) {
3680 CXIndex Idx;
3681 CXTranslationUnit TU;
3682 struct CXUnsavedFile *unsaved_files = 0;
3683 int num_unsaved_files = 0;
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003684 enum CXErrorCode Err;
Francois Pichetabcfbec2011-07-06 22:09:44 +00003685 int result = 0;
Douglas Gregore9386682010-08-13 05:36:37 +00003686
Stefanus Du Toitb3318502013-03-01 21:41:22 +00003687 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregore9386682010-08-13 05:36:37 +00003688
3689 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3690 clang_disposeIndex(Idx);
3691 return -1;
3692 }
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003693
3694 Err = clang_parseTranslationUnit2(
3695 Idx, 0, argv + num_unsaved_files, argc - num_unsaved_files,
3696 unsaved_files, num_unsaved_files,
3697 CXTranslationUnit_Incomplete |
3698 CXTranslationUnit_DetailedPreprocessingRecord |
3699 CXTranslationUnit_ForSerialization,
3700 &TU);
3701 if (Err != CXError_Success) {
Douglas Gregore9386682010-08-13 05:36:37 +00003702 fprintf(stderr, "Unable to load translation unit!\n");
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003703 describeLibclangFailure(Err);
Douglas Gregore9386682010-08-13 05:36:37 +00003704 free_remapped_files(unsaved_files, num_unsaved_files);
Dmitri Gribenkoea4d1c32014-02-12 19:12:37 +00003705 clang_disposeTranslationUnit(TU);
Douglas Gregore9386682010-08-13 05:36:37 +00003706 clang_disposeIndex(Idx);
3707 return 1;
3708 }
3709
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003710 switch (clang_saveTranslationUnit(TU, filename,
3711 clang_defaultSaveOptions(TU))) {
3712 case CXSaveError_None:
3713 break;
3714
3715 case CXSaveError_TranslationErrors:
3716 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3717 filename);
3718 result = 2;
3719 break;
3720
3721 case CXSaveError_InvalidTU:
3722 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3723 filename);
3724 result = 3;
3725 break;
3726
3727 case CXSaveError_Unknown:
3728 default:
3729 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3730 result = 1;
3731 break;
3732 }
3733
Douglas Gregore9386682010-08-13 05:36:37 +00003734 clang_disposeTranslationUnit(TU);
3735 free_remapped_files(unsaved_files, num_unsaved_files);
3736 clang_disposeIndex(Idx);
Douglas Gregor30c80fa2011-07-06 16:43:36 +00003737 return result;
Douglas Gregore9386682010-08-13 05:36:37 +00003738}
3739
3740/******************************************************************************/
Ted Kremenekd010ba42011-11-10 08:43:12 +00003741/* Serialized diagnostics. */
3742/******************************************************************************/
3743
3744static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3745 switch (error) {
3746 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3747 case CXLoadDiag_None: break;
3748 case CXLoadDiag_Unknown: return "Unknown";
3749 case CXLoadDiag_InvalidFile: return "Invalid File";
3750 }
3751 return "None";
3752}
3753
3754static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3755 switch (severity) {
3756 case CXDiagnostic_Note: return "note";
3757 case CXDiagnostic_Error: return "error";
3758 case CXDiagnostic_Fatal: return "fatal";
3759 case CXDiagnostic_Ignored: return "ignored";
3760 case CXDiagnostic_Warning: return "warning";
3761 }
3762 return "unknown";
3763}
3764
3765static void printIndent(unsigned indent) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003766 if (indent == 0)
3767 return;
3768 fprintf(stderr, "+");
3769 --indent;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003770 while (indent > 0) {
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003771 fprintf(stderr, "-");
Ted Kremenekd010ba42011-11-10 08:43:12 +00003772 --indent;
3773 }
3774}
3775
3776static void printLocation(CXSourceLocation L) {
3777 CXFile File;
3778 CXString FileName;
3779 unsigned line, column, offset;
3780
3781 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3782 FileName = clang_getFileName(File);
3783
3784 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3785 clang_disposeString(FileName);
3786}
3787
3788static void printRanges(CXDiagnostic D, unsigned indent) {
3789 unsigned i, n = clang_getDiagnosticNumRanges(D);
3790
3791 for (i = 0; i < n; ++i) {
3792 CXSourceLocation Start, End;
Enea Zaffanella476f38a2013-07-22 20:58:30 +00003793 CXSourceRange SR = clang_getDiagnosticRange(D, i);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003794 Start = clang_getRangeStart(SR);
3795 End = clang_getRangeEnd(SR);
3796
3797 printIndent(indent);
3798 fprintf(stderr, "Range: ");
3799 printLocation(Start);
3800 fprintf(stderr, " ");
3801 printLocation(End);
3802 fprintf(stderr, "\n");
3803 }
3804}
3805
3806static void printFixIts(CXDiagnostic D, unsigned indent) {
3807 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek4a642302012-03-20 20:49:45 +00003808 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003809 for (i = 0 ; i < n; ++i) {
3810 CXSourceRange ReplacementRange;
3811 CXString text;
3812 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3813
3814 printIndent(indent);
3815 fprintf(stderr, "FIXIT: (");
3816 printLocation(clang_getRangeStart(ReplacementRange));
3817 fprintf(stderr, " - ");
3818 printLocation(clang_getRangeEnd(ReplacementRange));
3819 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3820 clang_disposeString(text);
3821 }
3822}
3823
3824static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003825 unsigned i, n;
3826
Ted Kremenekd010ba42011-11-10 08:43:12 +00003827 if (!Diags)
3828 return;
3829
NAKAMURA Takumi77d97392011-11-10 09:30:15 +00003830 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenekd010ba42011-11-10 08:43:12 +00003831 for (i = 0; i < n; ++i) {
3832 CXSourceLocation DiagLoc;
3833 CXDiagnostic D;
3834 CXFile File;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003835 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003836 unsigned line, column, offset;
Ted Kremenek26a6d492012-04-12 00:03:31 +00003837 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenekd010ba42011-11-10 08:43:12 +00003838
3839 D = clang_getDiagnosticInSet(Diags, i);
3840 DiagLoc = clang_getDiagnosticLocation(D);
3841 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3842 FileName = clang_getFileName(File);
3843 DiagSpelling = clang_getDiagnosticSpelling(D);
3844
3845 printIndent(indent);
3846
3847 fprintf(stderr, "%s:%d:%d: %s: %s",
3848 clang_getCString(FileName),
3849 line,
3850 column,
3851 getSeverityString(clang_getDiagnosticSeverity(D)),
3852 clang_getCString(DiagSpelling));
3853
3854 DiagOption = clang_getDiagnosticOption(D, 0);
3855 DiagOptionStr = clang_getCString(DiagOption);
3856 if (DiagOptionStr) {
3857 fprintf(stderr, " [%s]", DiagOptionStr);
3858 }
3859
Ted Kremenek26a6d492012-04-12 00:03:31 +00003860 DiagCat = clang_getDiagnosticCategoryText(D);
3861 DiagCatStr = clang_getCString(DiagCat);
3862 if (DiagCatStr) {
3863 fprintf(stderr, " [%s]", DiagCatStr);
3864 }
3865
Ted Kremenekd010ba42011-11-10 08:43:12 +00003866 fprintf(stderr, "\n");
3867
3868 printRanges(D, indent);
3869 printFixIts(D, indent);
3870
NAKAMURA Takumi27dd3962011-11-10 10:07:57 +00003871 /* Print subdiagnostics. */
Ted Kremenekd010ba42011-11-10 08:43:12 +00003872 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3873
3874 clang_disposeString(FileName);
3875 clang_disposeString(DiagSpelling);
3876 clang_disposeString(DiagOption);
3877 }
3878}
3879
3880static int read_diagnostics(const char *filename) {
3881 enum CXLoadDiag_Error error;
3882 CXString errorString;
3883 CXDiagnosticSet Diags = 0;
3884
3885 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3886 if (!Diags) {
3887 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3888 getDiagnosticCodeStr(error),
3889 clang_getCString(errorString));
3890 clang_disposeString(errorString);
3891 return 1;
3892 }
3893
3894 printDiagnosticSet(Diags, 0);
Ted Kremeneka0e32fc2011-11-11 00:46:43 +00003895 fprintf(stderr, "Number of diagnostics: %d\n",
3896 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenekd010ba42011-11-10 08:43:12 +00003897 clang_disposeDiagnosticSet(Diags);
3898 return 0;
3899}
3900
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003901static int perform_print_build_session_timestamp(void) {
3902 printf("%lld\n", clang_getBuildSessionTimestamp());
3903 return 0;
3904}
3905
Ted Kremenekd010ba42011-11-10 08:43:12 +00003906/******************************************************************************/
Douglas Gregore9386682010-08-13 05:36:37 +00003907/* Command line processing. */
3908/******************************************************************************/
Ted Kremenekef3339b2009-11-17 18:09:14 +00003909
Douglas Gregor720d0052010-01-20 21:32:04 +00003910static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003911 if (s[0] == '\0')
Douglas Gregor720d0052010-01-20 21:32:04 +00003912 return FilteredPrintingVisitor;
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003913 if (strcmp(s, "-usrs") == 0)
3914 return USRVisitor;
Ted Kremenek83f642e2011-04-18 22:47:10 +00003915 if (strncmp(s, "-memory-usage", 13) == 0)
3916 return GetVisitor(s + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003917 return NULL;
3918}
3919
Ted Kremenekef3339b2009-11-17 18:09:14 +00003920static void print_usage(void) {
3921 fprintf(stderr,
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003922 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003923 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregor082c3e62010-01-15 19:40:17 +00003924 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003925 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3926 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi4deb9a92012-10-24 22:52:04 +00003927 fprintf(stderr,
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003928 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003929 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003930 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003931 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek0469b7e2009-11-18 02:02:52 +00003932 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen338b55c2011-10-06 11:38:08 +00003933 "[FileCheck prefix]\n");
3934 fprintf(stderr,
Ted Kremeneka44d99c2010-01-05 23:18:49 +00003935 " c-index-test -test-load-tu <AST file> <symbol filter> "
3936 "[FileCheck prefix]\n"
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003937 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3938 "[FileCheck prefix]\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003939 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregor082c3e62010-01-15 19:40:17 +00003940 fprintf(stderr,
Ted Kremenek83f642e2011-04-18 22:47:10 +00003941 " c-index-test -test-load-source-memory-usage "
3942 "<symbol filter> {<args>}*\n"
Douglas Gregoraa21cc42010-07-19 21:46:24 +00003943 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3944 " {<args>}*\n"
Douglas Gregor47815d52010-07-12 18:38:41 +00003945 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003946 " c-index-test -test-load-source-usrs-memory-usage "
3947 "<symbol filter> {<args>}*\n"
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00003948 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3949 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek11d1a422011-04-18 23:42:53 +00003950 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth718df592010-07-22 06:29:13 +00003951 fprintf(stderr,
Ted Kremenek11d1a422011-04-18 23:42:53 +00003952 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenko00353722013-02-15 21:15:49 +00003953 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00003954 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00003955 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek83f642e2011-04-18 22:47:10 +00003956 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregore9386682010-08-13 05:36:37 +00003957 " c-index-test -print-usr-file <file>\n"
Ted Kremenekd010ba42011-11-10 08:43:12 +00003958 " c-index-test -write-pch <file> <compiler arguments>\n");
3959 fprintf(stderr,
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00003960 " c-index-test -compilation-db [lookup <filename>] database\n");
3961 fprintf(stderr,
Dmitri Gribenkof430da42014-02-12 10:33:14 +00003962 " c-index-test -print-build-session-timestamp\n");
3963 fprintf(stderr,
Ted Kremenekd010ba42011-11-10 08:43:12 +00003964 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregor73a18fd2010-07-20 14:34:35 +00003965 fprintf(stderr,
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00003966 " <symbol filter> values:\n%s",
Ted Kremenek1cd27d52009-11-17 18:13:31 +00003967 " all - load all symbols, including those from PCH\n"
3968 " local - load all symbols except those in PCH\n"
3969 " category - only load ObjC categories (non-PCH)\n"
3970 " interface - only load ObjC interfaces (non-PCH)\n"
3971 " protocol - only load ObjC protocols (non-PCH)\n"
3972 " function - only load functions (non-PCH)\n"
Daniel Dunbar5442bfc2009-12-01 02:35:37 +00003973 " typedef - only load typdefs (non-PCH)\n"
3974 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekef3339b2009-11-17 18:09:14 +00003975}
3976
Daniel Dunbar08b33d02010-09-30 20:39:47 +00003977/***/
3978
3979int cindextest_main(int argc, const char **argv) {
Douglas Gregor1e21cc72010-02-18 23:07:20 +00003980 clang_enableStackTraces();
Ted Kremenekd010ba42011-11-10 08:43:12 +00003981 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3982 return read_diagnostics(argv[2]);
Ted Kremenekef3339b2009-11-17 18:09:14 +00003983 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor47815d52010-07-12 18:38:41 +00003984 return perform_code_completion(argc, argv, 0);
3985 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3986 return perform_code_completion(argc, argv, 1);
Douglas Gregor082c3e62010-01-15 19:40:17 +00003987 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3988 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidiscddafd32011-10-06 07:00:54 +00003989 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3990 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis503c83a2013-03-08 02:32:34 +00003991 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
3992 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidisdc199a32011-10-17 19:48:19 +00003993 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidise26c5572012-10-24 18:29:15 +00003994 return index_file(argc - 2, argv + 2, /*full=*/0);
3995 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3996 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidisd992e142011-11-15 06:20:16 +00003997 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3998 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisf75d4982012-12-05 21:53:37 +00003999 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
4000 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004001 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00004002 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004003 if (I)
Ted Kremenekb478ff42010-01-26 17:59:48 +00004004 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
4005 NULL);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004006 }
Douglas Gregoraa21cc42010-07-19 21:46:24 +00004007 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
4008 CXCursorVisitor I = GetVisitor(argv[1] + 25);
4009 if (I) {
4010 int trials = atoi(argv[2]);
4011 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
4012 NULL);
4013 }
4014 }
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004015 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregor720d0052010-01-20 21:32:04 +00004016 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek83f642e2011-04-18 22:47:10 +00004017
4018 PostVisitTU postVisit = 0;
4019 if (strstr(argv[1], "-memory-usage"))
4020 postVisit = PrintMemoryUsage;
4021
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004022 if (I)
Ted Kremenek83f642e2011-04-18 22:47:10 +00004023 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
4024 postVisit);
Ted Kremenek58a6a8e2010-01-12 23:34:26 +00004025 }
4026 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek0469b7e2009-11-18 02:02:52 +00004027 return perform_file_scan(argv[2], argv[3],
4028 argc >= 5 ? argv[4] : 0);
Douglas Gregor27b4fa92010-01-26 17:06:03 +00004029 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
4030 return perform_token_annotation(argc, argv);
Ted Kremenek0b86e3a2010-01-26 19:31:51 +00004031 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
4032 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
4033 PrintInclusionStack);
4034 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
4035 return perform_test_load_tu(argv[2], "all", NULL, NULL,
4036 PrintInclusionStack);
Ted Kremenek83b28a22010-03-03 06:37:58 +00004037 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
4038 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
4039 NULL);
Dmitri Gribenko00353722013-02-15 21:15:49 +00004040 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek6bca9842010-05-14 21:29:26 +00004041 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenko00353722013-02-15 21:15:49 +00004042 PrintType, 0);
Argyrios Kyrtzidise822f582013-04-11 01:20:11 +00004043 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
4044 return perform_test_load_source(argc - 2, argv + 2, "all",
4045 PrintTypeSize, 0);
Dmitri Gribenkob506ba12012-12-04 15:13:46 +00004046 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
4047 return perform_test_load_source(argc - 2, argv + 2, "all",
4048 PrintBitWidth, 0);
Ted Kremenek599d73a2010-03-25 02:00:39 +00004049 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
4050 if (argc > 2)
4051 return print_usrs(argv + 2, argv + argc);
4052 else {
4053 display_usrs();
4054 return 1;
4055 }
4056 }
4057 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
4058 return print_usrs_file(argv[2]);
Douglas Gregore9386682010-08-13 05:36:37 +00004059 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
4060 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004061 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
4062 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
Dmitri Gribenkof430da42014-02-12 10:33:14 +00004063 else if (argc == 2 && strcmp(argv[1], "-print-build-session-timestamp") == 0)
4064 return perform_print_build_session_timestamp();
Arnaud A. de Grandmaison0fe28a12012-06-30 11:27:57 +00004065
Ted Kremenekef3339b2009-11-17 18:09:14 +00004066 print_usage();
4067 return 1;
Steve Naroffa1c72842009-08-28 15:28:48 +00004068}
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004069
4070/***/
4071
4072/* We intentionally run in a separate thread to ensure we at least minimal
4073 * testing of a multithreaded environment (for example, having a reduced stack
4074 * size). */
4075
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004076typedef struct thread_info {
4077 int argc;
4078 const char **argv;
4079 int result;
4080} thread_info;
Benjamin Kramer112fc6c2010-11-04 19:11:31 +00004081void thread_runner(void *client_data_v) {
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004082 thread_info *client_data = client_data_v;
4083 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumia7d49882012-04-07 06:59:28 +00004084#ifdef __CYGWIN__
4085 fflush(stdout); /* stdout is not flushed on Cygwin. */
4086#endif
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004087}
4088
4089int main(int argc, const char **argv) {
Benjamin Kramer3a913ed2012-08-10 10:06:13 +00004090 thread_info client_data;
4091
Dmitri Gribenko740c0fb2012-08-07 17:54:38 +00004092#ifdef CLANG_HAVE_LIBXML
4093 LIBXML_TEST_VERSION
4094#endif
4095
Douglas Gregorf428bf82010-10-27 16:00:01 +00004096 if (getenv("CINDEXTEST_NOTHREADS"))
4097 return cindextest_main(argc, argv);
4098
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004099 client_data.argc = argc;
4100 client_data.argv = argv;
Daniel Dunbar23397c32010-11-04 01:26:31 +00004101 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar08b33d02010-09-30 20:39:47 +00004102 return client_data.result;
4103}