blob: 90a65282f4ca32ec979352b4d4287bf49ef45546 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00004#include "clang-c/CXCompilationDatabase.h"
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00005#include "llvm/Config/config.h"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00006#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00007#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00008#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +000010#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +000011
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +000012#ifdef CLANG_HAVE_LIBXML
13#include <libxml/parser.h>
14#include <libxml/relaxng.h>
15#include <libxml/xmlerror.h>
16#endif
17
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +000018#ifdef _WIN32
19# include <direct.h>
20#else
21# include <unistd.h>
22#endif
23
Ted Kremenek0d435192009-11-17 18:13:31 +000024/******************************************************************************/
25/* Utility functions. */
26/******************************************************************************/
27
John Thompson2e06fc82009-10-27 13:42:56 +000028#ifdef _MSC_VER
29char *basename(const char* path)
30{
31 char* base1 = (char*)strrchr(path, '/');
32 char* base2 = (char*)strrchr(path, '\\');
33 if (base1 && base2)
34 return((base1 > base2) ? base1 + 1 : base2 + 1);
35 else if (base1)
36 return(base1 + 1);
37 else if (base2)
38 return(base2 + 1);
39
40 return((char*)path);
41}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000042char *dirname(char* path)
43{
44 char* base1 = (char*)strrchr(path, '/');
45 char* base2 = (char*)strrchr(path, '\\');
46 if (base1 && base2)
47 if (base1 > base2)
48 *base1 = 0;
49 else
50 *base2 = 0;
51 else if (base1)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000052 *base1 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000053 else if (base2)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000054 *base2 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000055
56 return path;
57}
John Thompson2e06fc82009-10-27 13:42:56 +000058#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000059extern char *basename(const char *);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000060extern char *dirname(char *);
John Thompson2e06fc82009-10-27 13:42:56 +000061#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000062
Douglas Gregor45ba9a12010-07-25 17:39:21 +000063/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000064static unsigned getDefaultParsingOptions() {
65 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
66
67 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000068 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000069 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
70 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000071 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
72 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000073 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
74 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +000075 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
76 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregor44c181a2010-07-23 00:33:23 +000077
78 return options;
79}
80
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000081static int checkForErrors(CXTranslationUnit TU);
82
Daniel Dunbar51b058c2010-02-14 08:32:24 +000083static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
84 unsigned end_line, unsigned end_column) {
85 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000086 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000087}
88
Ted Kremenek1c6da172009-11-17 19:37:36 +000089static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
90 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000091
Douglas Gregora88084b2010-02-18 18:08:43 +000092 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000093 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000094 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
95 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000096 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000097 return 1;
98}
99
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100void free_remapped_files(struct CXUnsavedFile *unsaved_files,
101 int num_unsaved_files) {
102 int i;
103 for (i = 0; i != num_unsaved_files; ++i) {
104 free((char *)unsaved_files[i].Filename);
105 free((char *)unsaved_files[i].Contents);
106 }
Douglas Gregor653a55f2010-08-19 20:50:29 +0000107 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000108}
109
110int parse_remapped_files(int argc, const char **argv, int start_arg,
111 struct CXUnsavedFile **unsaved_files,
112 int *num_unsaved_files) {
113 int i;
114 int arg;
115 int prefix_len = strlen("-remap-file=");
116 *unsaved_files = 0;
117 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000118
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 /* Count the number of remapped files. */
120 for (arg = start_arg; arg < argc; ++arg) {
121 if (strncmp(argv[arg], "-remap-file=", prefix_len))
122 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000123
Douglas Gregor4db64a42010-01-23 00:14:00 +0000124 ++*num_unsaved_files;
125 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 if (*num_unsaved_files == 0)
128 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000129
Douglas Gregor4db64a42010-01-23 00:14:00 +0000130 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +0000131 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
132 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000133 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
134 struct CXUnsavedFile *unsaved = *unsaved_files + i;
135 const char *arg_string = argv[arg] + prefix_len;
136 int filename_len;
137 char *filename;
138 char *contents;
139 FILE *to_file;
140 const char *semi = strchr(arg_string, ';');
141 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000142 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000143 "error: -remap-file=from;to argument is missing semicolon\n");
144 free_remapped_files(*unsaved_files, i);
145 *unsaved_files = 0;
146 *num_unsaved_files = 0;
147 return -1;
148 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000151 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000152 if (!to_file) {
153 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
154 semi + 1);
155 free_remapped_files(*unsaved_files, i);
156 *unsaved_files = 0;
157 *num_unsaved_files = 0;
158 return -1;
159 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000160
Douglas Gregor4db64a42010-01-23 00:14:00 +0000161 /* Determine the length of the file we're remapping to. */
162 fseek(to_file, 0, SEEK_END);
163 unsaved->Length = ftell(to_file);
164 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000165
Douglas Gregor4db64a42010-01-23 00:14:00 +0000166 /* Read the contents of the file we're remapping to. */
167 contents = (char *)malloc(unsaved->Length + 1);
168 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
169 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
170 (feof(to_file) ? "EOF" : "error"), semi + 1);
171 fclose(to_file);
172 free_remapped_files(*unsaved_files, i);
Richard Smithe07c5f82012-07-05 08:20:49 +0000173 free(contents);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000174 *unsaved_files = 0;
175 *num_unsaved_files = 0;
176 return -1;
177 }
178 contents[unsaved->Length] = 0;
179 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000180
Douglas Gregor4db64a42010-01-23 00:14:00 +0000181 /* Close the file. */
182 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000183
Douglas Gregor4db64a42010-01-23 00:14:00 +0000184 /* Copy the file name that we're remapping from. */
185 filename_len = semi - arg_string;
186 filename = (char *)malloc(filename_len + 1);
187 memcpy(filename, arg_string, filename_len);
188 filename[filename_len] = 0;
189 unsaved->Filename = filename;
190 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000191
Douglas Gregor4db64a42010-01-23 00:14:00 +0000192 return 0;
193}
194
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000195static const char *parse_comments_schema(int argc, const char **argv) {
196 const char *CommentsSchemaArg = "-comments-xml-schema=";
197 const char *CommentSchemaFile = NULL;
198
199 if (argc == 0)
200 return CommentSchemaFile;
201
202 if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
203 CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
204
205 return CommentSchemaFile;
206}
207
Ted Kremenek0d435192009-11-17 18:13:31 +0000208/******************************************************************************/
209/* Pretty-printing. */
210/******************************************************************************/
211
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000212static const char *FileCheckPrefix = "CHECK";
213
214static void PrintCString(const char *CStr) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000215 if (CStr != NULL && CStr[0] != '\0') {
216 for ( ; *CStr; ++CStr) {
217 const char C = *CStr;
218 switch (C) {
219 case '\n': printf("\\n"); break;
220 case '\r': printf("\\r"); break;
221 case '\t': printf("\\t"); break;
222 case '\v': printf("\\v"); break;
223 case '\f': printf("\\f"); break;
224 default: putchar(C); break;
225 }
226 }
227 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000228}
229
230static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
231 printf(" %s=[", Prefix);
232 PrintCString(CStr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000233 printf("]");
234}
235
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000236static void PrintCXStringAndDispose(CXString Str) {
237 PrintCString(clang_getCString(Str));
238 clang_disposeString(Str);
239}
240
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000241static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
242 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
243}
244
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000245static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
246 CXString Str) {
247 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
248 clang_disposeString(Str);
249}
250
Douglas Gregor430d7a12011-07-25 17:48:11 +0000251static void PrintRange(CXSourceRange R, const char *str) {
252 CXFile begin_file, end_file;
253 unsigned begin_line, begin_column, end_line, end_column;
254
255 clang_getSpellingLocation(clang_getRangeStart(R),
256 &begin_file, &begin_line, &begin_column, 0);
257 clang_getSpellingLocation(clang_getRangeEnd(R),
258 &end_file, &end_line, &end_column, 0);
259 if (!begin_file || !end_file)
260 return;
261
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000262 if (str)
263 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000264 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
265}
266
Douglas Gregor358559d2010-10-02 22:49:11 +0000267int want_display_name = 0;
268
Douglas Gregorcc889662012-05-08 00:14:45 +0000269static void printVersion(const char *Prefix, CXVersion Version) {
270 if (Version.Major < 0)
271 return;
272 printf("%s%d", Prefix, Version.Major);
273
274 if (Version.Minor < 0)
275 return;
276 printf(".%d", Version.Minor);
277
278 if (Version.Subminor < 0)
279 return;
280 printf(".%d", Version.Subminor);
281}
282
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000283struct CommentASTDumpingContext {
284 int IndentLevel;
285};
286
287static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
288 CXComment Comment) {
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000289 unsigned i;
290 unsigned e;
291 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
292
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000293 Ctx->IndentLevel++;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000294 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000295 printf(" ");
296
297 printf("(");
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000298 switch (Kind) {
299 case CXComment_Null:
300 printf("CXComment_Null");
301 break;
302 case CXComment_Text:
303 printf("CXComment_Text");
304 PrintCXStringWithPrefixAndDispose("Text",
305 clang_TextComment_getText(Comment));
306 if (clang_Comment_isWhitespace(Comment))
307 printf(" IsWhitespace");
308 if (clang_InlineContentComment_hasTrailingNewline(Comment))
309 printf(" HasTrailingNewline");
310 break;
311 case CXComment_InlineCommand:
312 printf("CXComment_InlineCommand");
313 PrintCXStringWithPrefixAndDispose(
314 "CommandName",
315 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000316 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
317 case CXCommentInlineCommandRenderKind_Normal:
318 printf(" RenderNormal");
319 break;
320 case CXCommentInlineCommandRenderKind_Bold:
321 printf(" RenderBold");
322 break;
323 case CXCommentInlineCommandRenderKind_Monospaced:
324 printf(" RenderMonospaced");
325 break;
326 case CXCommentInlineCommandRenderKind_Emphasized:
327 printf(" RenderEmphasized");
328 break;
329 }
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000330 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000331 i != e; ++i) {
332 printf(" Arg[%u]=", i);
333 PrintCXStringAndDispose(
334 clang_InlineCommandComment_getArgText(Comment, i));
335 }
336 if (clang_InlineContentComment_hasTrailingNewline(Comment))
337 printf(" HasTrailingNewline");
338 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000339 case CXComment_HTMLStartTag: {
340 unsigned NumAttrs;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000341 printf("CXComment_HTMLStartTag");
342 PrintCXStringWithPrefixAndDispose(
343 "Name",
344 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000345 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000346 if (NumAttrs != 0) {
347 printf(" Attrs:");
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000348 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000349 printf(" ");
350 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
351 printf("=");
352 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
353 }
354 }
355 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
356 printf(" SelfClosing");
357 if (clang_InlineContentComment_hasTrailingNewline(Comment))
358 printf(" HasTrailingNewline");
359 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000360 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000361 case CXComment_HTMLEndTag:
362 printf("CXComment_HTMLEndTag");
363 PrintCXStringWithPrefixAndDispose(
364 "Name",
365 clang_HTMLTagComment_getTagName(Comment));
366 if (clang_InlineContentComment_hasTrailingNewline(Comment))
367 printf(" HasTrailingNewline");
368 break;
369 case CXComment_Paragraph:
370 printf("CXComment_Paragraph");
371 if (clang_Comment_isWhitespace(Comment))
372 printf(" IsWhitespace");
373 break;
374 case CXComment_BlockCommand:
375 printf("CXComment_BlockCommand");
376 PrintCXStringWithPrefixAndDispose(
377 "CommandName",
378 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000379 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000380 i != e; ++i) {
381 printf(" Arg[%u]=", i);
382 PrintCXStringAndDispose(
383 clang_BlockCommandComment_getArgText(Comment, i));
384 }
385 break;
386 case CXComment_ParamCommand:
387 printf("CXComment_ParamCommand");
388 switch (clang_ParamCommandComment_getDirection(Comment)) {
389 case CXCommentParamPassDirection_In:
390 printf(" in");
391 break;
392 case CXCommentParamPassDirection_Out:
393 printf(" out");
394 break;
395 case CXCommentParamPassDirection_InOut:
396 printf(" in,out");
397 break;
398 }
399 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
400 printf(" explicitly");
401 else
402 printf(" implicitly");
403 PrintCXStringWithPrefixAndDispose(
404 "ParamName",
405 clang_ParamCommandComment_getParamName(Comment));
406 if (clang_ParamCommandComment_isParamIndexValid(Comment))
407 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
408 else
409 printf(" ParamIndex=Invalid");
410 break;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000411 case CXComment_TParamCommand:
412 printf("CXComment_TParamCommand");
413 PrintCXStringWithPrefixAndDispose(
414 "ParamName",
415 clang_TParamCommandComment_getParamName(Comment));
416 if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
417 printf(" ParamPosition={");
418 for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
419 i != e; ++i) {
420 printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
421 if (i != e - 1)
422 printf(", ");
423 }
424 printf("}");
425 } else
426 printf(" ParamPosition=Invalid");
427 break;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000428 case CXComment_VerbatimBlockCommand:
429 printf("CXComment_VerbatimBlockCommand");
430 PrintCXStringWithPrefixAndDispose(
431 "CommandName",
432 clang_BlockCommandComment_getCommandName(Comment));
433 break;
434 case CXComment_VerbatimBlockLine:
435 printf("CXComment_VerbatimBlockLine");
436 PrintCXStringWithPrefixAndDispose(
437 "Text",
438 clang_VerbatimBlockLineComment_getText(Comment));
439 break;
440 case CXComment_VerbatimLine:
441 printf("CXComment_VerbatimLine");
442 PrintCXStringWithPrefixAndDispose(
443 "Text",
444 clang_VerbatimLineComment_getText(Comment));
445 break;
446 case CXComment_FullComment:
447 printf("CXComment_FullComment");
448 break;
449 }
450 if (Kind != CXComment_Null) {
451 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000452 unsigned i;
453 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000454 printf("\n// %s: ", FileCheckPrefix);
455 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
456 }
457 }
458 printf(")");
459 Ctx->IndentLevel--;
460}
461
462static void DumpCXComment(CXComment Comment) {
463 struct CommentASTDumpingContext Ctx;
464 Ctx.IndentLevel = 1;
465 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
466 DumpCXCommentInternal(&Ctx, Comment);
467 printf("]");
468}
469
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000470typedef struct {
471 const char *CommentSchemaFile;
472#ifdef CLANG_HAVE_LIBXML
473 xmlRelaxNGParserCtxtPtr RNGParser;
474 xmlRelaxNGPtr Schema;
475#endif
476} CommentXMLValidationData;
477
478static void ValidateCommentXML(const char *Str,
479 CommentXMLValidationData *ValidationData) {
480#ifdef CLANG_HAVE_LIBXML
481 xmlDocPtr Doc;
482 xmlRelaxNGValidCtxtPtr ValidationCtxt;
483 int status;
484
485 if (!ValidationData || !ValidationData->CommentSchemaFile)
486 return;
487
488 if (!ValidationData->RNGParser) {
489 ValidationData->RNGParser =
490 xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile);
491 ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser);
492 }
493 if (!ValidationData->RNGParser) {
494 printf(" libXMLError");
495 return;
496 }
497
498 Doc = xmlParseDoc((const xmlChar *) Str);
499
500 if (!Doc) {
501 xmlErrorPtr Error = xmlGetLastError();
502 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
503 return;
504 }
505
506 ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->Schema);
507 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
508 if (!status)
509 printf(" CommentXMLValid");
510 else if (status > 0) {
511 xmlErrorPtr Error = xmlGetLastError();
512 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
513 } else
514 printf(" libXMLError");
515
516 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
517 xmlFreeDoc(Doc);
518#endif
519}
520
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000521static void PrintCursorComments(CXCursor Cursor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000522 CommentXMLValidationData *ValidationData) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000523 {
524 CXString RawComment;
525 const char *RawCommentCString;
526 CXString BriefComment;
527 const char *BriefCommentCString;
528
529 RawComment = clang_Cursor_getRawCommentText(Cursor);
530 RawCommentCString = clang_getCString(RawComment);
531 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
532 PrintCStringWithPrefix("RawComment", RawCommentCString);
533 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
534
535 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
536 BriefCommentCString = clang_getCString(BriefComment);
537 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
538 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
539 clang_disposeString(BriefComment);
540 }
541 clang_disposeString(RawComment);
542 }
543
544 {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000545 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000546 if (clang_Comment_getKind(Comment) != CXComment_Null) {
547 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
548 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000549 {
550 CXString XML;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000551 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000552 PrintCXStringWithPrefix("FullCommentAsXML", XML);
553 ValidateCommentXML(clang_getCString(XML), ValidationData);
554 clang_disposeString(XML);
555 }
556
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000557 DumpCXComment(Comment);
558 }
559 }
560}
561
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000562typedef struct {
563 unsigned line;
564 unsigned col;
565} LineCol;
566
567static int lineCol_cmp(const void *p1, const void *p2) {
568 const LineCol *lhs = p1;
569 const LineCol *rhs = p2;
570 if (lhs->line != rhs->line)
571 return (int)lhs->line - (int)rhs->line;
572 return (int)lhs->col - (int)rhs->col;
573}
574
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000575static void PrintCursor(CXCursor Cursor,
576 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000577 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578 if (clang_isInvalid(Cursor.kind)) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000579 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000580 printf("Invalid Cursor => %s", clang_getCString(ks));
581 clang_disposeString(ks);
582 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000583 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000584 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000585 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000586 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000587 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000588 CXCursor *overridden;
589 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000590 unsigned RefNameRangeNr;
591 CXSourceRange CursorExtent;
592 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000593 int AlwaysUnavailable;
594 int AlwaysDeprecated;
595 CXString UnavailableMessage;
596 CXString DeprecatedMessage;
597 CXPlatformAvailability PlatformAvailability[2];
598 int NumPlatformAvailability;
599 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000600
Ted Kremeneke68fff62010-02-17 00:41:32 +0000601 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000602 string = want_display_name? clang_getCursorDisplayName(Cursor)
603 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000604 printf("%s=%s", clang_getCString(ks),
605 clang_getCString(string));
606 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000607 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000608
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000609 Referenced = clang_getCursorReferenced(Cursor);
610 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000611 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
612 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
613 printf("[");
614 for (I = 0; I != N; ++I) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000615 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000616 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000617 if (I)
618 printf(", ");
619
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000620 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000621 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000622 printf("%d:%d", line, column);
623 }
624 printf("]");
625 } else {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000626 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000627 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000628 printf(":%d:%d", line, column);
629 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000630 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000631
632 if (clang_isCursorDefinition(Cursor))
633 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000634
635 switch (clang_getCursorAvailability(Cursor)) {
636 case CXAvailability_Available:
637 break;
638
639 case CXAvailability_Deprecated:
640 printf(" (deprecated)");
641 break;
642
643 case CXAvailability_NotAvailable:
644 printf(" (unavailable)");
645 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000646
647 case CXAvailability_NotAccessible:
648 printf(" (inaccessible)");
649 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000650 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000651
Douglas Gregorcc889662012-05-08 00:14:45 +0000652 NumPlatformAvailability
653 = clang_getCursorPlatformAvailability(Cursor,
654 &AlwaysDeprecated,
655 &DeprecatedMessage,
656 &AlwaysUnavailable,
657 &UnavailableMessage,
658 PlatformAvailability, 2);
659 if (AlwaysUnavailable) {
660 printf(" (always unavailable: \"%s\")",
661 clang_getCString(UnavailableMessage));
662 } else if (AlwaysDeprecated) {
663 printf(" (always deprecated: \"%s\")",
664 clang_getCString(DeprecatedMessage));
665 } else {
666 for (I = 0; I != NumPlatformAvailability; ++I) {
667 if (I >= 2)
668 break;
669
670 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
671 if (PlatformAvailability[I].Unavailable)
672 printf(", unavailable");
673 else {
674 printVersion(", introduced=", PlatformAvailability[I].Introduced);
675 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
676 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
677 }
678 if (clang_getCString(PlatformAvailability[I].Message)[0])
679 printf(", message=\"%s\"",
680 clang_getCString(PlatformAvailability[I].Message));
681 printf(")");
682 }
683 }
684 for (I = 0; I != NumPlatformAvailability; ++I) {
685 if (I >= 2)
686 break;
687 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
688 }
689
690 clang_disposeString(DeprecatedMessage);
691 clang_disposeString(UnavailableMessage);
692
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000693 if (clang_CXXMethod_isStatic(Cursor))
694 printf(" (static)");
695 if (clang_CXXMethod_isVirtual(Cursor))
696 printf(" (virtual)");
Dmitri Gribenkoc965f762013-05-17 18:38:35 +0000697 if (clang_CXXMethod_isPureVirtual(Cursor))
698 printf(" (pure)");
Argyrios Kyrtzidis80e1aca2013-04-18 23:53:05 +0000699 if (clang_Cursor_isVariadic(Cursor))
700 printf(" (variadic)");
Argyrios Kyrtzidis514afc72013-07-05 20:44:37 +0000701 if (clang_Cursor_isObjCOptional(Cursor))
702 printf(" (@optional)");
703
Ted Kremenek95f33552010-08-26 01:42:22 +0000704 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000705 CXType T =
706 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
707 CXString S = clang_getTypeKindSpelling(T.kind);
Ted Kremenek95f33552010-08-26 01:42:22 +0000708 printf(" [IBOutletCollection=%s]", clang_getCString(S));
709 clang_disposeString(S);
710 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000711
712 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
713 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
714 unsigned isVirtual = clang_isVirtualBase(Cursor);
715 const char *accessStr = 0;
716
717 switch (access) {
718 case CX_CXXInvalidAccessSpecifier:
719 accessStr = "invalid"; break;
720 case CX_CXXPublic:
721 accessStr = "public"; break;
722 case CX_CXXProtected:
723 accessStr = "protected"; break;
724 case CX_CXXPrivate:
725 accessStr = "private"; break;
726 }
727
728 printf(" [access=%s isVirtual=%s]", accessStr,
729 isVirtual ? "true" : "false");
730 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000731
732 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
733 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000734 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
735 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000736 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000737 printf(" [Specialization of %s:%d:%d]",
738 clang_getCString(Name), line, column);
739 clang_disposeString(Name);
740 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000741
742 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
743 if (num_overridden) {
744 unsigned I;
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000745 LineCol lineCols[50];
746 assert(num_overridden <= 50);
Douglas Gregor9f592342010-10-01 20:25:15 +0000747 printf(" [Overrides ");
748 for (I = 0; I != num_overridden; ++I) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000749 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000750 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000751 lineCols[I].line = line;
752 lineCols[I].col = column;
753 }
Michael Liao64221492012-08-30 00:45:32 +0000754 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000755 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
756 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor9f592342010-10-01 20:25:15 +0000757 if (I)
758 printf(", ");
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000759 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor9f592342010-10-01 20:25:15 +0000760 }
761 printf("]");
762 clang_disposeOverriddenCursors(overridden);
763 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000764
765 if (Cursor.kind == CXCursor_InclusionDirective) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000766 CXFile File = clang_getIncludedFile(Cursor);
767 CXString Included = clang_getFileName(File);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000768 printf(" (%s)", clang_getCString(Included));
769 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000770
771 if (clang_isFileMultipleIncludeGuarded(TU, File))
772 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000773 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000774
775 CursorExtent = clang_getCursorExtent(Cursor);
776 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
777 CXNameRange_WantQualifier
778 | CXNameRange_WantSinglePiece
779 | CXNameRange_WantTemplateArgs,
780 0);
781 if (!clang_equalRanges(CursorExtent, RefNameRange))
782 PrintRange(RefNameRange, "SingleRefName");
783
784 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
785 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
786 CXNameRange_WantQualifier
787 | CXNameRange_WantTemplateArgs,
788 RefNameRangeNr);
789 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
790 break;
791 if (!clang_equalRanges(CursorExtent, RefNameRange))
792 PrintRange(RefNameRange, "RefName");
793 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000794
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000795 PrintCursorComments(Cursor, ValidationData);
Argyrios Kyrtzidis9ee6a662013-04-18 22:15:49 +0000796
797 {
798 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
799 if (PropAttrs != CXObjCPropertyAttr_noattr) {
800 printf(" [");
801 #define PRINT_PROP_ATTR(A) \
802 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
803 PRINT_PROP_ATTR(readonly);
804 PRINT_PROP_ATTR(getter);
805 PRINT_PROP_ATTR(assign);
806 PRINT_PROP_ATTR(readwrite);
807 PRINT_PROP_ATTR(retain);
808 PRINT_PROP_ATTR(copy);
809 PRINT_PROP_ATTR(nonatomic);
810 PRINT_PROP_ATTR(setter);
811 PRINT_PROP_ATTR(atomic);
812 PRINT_PROP_ATTR(weak);
813 PRINT_PROP_ATTR(strong);
814 PRINT_PROP_ATTR(unsafe_unretained);
815 printf("]");
816 }
817 }
Argyrios Kyrtzidis38dbad22013-04-18 23:29:12 +0000818
819 {
820 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
821 if (QT != CXObjCDeclQualifier_None) {
822 printf(" [");
823 #define PRINT_OBJC_QUAL(A) \
824 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
825 PRINT_OBJC_QUAL(In);
826 PRINT_OBJC_QUAL(Inout);
827 PRINT_OBJC_QUAL(Out);
828 PRINT_OBJC_QUAL(Bycopy);
829 PRINT_OBJC_QUAL(Byref);
830 PRINT_OBJC_QUAL(Oneway);
831 printf("]");
832 }
833 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000834 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000835}
Steve Naroff89922f82009-08-31 00:59:03 +0000836
Ted Kremeneke68fff62010-02-17 00:41:32 +0000837static const char* GetCursorSource(CXCursor Cursor) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000838 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000839 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000840 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000841 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000842 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000843 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000844 clang_disposeString(source);
845 return "<invalid loc>";
846 }
847 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000848 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000849 clang_disposeString(source);
850 return b;
851 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000852}
853
Ted Kremenek0d435192009-11-17 18:13:31 +0000854/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000855/* Callbacks. */
856/******************************************************************************/
857
858typedef void (*PostVisitTU)(CXTranslationUnit);
859
Douglas Gregora88084b2010-02-18 18:08:43 +0000860void PrintDiagnostic(CXDiagnostic Diagnostic) {
861 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000862 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000863 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000864 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000865 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
866 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000867 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000868
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000869 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000870 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000871
Douglas Gregor274f1902010-02-22 23:17:23 +0000872 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
873 fprintf(stderr, "%s\n", clang_getCString(Msg));
874 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000875
Douglas Gregora9b06d42010-11-09 06:24:54 +0000876 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
877 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000878 if (!file)
879 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000880
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000881 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000882 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000883 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000884 CXSourceRange range;
Enea Zaffanella10f90042013-07-22 20:58:30 +0000885 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
886 CXSourceLocation start = clang_getRangeStart(range);
887 CXSourceLocation end = clang_getRangeEnd(range);
Douglas Gregor473d7012010-02-19 18:16:06 +0000888 unsigned start_line, start_column, end_line, end_column;
889 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000890 clang_getSpellingLocation(start, &start_file, &start_line,
891 &start_column, 0);
892 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000893 if (clang_equalLocations(start, end)) {
894 /* Insertion. */
895 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000896 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000897 clang_getCString(insertion_text), start_line, start_column);
898 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
899 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000900 if (start_file == file && end_file == file) {
901 fprintf(out, "FIX-IT: Remove ");
902 PrintExtent(out, start_line, start_column, end_line, end_column);
903 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000904 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000905 } else {
906 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000907 if (start_file == end_file) {
908 fprintf(out, "FIX-IT: Replace ");
909 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000910 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000911 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000912 break;
913 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000914 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000915 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000916}
917
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000918void PrintDiagnosticSet(CXDiagnosticSet Set) {
919 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
920 for ( ; i != n ; ++i) {
921 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
922 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000923 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000924 if (ChildDiags)
925 PrintDiagnosticSet(ChildDiags);
926 }
927}
928
929void PrintDiagnostics(CXTranslationUnit TU) {
930 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
931 PrintDiagnosticSet(TUSet);
932 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000933}
934
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000935void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000936 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000937 unsigned i = 0;
Enea Zaffanella10f90042013-07-22 20:58:30 +0000938 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000939 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000940 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000941 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000942 unsigned long amount = usage.entries[i].amount;
943 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000944 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000945 ((double) amount)/(1024*1024));
946 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000947 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000948 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000949 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000950}
951
Ted Kremenekce2ae882010-01-26 17:59:48 +0000952/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000953/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000954/******************************************************************************/
955
Douglas Gregora7bde202010-01-19 00:34:46 +0000956static void PrintCursorExtent(CXCursor C) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000957 CXSourceRange extent = clang_getCursorExtent(C);
958 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000959}
960
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000961/* Data used by the visitors. */
962typedef struct {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000963 CXTranslationUnit TU;
964 enum CXCursorKind *Filter;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000965 CommentXMLValidationData ValidationData;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000966} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000967
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000968
Ted Kremeneke68fff62010-02-17 00:41:32 +0000969enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000970 CXCursor Parent,
971 CXClientData ClientData) {
972 VisitorData *Data = (VisitorData *)ClientData;
973 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella10f90042013-07-22 20:58:30 +0000974 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000975 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000976 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000977 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000978 GetCursorSource(Cursor), line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000979 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregora7bde202010-01-19 00:34:46 +0000980 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis04b67482013-04-11 17:02:10 +0000981 if (clang_isDeclaration(Cursor.kind)) {
982 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
983 const char *accessStr = 0;
984
985 switch (access) {
986 case CX_CXXInvalidAccessSpecifier: break;
987 case CX_CXXPublic:
988 accessStr = "public"; break;
989 case CX_CXXProtected:
990 accessStr = "protected"; break;
991 case CX_CXXPrivate:
992 accessStr = "private"; break;
993 }
994
995 if (accessStr)
996 printf(" [access=%s]", accessStr);
997 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000998 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000999 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +00001000 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001001
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001002 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +00001003}
Steve Naroff50398192009-08-28 15:28:48 +00001004
Ted Kremeneke68fff62010-02-17 00:41:32 +00001005static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001006 CXCursor Parent,
1007 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001008 const char *startBuf, *endBuf;
1009 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1010 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001011 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001012
Douglas Gregorb6998662010-01-19 19:34:47 +00001013 if (Cursor.kind != CXCursor_FunctionDecl ||
1014 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001015 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001016
1017 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1018 &startLine, &startColumn,
1019 &endLine, &endColumn);
1020 /* Probe the entire body, looking for both decls and refs. */
1021 curLine = startLine;
1022 curColumn = startColumn;
1023
1024 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001025 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +00001026 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +00001027 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001028
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001029 if (*startBuf == '\n') {
1030 startBuf++;
1031 curLine++;
1032 curColumn = 1;
1033 } else if (*startBuf != '\t')
1034 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001035
Douglas Gregor98258af2010-01-18 22:46:11 +00001036 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +00001037 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001038
Douglas Gregor1db19de2010-01-19 21:36:55 +00001039 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001040 if (clang_getCString(source)) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001041 CXSourceLocation RefLoc
1042 = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregorb9790342010-01-22 21:44:22 +00001043 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001044 if (Ref.kind == CXCursor_NoDeclFound) {
1045 /* Nothing found here; that's fine. */
1046 } else if (Ref.kind != CXCursor_FunctionDecl) {
1047 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1048 curLine, curColumn);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001049 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor98258af2010-01-18 22:46:11 +00001050 printf("\n");
1051 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001052 }
Ted Kremenek74844072010-02-17 00:41:20 +00001053 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001054 startBuf++;
1055 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001056
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001057 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001058}
1059
Ted Kremenek7d405622010-01-12 23:34:26 +00001060/******************************************************************************/
1061/* USR testing. */
1062/******************************************************************************/
1063
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001064enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1065 CXClientData ClientData) {
1066 VisitorData *Data = (VisitorData *)ClientData;
1067 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001068 CXString USR = clang_getCursorUSR(C);
1069 const char *cstr = clang_getCString(USR);
Ted Kremeneke542f772010-04-20 23:15:40 +00001070 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +00001071 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +00001072 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +00001073 }
Ted Kremeneke542f772010-04-20 23:15:40 +00001074 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1075
Douglas Gregora7bde202010-01-19 00:34:46 +00001076 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +00001077 printf("\n");
1078 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001079
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001080 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001081 }
1082
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001083 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +00001084}
1085
1086/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +00001087/* Inclusion stack testing. */
1088/******************************************************************************/
1089
1090void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1091 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001092
Ted Kremenek16b55a72010-01-26 19:31:51 +00001093 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +00001094 CXString fname;
1095
1096 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001097 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +00001098 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001099
Ted Kremenek16b55a72010-01-26 19:31:51 +00001100 for (i = 0; i < includeStackLen; ++i) {
1101 CXFile includingFile;
1102 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +00001103 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1104 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +00001105 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001106 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +00001107 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001108 }
1109 printf("\n");
1110}
1111
1112void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001113 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001114}
1115
1116/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +00001117/* Linkage testing. */
1118/******************************************************************************/
1119
1120static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1121 CXClientData d) {
1122 const char *linkage = 0;
1123
1124 if (clang_isInvalid(clang_getCursorKind(cursor)))
1125 return CXChildVisit_Recurse;
1126
1127 switch (clang_getCursorLinkage(cursor)) {
1128 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +00001129 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1130 case CXLinkage_Internal: linkage = "Internal"; break;
1131 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1132 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +00001133 }
1134
1135 if (linkage) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001136 PrintCursor(cursor, NULL);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001137 printf("linkage=%s\n", linkage);
1138 }
1139
1140 return CXChildVisit_Recurse;
1141}
1142
1143/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001144/* Typekind testing. */
1145/******************************************************************************/
1146
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001147static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1148 CXString TypeSpelling, TypeKindSpelling;
1149
1150 TypeSpelling = clang_getTypeSpelling(T);
1151 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1152 printf(Format,
1153 clang_getCString(TypeSpelling),
1154 clang_getCString(TypeKindSpelling));
1155 clang_disposeString(TypeSpelling);
1156 clang_disposeString(TypeKindSpelling);
1157}
1158
1159static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1160 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001161 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001162 CXType T = clang_getCursorType(cursor);
Argyrios Kyrtzidis659837e2013-10-11 19:58:38 +00001163 enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001164 PrintCursor(cursor, NULL);
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001165 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregore72fb6f2011-01-27 16:27:11 +00001166 if (clang_isConstQualifiedType(T))
1167 printf(" const");
1168 if (clang_isVolatileQualifiedType(T))
1169 printf(" volatile");
1170 if (clang_isRestrictQualifiedType(T))
1171 printf(" restrict");
Argyrios Kyrtzidis659837e2013-10-11 19:58:38 +00001172 if (RQ == CXRefQualifier_LValue)
1173 printf(" lvalue-ref-qualifier");
1174 if (RQ == CXRefQualifier_RValue)
1175 printf(" rvalue-ref-qualifier");
Benjamin Kramere1403d22010-06-22 09:29:44 +00001176 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001177 {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001178 CXType CT = clang_getCanonicalType(T);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001179 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001180 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001181 }
1182 }
Benjamin Kramere1403d22010-06-22 09:29:44 +00001183 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001184 {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001185 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001186 if (RT.kind != CXType_Invalid) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001187 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001188 }
1189 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001190 /* Print the argument types if they exist. */
1191 {
1192 int numArgs = clang_Cursor_getNumArguments(cursor);
1193 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001194 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001195 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001196 for (i = 0; i < numArgs; ++i) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001197 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001198 if (T.kind != CXType_Invalid) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001199 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001200 }
1201 }
1202 printf("]");
1203 }
1204 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +00001205 /* Print if this is a non-POD type. */
1206 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001207
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001208 printf("\n");
1209 }
1210 return CXChildVisit_Recurse;
1211}
1212
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00001213static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1214 CXClientData d) {
1215 CXType T;
1216 enum CXCursorKind K = clang_getCursorKind(cursor);
1217 if (clang_isInvalid(K))
1218 return CXChildVisit_Recurse;
1219 T = clang_getCursorType(cursor);
1220 PrintCursor(cursor, NULL);
1221 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1222 /* Print the type sizeof if applicable. */
1223 {
1224 long long Size = clang_Type_getSizeOf(T);
1225 if (Size >= 0 || Size < -1 ) {
1226 printf(" [sizeof=%lld]", Size);
1227 }
1228 }
1229 /* Print the type alignof if applicable. */
1230 {
1231 long long Align = clang_Type_getAlignOf(T);
1232 if (Align >= 0 || Align < -1) {
1233 printf(" [alignof=%lld]", Align);
1234 }
1235 }
1236 /* Print the record field offset if applicable. */
1237 {
1238 const char *FieldName = clang_getCString(clang_getCursorSpelling(cursor));
1239 /* recurse to get the root anonymous record parent */
1240 CXCursor Parent, Root;
1241 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl ) {
1242 const char *RootParentName;
1243 Root = Parent = p;
1244 do {
1245 Root = Parent;
1246 RootParentName = clang_getCString(clang_getCursorSpelling(Root));
1247 Parent = clang_getCursorSemanticParent(Root);
1248 } while ( clang_getCursorType(Parent).kind == CXType_Record &&
1249 !strcmp(RootParentName, "") );
1250 /* if RootParentName is "", record is anonymous. */
1251 {
1252 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1253 FieldName);
1254 printf(" [offsetof=%lld]", Offset);
1255 }
1256 }
1257 }
1258 /* Print if its a bitfield */
1259 {
1260 int IsBitfield = clang_Cursor_isBitField(cursor);
1261 if (IsBitfield)
1262 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1263 }
1264 printf("\n");
1265 return CXChildVisit_Recurse;
1266}
1267
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001268/******************************************************************************/
1269/* Bitwidth testing. */
1270/******************************************************************************/
1271
1272static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1273 CXClientData d) {
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001274 int Bitwidth;
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001275 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1276 return CXChildVisit_Recurse;
1277
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001278 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001279 if (Bitwidth >= 0) {
1280 PrintCursor(cursor, NULL);
1281 printf(" bitwidth=%d\n", Bitwidth);
1282 }
1283
1284 return CXChildVisit_Recurse;
1285}
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001286
1287/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +00001288/* Loading ASTs/source. */
1289/******************************************************************************/
1290
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001291static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +00001292 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001293 CXCursorVisitor Visitor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001294 PostVisitTU PV,
1295 const char *CommentSchemaFile) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001296
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001297 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001298 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001299
1300 if (Visitor) {
1301 enum CXCursorKind K = CXCursor_NotImplemented;
1302 enum CXCursorKind *ck = &K;
1303 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001304
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001305 /* Perform some simple filtering. */
1306 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001307 else if (!strcmp(filter, "all-display") ||
1308 !strcmp(filter, "local-display")) {
1309 ck = NULL;
1310 want_display_name = 1;
1311 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001312 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001313 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1314 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1315 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1316 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1317 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1318 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1319 else {
1320 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1321 return 1;
1322 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001323
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001324 Data.TU = TU;
1325 Data.Filter = ck;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001326 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1327#ifdef CLANG_HAVE_LIBXML
1328 Data.ValidationData.RNGParser = NULL;
1329 Data.ValidationData.Schema = NULL;
1330#endif
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001331 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001332 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001333
Ted Kremenekce2ae882010-01-26 17:59:48 +00001334 if (PV)
1335 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001336
Douglas Gregora88084b2010-02-18 18:08:43 +00001337 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001338 if (checkForErrors(TU) != 0) {
1339 clang_disposeTranslationUnit(TU);
1340 return -1;
1341 }
1342
Ted Kremenek0d435192009-11-17 18:13:31 +00001343 clang_disposeTranslationUnit(TU);
1344 return 0;
1345}
1346
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001347int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001348 const char *prefix, CXCursorVisitor Visitor,
1349 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001350 CXIndex Idx;
1351 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001352 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001353 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001354 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00001355 /* displayDiagnostics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001356
Ted Kremenek020a0952010-02-11 07:41:25 +00001357 if (!CreateTranslationUnit(Idx, file, &TU)) {
1358 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001359 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001360 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001361
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001362 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek020a0952010-02-11 07:41:25 +00001363 clang_disposeIndex(Idx);
1364 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001365}
1366
Ted Kremenekce2ae882010-01-26 17:59:48 +00001367int perform_test_load_source(int argc, const char **argv,
1368 const char *filter, CXCursorVisitor Visitor,
1369 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001370 CXIndex Idx;
1371 CXTranslationUnit TU;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001372 const char *CommentSchemaFile;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001373 struct CXUnsavedFile *unsaved_files = 0;
1374 int num_unsaved_files = 0;
1375 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001376
Daniel Dunbarada487d2009-12-01 02:03:10 +00001377 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001378 (!strcmp(filter, "local") ||
1379 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidiscd6dcb32013-04-09 20:29:24 +00001380 /* displayDiagnostics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001381
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001382 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1383 argc--;
1384 argv++;
1385 }
1386
Ted Kremenek020a0952010-02-11 07:41:25 +00001387 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1388 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001389 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001390 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001391
Douglas Gregordca8ee82011-05-06 16:33:08 +00001392 TU = clang_parseTranslationUnit(Idx, 0,
1393 argv + num_unsaved_files,
1394 argc - num_unsaved_files,
1395 unsaved_files, num_unsaved_files,
1396 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001397 if (!TU) {
1398 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001399 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001400 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001401 return 1;
1402 }
1403
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001404 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1405 CommentSchemaFile);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001406 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001407 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001408 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001409}
1410
Douglas Gregorabc563f2010-07-19 21:46:24 +00001411int perform_test_reparse_source(int argc, const char **argv, int trials,
1412 const char *filter, CXCursorVisitor Visitor,
1413 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001414 CXIndex Idx;
1415 CXTranslationUnit TU;
1416 struct CXUnsavedFile *unsaved_files = 0;
1417 int num_unsaved_files = 0;
1418 int result;
1419 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001420 int remap_after_trial = 0;
1421 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001422
1423 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1424 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidiscd6dcb32013-04-09 20:29:24 +00001425 /* displayDiagnostics=*/1);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001426
Douglas Gregorabc563f2010-07-19 21:46:24 +00001427 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1428 clang_disposeIndex(Idx);
1429 return -1;
1430 }
1431
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001432 /* Load the initial translation unit -- we do this without honoring remapped
1433 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001434 TU = clang_parseTranslationUnit(Idx, 0,
1435 argv + num_unsaved_files,
1436 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001437 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001438 if (!TU) {
1439 fprintf(stderr, "Unable to load translation unit!\n");
1440 free_remapped_files(unsaved_files, num_unsaved_files);
1441 clang_disposeIndex(Idx);
1442 return 1;
1443 }
1444
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001445 if (checkForErrors(TU) != 0)
1446 return -1;
1447
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001448 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1449 remap_after_trial =
1450 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1451 }
1452
Douglas Gregorabc563f2010-07-19 21:46:24 +00001453 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001454 if (clang_reparseTranslationUnit(TU,
1455 trial >= remap_after_trial ? num_unsaved_files : 0,
1456 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001457 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001458 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001459 clang_disposeTranslationUnit(TU);
1460 free_remapped_files(unsaved_files, num_unsaved_files);
1461 clang_disposeIndex(Idx);
1462 return -1;
1463 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001464
1465 if (checkForErrors(TU) != 0)
1466 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001467 }
1468
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001469 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001470
Douglas Gregorabc563f2010-07-19 21:46:24 +00001471 free_remapped_files(unsaved_files, num_unsaved_files);
1472 clang_disposeIndex(Idx);
1473 return result;
1474}
1475
Ted Kremenek0d435192009-11-17 18:13:31 +00001476/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001477/* Logic for testing clang_getCursor(). */
1478/******************************************************************************/
1479
Douglas Gregordd3e5542011-05-04 00:14:37 +00001480static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001481 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001482 unsigned end_line, unsigned end_col,
1483 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001484 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001485 if (prefix)
1486 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001487 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1488 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001489 PrintCursor(cursor, NULL);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001490 printf("\n");
1491}
1492
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001493static int perform_file_scan(const char *ast_file, const char *source_file,
1494 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001495 CXIndex Idx;
1496 CXTranslationUnit TU;
1497 FILE *fp;
Enea Zaffanella10f90042013-07-22 20:58:30 +00001498 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001499 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001500 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001501 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001502
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001503 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00001504 /* displayDiagnostics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001505 fprintf(stderr, "Could not create Index\n");
1506 return 1;
1507 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001508
Ted Kremenek1c6da172009-11-17 19:37:36 +00001509 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1510 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001511
Ted Kremenek1c6da172009-11-17 19:37:36 +00001512 if ((fp = fopen(source_file, "r")) == NULL) {
1513 fprintf(stderr, "Could not open '%s'\n", source_file);
1514 return 1;
1515 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001516
Douglas Gregorb9790342010-01-22 21:44:22 +00001517 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001518 for (;;) {
1519 CXCursor cursor;
1520 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001521
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001522 if (c == '\n') {
1523 ++line;
1524 col = 1;
1525 } else
1526 ++col;
1527
1528 /* Check the cursor at this position, and dump the previous one if we have
1529 * found something new.
1530 */
1531 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1532 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1533 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001534 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001535 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001536 start_line = line;
1537 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001538 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001539 if (c == EOF)
1540 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001541
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001542 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001543 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001544
Ted Kremenek1c6da172009-11-17 19:37:36 +00001545 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001546 clang_disposeTranslationUnit(TU);
1547 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001548 return 0;
1549}
1550
1551/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001552/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001553/******************************************************************************/
1554
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001555/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1556 on failure. If successful, the pointer *filename will contain newly-allocated
1557 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001558int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001559 unsigned *column, unsigned *second_line,
1560 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001561 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001562 const char *last_colon = strrchr(input, ':');
1563 unsigned values[4], i;
1564 unsigned num_values = (second_line && second_column)? 4 : 2;
1565
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001566 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001567 if (!last_colon || last_colon == input) {
1568 if (num_values == 4)
1569 fprintf(stderr, "could not parse filename:line:column:line:column in "
1570 "'%s'\n", input);
1571 else
1572 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001573 return 1;
1574 }
1575
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001576 for (i = 0; i != num_values; ++i) {
1577 const char *prev_colon;
1578
1579 /* Parse the next line or column. */
1580 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1581 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001582 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001583 (i % 2 ? "column" : "line"), input);
1584 return 1;
1585 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001586
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001587 if (i + 1 == num_values)
1588 break;
1589
1590 /* Find the previous colon. */
1591 prev_colon = last_colon - 1;
1592 while (prev_colon != input && *prev_colon != ':')
1593 --prev_colon;
1594 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001595 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001596 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001597 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001598 }
1599
1600 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001601 }
1602
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001603 *line = values[0];
1604 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001605
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001606 if (second_line && second_column) {
1607 *second_line = values[2];
1608 *second_column = values[3];
1609 }
1610
Douglas Gregor88d23952009-11-09 18:19:57 +00001611 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001612 *filename = (char*)malloc(last_colon - input + 1);
1613 memcpy(*filename, input, last_colon - input);
1614 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001615 return 0;
1616}
1617
1618const char *
1619clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1620 switch (Kind) {
1621 case CXCompletionChunk_Optional: return "Optional";
1622 case CXCompletionChunk_TypedText: return "TypedText";
1623 case CXCompletionChunk_Text: return "Text";
1624 case CXCompletionChunk_Placeholder: return "Placeholder";
1625 case CXCompletionChunk_Informative: return "Informative";
1626 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1627 case CXCompletionChunk_LeftParen: return "LeftParen";
1628 case CXCompletionChunk_RightParen: return "RightParen";
1629 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1630 case CXCompletionChunk_RightBracket: return "RightBracket";
1631 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1632 case CXCompletionChunk_RightBrace: return "RightBrace";
1633 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1634 case CXCompletionChunk_RightAngle: return "RightAngle";
1635 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001636 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001637 case CXCompletionChunk_Colon: return "Colon";
1638 case CXCompletionChunk_SemiColon: return "SemiColon";
1639 case CXCompletionChunk_Equal: return "Equal";
1640 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1641 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001642 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001643
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001644 return "Unknown";
1645}
1646
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001647static int checkForErrors(CXTranslationUnit TU) {
1648 unsigned Num, i;
1649 CXDiagnostic Diag;
1650 CXString DiagStr;
1651
1652 if (!getenv("CINDEXTEST_FAILONERROR"))
1653 return 0;
1654
1655 Num = clang_getNumDiagnostics(TU);
1656 for (i = 0; i != Num; ++i) {
1657 Diag = clang_getDiagnostic(TU, i);
1658 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1659 DiagStr = clang_formatDiagnostic(Diag,
1660 clang_defaultDiagnosticDisplayOptions());
1661 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1662 clang_disposeString(DiagStr);
1663 clang_disposeDiagnostic(Diag);
1664 return -1;
1665 }
1666 clang_disposeDiagnostic(Diag);
1667 }
1668
1669 return 0;
1670}
1671
Douglas Gregor3ac73852009-11-09 16:04:45 +00001672void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001673 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001674
Douglas Gregor3ac73852009-11-09 16:04:45 +00001675 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001676 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001677 CXString text;
1678 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001679 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001680 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001681
Douglas Gregor3ac73852009-11-09 16:04:45 +00001682 if (Kind == CXCompletionChunk_Optional) {
1683 fprintf(file, "{Optional ");
1684 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001685 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001686 file);
1687 fprintf(file, "}");
1688 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001689 }
1690
1691 if (Kind == CXCompletionChunk_VerticalSpace) {
1692 fprintf(file, "{VerticalSpace }");
1693 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001694 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001695
Douglas Gregord5a20892009-11-09 17:05:28 +00001696 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001697 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001698 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001699 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001700 cstr ? cstr : "");
1701 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001702 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001703
Douglas Gregor3ac73852009-11-09 16:04:45 +00001704}
1705
1706void print_completion_result(CXCompletionResult *completion_result,
1707 CXClientData client_data) {
1708 FILE *file = (FILE *)client_data;
Enea Zaffanella10f90042013-07-22 20:58:30 +00001709 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001710 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001711 enum CXCursorKind ParentKind;
1712 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001713 CXString BriefComment;
1714 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001715
Ted Kremeneke68fff62010-02-17 00:41:32 +00001716 fprintf(file, "%s:", clang_getCString(ks));
1717 clang_disposeString(ks);
1718
Douglas Gregor3ac73852009-11-09 16:04:45 +00001719 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001720 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001721 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001722 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1723 case CXAvailability_Available:
1724 break;
1725
1726 case CXAvailability_Deprecated:
1727 fprintf(file, " (deprecated)");
1728 break;
1729
1730 case CXAvailability_NotAvailable:
1731 fprintf(file, " (unavailable)");
1732 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001733
1734 case CXAvailability_NotAccessible:
1735 fprintf(file, " (inaccessible)");
1736 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001737 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001738
1739 annotationCount = clang_getCompletionNumAnnotations(
1740 completion_result->CompletionString);
1741 if (annotationCount) {
1742 unsigned i;
1743 fprintf(file, " (");
1744 for (i = 0; i < annotationCount; ++i) {
1745 if (i != 0)
1746 fprintf(file, ", ");
1747 fprintf(file, "\"%s\"",
1748 clang_getCString(clang_getCompletionAnnotation(
1749 completion_result->CompletionString, i)));
1750 }
1751 fprintf(file, ")");
1752 }
1753
Douglas Gregorba103062012-03-27 23:34:16 +00001754 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1755 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1756 &ParentKind);
1757 if (ParentKind != CXCursor_NotImplemented) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00001758 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregorba103062012-03-27 23:34:16 +00001759 fprintf(file, " (parent: %s '%s')",
1760 clang_getCString(KindSpelling),
1761 clang_getCString(ParentName));
1762 clang_disposeString(KindSpelling);
1763 }
1764 clang_disposeString(ParentName);
1765 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001766
1767 BriefComment = clang_getCompletionBriefComment(
1768 completion_result->CompletionString);
1769 BriefCommentCString = clang_getCString(BriefComment);
1770 if (BriefCommentCString && *BriefCommentCString != '\0') {
1771 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1772 }
1773 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001774
Douglas Gregor58ddb602010-08-23 23:00:57 +00001775 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001776}
1777
Douglas Gregor3da626b2011-07-07 16:03:39 +00001778void print_completion_contexts(unsigned long long contexts, FILE *file) {
1779 fprintf(file, "Completion contexts:\n");
1780 if (contexts == CXCompletionContext_Unknown) {
1781 fprintf(file, "Unknown\n");
1782 }
1783 if (contexts & CXCompletionContext_AnyType) {
1784 fprintf(file, "Any type\n");
1785 }
1786 if (contexts & CXCompletionContext_AnyValue) {
1787 fprintf(file, "Any value\n");
1788 }
1789 if (contexts & CXCompletionContext_ObjCObjectValue) {
1790 fprintf(file, "Objective-C object value\n");
1791 }
1792 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1793 fprintf(file, "Objective-C selector value\n");
1794 }
1795 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1796 fprintf(file, "C++ class type value\n");
1797 }
1798 if (contexts & CXCompletionContext_DotMemberAccess) {
1799 fprintf(file, "Dot member access\n");
1800 }
1801 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1802 fprintf(file, "Arrow member access\n");
1803 }
1804 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1805 fprintf(file, "Objective-C property access\n");
1806 }
1807 if (contexts & CXCompletionContext_EnumTag) {
1808 fprintf(file, "Enum tag\n");
1809 }
1810 if (contexts & CXCompletionContext_UnionTag) {
1811 fprintf(file, "Union tag\n");
1812 }
1813 if (contexts & CXCompletionContext_StructTag) {
1814 fprintf(file, "Struct tag\n");
1815 }
1816 if (contexts & CXCompletionContext_ClassTag) {
1817 fprintf(file, "Class name\n");
1818 }
1819 if (contexts & CXCompletionContext_Namespace) {
1820 fprintf(file, "Namespace or namespace alias\n");
1821 }
1822 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1823 fprintf(file, "Nested name specifier\n");
1824 }
1825 if (contexts & CXCompletionContext_ObjCInterface) {
1826 fprintf(file, "Objective-C interface\n");
1827 }
1828 if (contexts & CXCompletionContext_ObjCProtocol) {
1829 fprintf(file, "Objective-C protocol\n");
1830 }
1831 if (contexts & CXCompletionContext_ObjCCategory) {
1832 fprintf(file, "Objective-C category\n");
1833 }
1834 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1835 fprintf(file, "Objective-C instance method\n");
1836 }
1837 if (contexts & CXCompletionContext_ObjCClassMessage) {
1838 fprintf(file, "Objective-C class method\n");
1839 }
1840 if (contexts & CXCompletionContext_ObjCSelectorName) {
1841 fprintf(file, "Objective-C selector name\n");
1842 }
1843 if (contexts & CXCompletionContext_MacroName) {
1844 fprintf(file, "Macro name\n");
1845 }
1846 if (contexts & CXCompletionContext_NaturalLanguage) {
1847 fprintf(file, "Natural language\n");
1848 }
1849}
1850
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001851int my_stricmp(const char *s1, const char *s2) {
1852 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001853 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001854 if (c1 < c2)
1855 return -1;
1856 else if (c1 > c2)
1857 return 1;
1858
1859 ++s1;
1860 ++s2;
1861 }
1862
1863 if (*s1)
1864 return 1;
1865 else if (*s2)
1866 return -1;
1867 return 0;
1868}
1869
Douglas Gregor1982c182010-07-12 18:38:41 +00001870int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001871 const char *input = argv[1];
1872 char *filename = 0;
1873 unsigned line;
1874 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001875 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001876 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001877 struct CXUnsavedFile *unsaved_files = 0;
1878 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001879 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001880 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001881 unsigned I, Repeats = 1;
1882 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1883
1884 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1885 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001886 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1887 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001888
Douglas Gregor1982c182010-07-12 18:38:41 +00001889 if (timing_only)
1890 input += strlen("-code-completion-timing=");
1891 else
1892 input += strlen("-code-completion-at=");
1893
Ted Kremeneke68fff62010-02-17 00:41:32 +00001894 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001895 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001896 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001897
Douglas Gregor735df882009-12-02 09:21:34 +00001898 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1899 return -1;
1900
Douglas Gregor32be4a52010-10-11 21:37:58 +00001901 CIdx = clang_createIndex(0, 0);
1902
1903 if (getenv("CINDEXTEST_EDITING"))
1904 Repeats = 5;
1905
1906 TU = clang_parseTranslationUnit(CIdx, 0,
1907 argv + num_unsaved_files + 2,
1908 argc - num_unsaved_files - 2,
1909 0, 0, getDefaultParsingOptions());
1910 if (!TU) {
1911 fprintf(stderr, "Unable to load translation unit!\n");
1912 return 1;
1913 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001914
1915 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1916 fprintf(stderr, "Unable to reparse translation init!\n");
1917 return 1;
1918 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001919
1920 for (I = 0; I != Repeats; ++I) {
1921 results = clang_codeCompleteAt(TU, filename, line, column,
1922 unsaved_files, num_unsaved_files,
1923 completionOptions);
1924 if (!results) {
1925 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001926 return 1;
1927 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001928 if (I != Repeats-1)
1929 clang_disposeCodeCompleteResults(results);
1930 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001931
Douglas Gregorec6762c2009-12-18 16:20:58 +00001932 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001933 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001934 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001935 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001936 CXString objCSelector;
1937 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001938 if (!timing_only) {
1939 /* Sort the code-completion results based on the typed text. */
1940 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1941
Douglas Gregor1982c182010-07-12 18:38:41 +00001942 for (i = 0; i != n; ++i)
1943 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001944 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001945 n = clang_codeCompleteGetNumDiagnostics(results);
1946 for (i = 0; i != n; ++i) {
1947 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1948 PrintDiagnostic(diag);
1949 clang_disposeDiagnostic(diag);
1950 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001951
1952 contexts = clang_codeCompleteGetContexts(results);
1953 print_completion_contexts(contexts, stdout);
1954
Douglas Gregor0a47d692011-07-26 15:24:30 +00001955 containerKind = clang_codeCompleteGetContainerKind(results,
1956 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001957
1958 if (containerKind != CXCursor_InvalidCode) {
1959 /* We have found a container */
1960 CXString containerUSR, containerKindSpelling;
1961 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1962 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1963 clang_disposeString(containerKindSpelling);
1964
1965 if (containerIsIncomplete) {
1966 printf("Container is incomplete\n");
1967 }
1968 else {
1969 printf("Container is complete\n");
1970 }
1971
1972 containerUSR = clang_codeCompleteGetContainerUSR(results);
1973 printf("Container USR: %s\n", clang_getCString(containerUSR));
1974 clang_disposeString(containerUSR);
1975 }
1976
Douglas Gregor0a47d692011-07-26 15:24:30 +00001977 objCSelector = clang_codeCompleteGetObjCSelector(results);
1978 selectorString = clang_getCString(objCSelector);
1979 if (selectorString && strlen(selectorString) > 0) {
1980 printf("Objective-C selector: %s\n", selectorString);
1981 }
1982 clang_disposeString(objCSelector);
1983
Douglas Gregorec6762c2009-12-18 16:20:58 +00001984 clang_disposeCodeCompleteResults(results);
1985 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001986 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001987 clang_disposeIndex(CIdx);
1988 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001989
Douglas Gregor735df882009-12-02 09:21:34 +00001990 free_remapped_files(unsaved_files, num_unsaved_files);
1991
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001992 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001993}
1994
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001995typedef struct {
1996 char *filename;
1997 unsigned line;
1998 unsigned column;
1999} CursorSourceLocation;
2000
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002001static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002002 CXIndex CIdx;
2003 int errorCode;
2004 struct CXUnsavedFile *unsaved_files = 0;
2005 int num_unsaved_files = 0;
2006 CXTranslationUnit TU;
2007 CXCursor Cursor;
2008 CursorSourceLocation *Locations = 0;
2009 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002010 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00002011 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002012
Ted Kremeneke68fff62010-02-17 00:41:32 +00002013 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002014 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2015 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002016
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002017 /* Parse the locations. */
2018 assert(NumLocations > 0 && "Unable to count locations?");
2019 Locations = (CursorSourceLocation *)malloc(
2020 NumLocations * sizeof(CursorSourceLocation));
2021 for (Loc = 0; Loc < NumLocations; ++Loc) {
2022 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002023 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2024 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002025 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002026 return errorCode;
2027 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002028
2029 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002030 &num_unsaved_files))
2031 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002032
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002033 if (getenv("CINDEXTEST_EDITING"))
2034 Repeats = 5;
2035
2036 /* Parse the translation unit. When we're testing clang_getCursor() after
2037 reparsing, don't remap unsaved files until the second parse. */
2038 CIdx = clang_createIndex(1, 1);
2039 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2040 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002041 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002042 unsaved_files,
2043 Repeats > 1? 0 : num_unsaved_files,
2044 getDefaultParsingOptions());
2045
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002046 if (!TU) {
2047 fprintf(stderr, "unable to parse input\n");
2048 return -1;
2049 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002050
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002051 if (checkForErrors(TU) != 0)
2052 return -1;
2053
Douglas Gregorbdc4b362010-11-30 06:04:54 +00002054 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002055 if (Repeats > 1 &&
2056 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2057 clang_defaultReparseOptions(TU))) {
2058 clang_disposeTranslationUnit(TU);
2059 return 1;
2060 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002061
2062 if (checkForErrors(TU) != 0)
2063 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002064
2065 for (Loc = 0; Loc < NumLocations; ++Loc) {
2066 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2067 if (!file)
2068 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002069
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002070 Cursor = clang_getCursor(TU,
2071 clang_getLocation(TU, file, Locations[Loc].line,
2072 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002073
2074 if (checkForErrors(TU) != 0)
2075 return -1;
2076
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002077 if (I + 1 == Repeats) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002078 CXCompletionString completionString = clang_getCursorCompletionString(
2079 Cursor);
2080 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002081 CXString Spelling;
2082 const char *cspell;
2083 unsigned line, column;
2084 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2085 printf("%d:%d ", line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002086 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002087 PrintCursorExtent(Cursor);
2088 Spelling = clang_getCursorSpelling(Cursor);
2089 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002090 if (cspell && strlen(cspell) != 0) {
2091 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002092 printf(" Spelling=%s (", cspell);
2093 for (pieceIndex = 0; ; ++pieceIndex) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002094 CXSourceRange range =
2095 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002096 if (clang_Range_isNull(range))
2097 break;
2098 PrintRange(range, 0);
2099 }
2100 printf(")");
2101 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002102 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00002103 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2104 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00002105 if (clang_Cursor_isDynamicCall(Cursor))
2106 printf(" Dynamic-call");
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00002107 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002108 CXType T = clang_Cursor_getReceiverType(Cursor);
2109 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00002110 printf(" Receiver-type=%s", clang_getCString(S));
2111 clang_disposeString(S);
2112 }
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00002113
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002114 {
2115 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002116 CXFile astFile;
2117 CXString name, astFilename;
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002118 unsigned i, numHeaders;
2119 if (mod) {
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002120 astFile = clang_Module_getASTFile(mod);
2121 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002122 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002123 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002124 printf(" ModuleName=%s (%s) Headers(%d):",
2125 clang_getCString(name), clang_getCString(astFilename),
2126 numHeaders);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002127 clang_disposeString(name);
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002128 clang_disposeString(astFilename);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002129 for (i = 0; i < numHeaders; ++i) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002130 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
2131 CXString filename = clang_getFileName(file);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002132 printf("\n%s", clang_getCString(filename));
2133 clang_disposeString(filename);
2134 }
2135 }
2136 }
2137
Douglas Gregor8fa0a802011-08-04 20:04:59 +00002138 if (completionString != NULL) {
2139 printf("\nCompletion string: ");
2140 print_completion_string(completionString, stdout);
2141 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002142 printf("\n");
2143 free(Locations[Loc].filename);
2144 }
2145 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002146 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002147
Douglas Gregora88084b2010-02-18 18:08:43 +00002148 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002149 clang_disposeTranslationUnit(TU);
2150 clang_disposeIndex(CIdx);
2151 free(Locations);
2152 free_remapped_files(unsaved_files, num_unsaved_files);
2153 return 0;
2154}
2155
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002156static enum CXVisitorResult findFileRefsVisit(void *context,
2157 CXCursor cursor, CXSourceRange range) {
2158 if (clang_Range_isNull(range))
2159 return CXVisit_Continue;
2160
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002161 PrintCursor(cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002162 PrintRange(range, "");
2163 printf("\n");
2164 return CXVisit_Continue;
2165}
2166
2167static int find_file_refs_at(int argc, const char **argv) {
2168 CXIndex CIdx;
2169 int errorCode;
2170 struct CXUnsavedFile *unsaved_files = 0;
2171 int num_unsaved_files = 0;
2172 CXTranslationUnit TU;
2173 CXCursor Cursor;
2174 CursorSourceLocation *Locations = 0;
2175 unsigned NumLocations = 0, Loc;
2176 unsigned Repeats = 1;
2177 unsigned I;
2178
2179 /* Count the number of locations. */
2180 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2181 ++NumLocations;
2182
2183 /* Parse the locations. */
2184 assert(NumLocations > 0 && "Unable to count locations?");
2185 Locations = (CursorSourceLocation *)malloc(
2186 NumLocations * sizeof(CursorSourceLocation));
2187 for (Loc = 0; Loc < NumLocations; ++Loc) {
2188 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2189 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2190 &Locations[Loc].line,
2191 &Locations[Loc].column, 0, 0)))
2192 return errorCode;
2193 }
2194
2195 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2196 &num_unsaved_files))
2197 return -1;
2198
2199 if (getenv("CINDEXTEST_EDITING"))
2200 Repeats = 5;
2201
2202 /* Parse the translation unit. When we're testing clang_getCursor() after
2203 reparsing, don't remap unsaved files until the second parse. */
2204 CIdx = clang_createIndex(1, 1);
2205 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2206 argv + num_unsaved_files + 1 + NumLocations,
2207 argc - num_unsaved_files - 2 - NumLocations,
2208 unsaved_files,
2209 Repeats > 1? 0 : num_unsaved_files,
2210 getDefaultParsingOptions());
2211
2212 if (!TU) {
2213 fprintf(stderr, "unable to parse input\n");
2214 return -1;
2215 }
2216
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002217 if (checkForErrors(TU) != 0)
2218 return -1;
2219
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002220 for (I = 0; I != Repeats; ++I) {
2221 if (Repeats > 1 &&
2222 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2223 clang_defaultReparseOptions(TU))) {
2224 clang_disposeTranslationUnit(TU);
2225 return 1;
2226 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002227
2228 if (checkForErrors(TU) != 0)
2229 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002230
2231 for (Loc = 0; Loc < NumLocations; ++Loc) {
2232 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2233 if (!file)
2234 continue;
2235
2236 Cursor = clang_getCursor(TU,
2237 clang_getLocation(TU, file, Locations[Loc].line,
2238 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002239
2240 if (checkForErrors(TU) != 0)
2241 return -1;
2242
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002243 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002244 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002245 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002246 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002247 clang_findReferencesInFile(Cursor, file, visitor);
2248 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002249
2250 if (checkForErrors(TU) != 0)
2251 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002252 }
2253 }
2254 }
2255
2256 PrintDiagnostics(TU);
2257 clang_disposeTranslationUnit(TU);
2258 clang_disposeIndex(CIdx);
2259 free(Locations);
2260 free_remapped_files(unsaved_files, num_unsaved_files);
2261 return 0;
2262}
2263
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00002264static enum CXVisitorResult findFileIncludesVisit(void *context,
2265 CXCursor cursor, CXSourceRange range) {
2266 PrintCursor(cursor, NULL);
2267 PrintRange(range, "");
2268 printf("\n");
2269 return CXVisit_Continue;
2270}
2271
2272static int find_file_includes_in(int argc, const char **argv) {
2273 CXIndex CIdx;
2274 struct CXUnsavedFile *unsaved_files = 0;
2275 int num_unsaved_files = 0;
2276 CXTranslationUnit TU;
2277 const char **Filenames = 0;
2278 unsigned NumFilenames = 0;
2279 unsigned Repeats = 1;
2280 unsigned I, FI;
2281
2282 /* Count the number of locations. */
2283 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2284 ++NumFilenames;
2285
2286 /* Parse the locations. */
2287 assert(NumFilenames > 0 && "Unable to count filenames?");
2288 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2289 for (I = 0; I < NumFilenames; ++I) {
2290 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2291 /* Copy the file name. */
2292 Filenames[I] = input;
2293 }
2294
2295 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2296 &num_unsaved_files))
2297 return -1;
2298
2299 if (getenv("CINDEXTEST_EDITING"))
2300 Repeats = 2;
2301
2302 /* Parse the translation unit. When we're testing clang_getCursor() after
2303 reparsing, don't remap unsaved files until the second parse. */
2304 CIdx = clang_createIndex(1, 1);
2305 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2306 argv + num_unsaved_files + 1 + NumFilenames,
2307 argc - num_unsaved_files - 2 - NumFilenames,
2308 unsaved_files,
2309 Repeats > 1? 0 : num_unsaved_files,
2310 getDefaultParsingOptions());
2311
2312 if (!TU) {
2313 fprintf(stderr, "unable to parse input\n");
2314 return -1;
2315 }
2316
2317 if (checkForErrors(TU) != 0)
2318 return -1;
2319
2320 for (I = 0; I != Repeats; ++I) {
2321 if (Repeats > 1 &&
2322 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2323 clang_defaultReparseOptions(TU))) {
2324 clang_disposeTranslationUnit(TU);
2325 return 1;
2326 }
2327
2328 if (checkForErrors(TU) != 0)
2329 return -1;
2330
2331 for (FI = 0; FI < NumFilenames; ++FI) {
2332 CXFile file = clang_getFile(TU, Filenames[FI]);
2333 if (!file)
2334 continue;
2335
2336 if (checkForErrors(TU) != 0)
2337 return -1;
2338
2339 if (I + 1 == Repeats) {
2340 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2341 clang_findIncludesInFile(TU, file, visitor);
2342
2343 if (checkForErrors(TU) != 0)
2344 return -1;
2345 }
2346 }
2347 }
2348
2349 PrintDiagnostics(TU);
2350 clang_disposeTranslationUnit(TU);
2351 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis5256c1f2013-03-11 16:03:17 +00002352 free((void *)Filenames);
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00002353 free_remapped_files(unsaved_files, num_unsaved_files);
2354 return 0;
2355}
2356
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002357#define MAX_IMPORTED_ASTFILES 200
2358
2359typedef struct {
2360 char **filenames;
2361 unsigned num_files;
2362} ImportedASTFilesData;
2363
2364static ImportedASTFilesData *importedASTs_create() {
2365 ImportedASTFilesData *p;
2366 p = malloc(sizeof(ImportedASTFilesData));
2367 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2368 p->num_files = 0;
2369 return p;
2370}
2371
2372static void importedASTs_dispose(ImportedASTFilesData *p) {
2373 unsigned i;
2374 if (!p)
2375 return;
2376
2377 for (i = 0; i < p->num_files; ++i)
2378 free(p->filenames[i]);
2379 free(p->filenames);
2380 free(p);
2381}
2382
2383static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2384 unsigned i;
2385 assert(p && file);
2386 for (i = 0; i < p->num_files; ++i)
2387 if (strcmp(file, p->filenames[i]) == 0)
2388 return;
2389 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2390 p->filenames[p->num_files++] = strdup(file);
2391}
2392
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002393typedef struct {
2394 const char *check_prefix;
2395 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002396 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002397 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002398 const char *main_filename;
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002399 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002400} IndexData;
2401
2402static void printCheck(IndexData *data) {
2403 if (data->check_prefix) {
2404 if (data->first_check_printed) {
2405 printf("// %s-NEXT: ", data->check_prefix);
2406 } else {
2407 printf("// %s : ", data->check_prefix);
2408 data->first_check_printed = 1;
2409 }
2410 }
2411}
2412
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002413static void printCXIndexFile(CXIdxClientFile file) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002414 CXString filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002415 printf("%s", clang_getCString(filename));
2416 clang_disposeString(filename);
2417}
2418
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002419static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2420 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002421 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002422 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002423 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002424 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002425 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002426
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002427 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002428 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2429 if (line == 0) {
Argyrios Kyrtzidis8003fd62012-10-11 19:00:44 +00002430 printf("<invalid>");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002431 return;
2432 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002433 if (!file) {
2434 printf("<no idxfile>");
2435 return;
2436 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002437 filename = clang_getFileName((CXFile)file);
2438 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002439 if (strcmp(cname, index_data->main_filename) == 0)
2440 isMainFile = 1;
2441 else
2442 isMainFile = 0;
2443 clang_disposeString(filename);
2444
2445 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002446 printCXIndexFile(file);
2447 printf(":");
2448 }
2449 printf("%d:%d", line, column);
2450}
2451
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002452static unsigned digitCount(unsigned val) {
2453 unsigned c = 1;
2454 while (1) {
2455 if (val < 10)
2456 return c;
2457 ++c;
2458 val /= 10;
2459 }
2460}
2461
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002462static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2463 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002464 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002465 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002466 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002467 unsigned line, column;
2468
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002469 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002470 if (!name)
2471 name = "<anon-tag>";
2472
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002473 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002474 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002475 newStr = (char *)malloc(strlen(name) +
2476 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002477 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002478 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002479}
2480
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002481static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2482 CXIdxClientContainer container;
2483 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002484 if (!container)
2485 printf("[<<NULL>>]");
2486 else
2487 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002488}
2489
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002490static const char *getEntityKindString(CXIdxEntityKind kind) {
2491 switch (kind) {
2492 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2493 case CXIdxEntity_Typedef: return "typedef";
2494 case CXIdxEntity_Function: return "function";
2495 case CXIdxEntity_Variable: return "variable";
2496 case CXIdxEntity_Field: return "field";
2497 case CXIdxEntity_EnumConstant: return "enumerator";
2498 case CXIdxEntity_ObjCClass: return "objc-class";
2499 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2500 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002501 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2502 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002503 case CXIdxEntity_ObjCProperty: return "objc-property";
2504 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2505 case CXIdxEntity_Enum: return "enum";
2506 case CXIdxEntity_Struct: return "struct";
2507 case CXIdxEntity_Union: return "union";
2508 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002509 case CXIdxEntity_CXXNamespace: return "namespace";
2510 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2511 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2512 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2513 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2514 case CXIdxEntity_CXXConstructor: return "constructor";
2515 case CXIdxEntity_CXXDestructor: return "destructor";
2516 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2517 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikie35adca02012-08-31 21:55:26 +00002518 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002519 }
2520 assert(0 && "Garbage entity kind");
2521 return 0;
2522}
2523
2524static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2525 switch (kind) {
2526 case CXIdxEntity_NonTemplate: return "";
2527 case CXIdxEntity_Template: return "-template";
2528 case CXIdxEntity_TemplatePartialSpecialization:
2529 return "-template-partial-spec";
2530 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002531 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002532 assert(0 && "Garbage entity kind");
2533 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002534}
2535
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002536static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2537 switch (kind) {
2538 case CXIdxEntityLang_None: return "<none>";
2539 case CXIdxEntityLang_C: return "C";
2540 case CXIdxEntityLang_ObjC: return "ObjC";
2541 case CXIdxEntityLang_CXX: return "C++";
2542 }
2543 assert(0 && "Garbage language kind");
2544 return 0;
2545}
2546
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002547static void printEntityInfo(const char *cb,
2548 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002549 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002550 const char *name;
2551 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002552 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002553 index_data = (IndexData *)client_data;
2554 printCheck(index_data);
2555
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002556 if (!info) {
2557 printf("%s: <<NULL>>", cb);
2558 return;
2559 }
2560
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002561 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002562 if (!name)
2563 name = "<anon-tag>";
2564
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002565 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2566 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002567 printf(" | name: %s", name);
2568 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002569 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002570
2571 for (i = 0; i != info->numAttributes; ++i) {
2572 const CXIdxAttrInfo *Attr = info->attributes[i];
2573 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002574 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002575 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002576}
2577
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002578static void printBaseClassInfo(CXClientData client_data,
2579 const CXIdxBaseClassInfo *info) {
2580 printEntityInfo(" <base>", client_data, info->base);
2581 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002582 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002583 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002584 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002585}
2586
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002587static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2588 CXClientData client_data) {
2589 unsigned i;
2590 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2591 printEntityInfo(" <protocol>", client_data,
2592 ProtoInfo->protocols[i]->protocol);
2593 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002594 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002595 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002596 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002597 printf("\n");
2598 }
2599}
2600
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002601static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002602 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002603 CXString str;
2604 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002605 unsigned numDiags, i;
2606 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002607 IndexData *index_data;
2608 index_data = (IndexData *)client_data;
2609 printCheck(index_data);
2610
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002611 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2612 for (i = 0; i != numDiags; ++i) {
2613 diag = clang_getDiagnosticInSet(diagSet, i);
2614 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2615 cstr = clang_getCString(str);
2616 printf("[diagnostic]: %s\n", cstr);
2617 clang_disposeString(str);
2618
2619 if (getenv("CINDEXTEST_FAILONERROR") &&
2620 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2621 index_data->fail_for_error = 1;
2622 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002623 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002624}
2625
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002626static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2627 CXFile file, void *reserved) {
2628 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002629 CXString filename;
2630
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002631 index_data = (IndexData *)client_data;
2632 printCheck(index_data);
2633
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002634 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002635 index_data->main_filename = clang_getCString(filename);
2636 clang_disposeString(filename);
2637
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002638 printf("[enteredMainFile]: ");
2639 printCXIndexFile((CXIdxClientFile)file);
2640 printf("\n");
2641
2642 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002643}
2644
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002645static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002646 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002647 IndexData *index_data;
2648 index_data = (IndexData *)client_data;
2649 printCheck(index_data);
2650
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002651 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002652 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002653 printf(" | name: \"%s\"", info->filename);
2654 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002655 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis8d7a24e2012-10-18 00:17:05 +00002656 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2657 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002658
2659 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002660}
2661
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002662static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2663 const CXIdxImportedASTFileInfo *info) {
2664 IndexData *index_data;
2665 index_data = (IndexData *)client_data;
2666 printCheck(index_data);
2667
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002668 if (index_data->importedASTs) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002669 CXString filename = clang_getFileName(info->file);
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002670 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2671 clang_disposeString(filename);
2672 }
2673
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002674 printf("[importedASTFile]: ");
2675 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002676 if (info->module) {
Enea Zaffanella10f90042013-07-22 20:58:30 +00002677 CXString name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002678 printf(" | loc: ");
2679 printCXIndexLoc(info->loc, client_data);
2680 printf(" | name: \"%s\"", clang_getCString(name));
2681 printf(" | isImplicit: %d\n", info->isImplicit);
2682 clang_disposeString(name);
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002683 } else {
NAKAMURA Takumi3c5527e2012-10-12 14:25:52 +00002684 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002685 printf("\n");
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002686 }
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002687
2688 return (CXIdxClientFile)info->file;
2689}
2690
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002691static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002692 void *reserved) {
2693 IndexData *index_data;
2694 index_data = (IndexData *)client_data;
2695 printCheck(index_data);
2696
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002697 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002698 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002699}
2700
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002701static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002702 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002703 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002704 const CXIdxObjCCategoryDeclInfo *CatInfo;
2705 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002706 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002707 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002708 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002709 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002710 index_data = (IndexData *)client_data;
2711
2712 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2713 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002714 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002715 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002716 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002717 printf(" | semantic-container: ");
2718 printCXIndexContainer(info->semanticContainer);
2719 printf(" | lexical-container: ");
2720 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002721 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002722 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis838eb7e2012-12-06 19:41:16 +00002723 if (info->flags & CXIdxDeclFlag_Skipped) {
2724 assert(!info->isContainer);
2725 printf(" | isContainer: skipped");
2726 } else {
2727 printf(" | isContainer: %d", info->isContainer);
2728 }
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002729 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002730
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002731 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002732 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002733 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002734 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002735 printf("\n");
2736 }
2737
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002738 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2739 const char *kindName = 0;
2740 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2741 switch (K) {
2742 case CXIdxObjCContainer_ForwardRef:
2743 kindName = "forward-ref"; break;
2744 case CXIdxObjCContainer_Interface:
2745 kindName = "interface"; break;
2746 case CXIdxObjCContainer_Implementation:
2747 kindName = "implementation"; break;
2748 }
2749 printCheck(index_data);
2750 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2751 }
2752
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002753 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002754 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2755 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002756 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002757 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002758 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002759 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002760 printf("\n");
2761 }
2762
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002763 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2764 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002765 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002766 printf("\n");
2767 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002768 }
2769
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002770 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2771 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002772 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002773
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002774 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2775 if (PropInfo->getter) {
2776 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2777 printf("\n");
2778 }
2779 if (PropInfo->setter) {
2780 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2781 printf("\n");
2782 }
2783 }
2784
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002785 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2786 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2787 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2788 printf("\n");
2789 }
2790 }
2791
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002792 if (info->declAsContainer)
2793 clang_index_setClientContainer(info->declAsContainer,
2794 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002795}
2796
2797static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002798 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002799 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002800 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002801 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002802 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002803 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002804 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002805 printf(" | container: ");
2806 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002807 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002808 switch (info->kind) {
2809 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002810 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002811 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002812 printf("\n");
2813}
2814
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002815static int index_abortQuery(CXClientData client_data, void *reserved) {
2816 IndexData *index_data;
2817 index_data = (IndexData *)client_data;
2818 return index_data->abort;
2819}
2820
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002821static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002822 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002823 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002824 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002825 index_ppIncludedFile,
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002826 index_importedASTFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002827 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002828 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002829 index_indexEntityReference
2830};
2831
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002832static unsigned getIndexOptions(void) {
2833 unsigned index_opts;
2834 index_opts = 0;
2835 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2836 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2837 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2838 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis838eb7e2012-12-06 19:41:16 +00002839 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
2840 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002841
2842 return index_opts;
2843}
2844
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002845static int index_compile_args(int num_args, const char **args,
2846 CXIndexAction idxAction,
2847 ImportedASTFilesData *importedASTs,
2848 const char *check_prefix) {
2849 IndexData index_data;
2850 unsigned index_opts;
2851 int result;
2852
2853 if (num_args == 0) {
2854 fprintf(stderr, "no compiler arguments\n");
2855 return -1;
2856 }
2857
2858 index_data.check_prefix = check_prefix;
2859 index_data.first_check_printed = 0;
2860 index_data.fail_for_error = 0;
2861 index_data.abort = 0;
2862 index_data.main_filename = "";
2863 index_data.importedASTs = importedASTs;
2864
2865 index_opts = getIndexOptions();
2866 result = clang_indexSourceFile(idxAction, &index_data,
2867 &IndexCB,sizeof(IndexCB), index_opts,
2868 0, args, num_args, 0, 0, 0,
2869 getDefaultParsingOptions());
2870 if (index_data.fail_for_error)
2871 result = -1;
2872
2873 return result;
2874}
2875
2876static int index_ast_file(const char *ast_file,
2877 CXIndex Idx,
2878 CXIndexAction idxAction,
2879 ImportedASTFilesData *importedASTs,
2880 const char *check_prefix) {
2881 CXTranslationUnit TU;
2882 IndexData index_data;
2883 unsigned index_opts;
2884 int result;
2885
2886 if (!CreateTranslationUnit(Idx, ast_file, &TU))
2887 return -1;
2888
2889 index_data.check_prefix = check_prefix;
2890 index_data.first_check_printed = 0;
2891 index_data.fail_for_error = 0;
2892 index_data.abort = 0;
2893 index_data.main_filename = "";
2894 index_data.importedASTs = importedASTs;
2895
2896 index_opts = getIndexOptions();
2897 result = clang_indexTranslationUnit(idxAction, &index_data,
2898 &IndexCB,sizeof(IndexCB),
2899 index_opts, TU);
2900 if (index_data.fail_for_error)
2901 result = -1;
2902
2903 clang_disposeTranslationUnit(TU);
2904 return result;
2905}
2906
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002907static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002908 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002909 CXIndex Idx;
2910 CXIndexAction idxAction;
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002911 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002912 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002913
2914 check_prefix = 0;
2915 if (argc > 0) {
2916 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2917 check_prefix = argv[0] + strlen("-check-prefix=");
2918 ++argv;
2919 --argc;
2920 }
2921 }
2922
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002923 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002924 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002925 fprintf(stderr, "Could not create Index\n");
2926 return 1;
2927 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002928 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002929 importedASTs = 0;
2930 if (full)
2931 importedASTs = importedASTs_create();
2932
2933 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
2934 if (result != 0)
2935 goto finished;
2936
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002937 if (full) {
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002938 unsigned i;
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002939 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
2940 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
2941 importedASTs, check_prefix);
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002942 }
2943 }
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002944
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002945finished:
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002946 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002947 clang_IndexAction_dispose(idxAction);
2948 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002949 return result;
2950}
2951
2952static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002953 const char *check_prefix;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002954 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002955 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002956 int result;
2957
2958 check_prefix = 0;
2959 if (argc > 0) {
2960 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2961 check_prefix = argv[0] + strlen("-check-prefix=");
2962 ++argv;
2963 --argc;
2964 }
2965 }
2966
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002967 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002968 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002969 fprintf(stderr, "Could not create Index\n");
2970 return 1;
2971 }
2972 idxAction = clang_IndexAction_create(Idx);
2973
2974 result = index_ast_file(argv[0], Idx, idxAction,
2975 /*importedASTs=*/0, check_prefix);
2976
2977 clang_IndexAction_dispose(idxAction);
2978 clang_disposeIndex(Idx);
2979 return result;
2980}
2981
2982static int index_compile_db(int argc, const char **argv) {
2983 const char *check_prefix;
2984 CXIndex Idx;
2985 CXIndexAction idxAction;
2986 int errorCode = 0;
2987
2988 check_prefix = 0;
2989 if (argc > 0) {
2990 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2991 check_prefix = argv[0] + strlen("-check-prefix=");
2992 ++argv;
2993 --argc;
2994 }
2995 }
2996
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002997 if (argc == 0) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002998 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002999 return -1;
3000 }
3001
3002 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00003003 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003004 fprintf(stderr, "Could not create Index\n");
3005 return 1;
3006 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003007 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003008
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003009 {
3010 const char *database = argv[0];
3011 CXCompilationDatabase db = 0;
3012 CXCompileCommands CCmds = 0;
3013 CXCompileCommand CCmd;
3014 CXCompilationDatabase_Error ec;
3015 CXString wd;
3016#define MAX_COMPILE_ARGS 512
3017 CXString cxargs[MAX_COMPILE_ARGS];
3018 const char *args[MAX_COMPILE_ARGS];
3019 char *tmp;
3020 unsigned len;
3021 char *buildDir;
3022 int i, a, numCmds, numArgs;
3023
3024 len = strlen(database);
3025 tmp = (char *) malloc(len+1);
3026 memcpy(tmp, database, len+1);
3027 buildDir = dirname(tmp);
3028
3029 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3030
3031 if (db) {
3032
3033 if (ec!=CXCompilationDatabase_NoError) {
3034 printf("unexpected error %d code while loading compilation database\n", ec);
3035 errorCode = -1;
3036 goto cdb_end;
3037 }
3038
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003039 if (chdir(buildDir) != 0) {
3040 printf("Could not chdir to %s\n", buildDir);
3041 errorCode = -1;
3042 goto cdb_end;
3043 }
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003044
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003045 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003046 if (!CCmds) {
3047 printf("compilation db is empty\n");
3048 errorCode = -1;
3049 goto cdb_end;
3050 }
3051
3052 numCmds = clang_CompileCommands_getSize(CCmds);
3053
3054 if (numCmds==0) {
3055 fprintf(stderr, "should not get an empty compileCommand set\n");
3056 errorCode = -1;
3057 goto cdb_end;
3058 }
3059
3060 for (i=0; i<numCmds && errorCode == 0; ++i) {
3061 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3062
3063 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003064 if (chdir(clang_getCString(wd)) != 0) {
3065 printf("Could not chdir to %s\n", clang_getCString(wd));
3066 errorCode = -1;
3067 goto cdb_end;
3068 }
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003069 clang_disposeString(wd);
3070
3071 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3072 if (numArgs > MAX_COMPILE_ARGS){
3073 fprintf(stderr, "got more compile arguments than maximum\n");
3074 errorCode = -1;
3075 goto cdb_end;
3076 }
3077 for (a=0; a<numArgs; ++a) {
3078 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3079 args[a] = clang_getCString(cxargs[a]);
3080 }
3081
3082 errorCode = index_compile_args(numArgs, args, idxAction,
3083 /*importedASTs=*/0, check_prefix);
3084
3085 for (a=0; a<numArgs; ++a)
3086 clang_disposeString(cxargs[a]);
3087 }
3088 } else {
3089 printf("database loading failed with error code %d.\n", ec);
3090 errorCode = -1;
3091 }
3092
3093 cdb_end:
3094 clang_CompileCommands_dispose(CCmds);
3095 clang_CompilationDatabase_dispose(db);
3096 free(tmp);
3097
3098 }
3099
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003100 clang_IndexAction_dispose(idxAction);
3101 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003102 return errorCode;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003103}
3104
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003105int perform_token_annotation(int argc, const char **argv) {
3106 const char *input = argv[1];
3107 char *filename = 0;
3108 unsigned line, second_line;
3109 unsigned column, second_column;
3110 CXIndex CIdx;
3111 CXTranslationUnit TU = 0;
3112 int errorCode;
3113 struct CXUnsavedFile *unsaved_files = 0;
3114 int num_unsaved_files = 0;
3115 CXToken *tokens;
3116 unsigned num_tokens;
3117 CXSourceRange range;
3118 CXSourceLocation startLoc, endLoc;
3119 CXFile file = 0;
3120 CXCursor *cursors = 0;
3121 unsigned i;
3122
3123 input += strlen("-test-annotate-tokens=");
3124 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3125 &second_line, &second_column)))
3126 return errorCode;
3127
Richard Smithe07c5f82012-07-05 08:20:49 +00003128 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3129 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003130 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00003131 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003132
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003133 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00003134 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
3135 argv + num_unsaved_files + 2,
3136 argc - num_unsaved_files - 3,
3137 unsaved_files,
3138 num_unsaved_files,
3139 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003140 if (!TU) {
3141 fprintf(stderr, "unable to parse input\n");
3142 clang_disposeIndex(CIdx);
3143 free(filename);
3144 free_remapped_files(unsaved_files, num_unsaved_files);
3145 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003146 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003147 errorCode = 0;
3148
Richard Smithe07c5f82012-07-05 08:20:49 +00003149 if (checkForErrors(TU) != 0) {
3150 errorCode = -1;
3151 goto teardown;
3152 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003153
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00003154 if (getenv("CINDEXTEST_EDITING")) {
3155 for (i = 0; i < 5; ++i) {
3156 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3157 clang_defaultReparseOptions(TU))) {
3158 fprintf(stderr, "Unable to reparse translation unit!\n");
3159 errorCode = -1;
3160 goto teardown;
3161 }
3162 }
3163 }
3164
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003165 if (checkForErrors(TU) != 0) {
3166 errorCode = -1;
3167 goto teardown;
3168 }
3169
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003170 file = clang_getFile(TU, filename);
3171 if (!file) {
3172 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3173 errorCode = -1;
3174 goto teardown;
3175 }
3176
3177 startLoc = clang_getLocation(TU, file, line, column);
3178 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003179 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003180 column);
3181 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003182 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003183 }
3184
3185 endLoc = clang_getLocation(TU, file, second_line, second_column);
3186 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003187 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003188 second_line, second_column);
3189 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003190 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003191 }
3192
3193 range = clang_getRange(startLoc, endLoc);
3194 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003195
3196 if (checkForErrors(TU) != 0) {
3197 errorCode = -1;
3198 goto teardown;
3199 }
3200
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003201 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3202 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003203
3204 if (checkForErrors(TU) != 0) {
3205 errorCode = -1;
3206 goto teardown;
3207 }
3208
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003209 for (i = 0; i != num_tokens; ++i) {
3210 const char *kind = "<unknown>";
Enea Zaffanella10f90042013-07-22 20:58:30 +00003211 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3212 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003213 unsigned start_line, start_column, end_line, end_column;
3214
3215 switch (clang_getTokenKind(tokens[i])) {
3216 case CXToken_Punctuation: kind = "Punctuation"; break;
3217 case CXToken_Keyword: kind = "Keyword"; break;
3218 case CXToken_Identifier: kind = "Identifier"; break;
3219 case CXToken_Literal: kind = "Literal"; break;
3220 case CXToken_Comment: kind = "Comment"; break;
3221 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00003222 clang_getSpellingLocation(clang_getRangeStart(extent),
3223 0, &start_line, &start_column, 0);
3224 clang_getSpellingLocation(clang_getRangeEnd(extent),
3225 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00003226 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00003227 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00003228 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003229 if (!clang_isInvalid(cursors[i].kind)) {
3230 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003231 PrintCursor(cursors[i], NULL);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003232 }
3233 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003234 }
3235 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00003236 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003237
3238 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00003239 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003240 clang_disposeTranslationUnit(TU);
3241 clang_disposeIndex(CIdx);
3242 free(filename);
3243 free_remapped_files(unsaved_files, num_unsaved_files);
3244 return errorCode;
3245}
3246
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003247static int
3248perform_test_compilation_db(const char *database, int argc, const char **argv) {
3249 CXCompilationDatabase db;
3250 CXCompileCommands CCmds;
3251 CXCompileCommand CCmd;
3252 CXCompilationDatabase_Error ec;
3253 CXString wd;
3254 CXString arg;
3255 int errorCode = 0;
3256 char *tmp;
3257 unsigned len;
3258 char *buildDir;
3259 int i, j, a, numCmds, numArgs;
3260
3261 len = strlen(database);
3262 tmp = (char *) malloc(len+1);
3263 memcpy(tmp, database, len+1);
3264 buildDir = dirname(tmp);
3265
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003266 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003267
3268 if (db) {
3269
3270 if (ec!=CXCompilationDatabase_NoError) {
3271 printf("unexpected error %d code while loading compilation database\n", ec);
3272 errorCode = -1;
3273 goto cdb_end;
3274 }
3275
3276 for (i=0; i<argc && errorCode==0; ) {
3277 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003278 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003279
3280 if (!CCmds) {
3281 printf("file %s not found in compilation db\n", argv[i+1]);
3282 errorCode = -1;
3283 break;
3284 }
3285
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003286 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003287
3288 if (numCmds==0) {
3289 fprintf(stderr, "should not get an empty compileCommand set for file"
3290 " '%s'\n", argv[i+1]);
3291 errorCode = -1;
3292 break;
3293 }
3294
3295 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003296 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003297
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003298 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003299 printf("workdir:'%s'", clang_getCString(wd));
3300 clang_disposeString(wd);
3301
3302 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003303 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003304 for (a=0; a<numArgs; ++a) {
3305 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003306 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003307 printf("%s", clang_getCString(arg));
3308 clang_disposeString(arg);
3309 }
3310 printf("'\n");
3311 }
3312
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003313 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003314
3315 i += 2;
3316 }
3317 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003318 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003319 } else {
3320 printf("database loading failed with error code %d.\n", ec);
3321 errorCode = -1;
3322 }
3323
3324cdb_end:
3325 free(tmp);
3326
3327 return errorCode;
3328}
3329
Ted Kremenek0d435192009-11-17 18:13:31 +00003330/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003331/* USR printing. */
3332/******************************************************************************/
3333
3334static int insufficient_usr(const char *kind, const char *usage) {
3335 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3336 return 1;
3337}
3338
3339static unsigned isUSR(const char *s) {
3340 return s[0] == 'c' && s[1] == ':';
3341}
3342
3343static int not_usr(const char *s, const char *arg) {
3344 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3345 return 1;
3346}
3347
3348static void print_usr(CXString usr) {
3349 const char *s = clang_getCString(usr);
3350 printf("%s\n", s);
3351 clang_disposeString(usr);
3352}
3353
3354static void display_usrs() {
3355 fprintf(stderr, "-print-usrs options:\n"
3356 " ObjCCategory <class name> <category name>\n"
3357 " ObjCClass <class name>\n"
3358 " ObjCIvar <ivar name> <class USR>\n"
3359 " ObjCMethod <selector> [0=class method|1=instance method] "
3360 "<class USR>\n"
3361 " ObjCProperty <property name> <class USR>\n"
3362 " ObjCProtocol <protocol name>\n");
3363}
3364
3365int print_usrs(const char **I, const char **E) {
3366 while (I != E) {
3367 const char *kind = *I;
3368 unsigned len = strlen(kind);
3369 switch (len) {
3370 case 8:
3371 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3372 if (I + 2 >= E)
3373 return insufficient_usr(kind, "<ivar name> <class USR>");
3374 if (!isUSR(I[2]))
3375 return not_usr("<class USR>", I[2]);
3376 else {
3377 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003378 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003379 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003380 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3381 }
3382
3383 I += 3;
3384 continue;
3385 }
3386 break;
3387 case 9:
3388 if (memcmp(kind, "ObjCClass", 9) == 0) {
3389 if (I + 1 >= E)
3390 return insufficient_usr(kind, "<class name>");
3391 print_usr(clang_constructUSR_ObjCClass(I[1]));
3392 I += 2;
3393 continue;
3394 }
3395 break;
3396 case 10:
3397 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3398 if (I + 3 >= E)
3399 return insufficient_usr(kind, "<method selector> "
3400 "[0=class method|1=instance method] <class USR>");
3401 if (!isUSR(I[3]))
3402 return not_usr("<class USR>", I[3]);
3403 else {
3404 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003405 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00003406 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003407 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3408 }
3409 I += 4;
3410 continue;
3411 }
3412 break;
3413 case 12:
3414 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3415 if (I + 2 >= E)
3416 return insufficient_usr(kind, "<class name> <category name>");
3417 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3418 I += 3;
3419 continue;
3420 }
3421 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3422 if (I + 1 >= E)
3423 return insufficient_usr(kind, "<protocol name>");
3424 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3425 I += 2;
3426 continue;
3427 }
3428 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3429 if (I + 2 >= E)
3430 return insufficient_usr(kind, "<property name> <class USR>");
3431 if (!isUSR(I[2]))
3432 return not_usr("<class USR>", I[2]);
3433 else {
3434 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003435 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003436 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003437 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3438 }
3439 I += 3;
3440 continue;
3441 }
3442 break;
3443 default:
3444 break;
3445 }
3446 break;
3447 }
3448
3449 if (I != E) {
3450 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3451 display_usrs();
3452 return 1;
3453 }
3454 return 0;
3455}
3456
3457int print_usrs_file(const char *file_name) {
3458 char line[2048];
3459 const char *args[128];
3460 unsigned numChars = 0;
3461
3462 FILE *fp = fopen(file_name, "r");
3463 if (!fp) {
3464 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3465 return 1;
3466 }
3467
3468 /* This code is not really all that safe, but it works fine for testing. */
3469 while (!feof(fp)) {
3470 char c = fgetc(fp);
3471 if (c == '\n') {
3472 unsigned i = 0;
3473 const char *s = 0;
3474
3475 if (numChars == 0)
3476 continue;
3477
3478 line[numChars] = '\0';
3479 numChars = 0;
3480
3481 if (line[0] == '/' && line[1] == '/')
3482 continue;
3483
3484 s = strtok(line, " ");
3485 while (s) {
3486 args[i] = s;
3487 ++i;
3488 s = strtok(0, " ");
3489 }
3490 if (print_usrs(&args[0], &args[i]))
3491 return 1;
3492 }
3493 else
3494 line[numChars++] = c;
3495 }
3496
3497 fclose(fp);
3498 return 0;
3499}
3500
3501/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00003502/* Command line processing. */
3503/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003504int write_pch_file(const char *filename, int argc, const char *argv[]) {
3505 CXIndex Idx;
3506 CXTranslationUnit TU;
3507 struct CXUnsavedFile *unsaved_files = 0;
3508 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00003509 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003510
Stefanus Du Toitfc093362013-03-01 21:41:22 +00003511 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003512
3513 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3514 clang_disposeIndex(Idx);
3515 return -1;
3516 }
3517
3518 TU = clang_parseTranslationUnit(Idx, 0,
3519 argv + num_unsaved_files,
3520 argc - num_unsaved_files,
3521 unsaved_files,
3522 num_unsaved_files,
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003523 CXTranslationUnit_Incomplete |
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00003524 CXTranslationUnit_DetailedPreprocessingRecord|
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003525 CXTranslationUnit_ForSerialization);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003526 if (!TU) {
3527 fprintf(stderr, "Unable to load translation unit!\n");
3528 free_remapped_files(unsaved_files, num_unsaved_files);
3529 clang_disposeIndex(Idx);
3530 return 1;
3531 }
3532
Douglas Gregor39c411f2011-07-06 16:43:36 +00003533 switch (clang_saveTranslationUnit(TU, filename,
3534 clang_defaultSaveOptions(TU))) {
3535 case CXSaveError_None:
3536 break;
3537
3538 case CXSaveError_TranslationErrors:
3539 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3540 filename);
3541 result = 2;
3542 break;
3543
3544 case CXSaveError_InvalidTU:
3545 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3546 filename);
3547 result = 3;
3548 break;
3549
3550 case CXSaveError_Unknown:
3551 default:
3552 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3553 result = 1;
3554 break;
3555 }
3556
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003557 clang_disposeTranslationUnit(TU);
3558 free_remapped_files(unsaved_files, num_unsaved_files);
3559 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00003560 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003561}
3562
3563/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00003564/* Serialized diagnostics. */
3565/******************************************************************************/
3566
3567static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3568 switch (error) {
3569 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3570 case CXLoadDiag_None: break;
3571 case CXLoadDiag_Unknown: return "Unknown";
3572 case CXLoadDiag_InvalidFile: return "Invalid File";
3573 }
3574 return "None";
3575}
3576
3577static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3578 switch (severity) {
3579 case CXDiagnostic_Note: return "note";
3580 case CXDiagnostic_Error: return "error";
3581 case CXDiagnostic_Fatal: return "fatal";
3582 case CXDiagnostic_Ignored: return "ignored";
3583 case CXDiagnostic_Warning: return "warning";
3584 }
3585 return "unknown";
3586}
3587
3588static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003589 if (indent == 0)
3590 return;
3591 fprintf(stderr, "+");
3592 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00003593 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003594 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00003595 --indent;
3596 }
3597}
3598
3599static void printLocation(CXSourceLocation L) {
3600 CXFile File;
3601 CXString FileName;
3602 unsigned line, column, offset;
3603
3604 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3605 FileName = clang_getFileName(File);
3606
3607 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3608 clang_disposeString(FileName);
3609}
3610
3611static void printRanges(CXDiagnostic D, unsigned indent) {
3612 unsigned i, n = clang_getDiagnosticNumRanges(D);
3613
3614 for (i = 0; i < n; ++i) {
3615 CXSourceLocation Start, End;
Enea Zaffanella10f90042013-07-22 20:58:30 +00003616 CXSourceRange SR = clang_getDiagnosticRange(D, i);
Ted Kremenek15322172011-11-10 08:43:12 +00003617 Start = clang_getRangeStart(SR);
3618 End = clang_getRangeEnd(SR);
3619
3620 printIndent(indent);
3621 fprintf(stderr, "Range: ");
3622 printLocation(Start);
3623 fprintf(stderr, " ");
3624 printLocation(End);
3625 fprintf(stderr, "\n");
3626 }
3627}
3628
3629static void printFixIts(CXDiagnostic D, unsigned indent) {
3630 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00003631 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00003632 for (i = 0 ; i < n; ++i) {
3633 CXSourceRange ReplacementRange;
3634 CXString text;
3635 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3636
3637 printIndent(indent);
3638 fprintf(stderr, "FIXIT: (");
3639 printLocation(clang_getRangeStart(ReplacementRange));
3640 fprintf(stderr, " - ");
3641 printLocation(clang_getRangeEnd(ReplacementRange));
3642 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3643 clang_disposeString(text);
3644 }
3645}
3646
3647static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003648 unsigned i, n;
3649
Ted Kremenek15322172011-11-10 08:43:12 +00003650 if (!Diags)
3651 return;
3652
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003653 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00003654 for (i = 0; i < n; ++i) {
3655 CXSourceLocation DiagLoc;
3656 CXDiagnostic D;
3657 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003658 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00003659 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003660 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00003661
3662 D = clang_getDiagnosticInSet(Diags, i);
3663 DiagLoc = clang_getDiagnosticLocation(D);
3664 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3665 FileName = clang_getFileName(File);
3666 DiagSpelling = clang_getDiagnosticSpelling(D);
3667
3668 printIndent(indent);
3669
3670 fprintf(stderr, "%s:%d:%d: %s: %s",
3671 clang_getCString(FileName),
3672 line,
3673 column,
3674 getSeverityString(clang_getDiagnosticSeverity(D)),
3675 clang_getCString(DiagSpelling));
3676
3677 DiagOption = clang_getDiagnosticOption(D, 0);
3678 DiagOptionStr = clang_getCString(DiagOption);
3679 if (DiagOptionStr) {
3680 fprintf(stderr, " [%s]", DiagOptionStr);
3681 }
3682
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003683 DiagCat = clang_getDiagnosticCategoryText(D);
3684 DiagCatStr = clang_getCString(DiagCat);
3685 if (DiagCatStr) {
3686 fprintf(stderr, " [%s]", DiagCatStr);
3687 }
3688
Ted Kremenek15322172011-11-10 08:43:12 +00003689 fprintf(stderr, "\n");
3690
3691 printRanges(D, indent);
3692 printFixIts(D, indent);
3693
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003694 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003695 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3696
3697 clang_disposeString(FileName);
3698 clang_disposeString(DiagSpelling);
3699 clang_disposeString(DiagOption);
3700 }
3701}
3702
3703static int read_diagnostics(const char *filename) {
3704 enum CXLoadDiag_Error error;
3705 CXString errorString;
3706 CXDiagnosticSet Diags = 0;
3707
3708 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3709 if (!Diags) {
3710 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3711 getDiagnosticCodeStr(error),
3712 clang_getCString(errorString));
3713 clang_disposeString(errorString);
3714 return 1;
3715 }
3716
3717 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003718 fprintf(stderr, "Number of diagnostics: %d\n",
3719 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003720 clang_disposeDiagnosticSet(Diags);
3721 return 0;
3722}
3723
3724/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003725/* Command line processing. */
3726/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003727
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003728static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003729 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003730 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003731 if (strcmp(s, "-usrs") == 0)
3732 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003733 if (strncmp(s, "-memory-usage", 13) == 0)
3734 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003735 return NULL;
3736}
3737
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003738static void print_usage(void) {
3739 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003740 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003741 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003742 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00003743 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3744 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi35849722012-10-24 22:52:04 +00003745 fprintf(stderr,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003746 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003747 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003748 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003749 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003750 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003751 "[FileCheck prefix]\n");
3752 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003753 " c-index-test -test-load-tu <AST file> <symbol filter> "
3754 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003755 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3756 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003757 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003758 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003759 " c-index-test -test-load-source-memory-usage "
3760 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003761 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3762 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003763 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003764 " c-index-test -test-load-source-usrs-memory-usage "
3765 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003766 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3767 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003768 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003769 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003770 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003771 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00003772 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003773 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003774 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003775 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003776 " c-index-test -write-pch <file> <compiler arguments>\n");
3777 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003778 " c-index-test -compilation-db [lookup <filename>] database\n");
3779 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003780 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003781 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003782 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003783 " all - load all symbols, including those from PCH\n"
3784 " local - load all symbols except those in PCH\n"
3785 " category - only load ObjC categories (non-PCH)\n"
3786 " interface - only load ObjC interfaces (non-PCH)\n"
3787 " protocol - only load ObjC protocols (non-PCH)\n"
3788 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003789 " typedef - only load typdefs (non-PCH)\n"
3790 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003791}
3792
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003793/***/
3794
3795int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003796 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003797 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3798 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003799 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003800 return perform_code_completion(argc, argv, 0);
3801 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3802 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003803 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3804 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003805 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3806 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00003807 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
3808 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003809 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003810 return index_file(argc - 2, argv + 2, /*full=*/0);
3811 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3812 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003813 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3814 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003815 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
3816 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003817 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003818 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003819 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003820 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3821 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003822 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003823 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3824 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3825 if (I) {
3826 int trials = atoi(argv[2]);
3827 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3828 NULL);
3829 }
3830 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003831 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003832 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003833
3834 PostVisitTU postVisit = 0;
3835 if (strstr(argv[1], "-memory-usage"))
3836 postVisit = PrintMemoryUsage;
3837
Ted Kremenek7d405622010-01-12 23:34:26 +00003838 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003839 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3840 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003841 }
3842 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003843 return perform_file_scan(argv[2], argv[3],
3844 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003845 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3846 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003847 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3848 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3849 PrintInclusionStack);
3850 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3851 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3852 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003853 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3854 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3855 NULL);
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003856 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003857 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003858 PrintType, 0);
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00003859 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
3860 return perform_test_load_source(argc - 2, argv + 2, "all",
3861 PrintTypeSize, 0);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003862 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
3863 return perform_test_load_source(argc - 2, argv + 2, "all",
3864 PrintBitWidth, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003865 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3866 if (argc > 2)
3867 return print_usrs(argv + 2, argv + argc);
3868 else {
3869 display_usrs();
3870 return 1;
3871 }
3872 }
3873 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3874 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003875 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3876 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003877 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3878 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3879
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003880 print_usage();
3881 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003882}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003883
3884/***/
3885
3886/* We intentionally run in a separate thread to ensure we at least minimal
3887 * testing of a multithreaded environment (for example, having a reduced stack
3888 * size). */
3889
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003890typedef struct thread_info {
3891 int argc;
3892 const char **argv;
3893 int result;
3894} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003895void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003896 thread_info *client_data = client_data_v;
3897 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003898#ifdef __CYGWIN__
3899 fflush(stdout); /* stdout is not flushed on Cygwin. */
3900#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003901}
3902
3903int main(int argc, const char **argv) {
Benjamin Kramerd1a4f682012-08-10 10:06:13 +00003904 thread_info client_data;
3905
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003906#ifdef CLANG_HAVE_LIBXML
3907 LIBXML_TEST_VERSION
3908#endif
3909
Douglas Gregor61605982010-10-27 16:00:01 +00003910 if (getenv("CINDEXTEST_NOTHREADS"))
3911 return cindextest_main(argc, argv);
3912
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003913 client_data.argc = argc;
3914 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003915 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003916 return client_data.result;
3917}