blob: c651510bd47f1ceef5d9f640e3f7523e4ec25520 [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
Ted Kremenek0d435192009-11-17 18:13:31 +000018/******************************************************************************/
19/* Utility functions. */
20/******************************************************************************/
21
John Thompson2e06fc82009-10-27 13:42:56 +000022#ifdef _MSC_VER
23char *basename(const char* path)
24{
25 char* base1 = (char*)strrchr(path, '/');
26 char* base2 = (char*)strrchr(path, '\\');
27 if (base1 && base2)
28 return((base1 > base2) ? base1 + 1 : base2 + 1);
29 else if (base1)
30 return(base1 + 1);
31 else if (base2)
32 return(base2 + 1);
33
34 return((char*)path);
35}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000036char *dirname(char* path)
37{
38 char* base1 = (char*)strrchr(path, '/');
39 char* base2 = (char*)strrchr(path, '\\');
40 if (base1 && base2)
41 if (base1 > base2)
42 *base1 = 0;
43 else
44 *base2 = 0;
45 else if (base1)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000046 *base1 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000047 else if (base2)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000048 *base2 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000049
50 return path;
51}
John Thompson2e06fc82009-10-27 13:42:56 +000052#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000053extern char *basename(const char *);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000054extern char *dirname(char *);
John Thompson2e06fc82009-10-27 13:42:56 +000055#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000056
Douglas Gregor45ba9a12010-07-25 17:39:21 +000057/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000058static unsigned getDefaultParsingOptions() {
59 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
60
61 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000062 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000063 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
64 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000065 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
66 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000067 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
68 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +000069 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
70 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregor44c181a2010-07-23 00:33:23 +000071
72 return options;
73}
74
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000075static int checkForErrors(CXTranslationUnit TU);
76
Daniel Dunbar51b058c2010-02-14 08:32:24 +000077static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
78 unsigned end_line, unsigned end_column) {
79 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000080 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000081}
82
Ted Kremenek1c6da172009-11-17 19:37:36 +000083static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
84 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000085
Douglas Gregora88084b2010-02-18 18:08:43 +000086 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000087 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000088 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
89 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000090 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000091 return 1;
92}
93
Douglas Gregor4db64a42010-01-23 00:14:00 +000094void free_remapped_files(struct CXUnsavedFile *unsaved_files,
95 int num_unsaved_files) {
96 int i;
97 for (i = 0; i != num_unsaved_files; ++i) {
98 free((char *)unsaved_files[i].Filename);
99 free((char *)unsaved_files[i].Contents);
100 }
Douglas Gregor653a55f2010-08-19 20:50:29 +0000101 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000102}
103
104int parse_remapped_files(int argc, const char **argv, int start_arg,
105 struct CXUnsavedFile **unsaved_files,
106 int *num_unsaved_files) {
107 int i;
108 int arg;
109 int prefix_len = strlen("-remap-file=");
110 *unsaved_files = 0;
111 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000112
Douglas Gregor4db64a42010-01-23 00:14:00 +0000113 /* Count the number of remapped files. */
114 for (arg = start_arg; arg < argc; ++arg) {
115 if (strncmp(argv[arg], "-remap-file=", prefix_len))
116 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000117
Douglas Gregor4db64a42010-01-23 00:14:00 +0000118 ++*num_unsaved_files;
119 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000120
Douglas Gregor4db64a42010-01-23 00:14:00 +0000121 if (*num_unsaved_files == 0)
122 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000123
Douglas Gregor4db64a42010-01-23 00:14:00 +0000124 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +0000125 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
126 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
128 struct CXUnsavedFile *unsaved = *unsaved_files + i;
129 const char *arg_string = argv[arg] + prefix_len;
130 int filename_len;
131 char *filename;
132 char *contents;
133 FILE *to_file;
134 const char *semi = strchr(arg_string, ';');
135 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000136 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000137 "error: -remap-file=from;to argument is missing semicolon\n");
138 free_remapped_files(*unsaved_files, i);
139 *unsaved_files = 0;
140 *num_unsaved_files = 0;
141 return -1;
142 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000143
Douglas Gregor4db64a42010-01-23 00:14:00 +0000144 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000145 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000146 if (!to_file) {
147 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
148 semi + 1);
149 free_remapped_files(*unsaved_files, i);
150 *unsaved_files = 0;
151 *num_unsaved_files = 0;
152 return -1;
153 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000154
Douglas Gregor4db64a42010-01-23 00:14:00 +0000155 /* Determine the length of the file we're remapping to. */
156 fseek(to_file, 0, SEEK_END);
157 unsaved->Length = ftell(to_file);
158 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000159
Douglas Gregor4db64a42010-01-23 00:14:00 +0000160 /* Read the contents of the file we're remapping to. */
161 contents = (char *)malloc(unsaved->Length + 1);
162 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
163 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
164 (feof(to_file) ? "EOF" : "error"), semi + 1);
165 fclose(to_file);
166 free_remapped_files(*unsaved_files, i);
Richard Smithe07c5f82012-07-05 08:20:49 +0000167 free(contents);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000168 *unsaved_files = 0;
169 *num_unsaved_files = 0;
170 return -1;
171 }
172 contents[unsaved->Length] = 0;
173 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000174
Douglas Gregor4db64a42010-01-23 00:14:00 +0000175 /* Close the file. */
176 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000177
Douglas Gregor4db64a42010-01-23 00:14:00 +0000178 /* Copy the file name that we're remapping from. */
179 filename_len = semi - arg_string;
180 filename = (char *)malloc(filename_len + 1);
181 memcpy(filename, arg_string, filename_len);
182 filename[filename_len] = 0;
183 unsaved->Filename = filename;
184 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000185
Douglas Gregor4db64a42010-01-23 00:14:00 +0000186 return 0;
187}
188
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000189static const char *parse_comments_schema(int argc, const char **argv) {
190 const char *CommentsSchemaArg = "-comments-xml-schema=";
191 const char *CommentSchemaFile = NULL;
192
193 if (argc == 0)
194 return CommentSchemaFile;
195
196 if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
197 CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
198
199 return CommentSchemaFile;
200}
201
Ted Kremenek0d435192009-11-17 18:13:31 +0000202/******************************************************************************/
203/* Pretty-printing. */
204/******************************************************************************/
205
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000206static const char *FileCheckPrefix = "CHECK";
207
208static void PrintCString(const char *CStr) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000209 if (CStr != NULL && CStr[0] != '\0') {
210 for ( ; *CStr; ++CStr) {
211 const char C = *CStr;
212 switch (C) {
213 case '\n': printf("\\n"); break;
214 case '\r': printf("\\r"); break;
215 case '\t': printf("\\t"); break;
216 case '\v': printf("\\v"); break;
217 case '\f': printf("\\f"); break;
218 default: putchar(C); break;
219 }
220 }
221 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000222}
223
224static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
225 printf(" %s=[", Prefix);
226 PrintCString(CStr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000227 printf("]");
228}
229
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000230static void PrintCXStringAndDispose(CXString Str) {
231 PrintCString(clang_getCString(Str));
232 clang_disposeString(Str);
233}
234
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000235static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
236 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
237}
238
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000239static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
240 CXString Str) {
241 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
242 clang_disposeString(Str);
243}
244
Douglas Gregor430d7a12011-07-25 17:48:11 +0000245static void PrintRange(CXSourceRange R, const char *str) {
246 CXFile begin_file, end_file;
247 unsigned begin_line, begin_column, end_line, end_column;
248
249 clang_getSpellingLocation(clang_getRangeStart(R),
250 &begin_file, &begin_line, &begin_column, 0);
251 clang_getSpellingLocation(clang_getRangeEnd(R),
252 &end_file, &end_line, &end_column, 0);
253 if (!begin_file || !end_file)
254 return;
255
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000256 if (str)
257 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000258 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
259}
260
Douglas Gregor358559d2010-10-02 22:49:11 +0000261int want_display_name = 0;
262
Douglas Gregorcc889662012-05-08 00:14:45 +0000263static void printVersion(const char *Prefix, CXVersion Version) {
264 if (Version.Major < 0)
265 return;
266 printf("%s%d", Prefix, Version.Major);
267
268 if (Version.Minor < 0)
269 return;
270 printf(".%d", Version.Minor);
271
272 if (Version.Subminor < 0)
273 return;
274 printf(".%d", Version.Subminor);
275}
276
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000277struct CommentASTDumpingContext {
278 int IndentLevel;
279};
280
281static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
282 CXComment Comment) {
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000283 unsigned i;
284 unsigned e;
285 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
286
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000287 Ctx->IndentLevel++;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000288 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000289 printf(" ");
290
291 printf("(");
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000292 switch (Kind) {
293 case CXComment_Null:
294 printf("CXComment_Null");
295 break;
296 case CXComment_Text:
297 printf("CXComment_Text");
298 PrintCXStringWithPrefixAndDispose("Text",
299 clang_TextComment_getText(Comment));
300 if (clang_Comment_isWhitespace(Comment))
301 printf(" IsWhitespace");
302 if (clang_InlineContentComment_hasTrailingNewline(Comment))
303 printf(" HasTrailingNewline");
304 break;
305 case CXComment_InlineCommand:
306 printf("CXComment_InlineCommand");
307 PrintCXStringWithPrefixAndDispose(
308 "CommandName",
309 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000310 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
311 case CXCommentInlineCommandRenderKind_Normal:
312 printf(" RenderNormal");
313 break;
314 case CXCommentInlineCommandRenderKind_Bold:
315 printf(" RenderBold");
316 break;
317 case CXCommentInlineCommandRenderKind_Monospaced:
318 printf(" RenderMonospaced");
319 break;
320 case CXCommentInlineCommandRenderKind_Emphasized:
321 printf(" RenderEmphasized");
322 break;
323 }
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000324 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000325 i != e; ++i) {
326 printf(" Arg[%u]=", i);
327 PrintCXStringAndDispose(
328 clang_InlineCommandComment_getArgText(Comment, i));
329 }
330 if (clang_InlineContentComment_hasTrailingNewline(Comment))
331 printf(" HasTrailingNewline");
332 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000333 case CXComment_HTMLStartTag: {
334 unsigned NumAttrs;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000335 printf("CXComment_HTMLStartTag");
336 PrintCXStringWithPrefixAndDispose(
337 "Name",
338 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000339 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000340 if (NumAttrs != 0) {
341 printf(" Attrs:");
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000342 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000343 printf(" ");
344 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
345 printf("=");
346 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
347 }
348 }
349 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
350 printf(" SelfClosing");
351 if (clang_InlineContentComment_hasTrailingNewline(Comment))
352 printf(" HasTrailingNewline");
353 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000354 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000355 case CXComment_HTMLEndTag:
356 printf("CXComment_HTMLEndTag");
357 PrintCXStringWithPrefixAndDispose(
358 "Name",
359 clang_HTMLTagComment_getTagName(Comment));
360 if (clang_InlineContentComment_hasTrailingNewline(Comment))
361 printf(" HasTrailingNewline");
362 break;
363 case CXComment_Paragraph:
364 printf("CXComment_Paragraph");
365 if (clang_Comment_isWhitespace(Comment))
366 printf(" IsWhitespace");
367 break;
368 case CXComment_BlockCommand:
369 printf("CXComment_BlockCommand");
370 PrintCXStringWithPrefixAndDispose(
371 "CommandName",
372 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000373 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000374 i != e; ++i) {
375 printf(" Arg[%u]=", i);
376 PrintCXStringAndDispose(
377 clang_BlockCommandComment_getArgText(Comment, i));
378 }
379 break;
380 case CXComment_ParamCommand:
381 printf("CXComment_ParamCommand");
382 switch (clang_ParamCommandComment_getDirection(Comment)) {
383 case CXCommentParamPassDirection_In:
384 printf(" in");
385 break;
386 case CXCommentParamPassDirection_Out:
387 printf(" out");
388 break;
389 case CXCommentParamPassDirection_InOut:
390 printf(" in,out");
391 break;
392 }
393 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
394 printf(" explicitly");
395 else
396 printf(" implicitly");
397 PrintCXStringWithPrefixAndDispose(
398 "ParamName",
399 clang_ParamCommandComment_getParamName(Comment));
400 if (clang_ParamCommandComment_isParamIndexValid(Comment))
401 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
402 else
403 printf(" ParamIndex=Invalid");
404 break;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000405 case CXComment_TParamCommand:
406 printf("CXComment_TParamCommand");
407 PrintCXStringWithPrefixAndDispose(
408 "ParamName",
409 clang_TParamCommandComment_getParamName(Comment));
410 if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
411 printf(" ParamPosition={");
412 for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
413 i != e; ++i) {
414 printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
415 if (i != e - 1)
416 printf(", ");
417 }
418 printf("}");
419 } else
420 printf(" ParamPosition=Invalid");
421 break;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000422 case CXComment_VerbatimBlockCommand:
423 printf("CXComment_VerbatimBlockCommand");
424 PrintCXStringWithPrefixAndDispose(
425 "CommandName",
426 clang_BlockCommandComment_getCommandName(Comment));
427 break;
428 case CXComment_VerbatimBlockLine:
429 printf("CXComment_VerbatimBlockLine");
430 PrintCXStringWithPrefixAndDispose(
431 "Text",
432 clang_VerbatimBlockLineComment_getText(Comment));
433 break;
434 case CXComment_VerbatimLine:
435 printf("CXComment_VerbatimLine");
436 PrintCXStringWithPrefixAndDispose(
437 "Text",
438 clang_VerbatimLineComment_getText(Comment));
439 break;
440 case CXComment_FullComment:
441 printf("CXComment_FullComment");
442 break;
443 }
444 if (Kind != CXComment_Null) {
445 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000446 unsigned i;
447 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000448 printf("\n// %s: ", FileCheckPrefix);
449 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
450 }
451 }
452 printf(")");
453 Ctx->IndentLevel--;
454}
455
456static void DumpCXComment(CXComment Comment) {
457 struct CommentASTDumpingContext Ctx;
458 Ctx.IndentLevel = 1;
459 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
460 DumpCXCommentInternal(&Ctx, Comment);
461 printf("]");
462}
463
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000464typedef struct {
465 const char *CommentSchemaFile;
466#ifdef CLANG_HAVE_LIBXML
467 xmlRelaxNGParserCtxtPtr RNGParser;
468 xmlRelaxNGPtr Schema;
469#endif
470} CommentXMLValidationData;
471
472static void ValidateCommentXML(const char *Str,
473 CommentXMLValidationData *ValidationData) {
474#ifdef CLANG_HAVE_LIBXML
475 xmlDocPtr Doc;
476 xmlRelaxNGValidCtxtPtr ValidationCtxt;
477 int status;
478
479 if (!ValidationData || !ValidationData->CommentSchemaFile)
480 return;
481
482 if (!ValidationData->RNGParser) {
483 ValidationData->RNGParser =
484 xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile);
485 ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser);
486 }
487 if (!ValidationData->RNGParser) {
488 printf(" libXMLError");
489 return;
490 }
491
492 Doc = xmlParseDoc((const xmlChar *) Str);
493
494 if (!Doc) {
495 xmlErrorPtr Error = xmlGetLastError();
496 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
497 return;
498 }
499
500 ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->Schema);
501 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
502 if (!status)
503 printf(" CommentXMLValid");
504 else if (status > 0) {
505 xmlErrorPtr Error = xmlGetLastError();
506 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
507 } else
508 printf(" libXMLError");
509
510 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
511 xmlFreeDoc(Doc);
512#endif
513}
514
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000515static void PrintCursorComments(CXCursor Cursor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000516 CommentXMLValidationData *ValidationData) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000517 {
518 CXString RawComment;
519 const char *RawCommentCString;
520 CXString BriefComment;
521 const char *BriefCommentCString;
522
523 RawComment = clang_Cursor_getRawCommentText(Cursor);
524 RawCommentCString = clang_getCString(RawComment);
525 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
526 PrintCStringWithPrefix("RawComment", RawCommentCString);
527 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
528
529 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
530 BriefCommentCString = clang_getCString(BriefComment);
531 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
532 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
533 clang_disposeString(BriefComment);
534 }
535 clang_disposeString(RawComment);
536 }
537
538 {
539 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
540 if (clang_Comment_getKind(Comment) != CXComment_Null) {
541 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
542 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000543 {
544 CXString XML;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000545 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000546 PrintCXStringWithPrefix("FullCommentAsXML", XML);
547 ValidateCommentXML(clang_getCString(XML), ValidationData);
548 clang_disposeString(XML);
549 }
550
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000551 DumpCXComment(Comment);
552 }
553 }
554}
555
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000556typedef struct {
557 unsigned line;
558 unsigned col;
559} LineCol;
560
561static int lineCol_cmp(const void *p1, const void *p2) {
562 const LineCol *lhs = p1;
563 const LineCol *rhs = p2;
564 if (lhs->line != rhs->line)
565 return (int)lhs->line - (int)rhs->line;
566 return (int)lhs->col - (int)rhs->col;
567}
568
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000569static void PrintCursor(CXCursor Cursor,
570 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000571 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000572 if (clang_isInvalid(Cursor.kind)) {
573 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
574 printf("Invalid Cursor => %s", clang_getCString(ks));
575 clang_disposeString(ks);
576 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000577 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000579 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000580 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000581 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000582 CXCursor *overridden;
583 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000584 unsigned RefNameRangeNr;
585 CXSourceRange CursorExtent;
586 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000587 int AlwaysUnavailable;
588 int AlwaysDeprecated;
589 CXString UnavailableMessage;
590 CXString DeprecatedMessage;
591 CXPlatformAvailability PlatformAvailability[2];
592 int NumPlatformAvailability;
593 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000594
Ted Kremeneke68fff62010-02-17 00:41:32 +0000595 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000596 string = want_display_name? clang_getCursorDisplayName(Cursor)
597 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000598 printf("%s=%s", clang_getCString(ks),
599 clang_getCString(string));
600 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000601 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000602
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000603 Referenced = clang_getCursorReferenced(Cursor);
604 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000605 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
606 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
607 printf("[");
608 for (I = 0; I != N; ++I) {
609 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000610 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000611 if (I)
612 printf(", ");
613
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000614 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000615 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000616 printf("%d:%d", line, column);
617 }
618 printf("]");
619 } else {
620 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000621 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000622 printf(":%d:%d", line, column);
623 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000624 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000625
626 if (clang_isCursorDefinition(Cursor))
627 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000628
629 switch (clang_getCursorAvailability(Cursor)) {
630 case CXAvailability_Available:
631 break;
632
633 case CXAvailability_Deprecated:
634 printf(" (deprecated)");
635 break;
636
637 case CXAvailability_NotAvailable:
638 printf(" (unavailable)");
639 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000640
641 case CXAvailability_NotAccessible:
642 printf(" (inaccessible)");
643 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000644 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000645
Douglas Gregorcc889662012-05-08 00:14:45 +0000646 NumPlatformAvailability
647 = clang_getCursorPlatformAvailability(Cursor,
648 &AlwaysDeprecated,
649 &DeprecatedMessage,
650 &AlwaysUnavailable,
651 &UnavailableMessage,
652 PlatformAvailability, 2);
653 if (AlwaysUnavailable) {
654 printf(" (always unavailable: \"%s\")",
655 clang_getCString(UnavailableMessage));
656 } else if (AlwaysDeprecated) {
657 printf(" (always deprecated: \"%s\")",
658 clang_getCString(DeprecatedMessage));
659 } else {
660 for (I = 0; I != NumPlatformAvailability; ++I) {
661 if (I >= 2)
662 break;
663
664 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
665 if (PlatformAvailability[I].Unavailable)
666 printf(", unavailable");
667 else {
668 printVersion(", introduced=", PlatformAvailability[I].Introduced);
669 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
670 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
671 }
672 if (clang_getCString(PlatformAvailability[I].Message)[0])
673 printf(", message=\"%s\"",
674 clang_getCString(PlatformAvailability[I].Message));
675 printf(")");
676 }
677 }
678 for (I = 0; I != NumPlatformAvailability; ++I) {
679 if (I >= 2)
680 break;
681 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
682 }
683
684 clang_disposeString(DeprecatedMessage);
685 clang_disposeString(UnavailableMessage);
686
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000687 if (clang_CXXMethod_isStatic(Cursor))
688 printf(" (static)");
689 if (clang_CXXMethod_isVirtual(Cursor))
690 printf(" (virtual)");
691
Ted Kremenek95f33552010-08-26 01:42:22 +0000692 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
693 CXType T =
694 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
695 CXString S = clang_getTypeKindSpelling(T.kind);
696 printf(" [IBOutletCollection=%s]", clang_getCString(S));
697 clang_disposeString(S);
698 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000699
700 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
701 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
702 unsigned isVirtual = clang_isVirtualBase(Cursor);
703 const char *accessStr = 0;
704
705 switch (access) {
706 case CX_CXXInvalidAccessSpecifier:
707 accessStr = "invalid"; break;
708 case CX_CXXPublic:
709 accessStr = "public"; break;
710 case CX_CXXProtected:
711 accessStr = "protected"; break;
712 case CX_CXXPrivate:
713 accessStr = "private"; break;
714 }
715
716 printf(" [access=%s isVirtual=%s]", accessStr,
717 isVirtual ? "true" : "false");
718 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000719
720 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
721 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
722 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
723 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000724 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000725 printf(" [Specialization of %s:%d:%d]",
726 clang_getCString(Name), line, column);
727 clang_disposeString(Name);
728 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000729
730 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
731 if (num_overridden) {
732 unsigned I;
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000733 LineCol lineCols[50];
734 assert(num_overridden <= 50);
Douglas Gregor9f592342010-10-01 20:25:15 +0000735 printf(" [Overrides ");
736 for (I = 0; I != num_overridden; ++I) {
737 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000738 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000739 lineCols[I].line = line;
740 lineCols[I].col = column;
741 }
Michael Liao64221492012-08-30 00:45:32 +0000742 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000743 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
744 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor9f592342010-10-01 20:25:15 +0000745 if (I)
746 printf(", ");
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000747 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor9f592342010-10-01 20:25:15 +0000748 }
749 printf("]");
750 clang_disposeOverriddenCursors(overridden);
751 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000752
753 if (Cursor.kind == CXCursor_InclusionDirective) {
754 CXFile File = clang_getIncludedFile(Cursor);
755 CXString Included = clang_getFileName(File);
756 printf(" (%s)", clang_getCString(Included));
757 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000758
759 if (clang_isFileMultipleIncludeGuarded(TU, File))
760 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000761 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000762
763 CursorExtent = clang_getCursorExtent(Cursor);
764 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
765 CXNameRange_WantQualifier
766 | CXNameRange_WantSinglePiece
767 | CXNameRange_WantTemplateArgs,
768 0);
769 if (!clang_equalRanges(CursorExtent, RefNameRange))
770 PrintRange(RefNameRange, "SingleRefName");
771
772 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
773 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
774 CXNameRange_WantQualifier
775 | CXNameRange_WantTemplateArgs,
776 RefNameRangeNr);
777 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
778 break;
779 if (!clang_equalRanges(CursorExtent, RefNameRange))
780 PrintRange(RefNameRange, "RefName");
781 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000782
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000783 PrintCursorComments(Cursor, ValidationData);
Steve Naroff699a07d2009-09-25 21:32:34 +0000784 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000785}
Steve Naroff89922f82009-08-31 00:59:03 +0000786
Ted Kremeneke68fff62010-02-17 00:41:32 +0000787static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000788 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000789 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000790 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000791 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000792 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000793 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000794 clang_disposeString(source);
795 return "<invalid loc>";
796 }
797 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000798 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000799 clang_disposeString(source);
800 return b;
801 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000802}
803
Ted Kremenek0d435192009-11-17 18:13:31 +0000804/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000805/* Callbacks. */
806/******************************************************************************/
807
808typedef void (*PostVisitTU)(CXTranslationUnit);
809
Douglas Gregora88084b2010-02-18 18:08:43 +0000810void PrintDiagnostic(CXDiagnostic Diagnostic) {
811 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000812 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000813 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000814 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000815 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
816 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000817 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000818
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000819 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000820 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000821
Douglas Gregor274f1902010-02-22 23:17:23 +0000822 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
823 fprintf(stderr, "%s\n", clang_getCString(Msg));
824 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000825
Douglas Gregora9b06d42010-11-09 06:24:54 +0000826 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
827 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000828 if (!file)
829 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000830
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000831 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000832 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000833 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000834 CXSourceRange range;
835 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
836 CXSourceLocation start = clang_getRangeStart(range);
837 CXSourceLocation end = clang_getRangeEnd(range);
838 unsigned start_line, start_column, end_line, end_column;
839 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000840 clang_getSpellingLocation(start, &start_file, &start_line,
841 &start_column, 0);
842 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000843 if (clang_equalLocations(start, end)) {
844 /* Insertion. */
845 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000846 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000847 clang_getCString(insertion_text), start_line, start_column);
848 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
849 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000850 if (start_file == file && end_file == file) {
851 fprintf(out, "FIX-IT: Remove ");
852 PrintExtent(out, start_line, start_column, end_line, end_column);
853 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000854 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000855 } else {
856 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000857 if (start_file == end_file) {
858 fprintf(out, "FIX-IT: Replace ");
859 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000860 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000861 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000862 break;
863 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000864 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000865 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000866}
867
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000868void PrintDiagnosticSet(CXDiagnosticSet Set) {
869 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
870 for ( ; i != n ; ++i) {
871 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
872 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000873 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000874 if (ChildDiags)
875 PrintDiagnosticSet(ChildDiags);
876 }
877}
878
879void PrintDiagnostics(CXTranslationUnit TU) {
880 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
881 PrintDiagnosticSet(TUSet);
882 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000883}
884
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000885void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000886 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000887 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000888 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000889 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000890 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000891 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000892 unsigned long amount = usage.entries[i].amount;
893 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000894 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000895 ((double) amount)/(1024*1024));
896 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000897 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000898 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000899 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000900}
901
Ted Kremenekce2ae882010-01-26 17:59:48 +0000902/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000903/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000904/******************************************************************************/
905
Douglas Gregora7bde202010-01-19 00:34:46 +0000906static void PrintCursorExtent(CXCursor C) {
907 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000908 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000909}
910
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000911/* Data used by the visitors. */
912typedef struct {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000913 CXTranslationUnit TU;
914 enum CXCursorKind *Filter;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000915 CommentXMLValidationData ValidationData;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000916} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000917
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000918
Ted Kremeneke68fff62010-02-17 00:41:32 +0000919enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000920 CXCursor Parent,
921 CXClientData ClientData) {
922 VisitorData *Data = (VisitorData *)ClientData;
923 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000924 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000925 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000926 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000927 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000928 GetCursorSource(Cursor), line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000929 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregora7bde202010-01-19 00:34:46 +0000930 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000931 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000932 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000933 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000934
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000935 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000936}
Steve Naroff50398192009-08-28 15:28:48 +0000937
Ted Kremeneke68fff62010-02-17 00:41:32 +0000938static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000939 CXCursor Parent,
940 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000941 const char *startBuf, *endBuf;
942 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
943 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000944 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000945
Douglas Gregorb6998662010-01-19 19:34:47 +0000946 if (Cursor.kind != CXCursor_FunctionDecl ||
947 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000948 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000949
950 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
951 &startLine, &startColumn,
952 &endLine, &endColumn);
953 /* Probe the entire body, looking for both decls and refs. */
954 curLine = startLine;
955 curColumn = startColumn;
956
957 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000958 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000959 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000960 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000961
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000962 if (*startBuf == '\n') {
963 startBuf++;
964 curLine++;
965 curColumn = 1;
966 } else if (*startBuf != '\t')
967 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000968
Douglas Gregor98258af2010-01-18 22:46:11 +0000969 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000970 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000971
Douglas Gregor1db19de2010-01-19 21:36:55 +0000972 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000973 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000974 CXSourceLocation RefLoc
975 = clang_getLocation(Data->TU, file, curLine, curColumn);
976 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000977 if (Ref.kind == CXCursor_NoDeclFound) {
978 /* Nothing found here; that's fine. */
979 } else if (Ref.kind != CXCursor_FunctionDecl) {
980 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
981 curLine, curColumn);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000982 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor98258af2010-01-18 22:46:11 +0000983 printf("\n");
984 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000985 }
Ted Kremenek74844072010-02-17 00:41:20 +0000986 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000987 startBuf++;
988 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000989
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000990 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000991}
992
Ted Kremenek7d405622010-01-12 23:34:26 +0000993/******************************************************************************/
994/* USR testing. */
995/******************************************************************************/
996
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000997enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
998 CXClientData ClientData) {
999 VisitorData *Data = (VisitorData *)ClientData;
1000 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +00001001 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +00001002 const char *cstr = clang_getCString(USR);
1003 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +00001004 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +00001005 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +00001006 }
Ted Kremeneke542f772010-04-20 23:15:40 +00001007 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1008
Douglas Gregora7bde202010-01-19 00:34:46 +00001009 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +00001010 printf("\n");
1011 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001012
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001013 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001014 }
1015
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001016 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +00001017}
1018
1019/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +00001020/* Inclusion stack testing. */
1021/******************************************************************************/
1022
1023void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1024 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001025
Ted Kremenek16b55a72010-01-26 19:31:51 +00001026 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +00001027 CXString fname;
1028
1029 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001030 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +00001031 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001032
Ted Kremenek16b55a72010-01-26 19:31:51 +00001033 for (i = 0; i < includeStackLen; ++i) {
1034 CXFile includingFile;
1035 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +00001036 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1037 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +00001038 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001039 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +00001040 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001041 }
1042 printf("\n");
1043}
1044
1045void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001046 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001047}
1048
1049/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +00001050/* Linkage testing. */
1051/******************************************************************************/
1052
1053static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1054 CXClientData d) {
1055 const char *linkage = 0;
1056
1057 if (clang_isInvalid(clang_getCursorKind(cursor)))
1058 return CXChildVisit_Recurse;
1059
1060 switch (clang_getCursorLinkage(cursor)) {
1061 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +00001062 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1063 case CXLinkage_Internal: linkage = "Internal"; break;
1064 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1065 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +00001066 }
1067
1068 if (linkage) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001069 PrintCursor(cursor, NULL);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001070 printf("linkage=%s\n", linkage);
1071 }
1072
1073 return CXChildVisit_Recurse;
1074}
1075
1076/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001077/* Typekind testing. */
1078/******************************************************************************/
1079
1080static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
1081 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001082 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
1083 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001084 CXString S = clang_getTypeKindSpelling(T.kind);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001085 PrintCursor(cursor, NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001086 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +00001087 if (clang_isConstQualifiedType(T))
1088 printf(" const");
1089 if (clang_isVolatileQualifiedType(T))
1090 printf(" volatile");
1091 if (clang_isRestrictQualifiedType(T))
1092 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001093 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +00001094 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001095 {
1096 CXType CT = clang_getCanonicalType(T);
1097 if (!clang_equalTypes(T, CT)) {
1098 CXString CS = clang_getTypeKindSpelling(CT.kind);
1099 printf(" [canonical=%s]", clang_getCString(CS));
1100 clang_disposeString(CS);
1101 }
1102 }
Benjamin Kramere1403d22010-06-22 09:29:44 +00001103 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001104 {
Ted Kremenek9a140842010-06-21 20:48:56 +00001105 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001106 if (RT.kind != CXType_Invalid) {
1107 CXString RS = clang_getTypeKindSpelling(RT.kind);
1108 printf(" [result=%s]", clang_getCString(RS));
1109 clang_disposeString(RS);
1110 }
1111 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001112 /* Print the argument types if they exist. */
1113 {
1114 int numArgs = clang_Cursor_getNumArguments(cursor);
1115 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001116 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001117 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001118 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001119 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
1120 if (T.kind != CXType_Invalid) {
1121 CXString S = clang_getTypeKindSpelling(T.kind);
1122 printf(" %s", clang_getCString(S));
1123 clang_disposeString(S);
1124 }
1125 }
1126 printf("]");
1127 }
1128 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +00001129 /* Print if this is a non-POD type. */
1130 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001131
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001132 printf("\n");
1133 }
1134 return CXChildVisit_Recurse;
1135}
1136
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001137/******************************************************************************/
1138/* Bitwidth testing. */
1139/******************************************************************************/
1140
1141static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1142 CXClientData d) {
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001143 int Bitwidth;
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001144 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1145 return CXChildVisit_Recurse;
1146
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001147 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001148 if (Bitwidth >= 0) {
1149 PrintCursor(cursor, NULL);
1150 printf(" bitwidth=%d\n", Bitwidth);
1151 }
1152
1153 return CXChildVisit_Recurse;
1154}
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001155
1156/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +00001157/* Loading ASTs/source. */
1158/******************************************************************************/
1159
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001160static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +00001161 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001162 CXCursorVisitor Visitor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001163 PostVisitTU PV,
1164 const char *CommentSchemaFile) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001165
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001166 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001167 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001168
1169 if (Visitor) {
1170 enum CXCursorKind K = CXCursor_NotImplemented;
1171 enum CXCursorKind *ck = &K;
1172 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001173
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001174 /* Perform some simple filtering. */
1175 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001176 else if (!strcmp(filter, "all-display") ||
1177 !strcmp(filter, "local-display")) {
1178 ck = NULL;
1179 want_display_name = 1;
1180 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001181 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001182 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1183 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1184 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1185 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1186 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1187 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1188 else {
1189 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1190 return 1;
1191 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001192
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001193 Data.TU = TU;
1194 Data.Filter = ck;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001195 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1196#ifdef CLANG_HAVE_LIBXML
1197 Data.ValidationData.RNGParser = NULL;
1198 Data.ValidationData.Schema = NULL;
1199#endif
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001200 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001201 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001202
Ted Kremenekce2ae882010-01-26 17:59:48 +00001203 if (PV)
1204 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001205
Douglas Gregora88084b2010-02-18 18:08:43 +00001206 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001207 if (checkForErrors(TU) != 0) {
1208 clang_disposeTranslationUnit(TU);
1209 return -1;
1210 }
1211
Ted Kremenek0d435192009-11-17 18:13:31 +00001212 clang_disposeTranslationUnit(TU);
1213 return 0;
1214}
1215
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001216int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001217 const char *prefix, CXCursorVisitor Visitor,
1218 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001219 CXIndex Idx;
1220 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001221 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001222 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001223 !strcmp(filter, "local") ? 1 : 0,
1224 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001225
Ted Kremenek020a0952010-02-11 07:41:25 +00001226 if (!CreateTranslationUnit(Idx, file, &TU)) {
1227 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001228 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001229 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001230
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001231 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek020a0952010-02-11 07:41:25 +00001232 clang_disposeIndex(Idx);
1233 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001234}
1235
Ted Kremenekce2ae882010-01-26 17:59:48 +00001236int perform_test_load_source(int argc, const char **argv,
1237 const char *filter, CXCursorVisitor Visitor,
1238 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001239 CXIndex Idx;
1240 CXTranslationUnit TU;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001241 const char *CommentSchemaFile;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001242 struct CXUnsavedFile *unsaved_files = 0;
1243 int num_unsaved_files = 0;
1244 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001245
Daniel Dunbarada487d2009-12-01 02:03:10 +00001246 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001247 (!strcmp(filter, "local") ||
1248 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +00001249 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001250
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001251 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1252 argc--;
1253 argv++;
1254 }
1255
Ted Kremenek020a0952010-02-11 07:41:25 +00001256 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1257 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001258 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001259 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001260
Douglas Gregordca8ee82011-05-06 16:33:08 +00001261 TU = clang_parseTranslationUnit(Idx, 0,
1262 argv + num_unsaved_files,
1263 argc - num_unsaved_files,
1264 unsaved_files, num_unsaved_files,
1265 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001266 if (!TU) {
1267 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001268 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001269 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001270 return 1;
1271 }
1272
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001273 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1274 CommentSchemaFile);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001275 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001276 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001277 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001278}
1279
Douglas Gregorabc563f2010-07-19 21:46:24 +00001280int perform_test_reparse_source(int argc, const char **argv, int trials,
1281 const char *filter, CXCursorVisitor Visitor,
1282 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001283 CXIndex Idx;
1284 CXTranslationUnit TU;
1285 struct CXUnsavedFile *unsaved_files = 0;
1286 int num_unsaved_files = 0;
1287 int result;
1288 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001289 int remap_after_trial = 0;
1290 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001291
1292 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1293 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +00001294 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001295
Douglas Gregorabc563f2010-07-19 21:46:24 +00001296 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1297 clang_disposeIndex(Idx);
1298 return -1;
1299 }
1300
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001301 /* Load the initial translation unit -- we do this without honoring remapped
1302 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001303 TU = clang_parseTranslationUnit(Idx, 0,
1304 argv + num_unsaved_files,
1305 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001306 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001307 if (!TU) {
1308 fprintf(stderr, "Unable to load translation unit!\n");
1309 free_remapped_files(unsaved_files, num_unsaved_files);
1310 clang_disposeIndex(Idx);
1311 return 1;
1312 }
1313
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001314 if (checkForErrors(TU) != 0)
1315 return -1;
1316
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001317 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1318 remap_after_trial =
1319 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1320 }
1321
Douglas Gregorabc563f2010-07-19 21:46:24 +00001322 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001323 if (clang_reparseTranslationUnit(TU,
1324 trial >= remap_after_trial ? num_unsaved_files : 0,
1325 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001326 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001327 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001328 clang_disposeTranslationUnit(TU);
1329 free_remapped_files(unsaved_files, num_unsaved_files);
1330 clang_disposeIndex(Idx);
1331 return -1;
1332 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001333
1334 if (checkForErrors(TU) != 0)
1335 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001336 }
1337
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001338 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001339
Douglas Gregorabc563f2010-07-19 21:46:24 +00001340 free_remapped_files(unsaved_files, num_unsaved_files);
1341 clang_disposeIndex(Idx);
1342 return result;
1343}
1344
Ted Kremenek0d435192009-11-17 18:13:31 +00001345/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001346/* Logic for testing clang_getCursor(). */
1347/******************************************************************************/
1348
Douglas Gregordd3e5542011-05-04 00:14:37 +00001349static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001350 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001351 unsigned end_line, unsigned end_col,
1352 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001353 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001354 if (prefix)
1355 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001356 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1357 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001358 PrintCursor(cursor, NULL);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001359 printf("\n");
1360}
1361
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001362static int perform_file_scan(const char *ast_file, const char *source_file,
1363 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001364 CXIndex Idx;
1365 CXTranslationUnit TU;
1366 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001367 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001368 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001369 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001370 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001371
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001372 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1373 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001374 fprintf(stderr, "Could not create Index\n");
1375 return 1;
1376 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001377
Ted Kremenek1c6da172009-11-17 19:37:36 +00001378 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1379 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001380
Ted Kremenek1c6da172009-11-17 19:37:36 +00001381 if ((fp = fopen(source_file, "r")) == NULL) {
1382 fprintf(stderr, "Could not open '%s'\n", source_file);
1383 return 1;
1384 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001385
Douglas Gregorb9790342010-01-22 21:44:22 +00001386 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001387 for (;;) {
1388 CXCursor cursor;
1389 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001390
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001391 if (c == '\n') {
1392 ++line;
1393 col = 1;
1394 } else
1395 ++col;
1396
1397 /* Check the cursor at this position, and dump the previous one if we have
1398 * found something new.
1399 */
1400 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1401 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1402 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001403 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001404 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001405 start_line = line;
1406 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001407 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001408 if (c == EOF)
1409 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001410
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001411 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001412 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001413
Ted Kremenek1c6da172009-11-17 19:37:36 +00001414 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001415 clang_disposeTranslationUnit(TU);
1416 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001417 return 0;
1418}
1419
1420/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001421/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001422/******************************************************************************/
1423
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001424/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1425 on failure. If successful, the pointer *filename will contain newly-allocated
1426 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001427int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001428 unsigned *column, unsigned *second_line,
1429 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001430 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001431 const char *last_colon = strrchr(input, ':');
1432 unsigned values[4], i;
1433 unsigned num_values = (second_line && second_column)? 4 : 2;
1434
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001435 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001436 if (!last_colon || last_colon == input) {
1437 if (num_values == 4)
1438 fprintf(stderr, "could not parse filename:line:column:line:column in "
1439 "'%s'\n", input);
1440 else
1441 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001442 return 1;
1443 }
1444
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001445 for (i = 0; i != num_values; ++i) {
1446 const char *prev_colon;
1447
1448 /* Parse the next line or column. */
1449 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1450 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001451 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001452 (i % 2 ? "column" : "line"), input);
1453 return 1;
1454 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001455
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001456 if (i + 1 == num_values)
1457 break;
1458
1459 /* Find the previous colon. */
1460 prev_colon = last_colon - 1;
1461 while (prev_colon != input && *prev_colon != ':')
1462 --prev_colon;
1463 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001464 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001465 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001466 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001467 }
1468
1469 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001470 }
1471
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001472 *line = values[0];
1473 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001474
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001475 if (second_line && second_column) {
1476 *second_line = values[2];
1477 *second_column = values[3];
1478 }
1479
Douglas Gregor88d23952009-11-09 18:19:57 +00001480 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001481 *filename = (char*)malloc(last_colon - input + 1);
1482 memcpy(*filename, input, last_colon - input);
1483 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001484 return 0;
1485}
1486
1487const char *
1488clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1489 switch (Kind) {
1490 case CXCompletionChunk_Optional: return "Optional";
1491 case CXCompletionChunk_TypedText: return "TypedText";
1492 case CXCompletionChunk_Text: return "Text";
1493 case CXCompletionChunk_Placeholder: return "Placeholder";
1494 case CXCompletionChunk_Informative: return "Informative";
1495 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1496 case CXCompletionChunk_LeftParen: return "LeftParen";
1497 case CXCompletionChunk_RightParen: return "RightParen";
1498 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1499 case CXCompletionChunk_RightBracket: return "RightBracket";
1500 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1501 case CXCompletionChunk_RightBrace: return "RightBrace";
1502 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1503 case CXCompletionChunk_RightAngle: return "RightAngle";
1504 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001505 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001506 case CXCompletionChunk_Colon: return "Colon";
1507 case CXCompletionChunk_SemiColon: return "SemiColon";
1508 case CXCompletionChunk_Equal: return "Equal";
1509 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1510 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001511 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001512
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001513 return "Unknown";
1514}
1515
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001516static int checkForErrors(CXTranslationUnit TU) {
1517 unsigned Num, i;
1518 CXDiagnostic Diag;
1519 CXString DiagStr;
1520
1521 if (!getenv("CINDEXTEST_FAILONERROR"))
1522 return 0;
1523
1524 Num = clang_getNumDiagnostics(TU);
1525 for (i = 0; i != Num; ++i) {
1526 Diag = clang_getDiagnostic(TU, i);
1527 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1528 DiagStr = clang_formatDiagnostic(Diag,
1529 clang_defaultDiagnosticDisplayOptions());
1530 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1531 clang_disposeString(DiagStr);
1532 clang_disposeDiagnostic(Diag);
1533 return -1;
1534 }
1535 clang_disposeDiagnostic(Diag);
1536 }
1537
1538 return 0;
1539}
1540
Douglas Gregor3ac73852009-11-09 16:04:45 +00001541void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001542 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001543
Douglas Gregor3ac73852009-11-09 16:04:45 +00001544 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001545 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001546 CXString text;
1547 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001548 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001549 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001550
Douglas Gregor3ac73852009-11-09 16:04:45 +00001551 if (Kind == CXCompletionChunk_Optional) {
1552 fprintf(file, "{Optional ");
1553 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001554 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001555 file);
1556 fprintf(file, "}");
1557 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001558 }
1559
1560 if (Kind == CXCompletionChunk_VerticalSpace) {
1561 fprintf(file, "{VerticalSpace }");
1562 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001563 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001564
Douglas Gregord5a20892009-11-09 17:05:28 +00001565 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001566 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001567 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001568 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001569 cstr ? cstr : "");
1570 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001571 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001572
Douglas Gregor3ac73852009-11-09 16:04:45 +00001573}
1574
1575void print_completion_result(CXCompletionResult *completion_result,
1576 CXClientData client_data) {
1577 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001578 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001579 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001580 enum CXCursorKind ParentKind;
1581 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001582 CXString BriefComment;
1583 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001584
Ted Kremeneke68fff62010-02-17 00:41:32 +00001585 fprintf(file, "%s:", clang_getCString(ks));
1586 clang_disposeString(ks);
1587
Douglas Gregor3ac73852009-11-09 16:04:45 +00001588 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001589 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001590 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001591 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1592 case CXAvailability_Available:
1593 break;
1594
1595 case CXAvailability_Deprecated:
1596 fprintf(file, " (deprecated)");
1597 break;
1598
1599 case CXAvailability_NotAvailable:
1600 fprintf(file, " (unavailable)");
1601 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001602
1603 case CXAvailability_NotAccessible:
1604 fprintf(file, " (inaccessible)");
1605 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001606 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001607
1608 annotationCount = clang_getCompletionNumAnnotations(
1609 completion_result->CompletionString);
1610 if (annotationCount) {
1611 unsigned i;
1612 fprintf(file, " (");
1613 for (i = 0; i < annotationCount; ++i) {
1614 if (i != 0)
1615 fprintf(file, ", ");
1616 fprintf(file, "\"%s\"",
1617 clang_getCString(clang_getCompletionAnnotation(
1618 completion_result->CompletionString, i)));
1619 }
1620 fprintf(file, ")");
1621 }
1622
Douglas Gregorba103062012-03-27 23:34:16 +00001623 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1624 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1625 &ParentKind);
1626 if (ParentKind != CXCursor_NotImplemented) {
1627 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1628 fprintf(file, " (parent: %s '%s')",
1629 clang_getCString(KindSpelling),
1630 clang_getCString(ParentName));
1631 clang_disposeString(KindSpelling);
1632 }
1633 clang_disposeString(ParentName);
1634 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001635
1636 BriefComment = clang_getCompletionBriefComment(
1637 completion_result->CompletionString);
1638 BriefCommentCString = clang_getCString(BriefComment);
1639 if (BriefCommentCString && *BriefCommentCString != '\0') {
1640 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1641 }
1642 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001643
Douglas Gregor58ddb602010-08-23 23:00:57 +00001644 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001645}
1646
Douglas Gregor3da626b2011-07-07 16:03:39 +00001647void print_completion_contexts(unsigned long long contexts, FILE *file) {
1648 fprintf(file, "Completion contexts:\n");
1649 if (contexts == CXCompletionContext_Unknown) {
1650 fprintf(file, "Unknown\n");
1651 }
1652 if (contexts & CXCompletionContext_AnyType) {
1653 fprintf(file, "Any type\n");
1654 }
1655 if (contexts & CXCompletionContext_AnyValue) {
1656 fprintf(file, "Any value\n");
1657 }
1658 if (contexts & CXCompletionContext_ObjCObjectValue) {
1659 fprintf(file, "Objective-C object value\n");
1660 }
1661 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1662 fprintf(file, "Objective-C selector value\n");
1663 }
1664 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1665 fprintf(file, "C++ class type value\n");
1666 }
1667 if (contexts & CXCompletionContext_DotMemberAccess) {
1668 fprintf(file, "Dot member access\n");
1669 }
1670 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1671 fprintf(file, "Arrow member access\n");
1672 }
1673 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1674 fprintf(file, "Objective-C property access\n");
1675 }
1676 if (contexts & CXCompletionContext_EnumTag) {
1677 fprintf(file, "Enum tag\n");
1678 }
1679 if (contexts & CXCompletionContext_UnionTag) {
1680 fprintf(file, "Union tag\n");
1681 }
1682 if (contexts & CXCompletionContext_StructTag) {
1683 fprintf(file, "Struct tag\n");
1684 }
1685 if (contexts & CXCompletionContext_ClassTag) {
1686 fprintf(file, "Class name\n");
1687 }
1688 if (contexts & CXCompletionContext_Namespace) {
1689 fprintf(file, "Namespace or namespace alias\n");
1690 }
1691 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1692 fprintf(file, "Nested name specifier\n");
1693 }
1694 if (contexts & CXCompletionContext_ObjCInterface) {
1695 fprintf(file, "Objective-C interface\n");
1696 }
1697 if (contexts & CXCompletionContext_ObjCProtocol) {
1698 fprintf(file, "Objective-C protocol\n");
1699 }
1700 if (contexts & CXCompletionContext_ObjCCategory) {
1701 fprintf(file, "Objective-C category\n");
1702 }
1703 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1704 fprintf(file, "Objective-C instance method\n");
1705 }
1706 if (contexts & CXCompletionContext_ObjCClassMessage) {
1707 fprintf(file, "Objective-C class method\n");
1708 }
1709 if (contexts & CXCompletionContext_ObjCSelectorName) {
1710 fprintf(file, "Objective-C selector name\n");
1711 }
1712 if (contexts & CXCompletionContext_MacroName) {
1713 fprintf(file, "Macro name\n");
1714 }
1715 if (contexts & CXCompletionContext_NaturalLanguage) {
1716 fprintf(file, "Natural language\n");
1717 }
1718}
1719
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001720int my_stricmp(const char *s1, const char *s2) {
1721 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001722 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001723 if (c1 < c2)
1724 return -1;
1725 else if (c1 > c2)
1726 return 1;
1727
1728 ++s1;
1729 ++s2;
1730 }
1731
1732 if (*s1)
1733 return 1;
1734 else if (*s2)
1735 return -1;
1736 return 0;
1737}
1738
Douglas Gregor1982c182010-07-12 18:38:41 +00001739int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001740 const char *input = argv[1];
1741 char *filename = 0;
1742 unsigned line;
1743 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001744 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001745 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001746 struct CXUnsavedFile *unsaved_files = 0;
1747 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001748 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001749 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001750 unsigned I, Repeats = 1;
1751 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1752
1753 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1754 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001755 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1756 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001757
Douglas Gregor1982c182010-07-12 18:38:41 +00001758 if (timing_only)
1759 input += strlen("-code-completion-timing=");
1760 else
1761 input += strlen("-code-completion-at=");
1762
Ted Kremeneke68fff62010-02-17 00:41:32 +00001763 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001764 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001765 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001766
Douglas Gregor735df882009-12-02 09:21:34 +00001767 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1768 return -1;
1769
Douglas Gregor32be4a52010-10-11 21:37:58 +00001770 CIdx = clang_createIndex(0, 0);
1771
1772 if (getenv("CINDEXTEST_EDITING"))
1773 Repeats = 5;
1774
1775 TU = clang_parseTranslationUnit(CIdx, 0,
1776 argv + num_unsaved_files + 2,
1777 argc - num_unsaved_files - 2,
1778 0, 0, getDefaultParsingOptions());
1779 if (!TU) {
1780 fprintf(stderr, "Unable to load translation unit!\n");
1781 return 1;
1782 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001783
1784 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1785 fprintf(stderr, "Unable to reparse translation init!\n");
1786 return 1;
1787 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001788
1789 for (I = 0; I != Repeats; ++I) {
1790 results = clang_codeCompleteAt(TU, filename, line, column,
1791 unsaved_files, num_unsaved_files,
1792 completionOptions);
1793 if (!results) {
1794 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001795 return 1;
1796 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001797 if (I != Repeats-1)
1798 clang_disposeCodeCompleteResults(results);
1799 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001800
Douglas Gregorec6762c2009-12-18 16:20:58 +00001801 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001802 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001803 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001804 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001805 CXString objCSelector;
1806 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001807 if (!timing_only) {
1808 /* Sort the code-completion results based on the typed text. */
1809 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1810
Douglas Gregor1982c182010-07-12 18:38:41 +00001811 for (i = 0; i != n; ++i)
1812 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001813 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001814 n = clang_codeCompleteGetNumDiagnostics(results);
1815 for (i = 0; i != n; ++i) {
1816 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1817 PrintDiagnostic(diag);
1818 clang_disposeDiagnostic(diag);
1819 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001820
1821 contexts = clang_codeCompleteGetContexts(results);
1822 print_completion_contexts(contexts, stdout);
1823
Douglas Gregor0a47d692011-07-26 15:24:30 +00001824 containerKind = clang_codeCompleteGetContainerKind(results,
1825 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001826
1827 if (containerKind != CXCursor_InvalidCode) {
1828 /* We have found a container */
1829 CXString containerUSR, containerKindSpelling;
1830 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1831 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1832 clang_disposeString(containerKindSpelling);
1833
1834 if (containerIsIncomplete) {
1835 printf("Container is incomplete\n");
1836 }
1837 else {
1838 printf("Container is complete\n");
1839 }
1840
1841 containerUSR = clang_codeCompleteGetContainerUSR(results);
1842 printf("Container USR: %s\n", clang_getCString(containerUSR));
1843 clang_disposeString(containerUSR);
1844 }
1845
Douglas Gregor0a47d692011-07-26 15:24:30 +00001846 objCSelector = clang_codeCompleteGetObjCSelector(results);
1847 selectorString = clang_getCString(objCSelector);
1848 if (selectorString && strlen(selectorString) > 0) {
1849 printf("Objective-C selector: %s\n", selectorString);
1850 }
1851 clang_disposeString(objCSelector);
1852
Douglas Gregorec6762c2009-12-18 16:20:58 +00001853 clang_disposeCodeCompleteResults(results);
1854 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001855 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001856 clang_disposeIndex(CIdx);
1857 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001858
Douglas Gregor735df882009-12-02 09:21:34 +00001859 free_remapped_files(unsaved_files, num_unsaved_files);
1860
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001861 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001862}
1863
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001864typedef struct {
1865 char *filename;
1866 unsigned line;
1867 unsigned column;
1868} CursorSourceLocation;
1869
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001870static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001871 CXIndex CIdx;
1872 int errorCode;
1873 struct CXUnsavedFile *unsaved_files = 0;
1874 int num_unsaved_files = 0;
1875 CXTranslationUnit TU;
1876 CXCursor Cursor;
1877 CursorSourceLocation *Locations = 0;
1878 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001879 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001880 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001881
Ted Kremeneke68fff62010-02-17 00:41:32 +00001882 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001883 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1884 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001885
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001886 /* Parse the locations. */
1887 assert(NumLocations > 0 && "Unable to count locations?");
1888 Locations = (CursorSourceLocation *)malloc(
1889 NumLocations * sizeof(CursorSourceLocation));
1890 for (Loc = 0; Loc < NumLocations; ++Loc) {
1891 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001892 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1893 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001894 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001895 return errorCode;
1896 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001897
1898 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001899 &num_unsaved_files))
1900 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001901
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001902 if (getenv("CINDEXTEST_EDITING"))
1903 Repeats = 5;
1904
1905 /* Parse the translation unit. When we're testing clang_getCursor() after
1906 reparsing, don't remap unsaved files until the second parse. */
1907 CIdx = clang_createIndex(1, 1);
1908 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1909 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001910 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001911 unsaved_files,
1912 Repeats > 1? 0 : num_unsaved_files,
1913 getDefaultParsingOptions());
1914
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001915 if (!TU) {
1916 fprintf(stderr, "unable to parse input\n");
1917 return -1;
1918 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001919
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001920 if (checkForErrors(TU) != 0)
1921 return -1;
1922
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001923 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001924 if (Repeats > 1 &&
1925 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1926 clang_defaultReparseOptions(TU))) {
1927 clang_disposeTranslationUnit(TU);
1928 return 1;
1929 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001930
1931 if (checkForErrors(TU) != 0)
1932 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001933
1934 for (Loc = 0; Loc < NumLocations; ++Loc) {
1935 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1936 if (!file)
1937 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001938
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001939 Cursor = clang_getCursor(TU,
1940 clang_getLocation(TU, file, Locations[Loc].line,
1941 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001942
1943 if (checkForErrors(TU) != 0)
1944 return -1;
1945
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001946 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001947 CXCompletionString completionString = clang_getCursorCompletionString(
1948 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001949 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1950 CXString Spelling;
1951 const char *cspell;
1952 unsigned line, column;
1953 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1954 printf("%d:%d ", line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001955 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001956 PrintCursorExtent(Cursor);
1957 Spelling = clang_getCursorSpelling(Cursor);
1958 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001959 if (cspell && strlen(cspell) != 0) {
1960 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001961 printf(" Spelling=%s (", cspell);
1962 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001963 CXSourceRange range =
1964 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001965 if (clang_Range_isNull(range))
1966 break;
1967 PrintRange(range, 0);
1968 }
1969 printf(")");
1970 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001971 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001972 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1973 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001974 if (clang_Cursor_isDynamicCall(Cursor))
1975 printf(" Dynamic-call");
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00001976 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
1977 CXType T = clang_Cursor_getReceiverType(Cursor);
1978 CXString S = clang_getTypeKindSpelling(T.kind);
1979 printf(" Receiver-type=%s", clang_getCString(S));
1980 clang_disposeString(S);
1981 }
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001982
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00001983 {
1984 CXModule mod = clang_Cursor_getModule(Cursor);
1985 CXString name;
1986 unsigned i, numHeaders;
1987 if (mod) {
1988 name = clang_Module_getFullName(mod);
1989 numHeaders = clang_Module_getNumTopLevelHeaders(mod);
1990 printf(" ModuleName=%s Headers(%d):",
1991 clang_getCString(name), numHeaders);
1992 clang_disposeString(name);
1993 for (i = 0; i < numHeaders; ++i) {
1994 CXFile file = clang_Module_getTopLevelHeader(mod, i);
1995 CXString filename = clang_getFileName(file);
1996 printf("\n%s", clang_getCString(filename));
1997 clang_disposeString(filename);
1998 }
1999 }
2000 }
2001
Douglas Gregor8fa0a802011-08-04 20:04:59 +00002002 if (completionString != NULL) {
2003 printf("\nCompletion string: ");
2004 print_completion_string(completionString, stdout);
2005 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002006 printf("\n");
2007 free(Locations[Loc].filename);
2008 }
2009 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002010 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002011
Douglas Gregora88084b2010-02-18 18:08:43 +00002012 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002013 clang_disposeTranslationUnit(TU);
2014 clang_disposeIndex(CIdx);
2015 free(Locations);
2016 free_remapped_files(unsaved_files, num_unsaved_files);
2017 return 0;
2018}
2019
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002020static enum CXVisitorResult findFileRefsVisit(void *context,
2021 CXCursor cursor, CXSourceRange range) {
2022 if (clang_Range_isNull(range))
2023 return CXVisit_Continue;
2024
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002025 PrintCursor(cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002026 PrintRange(range, "");
2027 printf("\n");
2028 return CXVisit_Continue;
2029}
2030
2031static int find_file_refs_at(int argc, const char **argv) {
2032 CXIndex CIdx;
2033 int errorCode;
2034 struct CXUnsavedFile *unsaved_files = 0;
2035 int num_unsaved_files = 0;
2036 CXTranslationUnit TU;
2037 CXCursor Cursor;
2038 CursorSourceLocation *Locations = 0;
2039 unsigned NumLocations = 0, Loc;
2040 unsigned Repeats = 1;
2041 unsigned I;
2042
2043 /* Count the number of locations. */
2044 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2045 ++NumLocations;
2046
2047 /* Parse the locations. */
2048 assert(NumLocations > 0 && "Unable to count locations?");
2049 Locations = (CursorSourceLocation *)malloc(
2050 NumLocations * sizeof(CursorSourceLocation));
2051 for (Loc = 0; Loc < NumLocations; ++Loc) {
2052 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2053 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2054 &Locations[Loc].line,
2055 &Locations[Loc].column, 0, 0)))
2056 return errorCode;
2057 }
2058
2059 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2060 &num_unsaved_files))
2061 return -1;
2062
2063 if (getenv("CINDEXTEST_EDITING"))
2064 Repeats = 5;
2065
2066 /* Parse the translation unit. When we're testing clang_getCursor() after
2067 reparsing, don't remap unsaved files until the second parse. */
2068 CIdx = clang_createIndex(1, 1);
2069 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2070 argv + num_unsaved_files + 1 + NumLocations,
2071 argc - num_unsaved_files - 2 - NumLocations,
2072 unsaved_files,
2073 Repeats > 1? 0 : num_unsaved_files,
2074 getDefaultParsingOptions());
2075
2076 if (!TU) {
2077 fprintf(stderr, "unable to parse input\n");
2078 return -1;
2079 }
2080
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002081 if (checkForErrors(TU) != 0)
2082 return -1;
2083
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002084 for (I = 0; I != Repeats; ++I) {
2085 if (Repeats > 1 &&
2086 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2087 clang_defaultReparseOptions(TU))) {
2088 clang_disposeTranslationUnit(TU);
2089 return 1;
2090 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002091
2092 if (checkForErrors(TU) != 0)
2093 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002094
2095 for (Loc = 0; Loc < NumLocations; ++Loc) {
2096 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2097 if (!file)
2098 continue;
2099
2100 Cursor = clang_getCursor(TU,
2101 clang_getLocation(TU, file, Locations[Loc].line,
2102 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002103
2104 if (checkForErrors(TU) != 0)
2105 return -1;
2106
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002107 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002108 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002109 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002110 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002111 clang_findReferencesInFile(Cursor, file, visitor);
2112 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002113
2114 if (checkForErrors(TU) != 0)
2115 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002116 }
2117 }
2118 }
2119
2120 PrintDiagnostics(TU);
2121 clang_disposeTranslationUnit(TU);
2122 clang_disposeIndex(CIdx);
2123 free(Locations);
2124 free_remapped_files(unsaved_files, num_unsaved_files);
2125 return 0;
2126}
2127
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002128#define MAX_IMPORTED_ASTFILES 200
2129
2130typedef struct {
2131 char **filenames;
2132 unsigned num_files;
2133} ImportedASTFilesData;
2134
2135static ImportedASTFilesData *importedASTs_create() {
2136 ImportedASTFilesData *p;
2137 p = malloc(sizeof(ImportedASTFilesData));
2138 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2139 p->num_files = 0;
2140 return p;
2141}
2142
2143static void importedASTs_dispose(ImportedASTFilesData *p) {
2144 unsigned i;
2145 if (!p)
2146 return;
2147
2148 for (i = 0; i < p->num_files; ++i)
2149 free(p->filenames[i]);
2150 free(p->filenames);
2151 free(p);
2152}
2153
2154static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2155 unsigned i;
2156 assert(p && file);
2157 for (i = 0; i < p->num_files; ++i)
2158 if (strcmp(file, p->filenames[i]) == 0)
2159 return;
2160 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2161 p->filenames[p->num_files++] = strdup(file);
2162}
2163
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002164typedef struct {
2165 const char *check_prefix;
2166 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002167 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002168 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002169 const char *main_filename;
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002170 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002171} IndexData;
2172
2173static void printCheck(IndexData *data) {
2174 if (data->check_prefix) {
2175 if (data->first_check_printed) {
2176 printf("// %s-NEXT: ", data->check_prefix);
2177 } else {
2178 printf("// %s : ", data->check_prefix);
2179 data->first_check_printed = 1;
2180 }
2181 }
2182}
2183
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002184static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002185 CXString filename = clang_getFileName((CXFile)file);
2186 printf("%s", clang_getCString(filename));
2187 clang_disposeString(filename);
2188}
2189
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002190static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2191 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002192 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002193 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002194 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002195 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002196 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002197
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002198 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002199 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2200 if (line == 0) {
Argyrios Kyrtzidis8003fd62012-10-11 19:00:44 +00002201 printf("<invalid>");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002202 return;
2203 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002204 if (!file) {
2205 printf("<no idxfile>");
2206 return;
2207 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002208 filename = clang_getFileName((CXFile)file);
2209 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002210 if (strcmp(cname, index_data->main_filename) == 0)
2211 isMainFile = 1;
2212 else
2213 isMainFile = 0;
2214 clang_disposeString(filename);
2215
2216 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002217 printCXIndexFile(file);
2218 printf(":");
2219 }
2220 printf("%d:%d", line, column);
2221}
2222
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002223static unsigned digitCount(unsigned val) {
2224 unsigned c = 1;
2225 while (1) {
2226 if (val < 10)
2227 return c;
2228 ++c;
2229 val /= 10;
2230 }
2231}
2232
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002233static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2234 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002235 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002236 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002237 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002238 unsigned line, column;
2239
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002240 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002241 if (!name)
2242 name = "<anon-tag>";
2243
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002244 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002245 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002246 newStr = (char *)malloc(strlen(name) +
2247 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002248 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002249 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002250}
2251
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002252static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2253 CXIdxClientContainer container;
2254 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002255 if (!container)
2256 printf("[<<NULL>>]");
2257 else
2258 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002259}
2260
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002261static const char *getEntityKindString(CXIdxEntityKind kind) {
2262 switch (kind) {
2263 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2264 case CXIdxEntity_Typedef: return "typedef";
2265 case CXIdxEntity_Function: return "function";
2266 case CXIdxEntity_Variable: return "variable";
2267 case CXIdxEntity_Field: return "field";
2268 case CXIdxEntity_EnumConstant: return "enumerator";
2269 case CXIdxEntity_ObjCClass: return "objc-class";
2270 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2271 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002272 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2273 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002274 case CXIdxEntity_ObjCProperty: return "objc-property";
2275 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2276 case CXIdxEntity_Enum: return "enum";
2277 case CXIdxEntity_Struct: return "struct";
2278 case CXIdxEntity_Union: return "union";
2279 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002280 case CXIdxEntity_CXXNamespace: return "namespace";
2281 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2282 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2283 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2284 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2285 case CXIdxEntity_CXXConstructor: return "constructor";
2286 case CXIdxEntity_CXXDestructor: return "destructor";
2287 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2288 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikie35adca02012-08-31 21:55:26 +00002289 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002290 }
2291 assert(0 && "Garbage entity kind");
2292 return 0;
2293}
2294
2295static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2296 switch (kind) {
2297 case CXIdxEntity_NonTemplate: return "";
2298 case CXIdxEntity_Template: return "-template";
2299 case CXIdxEntity_TemplatePartialSpecialization:
2300 return "-template-partial-spec";
2301 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002302 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002303 assert(0 && "Garbage entity kind");
2304 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002305}
2306
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002307static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2308 switch (kind) {
2309 case CXIdxEntityLang_None: return "<none>";
2310 case CXIdxEntityLang_C: return "C";
2311 case CXIdxEntityLang_ObjC: return "ObjC";
2312 case CXIdxEntityLang_CXX: return "C++";
2313 }
2314 assert(0 && "Garbage language kind");
2315 return 0;
2316}
2317
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002318static void printEntityInfo(const char *cb,
2319 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002320 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002321 const char *name;
2322 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002323 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002324 index_data = (IndexData *)client_data;
2325 printCheck(index_data);
2326
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002327 if (!info) {
2328 printf("%s: <<NULL>>", cb);
2329 return;
2330 }
2331
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002332 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002333 if (!name)
2334 name = "<anon-tag>";
2335
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002336 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2337 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002338 printf(" | name: %s", name);
2339 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002340 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002341
2342 for (i = 0; i != info->numAttributes; ++i) {
2343 const CXIdxAttrInfo *Attr = info->attributes[i];
2344 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002345 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002346 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002347}
2348
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002349static void printBaseClassInfo(CXClientData client_data,
2350 const CXIdxBaseClassInfo *info) {
2351 printEntityInfo(" <base>", client_data, info->base);
2352 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002353 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002354 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002355 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002356}
2357
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002358static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2359 CXClientData client_data) {
2360 unsigned i;
2361 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2362 printEntityInfo(" <protocol>", client_data,
2363 ProtoInfo->protocols[i]->protocol);
2364 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002365 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002366 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002367 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002368 printf("\n");
2369 }
2370}
2371
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002372static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002373 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002374 CXString str;
2375 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002376 unsigned numDiags, i;
2377 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002378 IndexData *index_data;
2379 index_data = (IndexData *)client_data;
2380 printCheck(index_data);
2381
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002382 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2383 for (i = 0; i != numDiags; ++i) {
2384 diag = clang_getDiagnosticInSet(diagSet, i);
2385 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2386 cstr = clang_getCString(str);
2387 printf("[diagnostic]: %s\n", cstr);
2388 clang_disposeString(str);
2389
2390 if (getenv("CINDEXTEST_FAILONERROR") &&
2391 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2392 index_data->fail_for_error = 1;
2393 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002394 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002395}
2396
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002397static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2398 CXFile file, void *reserved) {
2399 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002400 CXString filename;
2401
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002402 index_data = (IndexData *)client_data;
2403 printCheck(index_data);
2404
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002405 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002406 index_data->main_filename = clang_getCString(filename);
2407 clang_disposeString(filename);
2408
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002409 printf("[enteredMainFile]: ");
2410 printCXIndexFile((CXIdxClientFile)file);
2411 printf("\n");
2412
2413 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002414}
2415
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002416static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002417 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002418 IndexData *index_data;
2419 index_data = (IndexData *)client_data;
2420 printCheck(index_data);
2421
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002422 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002423 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002424 printf(" | name: \"%s\"", info->filename);
2425 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002426 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis8d7a24e2012-10-18 00:17:05 +00002427 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2428 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002429
2430 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002431}
2432
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002433static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2434 const CXIdxImportedASTFileInfo *info) {
2435 IndexData *index_data;
2436 index_data = (IndexData *)client_data;
2437 printCheck(index_data);
2438
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002439 if (index_data->importedASTs) {
2440 CXString filename = clang_getFileName(info->file);
2441 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2442 clang_disposeString(filename);
2443 }
2444
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002445 printf("[importedASTFile]: ");
2446 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002447 if (info->module) {
2448 CXString name = clang_Module_getFullName(info->module);
2449 printf(" | loc: ");
2450 printCXIndexLoc(info->loc, client_data);
2451 printf(" | name: \"%s\"", clang_getCString(name));
2452 printf(" | isImplicit: %d\n", info->isImplicit);
2453 clang_disposeString(name);
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002454 } else {
NAKAMURA Takumi3c5527e2012-10-12 14:25:52 +00002455 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002456 printf("\n");
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002457 }
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002458
2459 return (CXIdxClientFile)info->file;
2460}
2461
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002462static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002463 void *reserved) {
2464 IndexData *index_data;
2465 index_data = (IndexData *)client_data;
2466 printCheck(index_data);
2467
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002468 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002469 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002470}
2471
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002472static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002473 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002474 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002475 const CXIdxObjCCategoryDeclInfo *CatInfo;
2476 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002477 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002478 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002479 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002480 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002481 index_data = (IndexData *)client_data;
2482
2483 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2484 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002485 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002486 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002487 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002488 printf(" | semantic-container: ");
2489 printCXIndexContainer(info->semanticContainer);
2490 printf(" | lexical-container: ");
2491 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002492 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002493 printf(" | isDef: %d", info->isDefinition);
2494 printf(" | isContainer: %d", info->isContainer);
2495 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002496
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002497 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002498 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002499 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002500 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002501 printf("\n");
2502 }
2503
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002504 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2505 const char *kindName = 0;
2506 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2507 switch (K) {
2508 case CXIdxObjCContainer_ForwardRef:
2509 kindName = "forward-ref"; break;
2510 case CXIdxObjCContainer_Interface:
2511 kindName = "interface"; break;
2512 case CXIdxObjCContainer_Implementation:
2513 kindName = "implementation"; break;
2514 }
2515 printCheck(index_data);
2516 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2517 }
2518
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002519 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002520 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2521 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002522 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002523 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002524 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002525 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002526 printf("\n");
2527 }
2528
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002529 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2530 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002531 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002532 printf("\n");
2533 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002534 }
2535
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002536 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2537 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002538 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002539
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002540 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2541 if (PropInfo->getter) {
2542 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2543 printf("\n");
2544 }
2545 if (PropInfo->setter) {
2546 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2547 printf("\n");
2548 }
2549 }
2550
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002551 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2552 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2553 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2554 printf("\n");
2555 }
2556 }
2557
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002558 if (info->declAsContainer)
2559 clang_index_setClientContainer(info->declAsContainer,
2560 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002561}
2562
2563static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002564 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002565 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002566 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002567 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002568 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002569 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002570 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002571 printf(" | container: ");
2572 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002573 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002574 switch (info->kind) {
2575 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002576 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002577 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002578 printf("\n");
2579}
2580
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002581static int index_abortQuery(CXClientData client_data, void *reserved) {
2582 IndexData *index_data;
2583 index_data = (IndexData *)client_data;
2584 return index_data->abort;
2585}
2586
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002587static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002588 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002589 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002590 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002591 index_ppIncludedFile,
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002592 index_importedASTFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002593 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002594 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002595 index_indexEntityReference
2596};
2597
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002598static unsigned getIndexOptions(void) {
2599 unsigned index_opts;
2600 index_opts = 0;
2601 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2602 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2603 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2604 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2605
2606 return index_opts;
2607}
2608
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002609static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002610 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002611 CXIndex Idx;
2612 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002613 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002614 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002615 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002616
2617 check_prefix = 0;
2618 if (argc > 0) {
2619 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2620 check_prefix = argv[0] + strlen("-check-prefix=");
2621 ++argv;
2622 --argc;
2623 }
2624 }
2625
2626 if (argc == 0) {
2627 fprintf(stderr, "no compiler arguments\n");
2628 return -1;
2629 }
2630
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002631 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2632 /* displayDiagnosics=*/1))) {
2633 fprintf(stderr, "Could not create Index\n");
2634 return 1;
2635 }
2636 idxAction = 0;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002637
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002638 index_data.check_prefix = check_prefix;
2639 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002640 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002641 index_data.abort = 0;
Argyrios Kyrtzidis0ef983e2012-10-11 19:38:23 +00002642 index_data.main_filename = "";
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002643 index_data.importedASTs = 0;
2644
2645 if (full)
2646 index_data.importedASTs = importedASTs_create();
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002647
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002648 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002649 idxAction = clang_IndexAction_create(Idx);
2650 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002651 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidis3fe97782012-10-02 16:10:41 +00002652 0, argv, argc, 0, 0, 0,
2653 getDefaultParsingOptions());
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002654 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002655 result = -1;
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002656
2657 if (full) {
2658 CXTranslationUnit TU;
2659 unsigned i;
2660
2661 for (i = 0; i < index_data.importedASTs->num_files; ++i) {
2662 if (!CreateTranslationUnit(Idx, index_data.importedASTs->filenames[i],
2663 &TU)) {
2664 result = -1;
2665 goto finished;
2666 }
2667 result = clang_indexTranslationUnit(idxAction, &index_data,
2668 &IndexCB,sizeof(IndexCB),
2669 index_opts, TU);
2670 clang_disposeTranslationUnit(TU);
2671 }
2672 }
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002673
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002674finished:
2675 importedASTs_dispose(index_data.importedASTs);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002676 clang_IndexAction_dispose(idxAction);
2677 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002678 return result;
2679}
2680
2681static int index_tu(int argc, const char **argv) {
2682 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002683 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002684 CXTranslationUnit TU;
2685 const char *check_prefix;
2686 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002687 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002688 int result;
2689
2690 check_prefix = 0;
2691 if (argc > 0) {
2692 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2693 check_prefix = argv[0] + strlen("-check-prefix=");
2694 ++argv;
2695 --argc;
2696 }
2697 }
2698
2699 if (argc == 0) {
2700 fprintf(stderr, "no ast file\n");
2701 return -1;
2702 }
2703
2704 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2705 /* displayDiagnosics=*/1))) {
2706 fprintf(stderr, "Could not create Index\n");
2707 return 1;
2708 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002709 idxAction = 0;
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002710 TU = 0;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002711 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002712
2713 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002714 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002715
2716 index_data.check_prefix = check_prefix;
2717 index_data.first_check_printed = 0;
2718 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002719 index_data.abort = 0;
Argyrios Kyrtzidis0ef983e2012-10-11 19:38:23 +00002720 index_data.main_filename = "";
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002721 index_data.importedASTs = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002722
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002723 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002724 idxAction = clang_IndexAction_create(Idx);
2725 result = clang_indexTranslationUnit(idxAction, &index_data,
2726 &IndexCB,sizeof(IndexCB),
2727 index_opts, TU);
2728 if (index_data.fail_for_error)
2729 goto finished;
2730
2731 finished:
2732 clang_IndexAction_dispose(idxAction);
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002733 clang_disposeTranslationUnit(TU);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002734 clang_disposeIndex(Idx);
2735
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002736 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002737}
2738
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002739int perform_token_annotation(int argc, const char **argv) {
2740 const char *input = argv[1];
2741 char *filename = 0;
2742 unsigned line, second_line;
2743 unsigned column, second_column;
2744 CXIndex CIdx;
2745 CXTranslationUnit TU = 0;
2746 int errorCode;
2747 struct CXUnsavedFile *unsaved_files = 0;
2748 int num_unsaved_files = 0;
2749 CXToken *tokens;
2750 unsigned num_tokens;
2751 CXSourceRange range;
2752 CXSourceLocation startLoc, endLoc;
2753 CXFile file = 0;
2754 CXCursor *cursors = 0;
2755 unsigned i;
2756
2757 input += strlen("-test-annotate-tokens=");
2758 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2759 &second_line, &second_column)))
2760 return errorCode;
2761
Richard Smithe07c5f82012-07-05 08:20:49 +00002762 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
2763 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002764 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00002765 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002766
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002767 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002768 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2769 argv + num_unsaved_files + 2,
2770 argc - num_unsaved_files - 3,
2771 unsaved_files,
2772 num_unsaved_files,
2773 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002774 if (!TU) {
2775 fprintf(stderr, "unable to parse input\n");
2776 clang_disposeIndex(CIdx);
2777 free(filename);
2778 free_remapped_files(unsaved_files, num_unsaved_files);
2779 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002780 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002781 errorCode = 0;
2782
Richard Smithe07c5f82012-07-05 08:20:49 +00002783 if (checkForErrors(TU) != 0) {
2784 errorCode = -1;
2785 goto teardown;
2786 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002787
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002788 if (getenv("CINDEXTEST_EDITING")) {
2789 for (i = 0; i < 5; ++i) {
2790 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2791 clang_defaultReparseOptions(TU))) {
2792 fprintf(stderr, "Unable to reparse translation unit!\n");
2793 errorCode = -1;
2794 goto teardown;
2795 }
2796 }
2797 }
2798
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002799 if (checkForErrors(TU) != 0) {
2800 errorCode = -1;
2801 goto teardown;
2802 }
2803
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002804 file = clang_getFile(TU, filename);
2805 if (!file) {
2806 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2807 errorCode = -1;
2808 goto teardown;
2809 }
2810
2811 startLoc = clang_getLocation(TU, file, line, column);
2812 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002813 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002814 column);
2815 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002816 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002817 }
2818
2819 endLoc = clang_getLocation(TU, file, second_line, second_column);
2820 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002821 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002822 second_line, second_column);
2823 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002824 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002825 }
2826
2827 range = clang_getRange(startLoc, endLoc);
2828 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002829
2830 if (checkForErrors(TU) != 0) {
2831 errorCode = -1;
2832 goto teardown;
2833 }
2834
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002835 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2836 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002837
2838 if (checkForErrors(TU) != 0) {
2839 errorCode = -1;
2840 goto teardown;
2841 }
2842
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002843 for (i = 0; i != num_tokens; ++i) {
2844 const char *kind = "<unknown>";
2845 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2846 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2847 unsigned start_line, start_column, end_line, end_column;
2848
2849 switch (clang_getTokenKind(tokens[i])) {
2850 case CXToken_Punctuation: kind = "Punctuation"; break;
2851 case CXToken_Keyword: kind = "Keyword"; break;
2852 case CXToken_Identifier: kind = "Identifier"; break;
2853 case CXToken_Literal: kind = "Literal"; break;
2854 case CXToken_Comment: kind = "Comment"; break;
2855 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002856 clang_getSpellingLocation(clang_getRangeStart(extent),
2857 0, &start_line, &start_column, 0);
2858 clang_getSpellingLocation(clang_getRangeEnd(extent),
2859 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002860 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002861 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002862 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002863 if (!clang_isInvalid(cursors[i].kind)) {
2864 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002865 PrintCursor(cursors[i], NULL);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002866 }
2867 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002868 }
2869 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002870 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002871
2872 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002873 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002874 clang_disposeTranslationUnit(TU);
2875 clang_disposeIndex(CIdx);
2876 free(filename);
2877 free_remapped_files(unsaved_files, num_unsaved_files);
2878 return errorCode;
2879}
2880
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002881static int
2882perform_test_compilation_db(const char *database, int argc, const char **argv) {
2883 CXCompilationDatabase db;
2884 CXCompileCommands CCmds;
2885 CXCompileCommand CCmd;
2886 CXCompilationDatabase_Error ec;
2887 CXString wd;
2888 CXString arg;
2889 int errorCode = 0;
2890 char *tmp;
2891 unsigned len;
2892 char *buildDir;
2893 int i, j, a, numCmds, numArgs;
2894
2895 len = strlen(database);
2896 tmp = (char *) malloc(len+1);
2897 memcpy(tmp, database, len+1);
2898 buildDir = dirname(tmp);
2899
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002900 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002901
2902 if (db) {
2903
2904 if (ec!=CXCompilationDatabase_NoError) {
2905 printf("unexpected error %d code while loading compilation database\n", ec);
2906 errorCode = -1;
2907 goto cdb_end;
2908 }
2909
2910 for (i=0; i<argc && errorCode==0; ) {
2911 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002912 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002913
2914 if (!CCmds) {
2915 printf("file %s not found in compilation db\n", argv[i+1]);
2916 errorCode = -1;
2917 break;
2918 }
2919
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002920 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002921
2922 if (numCmds==0) {
2923 fprintf(stderr, "should not get an empty compileCommand set for file"
2924 " '%s'\n", argv[i+1]);
2925 errorCode = -1;
2926 break;
2927 }
2928
2929 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002930 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002931
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002932 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002933 printf("workdir:'%s'", clang_getCString(wd));
2934 clang_disposeString(wd);
2935
2936 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002937 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002938 for (a=0; a<numArgs; ++a) {
2939 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002940 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002941 printf("%s", clang_getCString(arg));
2942 clang_disposeString(arg);
2943 }
2944 printf("'\n");
2945 }
2946
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002947 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002948
2949 i += 2;
2950 }
2951 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002952 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002953 } else {
2954 printf("database loading failed with error code %d.\n", ec);
2955 errorCode = -1;
2956 }
2957
2958cdb_end:
2959 free(tmp);
2960
2961 return errorCode;
2962}
2963
Ted Kremenek0d435192009-11-17 18:13:31 +00002964/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002965/* USR printing. */
2966/******************************************************************************/
2967
2968static int insufficient_usr(const char *kind, const char *usage) {
2969 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2970 return 1;
2971}
2972
2973static unsigned isUSR(const char *s) {
2974 return s[0] == 'c' && s[1] == ':';
2975}
2976
2977static int not_usr(const char *s, const char *arg) {
2978 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2979 return 1;
2980}
2981
2982static void print_usr(CXString usr) {
2983 const char *s = clang_getCString(usr);
2984 printf("%s\n", s);
2985 clang_disposeString(usr);
2986}
2987
2988static void display_usrs() {
2989 fprintf(stderr, "-print-usrs options:\n"
2990 " ObjCCategory <class name> <category name>\n"
2991 " ObjCClass <class name>\n"
2992 " ObjCIvar <ivar name> <class USR>\n"
2993 " ObjCMethod <selector> [0=class method|1=instance method] "
2994 "<class USR>\n"
2995 " ObjCProperty <property name> <class USR>\n"
2996 " ObjCProtocol <protocol name>\n");
2997}
2998
2999int print_usrs(const char **I, const char **E) {
3000 while (I != E) {
3001 const char *kind = *I;
3002 unsigned len = strlen(kind);
3003 switch (len) {
3004 case 8:
3005 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3006 if (I + 2 >= E)
3007 return insufficient_usr(kind, "<ivar name> <class USR>");
3008 if (!isUSR(I[2]))
3009 return not_usr("<class USR>", I[2]);
3010 else {
3011 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003012 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003013 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003014 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3015 }
3016
3017 I += 3;
3018 continue;
3019 }
3020 break;
3021 case 9:
3022 if (memcmp(kind, "ObjCClass", 9) == 0) {
3023 if (I + 1 >= E)
3024 return insufficient_usr(kind, "<class name>");
3025 print_usr(clang_constructUSR_ObjCClass(I[1]));
3026 I += 2;
3027 continue;
3028 }
3029 break;
3030 case 10:
3031 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3032 if (I + 3 >= E)
3033 return insufficient_usr(kind, "<method selector> "
3034 "[0=class method|1=instance method] <class USR>");
3035 if (!isUSR(I[3]))
3036 return not_usr("<class USR>", I[3]);
3037 else {
3038 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003039 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00003040 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003041 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3042 }
3043 I += 4;
3044 continue;
3045 }
3046 break;
3047 case 12:
3048 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3049 if (I + 2 >= E)
3050 return insufficient_usr(kind, "<class name> <category name>");
3051 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3052 I += 3;
3053 continue;
3054 }
3055 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3056 if (I + 1 >= E)
3057 return insufficient_usr(kind, "<protocol name>");
3058 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3059 I += 2;
3060 continue;
3061 }
3062 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3063 if (I + 2 >= E)
3064 return insufficient_usr(kind, "<property name> <class USR>");
3065 if (!isUSR(I[2]))
3066 return not_usr("<class USR>", I[2]);
3067 else {
3068 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003069 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003070 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003071 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3072 }
3073 I += 3;
3074 continue;
3075 }
3076 break;
3077 default:
3078 break;
3079 }
3080 break;
3081 }
3082
3083 if (I != E) {
3084 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3085 display_usrs();
3086 return 1;
3087 }
3088 return 0;
3089}
3090
3091int print_usrs_file(const char *file_name) {
3092 char line[2048];
3093 const char *args[128];
3094 unsigned numChars = 0;
3095
3096 FILE *fp = fopen(file_name, "r");
3097 if (!fp) {
3098 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3099 return 1;
3100 }
3101
3102 /* This code is not really all that safe, but it works fine for testing. */
3103 while (!feof(fp)) {
3104 char c = fgetc(fp);
3105 if (c == '\n') {
3106 unsigned i = 0;
3107 const char *s = 0;
3108
3109 if (numChars == 0)
3110 continue;
3111
3112 line[numChars] = '\0';
3113 numChars = 0;
3114
3115 if (line[0] == '/' && line[1] == '/')
3116 continue;
3117
3118 s = strtok(line, " ");
3119 while (s) {
3120 args[i] = s;
3121 ++i;
3122 s = strtok(0, " ");
3123 }
3124 if (print_usrs(&args[0], &args[i]))
3125 return 1;
3126 }
3127 else
3128 line[numChars++] = c;
3129 }
3130
3131 fclose(fp);
3132 return 0;
3133}
3134
3135/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00003136/* Command line processing. */
3137/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003138int write_pch_file(const char *filename, int argc, const char *argv[]) {
3139 CXIndex Idx;
3140 CXTranslationUnit TU;
3141 struct CXUnsavedFile *unsaved_files = 0;
3142 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00003143 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003144
3145 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
3146
3147 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3148 clang_disposeIndex(Idx);
3149 return -1;
3150 }
3151
3152 TU = clang_parseTranslationUnit(Idx, 0,
3153 argv + num_unsaved_files,
3154 argc - num_unsaved_files,
3155 unsaved_files,
3156 num_unsaved_files,
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003157 CXTranslationUnit_Incomplete |
3158 CXTranslationUnit_ForSerialization);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003159 if (!TU) {
3160 fprintf(stderr, "Unable to load translation unit!\n");
3161 free_remapped_files(unsaved_files, num_unsaved_files);
3162 clang_disposeIndex(Idx);
3163 return 1;
3164 }
3165
Douglas Gregor39c411f2011-07-06 16:43:36 +00003166 switch (clang_saveTranslationUnit(TU, filename,
3167 clang_defaultSaveOptions(TU))) {
3168 case CXSaveError_None:
3169 break;
3170
3171 case CXSaveError_TranslationErrors:
3172 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3173 filename);
3174 result = 2;
3175 break;
3176
3177 case CXSaveError_InvalidTU:
3178 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3179 filename);
3180 result = 3;
3181 break;
3182
3183 case CXSaveError_Unknown:
3184 default:
3185 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3186 result = 1;
3187 break;
3188 }
3189
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003190 clang_disposeTranslationUnit(TU);
3191 free_remapped_files(unsaved_files, num_unsaved_files);
3192 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00003193 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003194}
3195
3196/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00003197/* Serialized diagnostics. */
3198/******************************************************************************/
3199
3200static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3201 switch (error) {
3202 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3203 case CXLoadDiag_None: break;
3204 case CXLoadDiag_Unknown: return "Unknown";
3205 case CXLoadDiag_InvalidFile: return "Invalid File";
3206 }
3207 return "None";
3208}
3209
3210static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3211 switch (severity) {
3212 case CXDiagnostic_Note: return "note";
3213 case CXDiagnostic_Error: return "error";
3214 case CXDiagnostic_Fatal: return "fatal";
3215 case CXDiagnostic_Ignored: return "ignored";
3216 case CXDiagnostic_Warning: return "warning";
3217 }
3218 return "unknown";
3219}
3220
3221static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003222 if (indent == 0)
3223 return;
3224 fprintf(stderr, "+");
3225 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00003226 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003227 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00003228 --indent;
3229 }
3230}
3231
3232static void printLocation(CXSourceLocation L) {
3233 CXFile File;
3234 CXString FileName;
3235 unsigned line, column, offset;
3236
3237 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3238 FileName = clang_getFileName(File);
3239
3240 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3241 clang_disposeString(FileName);
3242}
3243
3244static void printRanges(CXDiagnostic D, unsigned indent) {
3245 unsigned i, n = clang_getDiagnosticNumRanges(D);
3246
3247 for (i = 0; i < n; ++i) {
3248 CXSourceLocation Start, End;
3249 CXSourceRange SR = clang_getDiagnosticRange(D, i);
3250 Start = clang_getRangeStart(SR);
3251 End = clang_getRangeEnd(SR);
3252
3253 printIndent(indent);
3254 fprintf(stderr, "Range: ");
3255 printLocation(Start);
3256 fprintf(stderr, " ");
3257 printLocation(End);
3258 fprintf(stderr, "\n");
3259 }
3260}
3261
3262static void printFixIts(CXDiagnostic D, unsigned indent) {
3263 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00003264 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00003265 for (i = 0 ; i < n; ++i) {
3266 CXSourceRange ReplacementRange;
3267 CXString text;
3268 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3269
3270 printIndent(indent);
3271 fprintf(stderr, "FIXIT: (");
3272 printLocation(clang_getRangeStart(ReplacementRange));
3273 fprintf(stderr, " - ");
3274 printLocation(clang_getRangeEnd(ReplacementRange));
3275 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3276 clang_disposeString(text);
3277 }
3278}
3279
3280static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003281 unsigned i, n;
3282
Ted Kremenek15322172011-11-10 08:43:12 +00003283 if (!Diags)
3284 return;
3285
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003286 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00003287 for (i = 0; i < n; ++i) {
3288 CXSourceLocation DiagLoc;
3289 CXDiagnostic D;
3290 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003291 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00003292 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003293 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00003294
3295 D = clang_getDiagnosticInSet(Diags, i);
3296 DiagLoc = clang_getDiagnosticLocation(D);
3297 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3298 FileName = clang_getFileName(File);
3299 DiagSpelling = clang_getDiagnosticSpelling(D);
3300
3301 printIndent(indent);
3302
3303 fprintf(stderr, "%s:%d:%d: %s: %s",
3304 clang_getCString(FileName),
3305 line,
3306 column,
3307 getSeverityString(clang_getDiagnosticSeverity(D)),
3308 clang_getCString(DiagSpelling));
3309
3310 DiagOption = clang_getDiagnosticOption(D, 0);
3311 DiagOptionStr = clang_getCString(DiagOption);
3312 if (DiagOptionStr) {
3313 fprintf(stderr, " [%s]", DiagOptionStr);
3314 }
3315
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003316 DiagCat = clang_getDiagnosticCategoryText(D);
3317 DiagCatStr = clang_getCString(DiagCat);
3318 if (DiagCatStr) {
3319 fprintf(stderr, " [%s]", DiagCatStr);
3320 }
3321
Ted Kremenek15322172011-11-10 08:43:12 +00003322 fprintf(stderr, "\n");
3323
3324 printRanges(D, indent);
3325 printFixIts(D, indent);
3326
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003327 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003328 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3329
3330 clang_disposeString(FileName);
3331 clang_disposeString(DiagSpelling);
3332 clang_disposeString(DiagOption);
3333 }
3334}
3335
3336static int read_diagnostics(const char *filename) {
3337 enum CXLoadDiag_Error error;
3338 CXString errorString;
3339 CXDiagnosticSet Diags = 0;
3340
3341 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3342 if (!Diags) {
3343 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3344 getDiagnosticCodeStr(error),
3345 clang_getCString(errorString));
3346 clang_disposeString(errorString);
3347 return 1;
3348 }
3349
3350 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003351 fprintf(stderr, "Number of diagnostics: %d\n",
3352 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003353 clang_disposeDiagnosticSet(Diags);
3354 return 0;
3355}
3356
3357/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003358/* Command line processing. */
3359/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003360
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003361static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003362 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003363 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003364 if (strcmp(s, "-usrs") == 0)
3365 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003366 if (strncmp(s, "-memory-usage", 13) == 0)
3367 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003368 return NULL;
3369}
3370
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003371static void print_usage(void) {
3372 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003373 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003374 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003375 " c-index-test -cursor-at=<site> <compiler arguments>\n"
NAKAMURA Takumi35849722012-10-24 22:52:04 +00003376 " c-index-test -file-refs-at=<site> <compiler arguments>\n");
3377 fprintf(stderr,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003378 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003379 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003380 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003381 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003382 "[FileCheck prefix]\n");
3383 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003384 " c-index-test -test-load-tu <AST file> <symbol filter> "
3385 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003386 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3387 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003388 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003389 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003390 " c-index-test -test-load-source-memory-usage "
3391 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003392 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3393 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003394 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003395 " c-index-test -test-load-source-usrs-memory-usage "
3396 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003397 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3398 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003399 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003400 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003401 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003402 " c-index-test -test-print-typekind {<args>}*\n"
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003403 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003404 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003405 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003406 " c-index-test -write-pch <file> <compiler arguments>\n");
3407 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003408 " c-index-test -compilation-db [lookup <filename>] database\n");
3409 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003410 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003411 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003412 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003413 " all - load all symbols, including those from PCH\n"
3414 " local - load all symbols except those in PCH\n"
3415 " category - only load ObjC categories (non-PCH)\n"
3416 " interface - only load ObjC interfaces (non-PCH)\n"
3417 " protocol - only load ObjC protocols (non-PCH)\n"
3418 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003419 " typedef - only load typdefs (non-PCH)\n"
3420 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003421}
3422
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003423/***/
3424
3425int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003426 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003427 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3428 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003429 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003430 return perform_code_completion(argc, argv, 0);
3431 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3432 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003433 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3434 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003435 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3436 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003437 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003438 return index_file(argc - 2, argv + 2, /*full=*/0);
3439 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3440 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003441 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3442 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003443 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003444 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003445 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003446 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3447 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003448 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003449 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3450 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3451 if (I) {
3452 int trials = atoi(argv[2]);
3453 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3454 NULL);
3455 }
3456 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003457 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003458 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003459
3460 PostVisitTU postVisit = 0;
3461 if (strstr(argv[1], "-memory-usage"))
3462 postVisit = PrintMemoryUsage;
3463
Ted Kremenek7d405622010-01-12 23:34:26 +00003464 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003465 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3466 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003467 }
3468 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003469 return perform_file_scan(argv[2], argv[3],
3470 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003471 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3472 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003473 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3474 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3475 PrintInclusionStack);
3476 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3477 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3478 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003479 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3480 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3481 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003482 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
3483 return perform_test_load_source(argc - 2, argv + 2, "all",
3484 PrintTypeKind, 0);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003485 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
3486 return perform_test_load_source(argc - 2, argv + 2, "all",
3487 PrintBitWidth, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003488 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3489 if (argc > 2)
3490 return print_usrs(argv + 2, argv + argc);
3491 else {
3492 display_usrs();
3493 return 1;
3494 }
3495 }
3496 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3497 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003498 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3499 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003500 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3501 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3502
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003503 print_usage();
3504 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003505}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003506
3507/***/
3508
3509/* We intentionally run in a separate thread to ensure we at least minimal
3510 * testing of a multithreaded environment (for example, having a reduced stack
3511 * size). */
3512
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003513typedef struct thread_info {
3514 int argc;
3515 const char **argv;
3516 int result;
3517} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003518void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003519 thread_info *client_data = client_data_v;
3520 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003521#ifdef __CYGWIN__
3522 fflush(stdout); /* stdout is not flushed on Cygwin. */
3523#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003524}
3525
3526int main(int argc, const char **argv) {
Benjamin Kramerd1a4f682012-08-10 10:06:13 +00003527 thread_info client_data;
3528
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003529#ifdef CLANG_HAVE_LIBXML
3530 LIBXML_TEST_VERSION
3531#endif
3532
Douglas Gregor61605982010-10-27 16:00:01 +00003533 if (getenv("CINDEXTEST_NOTHREADS"))
3534 return cindextest_main(argc, argv);
3535
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003536 client_data.argc = argc;
3537 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003538 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003539 return client_data.result;
3540}