blob: d9bf236dc9ba0047d92353629a4888a4850cf5ce [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 {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000545 CXComment Comment;
546 Comment = clang_Cursor_getParsedComment(Cursor);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000547 if (clang_Comment_getKind(Comment) != CXComment_Null) {
548 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
549 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000550 {
551 CXString XML;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000552 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000553 PrintCXStringWithPrefix("FullCommentAsXML", XML);
554 ValidateCommentXML(clang_getCString(XML), ValidationData);
555 clang_disposeString(XML);
556 }
557
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000558 DumpCXComment(Comment);
559 }
560 }
561}
562
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000563typedef struct {
564 unsigned line;
565 unsigned col;
566} LineCol;
567
568static int lineCol_cmp(const void *p1, const void *p2) {
569 const LineCol *lhs = p1;
570 const LineCol *rhs = p2;
571 if (lhs->line != rhs->line)
572 return (int)lhs->line - (int)rhs->line;
573 return (int)lhs->col - (int)rhs->col;
574}
575
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000576static void PrintCursor(CXCursor Cursor,
577 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000578 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579 if (clang_isInvalid(Cursor.kind)) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000580 CXString ks;
581 ks = clang_getCursorKindSpelling(Cursor.kind);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000582 printf("Invalid Cursor => %s", clang_getCString(ks));
583 clang_disposeString(ks);
584 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000585 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000586 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000587 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000588 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000589 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000590 CXCursor *overridden;
591 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000592 unsigned RefNameRangeNr;
593 CXSourceRange CursorExtent;
594 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000595 int AlwaysUnavailable;
596 int AlwaysDeprecated;
597 CXString UnavailableMessage;
598 CXString DeprecatedMessage;
599 CXPlatformAvailability PlatformAvailability[2];
600 int NumPlatformAvailability;
601 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000602
Ted Kremeneke68fff62010-02-17 00:41:32 +0000603 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000604 string = want_display_name? clang_getCursorDisplayName(Cursor)
605 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000606 printf("%s=%s", clang_getCString(ks),
607 clang_getCString(string));
608 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000609 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000610
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000611 Referenced = clang_getCursorReferenced(Cursor);
612 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000613 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
614 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
615 printf("[");
616 for (I = 0; I != N; ++I) {
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000617 CXSourceLocation Loc;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000618 CXCursor Ovl;
619 Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000620 if (I)
621 printf(", ");
622
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000623 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000624 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000625 printf("%d:%d", line, column);
626 }
627 printf("]");
628 } else {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000629 CXSourceLocation Loc;
630 Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000631 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000632 printf(":%d:%d", line, column);
633 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000634 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000635
636 if (clang_isCursorDefinition(Cursor))
637 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000638
639 switch (clang_getCursorAvailability(Cursor)) {
640 case CXAvailability_Available:
641 break;
642
643 case CXAvailability_Deprecated:
644 printf(" (deprecated)");
645 break;
646
647 case CXAvailability_NotAvailable:
648 printf(" (unavailable)");
649 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000650
651 case CXAvailability_NotAccessible:
652 printf(" (inaccessible)");
653 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000654 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000655
Douglas Gregorcc889662012-05-08 00:14:45 +0000656 NumPlatformAvailability
657 = clang_getCursorPlatformAvailability(Cursor,
658 &AlwaysDeprecated,
659 &DeprecatedMessage,
660 &AlwaysUnavailable,
661 &UnavailableMessage,
662 PlatformAvailability, 2);
663 if (AlwaysUnavailable) {
664 printf(" (always unavailable: \"%s\")",
665 clang_getCString(UnavailableMessage));
666 } else if (AlwaysDeprecated) {
667 printf(" (always deprecated: \"%s\")",
668 clang_getCString(DeprecatedMessage));
669 } else {
670 for (I = 0; I != NumPlatformAvailability; ++I) {
671 if (I >= 2)
672 break;
673
674 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
675 if (PlatformAvailability[I].Unavailable)
676 printf(", unavailable");
677 else {
678 printVersion(", introduced=", PlatformAvailability[I].Introduced);
679 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
680 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
681 }
682 if (clang_getCString(PlatformAvailability[I].Message)[0])
683 printf(", message=\"%s\"",
684 clang_getCString(PlatformAvailability[I].Message));
685 printf(")");
686 }
687 }
688 for (I = 0; I != NumPlatformAvailability; ++I) {
689 if (I >= 2)
690 break;
691 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
692 }
693
694 clang_disposeString(DeprecatedMessage);
695 clang_disposeString(UnavailableMessage);
696
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000697 if (clang_CXXMethod_isStatic(Cursor))
698 printf(" (static)");
699 if (clang_CXXMethod_isVirtual(Cursor))
700 printf(" (virtual)");
Dmitri Gribenkoc965f762013-05-17 18:38:35 +0000701 if (clang_CXXMethod_isPureVirtual(Cursor))
702 printf(" (pure)");
Argyrios Kyrtzidis80e1aca2013-04-18 23:53:05 +0000703 if (clang_Cursor_isVariadic(Cursor))
704 printf(" (variadic)");
Argyrios Kyrtzidis514afc72013-07-05 20:44:37 +0000705 if (clang_Cursor_isObjCOptional(Cursor))
706 printf(" (@optional)");
707
Ted Kremenek95f33552010-08-26 01:42:22 +0000708 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000709 CXType T;
710 CXString S;
711 T = clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
712 S = clang_getTypeKindSpelling(T.kind);
Ted Kremenek95f33552010-08-26 01:42:22 +0000713 printf(" [IBOutletCollection=%s]", clang_getCString(S));
714 clang_disposeString(S);
715 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000716
717 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
718 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
719 unsigned isVirtual = clang_isVirtualBase(Cursor);
720 const char *accessStr = 0;
721
722 switch (access) {
723 case CX_CXXInvalidAccessSpecifier:
724 accessStr = "invalid"; break;
725 case CX_CXXPublic:
726 accessStr = "public"; break;
727 case CX_CXXProtected:
728 accessStr = "protected"; break;
729 case CX_CXXPrivate:
730 accessStr = "private"; break;
731 }
732
733 printf(" [access=%s isVirtual=%s]", accessStr,
734 isVirtual ? "true" : "false");
735 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000736
737 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
738 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000739 CXSourceLocation Loc;
740 CXString Name;
741 Loc = clang_getCursorLocation(SpecializationOf);
742 Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000743 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000744 printf(" [Specialization of %s:%d:%d]",
745 clang_getCString(Name), line, column);
746 clang_disposeString(Name);
747 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000748
749 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
750 if (num_overridden) {
751 unsigned I;
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000752 LineCol lineCols[50];
753 assert(num_overridden <= 50);
Douglas Gregor9f592342010-10-01 20:25:15 +0000754 printf(" [Overrides ");
755 for (I = 0; I != num_overridden; ++I) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000756 CXSourceLocation Loc;
757 Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000758 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000759 lineCols[I].line = line;
760 lineCols[I].col = column;
761 }
Michael Liao64221492012-08-30 00:45:32 +0000762 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000763 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
764 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor9f592342010-10-01 20:25:15 +0000765 if (I)
766 printf(", ");
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000767 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor9f592342010-10-01 20:25:15 +0000768 }
769 printf("]");
770 clang_disposeOverriddenCursors(overridden);
771 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000772
773 if (Cursor.kind == CXCursor_InclusionDirective) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000774 CXFile File;
775 CXString Included;
776 File = clang_getIncludedFile(Cursor);
777 Included = clang_getFileName(File);
Douglas Gregorecdcb882010-10-20 22:00:55 +0000778 printf(" (%s)", clang_getCString(Included));
779 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000780
781 if (clang_isFileMultipleIncludeGuarded(TU, File))
782 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000783 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000784
785 CursorExtent = clang_getCursorExtent(Cursor);
786 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
787 CXNameRange_WantQualifier
788 | CXNameRange_WantSinglePiece
789 | CXNameRange_WantTemplateArgs,
790 0);
791 if (!clang_equalRanges(CursorExtent, RefNameRange))
792 PrintRange(RefNameRange, "SingleRefName");
793
794 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
795 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
796 CXNameRange_WantQualifier
797 | CXNameRange_WantTemplateArgs,
798 RefNameRangeNr);
799 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
800 break;
801 if (!clang_equalRanges(CursorExtent, RefNameRange))
802 PrintRange(RefNameRange, "RefName");
803 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000804
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000805 PrintCursorComments(Cursor, ValidationData);
Argyrios Kyrtzidis9ee6a662013-04-18 22:15:49 +0000806
807 {
808 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
809 if (PropAttrs != CXObjCPropertyAttr_noattr) {
810 printf(" [");
811 #define PRINT_PROP_ATTR(A) \
812 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
813 PRINT_PROP_ATTR(readonly);
814 PRINT_PROP_ATTR(getter);
815 PRINT_PROP_ATTR(assign);
816 PRINT_PROP_ATTR(readwrite);
817 PRINT_PROP_ATTR(retain);
818 PRINT_PROP_ATTR(copy);
819 PRINT_PROP_ATTR(nonatomic);
820 PRINT_PROP_ATTR(setter);
821 PRINT_PROP_ATTR(atomic);
822 PRINT_PROP_ATTR(weak);
823 PRINT_PROP_ATTR(strong);
824 PRINT_PROP_ATTR(unsafe_unretained);
825 printf("]");
826 }
827 }
Argyrios Kyrtzidis38dbad22013-04-18 23:29:12 +0000828
829 {
830 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
831 if (QT != CXObjCDeclQualifier_None) {
832 printf(" [");
833 #define PRINT_OBJC_QUAL(A) \
834 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
835 PRINT_OBJC_QUAL(In);
836 PRINT_OBJC_QUAL(Inout);
837 PRINT_OBJC_QUAL(Out);
838 PRINT_OBJC_QUAL(Bycopy);
839 PRINT_OBJC_QUAL(Byref);
840 PRINT_OBJC_QUAL(Oneway);
841 printf("]");
842 }
843 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000844 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000845}
Steve Naroff89922f82009-08-31 00:59:03 +0000846
Ted Kremeneke68fff62010-02-17 00:41:32 +0000847static const char* GetCursorSource(CXCursor Cursor) {
Ted Kremenek74844072010-02-17 00:41:20 +0000848 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000849 CXFile file;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000850 CXSourceLocation Loc;
851 Loc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000852 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000853 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000854 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000855 clang_disposeString(source);
856 return "<invalid loc>";
857 }
858 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000859 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000860 clang_disposeString(source);
861 return b;
862 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000863}
864
Ted Kremenek0d435192009-11-17 18:13:31 +0000865/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000866/* Callbacks. */
867/******************************************************************************/
868
869typedef void (*PostVisitTU)(CXTranslationUnit);
870
Douglas Gregora88084b2010-02-18 18:08:43 +0000871void PrintDiagnostic(CXDiagnostic Diagnostic) {
872 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000873 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000874 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000875 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000876 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
877 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000878 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000879
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000880 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000881 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000882
Douglas Gregor274f1902010-02-22 23:17:23 +0000883 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
884 fprintf(stderr, "%s\n", clang_getCString(Msg));
885 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000886
Douglas Gregora9b06d42010-11-09 06:24:54 +0000887 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
888 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000889 if (!file)
890 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000891
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000892 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000893 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000894 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000895 CXSourceRange range;
Douglas Gregor473d7012010-02-19 18:16:06 +0000896 unsigned start_line, start_column, end_line, end_column;
897 CXFile start_file, end_file;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000898 CXString insertion_text;
899 CXSourceLocation start, end;
900 insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
901 start = clang_getRangeStart(range);
902 end = clang_getRangeEnd(range);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000903 clang_getSpellingLocation(start, &start_file, &start_line,
904 &start_column, 0);
905 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000906 if (clang_equalLocations(start, end)) {
907 /* Insertion. */
908 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000909 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000910 clang_getCString(insertion_text), start_line, start_column);
911 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
912 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000913 if (start_file == file && end_file == file) {
914 fprintf(out, "FIX-IT: Remove ");
915 PrintExtent(out, start_line, start_column, end_line, end_column);
916 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000917 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000918 } else {
919 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000920 if (start_file == end_file) {
921 fprintf(out, "FIX-IT: Replace ");
922 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000923 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000924 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000925 break;
926 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000927 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000928 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000929}
930
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000931void PrintDiagnosticSet(CXDiagnosticSet Set) {
932 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
933 for ( ; i != n ; ++i) {
934 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
935 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000936 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000937 if (ChildDiags)
938 PrintDiagnosticSet(ChildDiags);
939 }
940}
941
942void PrintDiagnostics(CXTranslationUnit TU) {
943 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
944 PrintDiagnosticSet(TUSet);
945 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000946}
947
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000948void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000949 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000950 unsigned i = 0;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000951 CXTUResourceUsage usage;
952 usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000953 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000954 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000955 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000956 unsigned long amount = usage.entries[i].amount;
957 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000958 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000959 ((double) amount)/(1024*1024));
960 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000961 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000962 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000963 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000964}
965
Ted Kremenekce2ae882010-01-26 17:59:48 +0000966/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000967/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000968/******************************************************************************/
969
Douglas Gregora7bde202010-01-19 00:34:46 +0000970static void PrintCursorExtent(CXCursor C) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000971 PrintRange(clang_getCursorExtent(C), "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000972}
973
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000974/* Data used by the visitors. */
975typedef struct {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000976 CXTranslationUnit TU;
977 enum CXCursorKind *Filter;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000978 CommentXMLValidationData ValidationData;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000979} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000980
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000981
Ted Kremeneke68fff62010-02-17 00:41:32 +0000982enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000983 CXCursor Parent,
984 CXClientData ClientData) {
985 VisitorData *Data = (VisitorData *)ClientData;
986 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000987 unsigned line, column;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +0000988 CXSourceLocation Loc;
989 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000990 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000991 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000992 GetCursorSource(Cursor), line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000993 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregora7bde202010-01-19 00:34:46 +0000994 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis04b67482013-04-11 17:02:10 +0000995 if (clang_isDeclaration(Cursor.kind)) {
996 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
997 const char *accessStr = 0;
998
999 switch (access) {
1000 case CX_CXXInvalidAccessSpecifier: break;
1001 case CX_CXXPublic:
1002 accessStr = "public"; break;
1003 case CX_CXXProtected:
1004 accessStr = "protected"; break;
1005 case CX_CXXPrivate:
1006 accessStr = "private"; break;
1007 }
1008
1009 if (accessStr)
1010 printf(" [access=%s]", accessStr);
1011 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001012 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001013 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +00001014 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001015
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001016 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +00001017}
Steve Naroff50398192009-08-28 15:28:48 +00001018
Ted Kremeneke68fff62010-02-17 00:41:32 +00001019static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001020 CXCursor Parent,
1021 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001022 const char *startBuf, *endBuf;
1023 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1024 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001025 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001026
Douglas Gregorb6998662010-01-19 19:34:47 +00001027 if (Cursor.kind != CXCursor_FunctionDecl ||
1028 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001029 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001030
1031 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1032 &startLine, &startColumn,
1033 &endLine, &endColumn);
1034 /* Probe the entire body, looking for both decls and refs. */
1035 curLine = startLine;
1036 curColumn = startColumn;
1037
1038 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001039 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +00001040 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +00001041 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001042
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001043 if (*startBuf == '\n') {
1044 startBuf++;
1045 curLine++;
1046 curColumn = 1;
1047 } else if (*startBuf != '\t')
1048 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001049
Douglas Gregor98258af2010-01-18 22:46:11 +00001050 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +00001051 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001052
Douglas Gregor1db19de2010-01-19 21:36:55 +00001053 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001054 if (clang_getCString(source)) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001055 CXSourceLocation RefLoc;
1056 RefLoc = clang_getLocation(Data->TU, file, curLine, curColumn);
Douglas Gregorb9790342010-01-22 21:44:22 +00001057 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001058 if (Ref.kind == CXCursor_NoDeclFound) {
1059 /* Nothing found here; that's fine. */
1060 } else if (Ref.kind != CXCursor_FunctionDecl) {
1061 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1062 curLine, curColumn);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001063 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor98258af2010-01-18 22:46:11 +00001064 printf("\n");
1065 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001066 }
Ted Kremenek74844072010-02-17 00:41:20 +00001067 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001068 startBuf++;
1069 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001070
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001071 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001072}
1073
Ted Kremenek7d405622010-01-12 23:34:26 +00001074/******************************************************************************/
1075/* USR testing. */
1076/******************************************************************************/
1077
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001078enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1079 CXClientData ClientData) {
1080 VisitorData *Data = (VisitorData *)ClientData;
1081 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001082 const char *cstr;
1083 CXString USR;
1084 USR = clang_getCursorUSR(C);
1085 cstr = clang_getCString(USR);
Ted Kremeneke542f772010-04-20 23:15:40 +00001086 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +00001087 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +00001088 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +00001089 }
Ted Kremeneke542f772010-04-20 23:15:40 +00001090 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1091
Douglas Gregora7bde202010-01-19 00:34:46 +00001092 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +00001093 printf("\n");
1094 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001095
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001096 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001097 }
1098
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001099 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +00001100}
1101
1102/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +00001103/* Inclusion stack testing. */
1104/******************************************************************************/
1105
1106void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1107 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001108
Ted Kremenek16b55a72010-01-26 19:31:51 +00001109 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +00001110 CXString fname;
1111
1112 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001113 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +00001114 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001115
Ted Kremenek16b55a72010-01-26 19:31:51 +00001116 for (i = 0; i < includeStackLen; ++i) {
1117 CXFile includingFile;
1118 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +00001119 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1120 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +00001121 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001122 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +00001123 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001124 }
1125 printf("\n");
1126}
1127
1128void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001129 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001130}
1131
1132/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +00001133/* Linkage testing. */
1134/******************************************************************************/
1135
1136static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1137 CXClientData d) {
1138 const char *linkage = 0;
1139
1140 if (clang_isInvalid(clang_getCursorKind(cursor)))
1141 return CXChildVisit_Recurse;
1142
1143 switch (clang_getCursorLinkage(cursor)) {
1144 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +00001145 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1146 case CXLinkage_Internal: linkage = "Internal"; break;
1147 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1148 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +00001149 }
1150
1151 if (linkage) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001152 PrintCursor(cursor, NULL);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001153 printf("linkage=%s\n", linkage);
1154 }
1155
1156 return CXChildVisit_Recurse;
1157}
1158
1159/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001160/* Typekind testing. */
1161/******************************************************************************/
1162
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001163static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1164 CXString TypeSpelling, TypeKindSpelling;
1165
1166 TypeSpelling = clang_getTypeSpelling(T);
1167 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1168 printf(Format,
1169 clang_getCString(TypeSpelling),
1170 clang_getCString(TypeKindSpelling));
1171 clang_disposeString(TypeSpelling);
1172 clang_disposeString(TypeKindSpelling);
1173}
1174
1175static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1176 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001177 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001178 CXType T;
1179 T = clang_getCursorType(cursor);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001180 PrintCursor(cursor, NULL);
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001181 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregore72fb6f2011-01-27 16:27:11 +00001182 if (clang_isConstQualifiedType(T))
1183 printf(" const");
1184 if (clang_isVolatileQualifiedType(T))
1185 printf(" volatile");
1186 if (clang_isRestrictQualifiedType(T))
1187 printf(" restrict");
Benjamin Kramere1403d22010-06-22 09:29:44 +00001188 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001189 {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001190 CXType CT;
1191 CT = clang_getCanonicalType(T);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001192 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001193 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001194 }
1195 }
Benjamin Kramere1403d22010-06-22 09:29:44 +00001196 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001197 {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001198 CXType RT;
1199 RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001200 if (RT.kind != CXType_Invalid) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001201 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001202 }
1203 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001204 /* Print the argument types if they exist. */
1205 {
1206 int numArgs = clang_Cursor_getNumArguments(cursor);
1207 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001208 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001209 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001210 for (i = 0; i < numArgs; ++i) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001211 CXType T;
1212 T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001213 if (T.kind != CXType_Invalid) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001214 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001215 }
1216 }
1217 printf("]");
1218 }
1219 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +00001220 /* Print if this is a non-POD type. */
1221 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001222
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001223 printf("\n");
1224 }
1225 return CXChildVisit_Recurse;
1226}
1227
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00001228static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1229 CXClientData d) {
1230 CXType T;
1231 enum CXCursorKind K = clang_getCursorKind(cursor);
1232 if (clang_isInvalid(K))
1233 return CXChildVisit_Recurse;
1234 T = clang_getCursorType(cursor);
1235 PrintCursor(cursor, NULL);
1236 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1237 /* Print the type sizeof if applicable. */
1238 {
1239 long long Size = clang_Type_getSizeOf(T);
1240 if (Size >= 0 || Size < -1 ) {
1241 printf(" [sizeof=%lld]", Size);
1242 }
1243 }
1244 /* Print the type alignof if applicable. */
1245 {
1246 long long Align = clang_Type_getAlignOf(T);
1247 if (Align >= 0 || Align < -1) {
1248 printf(" [alignof=%lld]", Align);
1249 }
1250 }
1251 /* Print the record field offset if applicable. */
1252 {
1253 const char *FieldName = clang_getCString(clang_getCursorSpelling(cursor));
1254 /* recurse to get the root anonymous record parent */
1255 CXCursor Parent, Root;
1256 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl ) {
1257 const char *RootParentName;
1258 Root = Parent = p;
1259 do {
1260 Root = Parent;
1261 RootParentName = clang_getCString(clang_getCursorSpelling(Root));
1262 Parent = clang_getCursorSemanticParent(Root);
1263 } while ( clang_getCursorType(Parent).kind == CXType_Record &&
1264 !strcmp(RootParentName, "") );
1265 /* if RootParentName is "", record is anonymous. */
1266 {
1267 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1268 FieldName);
1269 printf(" [offsetof=%lld]", Offset);
1270 }
1271 }
1272 }
1273 /* Print if its a bitfield */
1274 {
1275 int IsBitfield = clang_Cursor_isBitField(cursor);
1276 if (IsBitfield)
1277 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1278 }
1279 printf("\n");
1280 return CXChildVisit_Recurse;
1281}
1282
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001283/******************************************************************************/
1284/* Bitwidth testing. */
1285/******************************************************************************/
1286
1287static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1288 CXClientData d) {
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001289 int Bitwidth;
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001290 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1291 return CXChildVisit_Recurse;
1292
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001293 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001294 if (Bitwidth >= 0) {
1295 PrintCursor(cursor, NULL);
1296 printf(" bitwidth=%d\n", Bitwidth);
1297 }
1298
1299 return CXChildVisit_Recurse;
1300}
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001301
1302/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +00001303/* Loading ASTs/source. */
1304/******************************************************************************/
1305
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001306static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +00001307 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001308 CXCursorVisitor Visitor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001309 PostVisitTU PV,
1310 const char *CommentSchemaFile) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001311
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001312 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001313 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001314
1315 if (Visitor) {
1316 enum CXCursorKind K = CXCursor_NotImplemented;
1317 enum CXCursorKind *ck = &K;
1318 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001319
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001320 /* Perform some simple filtering. */
1321 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001322 else if (!strcmp(filter, "all-display") ||
1323 !strcmp(filter, "local-display")) {
1324 ck = NULL;
1325 want_display_name = 1;
1326 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001327 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001328 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1329 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1330 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1331 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1332 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1333 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1334 else {
1335 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1336 return 1;
1337 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001338
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001339 Data.TU = TU;
1340 Data.Filter = ck;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001341 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1342#ifdef CLANG_HAVE_LIBXML
1343 Data.ValidationData.RNGParser = NULL;
1344 Data.ValidationData.Schema = NULL;
1345#endif
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001346 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001347 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001348
Ted Kremenekce2ae882010-01-26 17:59:48 +00001349 if (PV)
1350 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001351
Douglas Gregora88084b2010-02-18 18:08:43 +00001352 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001353 if (checkForErrors(TU) != 0) {
1354 clang_disposeTranslationUnit(TU);
1355 return -1;
1356 }
1357
Ted Kremenek0d435192009-11-17 18:13:31 +00001358 clang_disposeTranslationUnit(TU);
1359 return 0;
1360}
1361
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001362int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001363 const char *prefix, CXCursorVisitor Visitor,
1364 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001365 CXIndex Idx;
1366 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001367 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001369 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00001370 /* displayDiagnostics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001371
Ted Kremenek020a0952010-02-11 07:41:25 +00001372 if (!CreateTranslationUnit(Idx, file, &TU)) {
1373 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001374 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001375 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001376
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001377 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek020a0952010-02-11 07:41:25 +00001378 clang_disposeIndex(Idx);
1379 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001380}
1381
Ted Kremenekce2ae882010-01-26 17:59:48 +00001382int perform_test_load_source(int argc, const char **argv,
1383 const char *filter, CXCursorVisitor Visitor,
1384 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001385 CXIndex Idx;
1386 CXTranslationUnit TU;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001387 const char *CommentSchemaFile;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001388 struct CXUnsavedFile *unsaved_files = 0;
1389 int num_unsaved_files = 0;
1390 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001391
Daniel Dunbarada487d2009-12-01 02:03:10 +00001392 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001393 (!strcmp(filter, "local") ||
1394 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidiscd6dcb32013-04-09 20:29:24 +00001395 /* displayDiagnostics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001396
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001397 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1398 argc--;
1399 argv++;
1400 }
1401
Ted Kremenek020a0952010-02-11 07:41:25 +00001402 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1403 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001404 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001405 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001406
Douglas Gregordca8ee82011-05-06 16:33:08 +00001407 TU = clang_parseTranslationUnit(Idx, 0,
1408 argv + num_unsaved_files,
1409 argc - num_unsaved_files,
1410 unsaved_files, num_unsaved_files,
1411 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001412 if (!TU) {
1413 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001414 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001415 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001416 return 1;
1417 }
1418
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001419 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1420 CommentSchemaFile);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001421 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001422 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001423 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001424}
1425
Douglas Gregorabc563f2010-07-19 21:46:24 +00001426int perform_test_reparse_source(int argc, const char **argv, int trials,
1427 const char *filter, CXCursorVisitor Visitor,
1428 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001429 CXIndex Idx;
1430 CXTranslationUnit TU;
1431 struct CXUnsavedFile *unsaved_files = 0;
1432 int num_unsaved_files = 0;
1433 int result;
1434 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001435 int remap_after_trial = 0;
1436 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001437
1438 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1439 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidiscd6dcb32013-04-09 20:29:24 +00001440 /* displayDiagnostics=*/1);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001441
Douglas Gregorabc563f2010-07-19 21:46:24 +00001442 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1443 clang_disposeIndex(Idx);
1444 return -1;
1445 }
1446
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001447 /* Load the initial translation unit -- we do this without honoring remapped
1448 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001449 TU = clang_parseTranslationUnit(Idx, 0,
1450 argv + num_unsaved_files,
1451 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001452 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001453 if (!TU) {
1454 fprintf(stderr, "Unable to load translation unit!\n");
1455 free_remapped_files(unsaved_files, num_unsaved_files);
1456 clang_disposeIndex(Idx);
1457 return 1;
1458 }
1459
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001460 if (checkForErrors(TU) != 0)
1461 return -1;
1462
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001463 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1464 remap_after_trial =
1465 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1466 }
1467
Douglas Gregorabc563f2010-07-19 21:46:24 +00001468 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001469 if (clang_reparseTranslationUnit(TU,
1470 trial >= remap_after_trial ? num_unsaved_files : 0,
1471 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001472 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001473 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001474 clang_disposeTranslationUnit(TU);
1475 free_remapped_files(unsaved_files, num_unsaved_files);
1476 clang_disposeIndex(Idx);
1477 return -1;
1478 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001479
1480 if (checkForErrors(TU) != 0)
1481 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001482 }
1483
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001484 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001485
Douglas Gregorabc563f2010-07-19 21:46:24 +00001486 free_remapped_files(unsaved_files, num_unsaved_files);
1487 clang_disposeIndex(Idx);
1488 return result;
1489}
1490
Ted Kremenek0d435192009-11-17 18:13:31 +00001491/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001492/* Logic for testing clang_getCursor(). */
1493/******************************************************************************/
1494
Douglas Gregordd3e5542011-05-04 00:14:37 +00001495static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001496 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001497 unsigned end_line, unsigned end_col,
1498 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001499 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001500 if (prefix)
1501 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001502 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1503 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001504 PrintCursor(cursor, NULL);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001505 printf("\n");
1506}
1507
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001508static int perform_file_scan(const char *ast_file, const char *source_file,
1509 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001510 CXIndex Idx;
1511 CXTranslationUnit TU;
1512 FILE *fp;
Douglas Gregorb9790342010-01-22 21:44:22 +00001513 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001514 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001515 unsigned start_line = 1, start_col = 1;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001516 CXCursor prevCursor;
1517
1518 prevCursor = clang_getNullCursor();
Ted Kremeneke68fff62010-02-17 00:41:32 +00001519
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001520 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00001521 /* displayDiagnostics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001522 fprintf(stderr, "Could not create Index\n");
1523 return 1;
1524 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001525
Ted Kremenek1c6da172009-11-17 19:37:36 +00001526 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1527 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001528
Ted Kremenek1c6da172009-11-17 19:37:36 +00001529 if ((fp = fopen(source_file, "r")) == NULL) {
1530 fprintf(stderr, "Could not open '%s'\n", source_file);
1531 return 1;
1532 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001533
Douglas Gregorb9790342010-01-22 21:44:22 +00001534 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001535 for (;;) {
1536 CXCursor cursor;
1537 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001538
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001539 if (c == '\n') {
1540 ++line;
1541 col = 1;
1542 } else
1543 ++col;
1544
1545 /* Check the cursor at this position, and dump the previous one if we have
1546 * found something new.
1547 */
1548 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1549 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1550 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001551 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001552 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001553 start_line = line;
1554 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001555 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001556 if (c == EOF)
1557 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001558
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001559 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001560 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001561
Ted Kremenek1c6da172009-11-17 19:37:36 +00001562 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001563 clang_disposeTranslationUnit(TU);
1564 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001565 return 0;
1566}
1567
1568/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001569/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001570/******************************************************************************/
1571
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001572/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1573 on failure. If successful, the pointer *filename will contain newly-allocated
1574 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001575int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001576 unsigned *column, unsigned *second_line,
1577 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001578 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001579 const char *last_colon = strrchr(input, ':');
1580 unsigned values[4], i;
1581 unsigned num_values = (second_line && second_column)? 4 : 2;
1582
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001583 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001584 if (!last_colon || last_colon == input) {
1585 if (num_values == 4)
1586 fprintf(stderr, "could not parse filename:line:column:line:column in "
1587 "'%s'\n", input);
1588 else
1589 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001590 return 1;
1591 }
1592
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001593 for (i = 0; i != num_values; ++i) {
1594 const char *prev_colon;
1595
1596 /* Parse the next line or column. */
1597 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1598 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001599 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001600 (i % 2 ? "column" : "line"), input);
1601 return 1;
1602 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001603
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001604 if (i + 1 == num_values)
1605 break;
1606
1607 /* Find the previous colon. */
1608 prev_colon = last_colon - 1;
1609 while (prev_colon != input && *prev_colon != ':')
1610 --prev_colon;
1611 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001612 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001613 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001614 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001615 }
1616
1617 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001618 }
1619
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001620 *line = values[0];
1621 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001622
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001623 if (second_line && second_column) {
1624 *second_line = values[2];
1625 *second_column = values[3];
1626 }
1627
Douglas Gregor88d23952009-11-09 18:19:57 +00001628 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001629 *filename = (char*)malloc(last_colon - input + 1);
1630 memcpy(*filename, input, last_colon - input);
1631 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001632 return 0;
1633}
1634
1635const char *
1636clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1637 switch (Kind) {
1638 case CXCompletionChunk_Optional: return "Optional";
1639 case CXCompletionChunk_TypedText: return "TypedText";
1640 case CXCompletionChunk_Text: return "Text";
1641 case CXCompletionChunk_Placeholder: return "Placeholder";
1642 case CXCompletionChunk_Informative: return "Informative";
1643 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1644 case CXCompletionChunk_LeftParen: return "LeftParen";
1645 case CXCompletionChunk_RightParen: return "RightParen";
1646 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1647 case CXCompletionChunk_RightBracket: return "RightBracket";
1648 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1649 case CXCompletionChunk_RightBrace: return "RightBrace";
1650 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1651 case CXCompletionChunk_RightAngle: return "RightAngle";
1652 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001653 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001654 case CXCompletionChunk_Colon: return "Colon";
1655 case CXCompletionChunk_SemiColon: return "SemiColon";
1656 case CXCompletionChunk_Equal: return "Equal";
1657 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1658 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001659 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001660
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001661 return "Unknown";
1662}
1663
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001664static int checkForErrors(CXTranslationUnit TU) {
1665 unsigned Num, i;
1666 CXDiagnostic Diag;
1667 CXString DiagStr;
1668
1669 if (!getenv("CINDEXTEST_FAILONERROR"))
1670 return 0;
1671
1672 Num = clang_getNumDiagnostics(TU);
1673 for (i = 0; i != Num; ++i) {
1674 Diag = clang_getDiagnostic(TU, i);
1675 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1676 DiagStr = clang_formatDiagnostic(Diag,
1677 clang_defaultDiagnosticDisplayOptions());
1678 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1679 clang_disposeString(DiagStr);
1680 clang_disposeDiagnostic(Diag);
1681 return -1;
1682 }
1683 clang_disposeDiagnostic(Diag);
1684 }
1685
1686 return 0;
1687}
1688
Douglas Gregor3ac73852009-11-09 16:04:45 +00001689void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001690 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001691
Douglas Gregor3ac73852009-11-09 16:04:45 +00001692 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001693 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001694 CXString text;
1695 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001696 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001697 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001698
Douglas Gregor3ac73852009-11-09 16:04:45 +00001699 if (Kind == CXCompletionChunk_Optional) {
1700 fprintf(file, "{Optional ");
1701 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001702 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001703 file);
1704 fprintf(file, "}");
1705 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001706 }
1707
1708 if (Kind == CXCompletionChunk_VerticalSpace) {
1709 fprintf(file, "{VerticalSpace }");
1710 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001711 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001712
Douglas Gregord5a20892009-11-09 17:05:28 +00001713 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001714 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001715 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001716 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001717 cstr ? cstr : "");
1718 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001719 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001720
Douglas Gregor3ac73852009-11-09 16:04:45 +00001721}
1722
1723void print_completion_result(CXCompletionResult *completion_result,
1724 CXClientData client_data) {
1725 FILE *file = (FILE *)client_data;
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001726 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001727 enum CXCursorKind ParentKind;
1728 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001729 CXString BriefComment;
1730 const char *BriefCommentCString;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001731 CXString ks;
1732
1733 ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Douglas Gregorba103062012-03-27 23:34:16 +00001734
Ted Kremeneke68fff62010-02-17 00:41:32 +00001735 fprintf(file, "%s:", clang_getCString(ks));
1736 clang_disposeString(ks);
1737
Douglas Gregor3ac73852009-11-09 16:04:45 +00001738 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001739 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001740 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001741 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1742 case CXAvailability_Available:
1743 break;
1744
1745 case CXAvailability_Deprecated:
1746 fprintf(file, " (deprecated)");
1747 break;
1748
1749 case CXAvailability_NotAvailable:
1750 fprintf(file, " (unavailable)");
1751 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001752
1753 case CXAvailability_NotAccessible:
1754 fprintf(file, " (inaccessible)");
1755 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001756 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001757
1758 annotationCount = clang_getCompletionNumAnnotations(
1759 completion_result->CompletionString);
1760 if (annotationCount) {
1761 unsigned i;
1762 fprintf(file, " (");
1763 for (i = 0; i < annotationCount; ++i) {
1764 if (i != 0)
1765 fprintf(file, ", ");
1766 fprintf(file, "\"%s\"",
1767 clang_getCString(clang_getCompletionAnnotation(
1768 completion_result->CompletionString, i)));
1769 }
1770 fprintf(file, ")");
1771 }
1772
Douglas Gregorba103062012-03-27 23:34:16 +00001773 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1774 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1775 &ParentKind);
1776 if (ParentKind != CXCursor_NotImplemented) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00001777 CXString KindSpelling;
1778 KindSpelling = clang_getCursorKindSpelling(ParentKind);
Douglas Gregorba103062012-03-27 23:34:16 +00001779 fprintf(file, " (parent: %s '%s')",
1780 clang_getCString(KindSpelling),
1781 clang_getCString(ParentName));
1782 clang_disposeString(KindSpelling);
1783 }
1784 clang_disposeString(ParentName);
1785 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001786
1787 BriefComment = clang_getCompletionBriefComment(
1788 completion_result->CompletionString);
1789 BriefCommentCString = clang_getCString(BriefComment);
1790 if (BriefCommentCString && *BriefCommentCString != '\0') {
1791 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1792 }
1793 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001794
Douglas Gregor58ddb602010-08-23 23:00:57 +00001795 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001796}
1797
Douglas Gregor3da626b2011-07-07 16:03:39 +00001798void print_completion_contexts(unsigned long long contexts, FILE *file) {
1799 fprintf(file, "Completion contexts:\n");
1800 if (contexts == CXCompletionContext_Unknown) {
1801 fprintf(file, "Unknown\n");
1802 }
1803 if (contexts & CXCompletionContext_AnyType) {
1804 fprintf(file, "Any type\n");
1805 }
1806 if (contexts & CXCompletionContext_AnyValue) {
1807 fprintf(file, "Any value\n");
1808 }
1809 if (contexts & CXCompletionContext_ObjCObjectValue) {
1810 fprintf(file, "Objective-C object value\n");
1811 }
1812 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1813 fprintf(file, "Objective-C selector value\n");
1814 }
1815 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1816 fprintf(file, "C++ class type value\n");
1817 }
1818 if (contexts & CXCompletionContext_DotMemberAccess) {
1819 fprintf(file, "Dot member access\n");
1820 }
1821 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1822 fprintf(file, "Arrow member access\n");
1823 }
1824 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1825 fprintf(file, "Objective-C property access\n");
1826 }
1827 if (contexts & CXCompletionContext_EnumTag) {
1828 fprintf(file, "Enum tag\n");
1829 }
1830 if (contexts & CXCompletionContext_UnionTag) {
1831 fprintf(file, "Union tag\n");
1832 }
1833 if (contexts & CXCompletionContext_StructTag) {
1834 fprintf(file, "Struct tag\n");
1835 }
1836 if (contexts & CXCompletionContext_ClassTag) {
1837 fprintf(file, "Class name\n");
1838 }
1839 if (contexts & CXCompletionContext_Namespace) {
1840 fprintf(file, "Namespace or namespace alias\n");
1841 }
1842 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1843 fprintf(file, "Nested name specifier\n");
1844 }
1845 if (contexts & CXCompletionContext_ObjCInterface) {
1846 fprintf(file, "Objective-C interface\n");
1847 }
1848 if (contexts & CXCompletionContext_ObjCProtocol) {
1849 fprintf(file, "Objective-C protocol\n");
1850 }
1851 if (contexts & CXCompletionContext_ObjCCategory) {
1852 fprintf(file, "Objective-C category\n");
1853 }
1854 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1855 fprintf(file, "Objective-C instance method\n");
1856 }
1857 if (contexts & CXCompletionContext_ObjCClassMessage) {
1858 fprintf(file, "Objective-C class method\n");
1859 }
1860 if (contexts & CXCompletionContext_ObjCSelectorName) {
1861 fprintf(file, "Objective-C selector name\n");
1862 }
1863 if (contexts & CXCompletionContext_MacroName) {
1864 fprintf(file, "Macro name\n");
1865 }
1866 if (contexts & CXCompletionContext_NaturalLanguage) {
1867 fprintf(file, "Natural language\n");
1868 }
1869}
1870
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001871int my_stricmp(const char *s1, const char *s2) {
1872 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001873 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001874 if (c1 < c2)
1875 return -1;
1876 else if (c1 > c2)
1877 return 1;
1878
1879 ++s1;
1880 ++s2;
1881 }
1882
1883 if (*s1)
1884 return 1;
1885 else if (*s2)
1886 return -1;
1887 return 0;
1888}
1889
Douglas Gregor1982c182010-07-12 18:38:41 +00001890int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001891 const char *input = argv[1];
1892 char *filename = 0;
1893 unsigned line;
1894 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001895 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001896 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001897 struct CXUnsavedFile *unsaved_files = 0;
1898 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001899 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001900 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001901 unsigned I, Repeats = 1;
1902 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1903
1904 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1905 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001906 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1907 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001908
Douglas Gregor1982c182010-07-12 18:38:41 +00001909 if (timing_only)
1910 input += strlen("-code-completion-timing=");
1911 else
1912 input += strlen("-code-completion-at=");
1913
Ted Kremeneke68fff62010-02-17 00:41:32 +00001914 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001915 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001916 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001917
Douglas Gregor735df882009-12-02 09:21:34 +00001918 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1919 return -1;
1920
Douglas Gregor32be4a52010-10-11 21:37:58 +00001921 CIdx = clang_createIndex(0, 0);
1922
1923 if (getenv("CINDEXTEST_EDITING"))
1924 Repeats = 5;
1925
1926 TU = clang_parseTranslationUnit(CIdx, 0,
1927 argv + num_unsaved_files + 2,
1928 argc - num_unsaved_files - 2,
1929 0, 0, getDefaultParsingOptions());
1930 if (!TU) {
1931 fprintf(stderr, "Unable to load translation unit!\n");
1932 return 1;
1933 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001934
1935 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1936 fprintf(stderr, "Unable to reparse translation init!\n");
1937 return 1;
1938 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001939
1940 for (I = 0; I != Repeats; ++I) {
1941 results = clang_codeCompleteAt(TU, filename, line, column,
1942 unsaved_files, num_unsaved_files,
1943 completionOptions);
1944 if (!results) {
1945 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001946 return 1;
1947 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001948 if (I != Repeats-1)
1949 clang_disposeCodeCompleteResults(results);
1950 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001951
Douglas Gregorec6762c2009-12-18 16:20:58 +00001952 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001953 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001954 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001955 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001956 CXString objCSelector;
1957 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001958 if (!timing_only) {
1959 /* Sort the code-completion results based on the typed text. */
1960 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1961
Douglas Gregor1982c182010-07-12 18:38:41 +00001962 for (i = 0; i != n; ++i)
1963 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001964 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001965 n = clang_codeCompleteGetNumDiagnostics(results);
1966 for (i = 0; i != n; ++i) {
1967 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1968 PrintDiagnostic(diag);
1969 clang_disposeDiagnostic(diag);
1970 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001971
1972 contexts = clang_codeCompleteGetContexts(results);
1973 print_completion_contexts(contexts, stdout);
1974
Douglas Gregor0a47d692011-07-26 15:24:30 +00001975 containerKind = clang_codeCompleteGetContainerKind(results,
1976 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001977
1978 if (containerKind != CXCursor_InvalidCode) {
1979 /* We have found a container */
1980 CXString containerUSR, containerKindSpelling;
1981 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1982 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1983 clang_disposeString(containerKindSpelling);
1984
1985 if (containerIsIncomplete) {
1986 printf("Container is incomplete\n");
1987 }
1988 else {
1989 printf("Container is complete\n");
1990 }
1991
1992 containerUSR = clang_codeCompleteGetContainerUSR(results);
1993 printf("Container USR: %s\n", clang_getCString(containerUSR));
1994 clang_disposeString(containerUSR);
1995 }
1996
Douglas Gregor0a47d692011-07-26 15:24:30 +00001997 objCSelector = clang_codeCompleteGetObjCSelector(results);
1998 selectorString = clang_getCString(objCSelector);
1999 if (selectorString && strlen(selectorString) > 0) {
2000 printf("Objective-C selector: %s\n", selectorString);
2001 }
2002 clang_disposeString(objCSelector);
2003
Douglas Gregorec6762c2009-12-18 16:20:58 +00002004 clang_disposeCodeCompleteResults(results);
2005 }
Douglas Gregordf95a132010-08-09 20:45:32 +00002006 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002007 clang_disposeIndex(CIdx);
2008 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00002009
Douglas Gregor735df882009-12-02 09:21:34 +00002010 free_remapped_files(unsaved_files, num_unsaved_files);
2011
Ted Kremenekf5d9c932009-11-17 18:09:14 +00002012 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00002013}
2014
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002015typedef struct {
2016 char *filename;
2017 unsigned line;
2018 unsigned column;
2019} CursorSourceLocation;
2020
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002021static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002022 CXIndex CIdx;
2023 int errorCode;
2024 struct CXUnsavedFile *unsaved_files = 0;
2025 int num_unsaved_files = 0;
2026 CXTranslationUnit TU;
2027 CXCursor Cursor;
2028 CursorSourceLocation *Locations = 0;
2029 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002030 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00002031 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002032
Ted Kremeneke68fff62010-02-17 00:41:32 +00002033 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002034 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2035 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002036
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002037 /* Parse the locations. */
2038 assert(NumLocations > 0 && "Unable to count locations?");
2039 Locations = (CursorSourceLocation *)malloc(
2040 NumLocations * sizeof(CursorSourceLocation));
2041 for (Loc = 0; Loc < NumLocations; ++Loc) {
2042 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002043 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2044 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002045 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002046 return errorCode;
2047 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002048
2049 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002050 &num_unsaved_files))
2051 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002052
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002053 if (getenv("CINDEXTEST_EDITING"))
2054 Repeats = 5;
2055
2056 /* Parse the translation unit. When we're testing clang_getCursor() after
2057 reparsing, don't remap unsaved files until the second parse. */
2058 CIdx = clang_createIndex(1, 1);
2059 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2060 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002061 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002062 unsaved_files,
2063 Repeats > 1? 0 : num_unsaved_files,
2064 getDefaultParsingOptions());
2065
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002066 if (!TU) {
2067 fprintf(stderr, "unable to parse input\n");
2068 return -1;
2069 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002070
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002071 if (checkForErrors(TU) != 0)
2072 return -1;
2073
Douglas Gregorbdc4b362010-11-30 06:04:54 +00002074 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002075 if (Repeats > 1 &&
2076 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2077 clang_defaultReparseOptions(TU))) {
2078 clang_disposeTranslationUnit(TU);
2079 return 1;
2080 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002081
2082 if (checkForErrors(TU) != 0)
2083 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002084
2085 for (Loc = 0; Loc < NumLocations; ++Loc) {
2086 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2087 if (!file)
2088 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002089
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002090 Cursor = clang_getCursor(TU,
2091 clang_getLocation(TU, file, Locations[Loc].line,
2092 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002093
2094 if (checkForErrors(TU) != 0)
2095 return -1;
2096
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002097 if (I + 1 == Repeats) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002098 CXCompletionString completionString;
2099 CXSourceLocation CursorLoc;
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002100 CXString Spelling;
2101 const char *cspell;
2102 unsigned line, column;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002103 completionString = clang_getCursorCompletionString(Cursor);
2104 CursorLoc = clang_getCursorLocation(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002105 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2106 printf("%d:%d ", line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002107 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002108 PrintCursorExtent(Cursor);
2109 Spelling = clang_getCursorSpelling(Cursor);
2110 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002111 if (cspell && strlen(cspell) != 0) {
2112 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002113 printf(" Spelling=%s (", cspell);
2114 for (pieceIndex = 0; ; ++pieceIndex) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002115 CXSourceRange range;
2116 range = clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002117 if (clang_Range_isNull(range))
2118 break;
2119 PrintRange(range, 0);
2120 }
2121 printf(")");
2122 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002123 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00002124 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2125 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00002126 if (clang_Cursor_isDynamicCall(Cursor))
2127 printf(" Dynamic-call");
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00002128 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002129 CXType T;
2130 CXString S;
2131 T = clang_Cursor_getReceiverType(Cursor);
2132 S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00002133 printf(" Receiver-type=%s", clang_getCString(S));
2134 clang_disposeString(S);
2135 }
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00002136
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002137 {
2138 CXModule mod = clang_Cursor_getModule(Cursor);
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002139 CXFile astFile;
2140 CXString name, astFilename;
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002141 unsigned i, numHeaders;
2142 if (mod) {
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002143 astFile = clang_Module_getASTFile(mod);
2144 astFilename = clang_getFileName(astFile);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002145 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002146 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002147 printf(" ModuleName=%s (%s) Headers(%d):",
2148 clang_getCString(name), clang_getCString(astFilename),
2149 numHeaders);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002150 clang_disposeString(name);
Argyrios Kyrtzidise858e662013-04-26 22:47:49 +00002151 clang_disposeString(astFilename);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002152 for (i = 0; i < numHeaders; ++i) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002153 CXFile file;
2154 CXString filename;
2155 file = clang_Module_getTopLevelHeader(TU, mod, i);
2156 filename = clang_getFileName(file);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002157 printf("\n%s", clang_getCString(filename));
2158 clang_disposeString(filename);
2159 }
2160 }
2161 }
2162
Douglas Gregor8fa0a802011-08-04 20:04:59 +00002163 if (completionString != NULL) {
2164 printf("\nCompletion string: ");
2165 print_completion_string(completionString, stdout);
2166 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002167 printf("\n");
2168 free(Locations[Loc].filename);
2169 }
2170 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002171 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002172
Douglas Gregora88084b2010-02-18 18:08:43 +00002173 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002174 clang_disposeTranslationUnit(TU);
2175 clang_disposeIndex(CIdx);
2176 free(Locations);
2177 free_remapped_files(unsaved_files, num_unsaved_files);
2178 return 0;
2179}
2180
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002181static enum CXVisitorResult findFileRefsVisit(void *context,
2182 CXCursor cursor, CXSourceRange range) {
2183 if (clang_Range_isNull(range))
2184 return CXVisit_Continue;
2185
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002186 PrintCursor(cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002187 PrintRange(range, "");
2188 printf("\n");
2189 return CXVisit_Continue;
2190}
2191
2192static int find_file_refs_at(int argc, const char **argv) {
2193 CXIndex CIdx;
2194 int errorCode;
2195 struct CXUnsavedFile *unsaved_files = 0;
2196 int num_unsaved_files = 0;
2197 CXTranslationUnit TU;
2198 CXCursor Cursor;
2199 CursorSourceLocation *Locations = 0;
2200 unsigned NumLocations = 0, Loc;
2201 unsigned Repeats = 1;
2202 unsigned I;
2203
2204 /* Count the number of locations. */
2205 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2206 ++NumLocations;
2207
2208 /* Parse the locations. */
2209 assert(NumLocations > 0 && "Unable to count locations?");
2210 Locations = (CursorSourceLocation *)malloc(
2211 NumLocations * sizeof(CursorSourceLocation));
2212 for (Loc = 0; Loc < NumLocations; ++Loc) {
2213 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2214 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2215 &Locations[Loc].line,
2216 &Locations[Loc].column, 0, 0)))
2217 return errorCode;
2218 }
2219
2220 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2221 &num_unsaved_files))
2222 return -1;
2223
2224 if (getenv("CINDEXTEST_EDITING"))
2225 Repeats = 5;
2226
2227 /* Parse the translation unit. When we're testing clang_getCursor() after
2228 reparsing, don't remap unsaved files until the second parse. */
2229 CIdx = clang_createIndex(1, 1);
2230 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2231 argv + num_unsaved_files + 1 + NumLocations,
2232 argc - num_unsaved_files - 2 - NumLocations,
2233 unsaved_files,
2234 Repeats > 1? 0 : num_unsaved_files,
2235 getDefaultParsingOptions());
2236
2237 if (!TU) {
2238 fprintf(stderr, "unable to parse input\n");
2239 return -1;
2240 }
2241
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002242 if (checkForErrors(TU) != 0)
2243 return -1;
2244
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002245 for (I = 0; I != Repeats; ++I) {
2246 if (Repeats > 1 &&
2247 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2248 clang_defaultReparseOptions(TU))) {
2249 clang_disposeTranslationUnit(TU);
2250 return 1;
2251 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002252
2253 if (checkForErrors(TU) != 0)
2254 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002255
2256 for (Loc = 0; Loc < NumLocations; ++Loc) {
2257 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2258 if (!file)
2259 continue;
2260
2261 Cursor = clang_getCursor(TU,
2262 clang_getLocation(TU, file, Locations[Loc].line,
2263 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002264
2265 if (checkForErrors(TU) != 0)
2266 return -1;
2267
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002268 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002269 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002270 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002271 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002272 clang_findReferencesInFile(Cursor, file, visitor);
2273 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002274
2275 if (checkForErrors(TU) != 0)
2276 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002277 }
2278 }
2279 }
2280
2281 PrintDiagnostics(TU);
2282 clang_disposeTranslationUnit(TU);
2283 clang_disposeIndex(CIdx);
2284 free(Locations);
2285 free_remapped_files(unsaved_files, num_unsaved_files);
2286 return 0;
2287}
2288
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00002289static enum CXVisitorResult findFileIncludesVisit(void *context,
2290 CXCursor cursor, CXSourceRange range) {
2291 PrintCursor(cursor, NULL);
2292 PrintRange(range, "");
2293 printf("\n");
2294 return CXVisit_Continue;
2295}
2296
2297static int find_file_includes_in(int argc, const char **argv) {
2298 CXIndex CIdx;
2299 struct CXUnsavedFile *unsaved_files = 0;
2300 int num_unsaved_files = 0;
2301 CXTranslationUnit TU;
2302 const char **Filenames = 0;
2303 unsigned NumFilenames = 0;
2304 unsigned Repeats = 1;
2305 unsigned I, FI;
2306
2307 /* Count the number of locations. */
2308 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2309 ++NumFilenames;
2310
2311 /* Parse the locations. */
2312 assert(NumFilenames > 0 && "Unable to count filenames?");
2313 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2314 for (I = 0; I < NumFilenames; ++I) {
2315 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2316 /* Copy the file name. */
2317 Filenames[I] = input;
2318 }
2319
2320 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2321 &num_unsaved_files))
2322 return -1;
2323
2324 if (getenv("CINDEXTEST_EDITING"))
2325 Repeats = 2;
2326
2327 /* Parse the translation unit. When we're testing clang_getCursor() after
2328 reparsing, don't remap unsaved files until the second parse. */
2329 CIdx = clang_createIndex(1, 1);
2330 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2331 argv + num_unsaved_files + 1 + NumFilenames,
2332 argc - num_unsaved_files - 2 - NumFilenames,
2333 unsaved_files,
2334 Repeats > 1? 0 : num_unsaved_files,
2335 getDefaultParsingOptions());
2336
2337 if (!TU) {
2338 fprintf(stderr, "unable to parse input\n");
2339 return -1;
2340 }
2341
2342 if (checkForErrors(TU) != 0)
2343 return -1;
2344
2345 for (I = 0; I != Repeats; ++I) {
2346 if (Repeats > 1 &&
2347 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2348 clang_defaultReparseOptions(TU))) {
2349 clang_disposeTranslationUnit(TU);
2350 return 1;
2351 }
2352
2353 if (checkForErrors(TU) != 0)
2354 return -1;
2355
2356 for (FI = 0; FI < NumFilenames; ++FI) {
2357 CXFile file = clang_getFile(TU, Filenames[FI]);
2358 if (!file)
2359 continue;
2360
2361 if (checkForErrors(TU) != 0)
2362 return -1;
2363
2364 if (I + 1 == Repeats) {
2365 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2366 clang_findIncludesInFile(TU, file, visitor);
2367
2368 if (checkForErrors(TU) != 0)
2369 return -1;
2370 }
2371 }
2372 }
2373
2374 PrintDiagnostics(TU);
2375 clang_disposeTranslationUnit(TU);
2376 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis5256c1f2013-03-11 16:03:17 +00002377 free((void *)Filenames);
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00002378 free_remapped_files(unsaved_files, num_unsaved_files);
2379 return 0;
2380}
2381
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002382#define MAX_IMPORTED_ASTFILES 200
2383
2384typedef struct {
2385 char **filenames;
2386 unsigned num_files;
2387} ImportedASTFilesData;
2388
2389static ImportedASTFilesData *importedASTs_create() {
2390 ImportedASTFilesData *p;
2391 p = malloc(sizeof(ImportedASTFilesData));
2392 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2393 p->num_files = 0;
2394 return p;
2395}
2396
2397static void importedASTs_dispose(ImportedASTFilesData *p) {
2398 unsigned i;
2399 if (!p)
2400 return;
2401
2402 for (i = 0; i < p->num_files; ++i)
2403 free(p->filenames[i]);
2404 free(p->filenames);
2405 free(p);
2406}
2407
2408static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2409 unsigned i;
2410 assert(p && file);
2411 for (i = 0; i < p->num_files; ++i)
2412 if (strcmp(file, p->filenames[i]) == 0)
2413 return;
2414 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2415 p->filenames[p->num_files++] = strdup(file);
2416}
2417
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002418typedef struct {
2419 const char *check_prefix;
2420 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002421 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002422 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002423 const char *main_filename;
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002424 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002425} IndexData;
2426
2427static void printCheck(IndexData *data) {
2428 if (data->check_prefix) {
2429 if (data->first_check_printed) {
2430 printf("// %s-NEXT: ", data->check_prefix);
2431 } else {
2432 printf("// %s : ", data->check_prefix);
2433 data->first_check_printed = 1;
2434 }
2435 }
2436}
2437
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002438static void printCXIndexFile(CXIdxClientFile file) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002439 CXString filename;
2440 filename = clang_getFileName((CXFile)file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002441 printf("%s", clang_getCString(filename));
2442 clang_disposeString(filename);
2443}
2444
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002445static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2446 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002447 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002448 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002449 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002450 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002451 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002452
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002453 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002454 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2455 if (line == 0) {
Argyrios Kyrtzidis8003fd62012-10-11 19:00:44 +00002456 printf("<invalid>");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002457 return;
2458 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002459 if (!file) {
2460 printf("<no idxfile>");
2461 return;
2462 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002463 filename = clang_getFileName((CXFile)file);
2464 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002465 if (strcmp(cname, index_data->main_filename) == 0)
2466 isMainFile = 1;
2467 else
2468 isMainFile = 0;
2469 clang_disposeString(filename);
2470
2471 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002472 printCXIndexFile(file);
2473 printf(":");
2474 }
2475 printf("%d:%d", line, column);
2476}
2477
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002478static unsigned digitCount(unsigned val) {
2479 unsigned c = 1;
2480 while (1) {
2481 if (val < 10)
2482 return c;
2483 ++c;
2484 val /= 10;
2485 }
2486}
2487
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002488static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2489 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002490 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002491 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002492 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002493 unsigned line, column;
2494
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002495 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002496 if (!name)
2497 name = "<anon-tag>";
2498
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002499 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002500 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002501 newStr = (char *)malloc(strlen(name) +
2502 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002503 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002504 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002505}
2506
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002507static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2508 CXIdxClientContainer container;
2509 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002510 if (!container)
2511 printf("[<<NULL>>]");
2512 else
2513 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002514}
2515
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002516static const char *getEntityKindString(CXIdxEntityKind kind) {
2517 switch (kind) {
2518 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2519 case CXIdxEntity_Typedef: return "typedef";
2520 case CXIdxEntity_Function: return "function";
2521 case CXIdxEntity_Variable: return "variable";
2522 case CXIdxEntity_Field: return "field";
2523 case CXIdxEntity_EnumConstant: return "enumerator";
2524 case CXIdxEntity_ObjCClass: return "objc-class";
2525 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2526 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002527 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2528 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002529 case CXIdxEntity_ObjCProperty: return "objc-property";
2530 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2531 case CXIdxEntity_Enum: return "enum";
2532 case CXIdxEntity_Struct: return "struct";
2533 case CXIdxEntity_Union: return "union";
2534 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002535 case CXIdxEntity_CXXNamespace: return "namespace";
2536 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2537 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2538 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2539 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2540 case CXIdxEntity_CXXConstructor: return "constructor";
2541 case CXIdxEntity_CXXDestructor: return "destructor";
2542 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2543 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikie35adca02012-08-31 21:55:26 +00002544 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002545 }
2546 assert(0 && "Garbage entity kind");
2547 return 0;
2548}
2549
2550static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2551 switch (kind) {
2552 case CXIdxEntity_NonTemplate: return "";
2553 case CXIdxEntity_Template: return "-template";
2554 case CXIdxEntity_TemplatePartialSpecialization:
2555 return "-template-partial-spec";
2556 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002557 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002558 assert(0 && "Garbage entity kind");
2559 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002560}
2561
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002562static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2563 switch (kind) {
2564 case CXIdxEntityLang_None: return "<none>";
2565 case CXIdxEntityLang_C: return "C";
2566 case CXIdxEntityLang_ObjC: return "ObjC";
2567 case CXIdxEntityLang_CXX: return "C++";
2568 }
2569 assert(0 && "Garbage language kind");
2570 return 0;
2571}
2572
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002573static void printEntityInfo(const char *cb,
2574 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002575 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002576 const char *name;
2577 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002578 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002579 index_data = (IndexData *)client_data;
2580 printCheck(index_data);
2581
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002582 if (!info) {
2583 printf("%s: <<NULL>>", cb);
2584 return;
2585 }
2586
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002587 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002588 if (!name)
2589 name = "<anon-tag>";
2590
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002591 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2592 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002593 printf(" | name: %s", name);
2594 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002595 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002596
2597 for (i = 0; i != info->numAttributes; ++i) {
2598 const CXIdxAttrInfo *Attr = info->attributes[i];
2599 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002600 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002601 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002602}
2603
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002604static void printBaseClassInfo(CXClientData client_data,
2605 const CXIdxBaseClassInfo *info) {
2606 printEntityInfo(" <base>", client_data, info->base);
2607 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002608 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002609 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002610 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002611}
2612
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002613static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2614 CXClientData client_data) {
2615 unsigned i;
2616 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2617 printEntityInfo(" <protocol>", client_data,
2618 ProtoInfo->protocols[i]->protocol);
2619 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002620 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002621 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002622 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002623 printf("\n");
2624 }
2625}
2626
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002627static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002628 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002629 CXString str;
2630 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002631 unsigned numDiags, i;
2632 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002633 IndexData *index_data;
2634 index_data = (IndexData *)client_data;
2635 printCheck(index_data);
2636
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002637 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2638 for (i = 0; i != numDiags; ++i) {
2639 diag = clang_getDiagnosticInSet(diagSet, i);
2640 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2641 cstr = clang_getCString(str);
2642 printf("[diagnostic]: %s\n", cstr);
2643 clang_disposeString(str);
2644
2645 if (getenv("CINDEXTEST_FAILONERROR") &&
2646 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2647 index_data->fail_for_error = 1;
2648 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002649 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002650}
2651
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002652static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2653 CXFile file, void *reserved) {
2654 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002655 CXString filename;
2656
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002657 index_data = (IndexData *)client_data;
2658 printCheck(index_data);
2659
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002660 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002661 index_data->main_filename = clang_getCString(filename);
2662 clang_disposeString(filename);
2663
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002664 printf("[enteredMainFile]: ");
2665 printCXIndexFile((CXIdxClientFile)file);
2666 printf("\n");
2667
2668 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002669}
2670
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002671static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002672 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002673 IndexData *index_data;
2674 index_data = (IndexData *)client_data;
2675 printCheck(index_data);
2676
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002677 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002678 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002679 printf(" | name: \"%s\"", info->filename);
2680 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002681 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis8d7a24e2012-10-18 00:17:05 +00002682 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2683 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002684
2685 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002686}
2687
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002688static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2689 const CXIdxImportedASTFileInfo *info) {
2690 IndexData *index_data;
2691 index_data = (IndexData *)client_data;
2692 printCheck(index_data);
2693
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002694 if (index_data->importedASTs) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002695 CXString filename;
2696 filename = clang_getFileName(info->file);
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002697 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2698 clang_disposeString(filename);
2699 }
2700
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002701 printf("[importedASTFile]: ");
2702 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002703 if (info->module) {
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00002704 CXString name;
2705 name = clang_Module_getFullName(info->module);
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002706 printf(" | loc: ");
2707 printCXIndexLoc(info->loc, client_data);
2708 printf(" | name: \"%s\"", clang_getCString(name));
2709 printf(" | isImplicit: %d\n", info->isImplicit);
2710 clang_disposeString(name);
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002711 } else {
NAKAMURA Takumi3c5527e2012-10-12 14:25:52 +00002712 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002713 printf("\n");
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002714 }
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002715
2716 return (CXIdxClientFile)info->file;
2717}
2718
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002719static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002720 void *reserved) {
2721 IndexData *index_data;
2722 index_data = (IndexData *)client_data;
2723 printCheck(index_data);
2724
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002725 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002726 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002727}
2728
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002729static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002730 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002731 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002732 const CXIdxObjCCategoryDeclInfo *CatInfo;
2733 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002734 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002735 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002736 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002737 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002738 index_data = (IndexData *)client_data;
2739
2740 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2741 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002742 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002743 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002744 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002745 printf(" | semantic-container: ");
2746 printCXIndexContainer(info->semanticContainer);
2747 printf(" | lexical-container: ");
2748 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002749 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002750 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis838eb7e2012-12-06 19:41:16 +00002751 if (info->flags & CXIdxDeclFlag_Skipped) {
2752 assert(!info->isContainer);
2753 printf(" | isContainer: skipped");
2754 } else {
2755 printf(" | isContainer: %d", info->isContainer);
2756 }
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002757 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002758
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002759 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002760 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002761 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002762 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002763 printf("\n");
2764 }
2765
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002766 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2767 const char *kindName = 0;
2768 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2769 switch (K) {
2770 case CXIdxObjCContainer_ForwardRef:
2771 kindName = "forward-ref"; break;
2772 case CXIdxObjCContainer_Interface:
2773 kindName = "interface"; break;
2774 case CXIdxObjCContainer_Implementation:
2775 kindName = "implementation"; break;
2776 }
2777 printCheck(index_data);
2778 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2779 }
2780
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002781 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002782 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2783 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002784 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002785 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002786 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002787 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002788 printf("\n");
2789 }
2790
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002791 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2792 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002793 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002794 printf("\n");
2795 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002796 }
2797
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002798 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2799 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002800 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002801
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002802 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2803 if (PropInfo->getter) {
2804 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2805 printf("\n");
2806 }
2807 if (PropInfo->setter) {
2808 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2809 printf("\n");
2810 }
2811 }
2812
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002813 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2814 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2815 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2816 printf("\n");
2817 }
2818 }
2819
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002820 if (info->declAsContainer)
2821 clang_index_setClientContainer(info->declAsContainer,
2822 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002823}
2824
2825static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002826 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002827 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002828 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002829 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002830 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002831 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002832 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002833 printf(" | container: ");
2834 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002835 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002836 switch (info->kind) {
2837 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002838 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002839 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002840 printf("\n");
2841}
2842
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002843static int index_abortQuery(CXClientData client_data, void *reserved) {
2844 IndexData *index_data;
2845 index_data = (IndexData *)client_data;
2846 return index_data->abort;
2847}
2848
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002849static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002850 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002851 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002852 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002853 index_ppIncludedFile,
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002854 index_importedASTFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002855 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002856 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002857 index_indexEntityReference
2858};
2859
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002860static unsigned getIndexOptions(void) {
2861 unsigned index_opts;
2862 index_opts = 0;
2863 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2864 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2865 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2866 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis838eb7e2012-12-06 19:41:16 +00002867 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
2868 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002869
2870 return index_opts;
2871}
2872
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002873static int index_compile_args(int num_args, const char **args,
2874 CXIndexAction idxAction,
2875 ImportedASTFilesData *importedASTs,
2876 const char *check_prefix) {
2877 IndexData index_data;
2878 unsigned index_opts;
2879 int result;
2880
2881 if (num_args == 0) {
2882 fprintf(stderr, "no compiler arguments\n");
2883 return -1;
2884 }
2885
2886 index_data.check_prefix = check_prefix;
2887 index_data.first_check_printed = 0;
2888 index_data.fail_for_error = 0;
2889 index_data.abort = 0;
2890 index_data.main_filename = "";
2891 index_data.importedASTs = importedASTs;
2892
2893 index_opts = getIndexOptions();
2894 result = clang_indexSourceFile(idxAction, &index_data,
2895 &IndexCB,sizeof(IndexCB), index_opts,
2896 0, args, num_args, 0, 0, 0,
2897 getDefaultParsingOptions());
2898 if (index_data.fail_for_error)
2899 result = -1;
2900
2901 return result;
2902}
2903
2904static int index_ast_file(const char *ast_file,
2905 CXIndex Idx,
2906 CXIndexAction idxAction,
2907 ImportedASTFilesData *importedASTs,
2908 const char *check_prefix) {
2909 CXTranslationUnit TU;
2910 IndexData index_data;
2911 unsigned index_opts;
2912 int result;
2913
2914 if (!CreateTranslationUnit(Idx, ast_file, &TU))
2915 return -1;
2916
2917 index_data.check_prefix = check_prefix;
2918 index_data.first_check_printed = 0;
2919 index_data.fail_for_error = 0;
2920 index_data.abort = 0;
2921 index_data.main_filename = "";
2922 index_data.importedASTs = importedASTs;
2923
2924 index_opts = getIndexOptions();
2925 result = clang_indexTranslationUnit(idxAction, &index_data,
2926 &IndexCB,sizeof(IndexCB),
2927 index_opts, TU);
2928 if (index_data.fail_for_error)
2929 result = -1;
2930
2931 clang_disposeTranslationUnit(TU);
2932 return result;
2933}
2934
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002935static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002936 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002937 CXIndex Idx;
2938 CXIndexAction idxAction;
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002939 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002940 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002941
2942 check_prefix = 0;
2943 if (argc > 0) {
2944 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2945 check_prefix = argv[0] + strlen("-check-prefix=");
2946 ++argv;
2947 --argc;
2948 }
2949 }
2950
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002951 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002952 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002953 fprintf(stderr, "Could not create Index\n");
2954 return 1;
2955 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002956 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002957 importedASTs = 0;
2958 if (full)
2959 importedASTs = importedASTs_create();
2960
2961 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
2962 if (result != 0)
2963 goto finished;
2964
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002965 if (full) {
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002966 unsigned i;
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002967 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
2968 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
2969 importedASTs, check_prefix);
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002970 }
2971 }
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002972
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002973finished:
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002974 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002975 clang_IndexAction_dispose(idxAction);
2976 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002977 return result;
2978}
2979
2980static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002981 const char *check_prefix;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002982 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002983 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002984 int result;
2985
2986 check_prefix = 0;
2987 if (argc > 0) {
2988 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2989 check_prefix = argv[0] + strlen("-check-prefix=");
2990 ++argv;
2991 --argc;
2992 }
2993 }
2994
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002995 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002996 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002997 fprintf(stderr, "Could not create Index\n");
2998 return 1;
2999 }
3000 idxAction = clang_IndexAction_create(Idx);
3001
3002 result = index_ast_file(argv[0], Idx, idxAction,
3003 /*importedASTs=*/0, check_prefix);
3004
3005 clang_IndexAction_dispose(idxAction);
3006 clang_disposeIndex(Idx);
3007 return result;
3008}
3009
3010static int index_compile_db(int argc, const char **argv) {
3011 const char *check_prefix;
3012 CXIndex Idx;
3013 CXIndexAction idxAction;
3014 int errorCode = 0;
3015
3016 check_prefix = 0;
3017 if (argc > 0) {
3018 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
3019 check_prefix = argv[0] + strlen("-check-prefix=");
3020 ++argv;
3021 --argc;
3022 }
3023 }
3024
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003025 if (argc == 0) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003026 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003027 return -1;
3028 }
3029
3030 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00003031 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003032 fprintf(stderr, "Could not create Index\n");
3033 return 1;
3034 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003035 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003036
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003037 {
3038 const char *database = argv[0];
3039 CXCompilationDatabase db = 0;
3040 CXCompileCommands CCmds = 0;
3041 CXCompileCommand CCmd;
3042 CXCompilationDatabase_Error ec;
3043 CXString wd;
3044#define MAX_COMPILE_ARGS 512
3045 CXString cxargs[MAX_COMPILE_ARGS];
3046 const char *args[MAX_COMPILE_ARGS];
3047 char *tmp;
3048 unsigned len;
3049 char *buildDir;
3050 int i, a, numCmds, numArgs;
3051
3052 len = strlen(database);
3053 tmp = (char *) malloc(len+1);
3054 memcpy(tmp, database, len+1);
3055 buildDir = dirname(tmp);
3056
3057 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3058
3059 if (db) {
3060
3061 if (ec!=CXCompilationDatabase_NoError) {
3062 printf("unexpected error %d code while loading compilation database\n", ec);
3063 errorCode = -1;
3064 goto cdb_end;
3065 }
3066
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003067 if (chdir(buildDir) != 0) {
3068 printf("Could not chdir to %s\n", buildDir);
3069 errorCode = -1;
3070 goto cdb_end;
3071 }
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003072
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003073 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003074 if (!CCmds) {
3075 printf("compilation db is empty\n");
3076 errorCode = -1;
3077 goto cdb_end;
3078 }
3079
3080 numCmds = clang_CompileCommands_getSize(CCmds);
3081
3082 if (numCmds==0) {
3083 fprintf(stderr, "should not get an empty compileCommand set\n");
3084 errorCode = -1;
3085 goto cdb_end;
3086 }
3087
3088 for (i=0; i<numCmds && errorCode == 0; ++i) {
3089 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3090
3091 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003092 if (chdir(clang_getCString(wd)) != 0) {
3093 printf("Could not chdir to %s\n", clang_getCString(wd));
3094 errorCode = -1;
3095 goto cdb_end;
3096 }
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003097 clang_disposeString(wd);
3098
3099 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3100 if (numArgs > MAX_COMPILE_ARGS){
3101 fprintf(stderr, "got more compile arguments than maximum\n");
3102 errorCode = -1;
3103 goto cdb_end;
3104 }
3105 for (a=0; a<numArgs; ++a) {
3106 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3107 args[a] = clang_getCString(cxargs[a]);
3108 }
3109
3110 errorCode = index_compile_args(numArgs, args, idxAction,
3111 /*importedASTs=*/0, check_prefix);
3112
3113 for (a=0; a<numArgs; ++a)
3114 clang_disposeString(cxargs[a]);
3115 }
3116 } else {
3117 printf("database loading failed with error code %d.\n", ec);
3118 errorCode = -1;
3119 }
3120
3121 cdb_end:
3122 clang_CompileCommands_dispose(CCmds);
3123 clang_CompilationDatabase_dispose(db);
3124 free(tmp);
3125
3126 }
3127
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003128 clang_IndexAction_dispose(idxAction);
3129 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003130 return errorCode;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003131}
3132
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003133int perform_token_annotation(int argc, const char **argv) {
3134 const char *input = argv[1];
3135 char *filename = 0;
3136 unsigned line, second_line;
3137 unsigned column, second_column;
3138 CXIndex CIdx;
3139 CXTranslationUnit TU = 0;
3140 int errorCode;
3141 struct CXUnsavedFile *unsaved_files = 0;
3142 int num_unsaved_files = 0;
3143 CXToken *tokens;
3144 unsigned num_tokens;
3145 CXSourceRange range;
3146 CXSourceLocation startLoc, endLoc;
3147 CXFile file = 0;
3148 CXCursor *cursors = 0;
3149 unsigned i;
3150
3151 input += strlen("-test-annotate-tokens=");
3152 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3153 &second_line, &second_column)))
3154 return errorCode;
3155
Richard Smithe07c5f82012-07-05 08:20:49 +00003156 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3157 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003158 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00003159 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003160
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003161 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00003162 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
3163 argv + num_unsaved_files + 2,
3164 argc - num_unsaved_files - 3,
3165 unsaved_files,
3166 num_unsaved_files,
3167 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003168 if (!TU) {
3169 fprintf(stderr, "unable to parse input\n");
3170 clang_disposeIndex(CIdx);
3171 free(filename);
3172 free_remapped_files(unsaved_files, num_unsaved_files);
3173 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003174 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003175 errorCode = 0;
3176
Richard Smithe07c5f82012-07-05 08:20:49 +00003177 if (checkForErrors(TU) != 0) {
3178 errorCode = -1;
3179 goto teardown;
3180 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003181
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00003182 if (getenv("CINDEXTEST_EDITING")) {
3183 for (i = 0; i < 5; ++i) {
3184 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3185 clang_defaultReparseOptions(TU))) {
3186 fprintf(stderr, "Unable to reparse translation unit!\n");
3187 errorCode = -1;
3188 goto teardown;
3189 }
3190 }
3191 }
3192
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003193 if (checkForErrors(TU) != 0) {
3194 errorCode = -1;
3195 goto teardown;
3196 }
3197
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003198 file = clang_getFile(TU, filename);
3199 if (!file) {
3200 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3201 errorCode = -1;
3202 goto teardown;
3203 }
3204
3205 startLoc = clang_getLocation(TU, file, line, column);
3206 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003207 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003208 column);
3209 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003210 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003211 }
3212
3213 endLoc = clang_getLocation(TU, file, second_line, second_column);
3214 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003215 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003216 second_line, second_column);
3217 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003218 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003219 }
3220
3221 range = clang_getRange(startLoc, endLoc);
3222 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003223
3224 if (checkForErrors(TU) != 0) {
3225 errorCode = -1;
3226 goto teardown;
3227 }
3228
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003229 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3230 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003231
3232 if (checkForErrors(TU) != 0) {
3233 errorCode = -1;
3234 goto teardown;
3235 }
3236
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003237 for (i = 0; i != num_tokens; ++i) {
3238 const char *kind = "<unknown>";
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00003239 CXString spelling;
3240 CXSourceRange extent;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003241 unsigned start_line, start_column, end_line, end_column;
3242
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00003243 spelling = clang_getTokenSpelling(TU, tokens[i]);
3244 extent = clang_getTokenExtent(TU, tokens[i]);
3245
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003246 switch (clang_getTokenKind(tokens[i])) {
3247 case CXToken_Punctuation: kind = "Punctuation"; break;
3248 case CXToken_Keyword: kind = "Keyword"; break;
3249 case CXToken_Identifier: kind = "Identifier"; break;
3250 case CXToken_Literal: kind = "Literal"; break;
3251 case CXToken_Comment: kind = "Comment"; break;
3252 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00003253 clang_getSpellingLocation(clang_getRangeStart(extent),
3254 0, &start_line, &start_column, 0);
3255 clang_getSpellingLocation(clang_getRangeEnd(extent),
3256 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00003257 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00003258 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00003259 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003260 if (!clang_isInvalid(cursors[i].kind)) {
3261 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003262 PrintCursor(cursors[i], NULL);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003263 }
3264 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003265 }
3266 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00003267 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003268
3269 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00003270 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003271 clang_disposeTranslationUnit(TU);
3272 clang_disposeIndex(CIdx);
3273 free(filename);
3274 free_remapped_files(unsaved_files, num_unsaved_files);
3275 return errorCode;
3276}
3277
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003278static int
3279perform_test_compilation_db(const char *database, int argc, const char **argv) {
3280 CXCompilationDatabase db;
3281 CXCompileCommands CCmds;
3282 CXCompileCommand CCmd;
3283 CXCompilationDatabase_Error ec;
3284 CXString wd;
3285 CXString arg;
3286 int errorCode = 0;
3287 char *tmp;
3288 unsigned len;
3289 char *buildDir;
3290 int i, j, a, numCmds, numArgs;
3291
3292 len = strlen(database);
3293 tmp = (char *) malloc(len+1);
3294 memcpy(tmp, database, len+1);
3295 buildDir = dirname(tmp);
3296
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003297 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003298
3299 if (db) {
3300
3301 if (ec!=CXCompilationDatabase_NoError) {
3302 printf("unexpected error %d code while loading compilation database\n", ec);
3303 errorCode = -1;
3304 goto cdb_end;
3305 }
3306
3307 for (i=0; i<argc && errorCode==0; ) {
3308 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003309 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003310
3311 if (!CCmds) {
3312 printf("file %s not found in compilation db\n", argv[i+1]);
3313 errorCode = -1;
3314 break;
3315 }
3316
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003317 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003318
3319 if (numCmds==0) {
3320 fprintf(stderr, "should not get an empty compileCommand set for file"
3321 " '%s'\n", argv[i+1]);
3322 errorCode = -1;
3323 break;
3324 }
3325
3326 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003327 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003328
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003329 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003330 printf("workdir:'%s'", clang_getCString(wd));
3331 clang_disposeString(wd);
3332
3333 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003334 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003335 for (a=0; a<numArgs; ++a) {
3336 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003337 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003338 printf("%s", clang_getCString(arg));
3339 clang_disposeString(arg);
3340 }
3341 printf("'\n");
3342 }
3343
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003344 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003345
3346 i += 2;
3347 }
3348 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003349 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003350 } else {
3351 printf("database loading failed with error code %d.\n", ec);
3352 errorCode = -1;
3353 }
3354
3355cdb_end:
3356 free(tmp);
3357
3358 return errorCode;
3359}
3360
Ted Kremenek0d435192009-11-17 18:13:31 +00003361/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003362/* USR printing. */
3363/******************************************************************************/
3364
3365static int insufficient_usr(const char *kind, const char *usage) {
3366 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3367 return 1;
3368}
3369
3370static unsigned isUSR(const char *s) {
3371 return s[0] == 'c' && s[1] == ':';
3372}
3373
3374static int not_usr(const char *s, const char *arg) {
3375 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3376 return 1;
3377}
3378
3379static void print_usr(CXString usr) {
3380 const char *s = clang_getCString(usr);
3381 printf("%s\n", s);
3382 clang_disposeString(usr);
3383}
3384
3385static void display_usrs() {
3386 fprintf(stderr, "-print-usrs options:\n"
3387 " ObjCCategory <class name> <category name>\n"
3388 " ObjCClass <class name>\n"
3389 " ObjCIvar <ivar name> <class USR>\n"
3390 " ObjCMethod <selector> [0=class method|1=instance method] "
3391 "<class USR>\n"
3392 " ObjCProperty <property name> <class USR>\n"
3393 " ObjCProtocol <protocol name>\n");
3394}
3395
3396int print_usrs(const char **I, const char **E) {
3397 while (I != E) {
3398 const char *kind = *I;
3399 unsigned len = strlen(kind);
3400 switch (len) {
3401 case 8:
3402 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3403 if (I + 2 >= E)
3404 return insufficient_usr(kind, "<ivar name> <class USR>");
3405 if (!isUSR(I[2]))
3406 return not_usr("<class USR>", I[2]);
3407 else {
3408 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003409 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003410 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003411 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3412 }
3413
3414 I += 3;
3415 continue;
3416 }
3417 break;
3418 case 9:
3419 if (memcmp(kind, "ObjCClass", 9) == 0) {
3420 if (I + 1 >= E)
3421 return insufficient_usr(kind, "<class name>");
3422 print_usr(clang_constructUSR_ObjCClass(I[1]));
3423 I += 2;
3424 continue;
3425 }
3426 break;
3427 case 10:
3428 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3429 if (I + 3 >= E)
3430 return insufficient_usr(kind, "<method selector> "
3431 "[0=class method|1=instance method] <class USR>");
3432 if (!isUSR(I[3]))
3433 return not_usr("<class USR>", I[3]);
3434 else {
3435 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003436 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00003437 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003438 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3439 }
3440 I += 4;
3441 continue;
3442 }
3443 break;
3444 case 12:
3445 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3446 if (I + 2 >= E)
3447 return insufficient_usr(kind, "<class name> <category name>");
3448 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3449 I += 3;
3450 continue;
3451 }
3452 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3453 if (I + 1 >= E)
3454 return insufficient_usr(kind, "<protocol name>");
3455 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3456 I += 2;
3457 continue;
3458 }
3459 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3460 if (I + 2 >= E)
3461 return insufficient_usr(kind, "<property name> <class USR>");
3462 if (!isUSR(I[2]))
3463 return not_usr("<class USR>", I[2]);
3464 else {
3465 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003466 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003467 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003468 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3469 }
3470 I += 3;
3471 continue;
3472 }
3473 break;
3474 default:
3475 break;
3476 }
3477 break;
3478 }
3479
3480 if (I != E) {
3481 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3482 display_usrs();
3483 return 1;
3484 }
3485 return 0;
3486}
3487
3488int print_usrs_file(const char *file_name) {
3489 char line[2048];
3490 const char *args[128];
3491 unsigned numChars = 0;
3492
3493 FILE *fp = fopen(file_name, "r");
3494 if (!fp) {
3495 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3496 return 1;
3497 }
3498
3499 /* This code is not really all that safe, but it works fine for testing. */
3500 while (!feof(fp)) {
3501 char c = fgetc(fp);
3502 if (c == '\n') {
3503 unsigned i = 0;
3504 const char *s = 0;
3505
3506 if (numChars == 0)
3507 continue;
3508
3509 line[numChars] = '\0';
3510 numChars = 0;
3511
3512 if (line[0] == '/' && line[1] == '/')
3513 continue;
3514
3515 s = strtok(line, " ");
3516 while (s) {
3517 args[i] = s;
3518 ++i;
3519 s = strtok(0, " ");
3520 }
3521 if (print_usrs(&args[0], &args[i]))
3522 return 1;
3523 }
3524 else
3525 line[numChars++] = c;
3526 }
3527
3528 fclose(fp);
3529 return 0;
3530}
3531
3532/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00003533/* Command line processing. */
3534/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003535int write_pch_file(const char *filename, int argc, const char *argv[]) {
3536 CXIndex Idx;
3537 CXTranslationUnit TU;
3538 struct CXUnsavedFile *unsaved_files = 0;
3539 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00003540 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003541
Stefanus Du Toitfc093362013-03-01 21:41:22 +00003542 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003543
3544 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3545 clang_disposeIndex(Idx);
3546 return -1;
3547 }
3548
3549 TU = clang_parseTranslationUnit(Idx, 0,
3550 argv + num_unsaved_files,
3551 argc - num_unsaved_files,
3552 unsaved_files,
3553 num_unsaved_files,
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003554 CXTranslationUnit_Incomplete |
Argyrios Kyrtzidis65110ca2013-04-26 21:33:40 +00003555 CXTranslationUnit_DetailedPreprocessingRecord|
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003556 CXTranslationUnit_ForSerialization);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003557 if (!TU) {
3558 fprintf(stderr, "Unable to load translation unit!\n");
3559 free_remapped_files(unsaved_files, num_unsaved_files);
3560 clang_disposeIndex(Idx);
3561 return 1;
3562 }
3563
Douglas Gregor39c411f2011-07-06 16:43:36 +00003564 switch (clang_saveTranslationUnit(TU, filename,
3565 clang_defaultSaveOptions(TU))) {
3566 case CXSaveError_None:
3567 break;
3568
3569 case CXSaveError_TranslationErrors:
3570 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3571 filename);
3572 result = 2;
3573 break;
3574
3575 case CXSaveError_InvalidTU:
3576 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3577 filename);
3578 result = 3;
3579 break;
3580
3581 case CXSaveError_Unknown:
3582 default:
3583 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3584 result = 1;
3585 break;
3586 }
3587
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003588 clang_disposeTranslationUnit(TU);
3589 free_remapped_files(unsaved_files, num_unsaved_files);
3590 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00003591 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003592}
3593
3594/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00003595/* Serialized diagnostics. */
3596/******************************************************************************/
3597
3598static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3599 switch (error) {
3600 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3601 case CXLoadDiag_None: break;
3602 case CXLoadDiag_Unknown: return "Unknown";
3603 case CXLoadDiag_InvalidFile: return "Invalid File";
3604 }
3605 return "None";
3606}
3607
3608static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3609 switch (severity) {
3610 case CXDiagnostic_Note: return "note";
3611 case CXDiagnostic_Error: return "error";
3612 case CXDiagnostic_Fatal: return "fatal";
3613 case CXDiagnostic_Ignored: return "ignored";
3614 case CXDiagnostic_Warning: return "warning";
3615 }
3616 return "unknown";
3617}
3618
3619static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003620 if (indent == 0)
3621 return;
3622 fprintf(stderr, "+");
3623 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00003624 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003625 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00003626 --indent;
3627 }
3628}
3629
3630static void printLocation(CXSourceLocation L) {
3631 CXFile File;
3632 CXString FileName;
3633 unsigned line, column, offset;
3634
3635 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3636 FileName = clang_getFileName(File);
3637
3638 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3639 clang_disposeString(FileName);
3640}
3641
3642static void printRanges(CXDiagnostic D, unsigned indent) {
3643 unsigned i, n = clang_getDiagnosticNumRanges(D);
3644
3645 for (i = 0; i < n; ++i) {
3646 CXSourceLocation Start, End;
NAKAMURA Takumi212e3d72013-07-22 15:59:52 +00003647 CXSourceRange SR;
3648 SR = clang_getDiagnosticRange(D, i);
Ted Kremenek15322172011-11-10 08:43:12 +00003649 Start = clang_getRangeStart(SR);
3650 End = clang_getRangeEnd(SR);
3651
3652 printIndent(indent);
3653 fprintf(stderr, "Range: ");
3654 printLocation(Start);
3655 fprintf(stderr, " ");
3656 printLocation(End);
3657 fprintf(stderr, "\n");
3658 }
3659}
3660
3661static void printFixIts(CXDiagnostic D, unsigned indent) {
3662 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00003663 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00003664 for (i = 0 ; i < n; ++i) {
3665 CXSourceRange ReplacementRange;
3666 CXString text;
3667 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3668
3669 printIndent(indent);
3670 fprintf(stderr, "FIXIT: (");
3671 printLocation(clang_getRangeStart(ReplacementRange));
3672 fprintf(stderr, " - ");
3673 printLocation(clang_getRangeEnd(ReplacementRange));
3674 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3675 clang_disposeString(text);
3676 }
3677}
3678
3679static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003680 unsigned i, n;
3681
Ted Kremenek15322172011-11-10 08:43:12 +00003682 if (!Diags)
3683 return;
3684
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003685 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00003686 for (i = 0; i < n; ++i) {
3687 CXSourceLocation DiagLoc;
3688 CXDiagnostic D;
3689 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003690 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00003691 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003692 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00003693
3694 D = clang_getDiagnosticInSet(Diags, i);
3695 DiagLoc = clang_getDiagnosticLocation(D);
3696 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3697 FileName = clang_getFileName(File);
3698 DiagSpelling = clang_getDiagnosticSpelling(D);
3699
3700 printIndent(indent);
3701
3702 fprintf(stderr, "%s:%d:%d: %s: %s",
3703 clang_getCString(FileName),
3704 line,
3705 column,
3706 getSeverityString(clang_getDiagnosticSeverity(D)),
3707 clang_getCString(DiagSpelling));
3708
3709 DiagOption = clang_getDiagnosticOption(D, 0);
3710 DiagOptionStr = clang_getCString(DiagOption);
3711 if (DiagOptionStr) {
3712 fprintf(stderr, " [%s]", DiagOptionStr);
3713 }
3714
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003715 DiagCat = clang_getDiagnosticCategoryText(D);
3716 DiagCatStr = clang_getCString(DiagCat);
3717 if (DiagCatStr) {
3718 fprintf(stderr, " [%s]", DiagCatStr);
3719 }
3720
Ted Kremenek15322172011-11-10 08:43:12 +00003721 fprintf(stderr, "\n");
3722
3723 printRanges(D, indent);
3724 printFixIts(D, indent);
3725
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003726 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003727 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3728
3729 clang_disposeString(FileName);
3730 clang_disposeString(DiagSpelling);
3731 clang_disposeString(DiagOption);
3732 }
3733}
3734
3735static int read_diagnostics(const char *filename) {
3736 enum CXLoadDiag_Error error;
3737 CXString errorString;
3738 CXDiagnosticSet Diags = 0;
3739
3740 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3741 if (!Diags) {
3742 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3743 getDiagnosticCodeStr(error),
3744 clang_getCString(errorString));
3745 clang_disposeString(errorString);
3746 return 1;
3747 }
3748
3749 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003750 fprintf(stderr, "Number of diagnostics: %d\n",
3751 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003752 clang_disposeDiagnosticSet(Diags);
3753 return 0;
3754}
3755
3756/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003757/* Command line processing. */
3758/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003759
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003760static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003761 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003762 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003763 if (strcmp(s, "-usrs") == 0)
3764 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003765 if (strncmp(s, "-memory-usage", 13) == 0)
3766 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003767 return NULL;
3768}
3769
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003770static void print_usage(void) {
3771 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003772 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003773 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003774 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00003775 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3776 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi35849722012-10-24 22:52:04 +00003777 fprintf(stderr,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003778 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003779 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003780 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003781 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003782 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003783 "[FileCheck prefix]\n");
3784 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003785 " c-index-test -test-load-tu <AST file> <symbol filter> "
3786 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003787 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3788 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003789 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003790 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003791 " c-index-test -test-load-source-memory-usage "
3792 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003793 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3794 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003795 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003796 " c-index-test -test-load-source-usrs-memory-usage "
3797 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003798 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3799 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003800 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003801 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003802 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003803 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00003804 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003805 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003806 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003807 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003808 " c-index-test -write-pch <file> <compiler arguments>\n");
3809 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003810 " c-index-test -compilation-db [lookup <filename>] database\n");
3811 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003812 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003813 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003814 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003815 " all - load all symbols, including those from PCH\n"
3816 " local - load all symbols except those in PCH\n"
3817 " category - only load ObjC categories (non-PCH)\n"
3818 " interface - only load ObjC interfaces (non-PCH)\n"
3819 " protocol - only load ObjC protocols (non-PCH)\n"
3820 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003821 " typedef - only load typdefs (non-PCH)\n"
3822 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003823}
3824
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003825/***/
3826
3827int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003828 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003829 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3830 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003831 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003832 return perform_code_completion(argc, argv, 0);
3833 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3834 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003835 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3836 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003837 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3838 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00003839 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
3840 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003841 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003842 return index_file(argc - 2, argv + 2, /*full=*/0);
3843 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3844 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003845 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3846 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003847 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
3848 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003849 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003850 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003851 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003852 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3853 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003854 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003855 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3856 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3857 if (I) {
3858 int trials = atoi(argv[2]);
3859 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3860 NULL);
3861 }
3862 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003863 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003864 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003865
3866 PostVisitTU postVisit = 0;
3867 if (strstr(argv[1], "-memory-usage"))
3868 postVisit = PrintMemoryUsage;
3869
Ted Kremenek7d405622010-01-12 23:34:26 +00003870 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003871 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3872 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003873 }
3874 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003875 return perform_file_scan(argv[2], argv[3],
3876 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003877 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3878 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003879 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3880 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3881 PrintInclusionStack);
3882 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3883 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3884 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003885 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3886 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3887 NULL);
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003888 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003889 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003890 PrintType, 0);
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00003891 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
3892 return perform_test_load_source(argc - 2, argv + 2, "all",
3893 PrintTypeSize, 0);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003894 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
3895 return perform_test_load_source(argc - 2, argv + 2, "all",
3896 PrintBitWidth, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003897 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3898 if (argc > 2)
3899 return print_usrs(argv + 2, argv + argc);
3900 else {
3901 display_usrs();
3902 return 1;
3903 }
3904 }
3905 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3906 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003907 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3908 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003909 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3910 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3911
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003912 print_usage();
3913 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003914}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003915
3916/***/
3917
3918/* We intentionally run in a separate thread to ensure we at least minimal
3919 * testing of a multithreaded environment (for example, having a reduced stack
3920 * size). */
3921
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003922typedef struct thread_info {
3923 int argc;
3924 const char **argv;
3925 int result;
3926} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003927void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003928 thread_info *client_data = client_data_v;
3929 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003930#ifdef __CYGWIN__
3931 fflush(stdout); /* stdout is not flushed on Cygwin. */
3932#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003933}
3934
3935int main(int argc, const char **argv) {
Benjamin Kramerd1a4f682012-08-10 10:06:13 +00003936 thread_info client_data;
3937
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003938#ifdef CLANG_HAVE_LIBXML
3939 LIBXML_TEST_VERSION
3940#endif
3941
Douglas Gregor61605982010-10-27 16:00:01 +00003942 if (getenv("CINDEXTEST_NOTHREADS"))
3943 return cindextest_main(argc, argv);
3944
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003945 client_data.argc = argc;
3946 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003947 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003948 return client_data.result;
3949}