blob: 511f1a90bb13bf7beeb5e5410e345bebcd999dcd [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
515static void PrintCursorComments(CXTranslationUnit TU,
516 CXCursor Cursor,
517 CommentXMLValidationData *ValidationData) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000518 {
519 CXString RawComment;
520 const char *RawCommentCString;
521 CXString BriefComment;
522 const char *BriefCommentCString;
523
524 RawComment = clang_Cursor_getRawCommentText(Cursor);
525 RawCommentCString = clang_getCString(RawComment);
526 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
527 PrintCStringWithPrefix("RawComment", RawCommentCString);
528 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
529
530 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
531 BriefCommentCString = clang_getCString(BriefComment);
532 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
533 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
534 clang_disposeString(BriefComment);
535 }
536 clang_disposeString(RawComment);
537 }
538
539 {
540 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
541 if (clang_Comment_getKind(Comment) != CXComment_Null) {
542 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
543 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000544 {
545 CXString XML;
546 XML = clang_FullComment_getAsXML(TU, Comment);
547 PrintCXStringWithPrefix("FullCommentAsXML", XML);
548 ValidateCommentXML(clang_getCString(XML), ValidationData);
549 clang_disposeString(XML);
550 }
551
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000552 DumpCXComment(Comment);
553 }
554 }
555}
556
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000557typedef struct {
558 unsigned line;
559 unsigned col;
560} LineCol;
561
562static int lineCol_cmp(const void *p1, const void *p2) {
563 const LineCol *lhs = p1;
564 const LineCol *rhs = p2;
565 if (lhs->line != rhs->line)
566 return (int)lhs->line - (int)rhs->line;
567 return (int)lhs->col - (int)rhs->col;
568}
569
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000570static void PrintCursor(CXCursor Cursor,
571 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000572 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000573 if (clang_isInvalid(Cursor.kind)) {
574 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
575 printf("Invalid Cursor => %s", clang_getCString(ks));
576 clang_disposeString(ks);
577 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000578 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000579 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000580 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000581 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000582 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000583 CXCursor *overridden;
584 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000585 unsigned RefNameRangeNr;
586 CXSourceRange CursorExtent;
587 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000588 int AlwaysUnavailable;
589 int AlwaysDeprecated;
590 CXString UnavailableMessage;
591 CXString DeprecatedMessage;
592 CXPlatformAvailability PlatformAvailability[2];
593 int NumPlatformAvailability;
594 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000595
Ted Kremeneke68fff62010-02-17 00:41:32 +0000596 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000597 string = want_display_name? clang_getCursorDisplayName(Cursor)
598 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000599 printf("%s=%s", clang_getCString(ks),
600 clang_getCString(string));
601 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000602 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000603
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000604 Referenced = clang_getCursorReferenced(Cursor);
605 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000606 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
607 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
608 printf("[");
609 for (I = 0; I != N; ++I) {
610 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000611 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000612 if (I)
613 printf(", ");
614
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000615 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000616 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000617 printf("%d:%d", line, column);
618 }
619 printf("]");
620 } else {
621 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000622 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000623 printf(":%d:%d", line, column);
624 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000625 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000626
627 if (clang_isCursorDefinition(Cursor))
628 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000629
630 switch (clang_getCursorAvailability(Cursor)) {
631 case CXAvailability_Available:
632 break;
633
634 case CXAvailability_Deprecated:
635 printf(" (deprecated)");
636 break;
637
638 case CXAvailability_NotAvailable:
639 printf(" (unavailable)");
640 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000641
642 case CXAvailability_NotAccessible:
643 printf(" (inaccessible)");
644 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000645 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000646
Douglas Gregorcc889662012-05-08 00:14:45 +0000647 NumPlatformAvailability
648 = clang_getCursorPlatformAvailability(Cursor,
649 &AlwaysDeprecated,
650 &DeprecatedMessage,
651 &AlwaysUnavailable,
652 &UnavailableMessage,
653 PlatformAvailability, 2);
654 if (AlwaysUnavailable) {
655 printf(" (always unavailable: \"%s\")",
656 clang_getCString(UnavailableMessage));
657 } else if (AlwaysDeprecated) {
658 printf(" (always deprecated: \"%s\")",
659 clang_getCString(DeprecatedMessage));
660 } else {
661 for (I = 0; I != NumPlatformAvailability; ++I) {
662 if (I >= 2)
663 break;
664
665 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
666 if (PlatformAvailability[I].Unavailable)
667 printf(", unavailable");
668 else {
669 printVersion(", introduced=", PlatformAvailability[I].Introduced);
670 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
671 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
672 }
673 if (clang_getCString(PlatformAvailability[I].Message)[0])
674 printf(", message=\"%s\"",
675 clang_getCString(PlatformAvailability[I].Message));
676 printf(")");
677 }
678 }
679 for (I = 0; I != NumPlatformAvailability; ++I) {
680 if (I >= 2)
681 break;
682 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
683 }
684
685 clang_disposeString(DeprecatedMessage);
686 clang_disposeString(UnavailableMessage);
687
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000688 if (clang_CXXMethod_isStatic(Cursor))
689 printf(" (static)");
690 if (clang_CXXMethod_isVirtual(Cursor))
691 printf(" (virtual)");
692
Ted Kremenek95f33552010-08-26 01:42:22 +0000693 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
694 CXType T =
695 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
696 CXString S = clang_getTypeKindSpelling(T.kind);
697 printf(" [IBOutletCollection=%s]", clang_getCString(S));
698 clang_disposeString(S);
699 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000700
701 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
702 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
703 unsigned isVirtual = clang_isVirtualBase(Cursor);
704 const char *accessStr = 0;
705
706 switch (access) {
707 case CX_CXXInvalidAccessSpecifier:
708 accessStr = "invalid"; break;
709 case CX_CXXPublic:
710 accessStr = "public"; break;
711 case CX_CXXProtected:
712 accessStr = "protected"; break;
713 case CX_CXXPrivate:
714 accessStr = "private"; break;
715 }
716
717 printf(" [access=%s isVirtual=%s]", accessStr,
718 isVirtual ? "true" : "false");
719 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000720
721 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
722 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
723 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
724 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000725 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000726 printf(" [Specialization of %s:%d:%d]",
727 clang_getCString(Name), line, column);
728 clang_disposeString(Name);
729 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000730
731 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
732 if (num_overridden) {
733 unsigned I;
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000734 LineCol lineCols[50];
735 assert(num_overridden <= 50);
Douglas Gregor9f592342010-10-01 20:25:15 +0000736 printf(" [Overrides ");
737 for (I = 0; I != num_overridden; ++I) {
738 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000739 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000740 lineCols[I].line = line;
741 lineCols[I].col = column;
742 }
Michael Liao64221492012-08-30 00:45:32 +0000743 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000744 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
745 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor9f592342010-10-01 20:25:15 +0000746 if (I)
747 printf(", ");
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000748 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor9f592342010-10-01 20:25:15 +0000749 }
750 printf("]");
751 clang_disposeOverriddenCursors(overridden);
752 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000753
754 if (Cursor.kind == CXCursor_InclusionDirective) {
755 CXFile File = clang_getIncludedFile(Cursor);
756 CXString Included = clang_getFileName(File);
757 printf(" (%s)", clang_getCString(Included));
758 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000759
760 if (clang_isFileMultipleIncludeGuarded(TU, File))
761 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000762 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000763
764 CursorExtent = clang_getCursorExtent(Cursor);
765 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
766 CXNameRange_WantQualifier
767 | CXNameRange_WantSinglePiece
768 | CXNameRange_WantTemplateArgs,
769 0);
770 if (!clang_equalRanges(CursorExtent, RefNameRange))
771 PrintRange(RefNameRange, "SingleRefName");
772
773 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
774 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
775 CXNameRange_WantQualifier
776 | CXNameRange_WantTemplateArgs,
777 RefNameRangeNr);
778 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
779 break;
780 if (!clang_equalRanges(CursorExtent, RefNameRange))
781 PrintRange(RefNameRange, "RefName");
782 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000783
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000784 PrintCursorComments(TU, Cursor, ValidationData);
Steve Naroff699a07d2009-09-25 21:32:34 +0000785 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000786}
Steve Naroff89922f82009-08-31 00:59:03 +0000787
Ted Kremeneke68fff62010-02-17 00:41:32 +0000788static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000789 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000790 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000791 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000792 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000793 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000794 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000795 clang_disposeString(source);
796 return "<invalid loc>";
797 }
798 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000799 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000800 clang_disposeString(source);
801 return b;
802 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000803}
804
Ted Kremenek0d435192009-11-17 18:13:31 +0000805/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000806/* Callbacks. */
807/******************************************************************************/
808
809typedef void (*PostVisitTU)(CXTranslationUnit);
810
Douglas Gregora88084b2010-02-18 18:08:43 +0000811void PrintDiagnostic(CXDiagnostic Diagnostic) {
812 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000813 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000814 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000815 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000816 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
817 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000818 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000819
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000820 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000821 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000822
Douglas Gregor274f1902010-02-22 23:17:23 +0000823 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
824 fprintf(stderr, "%s\n", clang_getCString(Msg));
825 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000826
Douglas Gregora9b06d42010-11-09 06:24:54 +0000827 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
828 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000829 if (!file)
830 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000831
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000832 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000833 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000834 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000835 CXSourceRange range;
836 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
837 CXSourceLocation start = clang_getRangeStart(range);
838 CXSourceLocation end = clang_getRangeEnd(range);
839 unsigned start_line, start_column, end_line, end_column;
840 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000841 clang_getSpellingLocation(start, &start_file, &start_line,
842 &start_column, 0);
843 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000844 if (clang_equalLocations(start, end)) {
845 /* Insertion. */
846 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000847 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000848 clang_getCString(insertion_text), start_line, start_column);
849 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
850 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000851 if (start_file == file && end_file == file) {
852 fprintf(out, "FIX-IT: Remove ");
853 PrintExtent(out, start_line, start_column, end_line, end_column);
854 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000855 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000856 } else {
857 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000858 if (start_file == end_file) {
859 fprintf(out, "FIX-IT: Replace ");
860 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000861 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000862 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000863 break;
864 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000865 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000866 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000867}
868
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000869void PrintDiagnosticSet(CXDiagnosticSet Set) {
870 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
871 for ( ; i != n ; ++i) {
872 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
873 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000874 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000875 if (ChildDiags)
876 PrintDiagnosticSet(ChildDiags);
877 }
878}
879
880void PrintDiagnostics(CXTranslationUnit TU) {
881 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
882 PrintDiagnosticSet(TUSet);
883 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000884}
885
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000886void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000887 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000888 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000889 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000890 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000891 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000892 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000893 unsigned long amount = usage.entries[i].amount;
894 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000895 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000896 ((double) amount)/(1024*1024));
897 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000898 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000899 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000900 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000901}
902
Ted Kremenekce2ae882010-01-26 17:59:48 +0000903/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000904/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000905/******************************************************************************/
906
Douglas Gregora7bde202010-01-19 00:34:46 +0000907static void PrintCursorExtent(CXCursor C) {
908 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000909 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000910}
911
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000912/* Data used by the visitors. */
913typedef struct {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000914 CXTranslationUnit TU;
915 enum CXCursorKind *Filter;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000916 CommentXMLValidationData ValidationData;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000917} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000918
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000919
Ted Kremeneke68fff62010-02-17 00:41:32 +0000920enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000921 CXCursor Parent,
922 CXClientData ClientData) {
923 VisitorData *Data = (VisitorData *)ClientData;
924 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000925 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000926 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000927 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000928 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000929 GetCursorSource(Cursor), line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000930 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregora7bde202010-01-19 00:34:46 +0000931 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000932 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000933 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000934 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000935
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000936 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000937}
Steve Naroff50398192009-08-28 15:28:48 +0000938
Ted Kremeneke68fff62010-02-17 00:41:32 +0000939static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000940 CXCursor Parent,
941 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000942 const char *startBuf, *endBuf;
943 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
944 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000945 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000946
Douglas Gregorb6998662010-01-19 19:34:47 +0000947 if (Cursor.kind != CXCursor_FunctionDecl ||
948 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000949 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000950
951 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
952 &startLine, &startColumn,
953 &endLine, &endColumn);
954 /* Probe the entire body, looking for both decls and refs. */
955 curLine = startLine;
956 curColumn = startColumn;
957
958 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000959 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000960 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000961 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000962
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000963 if (*startBuf == '\n') {
964 startBuf++;
965 curLine++;
966 curColumn = 1;
967 } else if (*startBuf != '\t')
968 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000969
Douglas Gregor98258af2010-01-18 22:46:11 +0000970 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000971 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000972
Douglas Gregor1db19de2010-01-19 21:36:55 +0000973 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000974 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000975 CXSourceLocation RefLoc
976 = clang_getLocation(Data->TU, file, curLine, curColumn);
977 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000978 if (Ref.kind == CXCursor_NoDeclFound) {
979 /* Nothing found here; that's fine. */
980 } else if (Ref.kind != CXCursor_FunctionDecl) {
981 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
982 curLine, curColumn);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000983 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor98258af2010-01-18 22:46:11 +0000984 printf("\n");
985 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000986 }
Ted Kremenek74844072010-02-17 00:41:20 +0000987 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000988 startBuf++;
989 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000990
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000991 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000992}
993
Ted Kremenek7d405622010-01-12 23:34:26 +0000994/******************************************************************************/
995/* USR testing. */
996/******************************************************************************/
997
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000998enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
999 CXClientData ClientData) {
1000 VisitorData *Data = (VisitorData *)ClientData;
1001 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +00001002 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +00001003 const char *cstr = clang_getCString(USR);
1004 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +00001005 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +00001006 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +00001007 }
Ted Kremeneke542f772010-04-20 23:15:40 +00001008 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1009
Douglas Gregora7bde202010-01-19 00:34:46 +00001010 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +00001011 printf("\n");
1012 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001013
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001014 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001015 }
1016
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001017 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +00001018}
1019
1020/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +00001021/* Inclusion stack testing. */
1022/******************************************************************************/
1023
1024void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1025 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001026
Ted Kremenek16b55a72010-01-26 19:31:51 +00001027 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +00001028 CXString fname;
1029
1030 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001031 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +00001032 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001033
Ted Kremenek16b55a72010-01-26 19:31:51 +00001034 for (i = 0; i < includeStackLen; ++i) {
1035 CXFile includingFile;
1036 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +00001037 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1038 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +00001039 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001040 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +00001041 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001042 }
1043 printf("\n");
1044}
1045
1046void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001047 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001048}
1049
1050/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +00001051/* Linkage testing. */
1052/******************************************************************************/
1053
1054static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1055 CXClientData d) {
1056 const char *linkage = 0;
1057
1058 if (clang_isInvalid(clang_getCursorKind(cursor)))
1059 return CXChildVisit_Recurse;
1060
1061 switch (clang_getCursorLinkage(cursor)) {
1062 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +00001063 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1064 case CXLinkage_Internal: linkage = "Internal"; break;
1065 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1066 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +00001067 }
1068
1069 if (linkage) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001070 PrintCursor(cursor, NULL);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001071 printf("linkage=%s\n", linkage);
1072 }
1073
1074 return CXChildVisit_Recurse;
1075}
1076
1077/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001078/* Typekind testing. */
1079/******************************************************************************/
1080
1081static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
1082 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001083 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
1084 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001085 CXString S = clang_getTypeKindSpelling(T.kind);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001086 PrintCursor(cursor, NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001087 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +00001088 if (clang_isConstQualifiedType(T))
1089 printf(" const");
1090 if (clang_isVolatileQualifiedType(T))
1091 printf(" volatile");
1092 if (clang_isRestrictQualifiedType(T))
1093 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001094 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +00001095 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001096 {
1097 CXType CT = clang_getCanonicalType(T);
1098 if (!clang_equalTypes(T, CT)) {
1099 CXString CS = clang_getTypeKindSpelling(CT.kind);
1100 printf(" [canonical=%s]", clang_getCString(CS));
1101 clang_disposeString(CS);
1102 }
1103 }
Benjamin Kramere1403d22010-06-22 09:29:44 +00001104 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001105 {
Ted Kremenek9a140842010-06-21 20:48:56 +00001106 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001107 if (RT.kind != CXType_Invalid) {
1108 CXString RS = clang_getTypeKindSpelling(RT.kind);
1109 printf(" [result=%s]", clang_getCString(RS));
1110 clang_disposeString(RS);
1111 }
1112 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001113 /* Print the argument types if they exist. */
1114 {
1115 int numArgs = clang_Cursor_getNumArguments(cursor);
1116 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001117 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001118 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001119 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001120 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
1121 if (T.kind != CXType_Invalid) {
1122 CXString S = clang_getTypeKindSpelling(T.kind);
1123 printf(" %s", clang_getCString(S));
1124 clang_disposeString(S);
1125 }
1126 }
1127 printf("]");
1128 }
1129 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +00001130 /* Print if this is a non-POD type. */
1131 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001132
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001133 printf("\n");
1134 }
1135 return CXChildVisit_Recurse;
1136}
1137
1138
1139/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +00001140/* Loading ASTs/source. */
1141/******************************************************************************/
1142
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001143static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +00001144 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001145 CXCursorVisitor Visitor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001146 PostVisitTU PV,
1147 const char *CommentSchemaFile) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001148
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001149 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001150 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001151
1152 if (Visitor) {
1153 enum CXCursorKind K = CXCursor_NotImplemented;
1154 enum CXCursorKind *ck = &K;
1155 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001156
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001157 /* Perform some simple filtering. */
1158 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001159 else if (!strcmp(filter, "all-display") ||
1160 !strcmp(filter, "local-display")) {
1161 ck = NULL;
1162 want_display_name = 1;
1163 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001164 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001165 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1166 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1167 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1168 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1169 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1170 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1171 else {
1172 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1173 return 1;
1174 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001175
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001176 Data.TU = TU;
1177 Data.Filter = ck;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001178 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1179#ifdef CLANG_HAVE_LIBXML
1180 Data.ValidationData.RNGParser = NULL;
1181 Data.ValidationData.Schema = NULL;
1182#endif
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001183 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001184 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001185
Ted Kremenekce2ae882010-01-26 17:59:48 +00001186 if (PV)
1187 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001188
Douglas Gregora88084b2010-02-18 18:08:43 +00001189 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001190 if (checkForErrors(TU) != 0) {
1191 clang_disposeTranslationUnit(TU);
1192 return -1;
1193 }
1194
Ted Kremenek0d435192009-11-17 18:13:31 +00001195 clang_disposeTranslationUnit(TU);
1196 return 0;
1197}
1198
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001199int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001200 const char *prefix, CXCursorVisitor Visitor,
1201 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001202 CXIndex Idx;
1203 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001204 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001205 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001206 !strcmp(filter, "local") ? 1 : 0,
1207 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001208
Ted Kremenek020a0952010-02-11 07:41:25 +00001209 if (!CreateTranslationUnit(Idx, file, &TU)) {
1210 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001211 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001212 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001213
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001214 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek020a0952010-02-11 07:41:25 +00001215 clang_disposeIndex(Idx);
1216 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001217}
1218
Ted Kremenekce2ae882010-01-26 17:59:48 +00001219int perform_test_load_source(int argc, const char **argv,
1220 const char *filter, CXCursorVisitor Visitor,
1221 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001222 CXIndex Idx;
1223 CXTranslationUnit TU;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001224 const char *CommentSchemaFile;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001225 struct CXUnsavedFile *unsaved_files = 0;
1226 int num_unsaved_files = 0;
1227 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001228
Daniel Dunbarada487d2009-12-01 02:03:10 +00001229 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001230 (!strcmp(filter, "local") ||
1231 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +00001232 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001233
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001234 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1235 argc--;
1236 argv++;
1237 }
1238
Ted Kremenek020a0952010-02-11 07:41:25 +00001239 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1240 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001241 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001242 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001243
Douglas Gregordca8ee82011-05-06 16:33:08 +00001244 TU = clang_parseTranslationUnit(Idx, 0,
1245 argv + num_unsaved_files,
1246 argc - num_unsaved_files,
1247 unsaved_files, num_unsaved_files,
1248 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001249 if (!TU) {
1250 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001251 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001252 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001253 return 1;
1254 }
1255
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001256 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1257 CommentSchemaFile);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001258 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001259 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001260 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001261}
1262
Douglas Gregorabc563f2010-07-19 21:46:24 +00001263int perform_test_reparse_source(int argc, const char **argv, int trials,
1264 const char *filter, CXCursorVisitor Visitor,
1265 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001266 CXIndex Idx;
1267 CXTranslationUnit TU;
1268 struct CXUnsavedFile *unsaved_files = 0;
1269 int num_unsaved_files = 0;
1270 int result;
1271 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001272 int remap_after_trial = 0;
1273 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001274
1275 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1276 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +00001277 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001278
Douglas Gregorabc563f2010-07-19 21:46:24 +00001279 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1280 clang_disposeIndex(Idx);
1281 return -1;
1282 }
1283
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001284 /* Load the initial translation unit -- we do this without honoring remapped
1285 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001286 TU = clang_parseTranslationUnit(Idx, 0,
1287 argv + num_unsaved_files,
1288 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001289 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001290 if (!TU) {
1291 fprintf(stderr, "Unable to load translation unit!\n");
1292 free_remapped_files(unsaved_files, num_unsaved_files);
1293 clang_disposeIndex(Idx);
1294 return 1;
1295 }
1296
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001297 if (checkForErrors(TU) != 0)
1298 return -1;
1299
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001300 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1301 remap_after_trial =
1302 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1303 }
1304
Douglas Gregorabc563f2010-07-19 21:46:24 +00001305 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001306 if (clang_reparseTranslationUnit(TU,
1307 trial >= remap_after_trial ? num_unsaved_files : 0,
1308 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001309 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001310 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001311 clang_disposeTranslationUnit(TU);
1312 free_remapped_files(unsaved_files, num_unsaved_files);
1313 clang_disposeIndex(Idx);
1314 return -1;
1315 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001316
1317 if (checkForErrors(TU) != 0)
1318 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001319 }
1320
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001321 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001322
Douglas Gregorabc563f2010-07-19 21:46:24 +00001323 free_remapped_files(unsaved_files, num_unsaved_files);
1324 clang_disposeIndex(Idx);
1325 return result;
1326}
1327
Ted Kremenek0d435192009-11-17 18:13:31 +00001328/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001329/* Logic for testing clang_getCursor(). */
1330/******************************************************************************/
1331
Douglas Gregordd3e5542011-05-04 00:14:37 +00001332static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001333 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001334 unsigned end_line, unsigned end_col,
1335 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001336 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001337 if (prefix)
1338 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001339 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1340 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001341 PrintCursor(cursor, NULL);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001342 printf("\n");
1343}
1344
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001345static int perform_file_scan(const char *ast_file, const char *source_file,
1346 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001347 CXIndex Idx;
1348 CXTranslationUnit TU;
1349 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001350 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001351 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001352 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001353 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001354
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001355 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1356 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001357 fprintf(stderr, "Could not create Index\n");
1358 return 1;
1359 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001360
Ted Kremenek1c6da172009-11-17 19:37:36 +00001361 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1362 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001363
Ted Kremenek1c6da172009-11-17 19:37:36 +00001364 if ((fp = fopen(source_file, "r")) == NULL) {
1365 fprintf(stderr, "Could not open '%s'\n", source_file);
1366 return 1;
1367 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368
Douglas Gregorb9790342010-01-22 21:44:22 +00001369 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001370 for (;;) {
1371 CXCursor cursor;
1372 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001373
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001374 if (c == '\n') {
1375 ++line;
1376 col = 1;
1377 } else
1378 ++col;
1379
1380 /* Check the cursor at this position, and dump the previous one if we have
1381 * found something new.
1382 */
1383 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1384 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1385 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001386 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001387 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001388 start_line = line;
1389 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001390 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001391 if (c == EOF)
1392 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001393
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001394 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001395 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001396
Ted Kremenek1c6da172009-11-17 19:37:36 +00001397 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001398 clang_disposeTranslationUnit(TU);
1399 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001400 return 0;
1401}
1402
1403/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001404/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001405/******************************************************************************/
1406
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001407/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1408 on failure. If successful, the pointer *filename will contain newly-allocated
1409 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001410int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001411 unsigned *column, unsigned *second_line,
1412 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001413 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001414 const char *last_colon = strrchr(input, ':');
1415 unsigned values[4], i;
1416 unsigned num_values = (second_line && second_column)? 4 : 2;
1417
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001418 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001419 if (!last_colon || last_colon == input) {
1420 if (num_values == 4)
1421 fprintf(stderr, "could not parse filename:line:column:line:column in "
1422 "'%s'\n", input);
1423 else
1424 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001425 return 1;
1426 }
1427
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001428 for (i = 0; i != num_values; ++i) {
1429 const char *prev_colon;
1430
1431 /* Parse the next line or column. */
1432 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1433 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001434 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001435 (i % 2 ? "column" : "line"), input);
1436 return 1;
1437 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001438
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001439 if (i + 1 == num_values)
1440 break;
1441
1442 /* Find the previous colon. */
1443 prev_colon = last_colon - 1;
1444 while (prev_colon != input && *prev_colon != ':')
1445 --prev_colon;
1446 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001447 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001448 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001449 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001450 }
1451
1452 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001453 }
1454
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001455 *line = values[0];
1456 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001457
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001458 if (second_line && second_column) {
1459 *second_line = values[2];
1460 *second_column = values[3];
1461 }
1462
Douglas Gregor88d23952009-11-09 18:19:57 +00001463 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001464 *filename = (char*)malloc(last_colon - input + 1);
1465 memcpy(*filename, input, last_colon - input);
1466 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001467 return 0;
1468}
1469
1470const char *
1471clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1472 switch (Kind) {
1473 case CXCompletionChunk_Optional: return "Optional";
1474 case CXCompletionChunk_TypedText: return "TypedText";
1475 case CXCompletionChunk_Text: return "Text";
1476 case CXCompletionChunk_Placeholder: return "Placeholder";
1477 case CXCompletionChunk_Informative: return "Informative";
1478 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1479 case CXCompletionChunk_LeftParen: return "LeftParen";
1480 case CXCompletionChunk_RightParen: return "RightParen";
1481 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1482 case CXCompletionChunk_RightBracket: return "RightBracket";
1483 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1484 case CXCompletionChunk_RightBrace: return "RightBrace";
1485 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1486 case CXCompletionChunk_RightAngle: return "RightAngle";
1487 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001488 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001489 case CXCompletionChunk_Colon: return "Colon";
1490 case CXCompletionChunk_SemiColon: return "SemiColon";
1491 case CXCompletionChunk_Equal: return "Equal";
1492 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1493 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001494 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001495
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001496 return "Unknown";
1497}
1498
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001499static int checkForErrors(CXTranslationUnit TU) {
1500 unsigned Num, i;
1501 CXDiagnostic Diag;
1502 CXString DiagStr;
1503
1504 if (!getenv("CINDEXTEST_FAILONERROR"))
1505 return 0;
1506
1507 Num = clang_getNumDiagnostics(TU);
1508 for (i = 0; i != Num; ++i) {
1509 Diag = clang_getDiagnostic(TU, i);
1510 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1511 DiagStr = clang_formatDiagnostic(Diag,
1512 clang_defaultDiagnosticDisplayOptions());
1513 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1514 clang_disposeString(DiagStr);
1515 clang_disposeDiagnostic(Diag);
1516 return -1;
1517 }
1518 clang_disposeDiagnostic(Diag);
1519 }
1520
1521 return 0;
1522}
1523
Douglas Gregor3ac73852009-11-09 16:04:45 +00001524void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001525 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001526
Douglas Gregor3ac73852009-11-09 16:04:45 +00001527 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001528 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001529 CXString text;
1530 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001531 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001532 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001533
Douglas Gregor3ac73852009-11-09 16:04:45 +00001534 if (Kind == CXCompletionChunk_Optional) {
1535 fprintf(file, "{Optional ");
1536 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001537 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001538 file);
1539 fprintf(file, "}");
1540 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001541 }
1542
1543 if (Kind == CXCompletionChunk_VerticalSpace) {
1544 fprintf(file, "{VerticalSpace }");
1545 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001546 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001547
Douglas Gregord5a20892009-11-09 17:05:28 +00001548 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001549 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001550 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001551 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001552 cstr ? cstr : "");
1553 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001554 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001555
Douglas Gregor3ac73852009-11-09 16:04:45 +00001556}
1557
1558void print_completion_result(CXCompletionResult *completion_result,
1559 CXClientData client_data) {
1560 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001561 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001562 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001563 enum CXCursorKind ParentKind;
1564 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001565 CXString BriefComment;
1566 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001567
Ted Kremeneke68fff62010-02-17 00:41:32 +00001568 fprintf(file, "%s:", clang_getCString(ks));
1569 clang_disposeString(ks);
1570
Douglas Gregor3ac73852009-11-09 16:04:45 +00001571 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001572 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001573 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001574 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1575 case CXAvailability_Available:
1576 break;
1577
1578 case CXAvailability_Deprecated:
1579 fprintf(file, " (deprecated)");
1580 break;
1581
1582 case CXAvailability_NotAvailable:
1583 fprintf(file, " (unavailable)");
1584 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001585
1586 case CXAvailability_NotAccessible:
1587 fprintf(file, " (inaccessible)");
1588 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001589 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001590
1591 annotationCount = clang_getCompletionNumAnnotations(
1592 completion_result->CompletionString);
1593 if (annotationCount) {
1594 unsigned i;
1595 fprintf(file, " (");
1596 for (i = 0; i < annotationCount; ++i) {
1597 if (i != 0)
1598 fprintf(file, ", ");
1599 fprintf(file, "\"%s\"",
1600 clang_getCString(clang_getCompletionAnnotation(
1601 completion_result->CompletionString, i)));
1602 }
1603 fprintf(file, ")");
1604 }
1605
Douglas Gregorba103062012-03-27 23:34:16 +00001606 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1607 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1608 &ParentKind);
1609 if (ParentKind != CXCursor_NotImplemented) {
1610 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1611 fprintf(file, " (parent: %s '%s')",
1612 clang_getCString(KindSpelling),
1613 clang_getCString(ParentName));
1614 clang_disposeString(KindSpelling);
1615 }
1616 clang_disposeString(ParentName);
1617 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001618
1619 BriefComment = clang_getCompletionBriefComment(
1620 completion_result->CompletionString);
1621 BriefCommentCString = clang_getCString(BriefComment);
1622 if (BriefCommentCString && *BriefCommentCString != '\0') {
1623 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1624 }
1625 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001626
Douglas Gregor58ddb602010-08-23 23:00:57 +00001627 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001628}
1629
Douglas Gregor3da626b2011-07-07 16:03:39 +00001630void print_completion_contexts(unsigned long long contexts, FILE *file) {
1631 fprintf(file, "Completion contexts:\n");
1632 if (contexts == CXCompletionContext_Unknown) {
1633 fprintf(file, "Unknown\n");
1634 }
1635 if (contexts & CXCompletionContext_AnyType) {
1636 fprintf(file, "Any type\n");
1637 }
1638 if (contexts & CXCompletionContext_AnyValue) {
1639 fprintf(file, "Any value\n");
1640 }
1641 if (contexts & CXCompletionContext_ObjCObjectValue) {
1642 fprintf(file, "Objective-C object value\n");
1643 }
1644 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1645 fprintf(file, "Objective-C selector value\n");
1646 }
1647 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1648 fprintf(file, "C++ class type value\n");
1649 }
1650 if (contexts & CXCompletionContext_DotMemberAccess) {
1651 fprintf(file, "Dot member access\n");
1652 }
1653 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1654 fprintf(file, "Arrow member access\n");
1655 }
1656 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1657 fprintf(file, "Objective-C property access\n");
1658 }
1659 if (contexts & CXCompletionContext_EnumTag) {
1660 fprintf(file, "Enum tag\n");
1661 }
1662 if (contexts & CXCompletionContext_UnionTag) {
1663 fprintf(file, "Union tag\n");
1664 }
1665 if (contexts & CXCompletionContext_StructTag) {
1666 fprintf(file, "Struct tag\n");
1667 }
1668 if (contexts & CXCompletionContext_ClassTag) {
1669 fprintf(file, "Class name\n");
1670 }
1671 if (contexts & CXCompletionContext_Namespace) {
1672 fprintf(file, "Namespace or namespace alias\n");
1673 }
1674 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1675 fprintf(file, "Nested name specifier\n");
1676 }
1677 if (contexts & CXCompletionContext_ObjCInterface) {
1678 fprintf(file, "Objective-C interface\n");
1679 }
1680 if (contexts & CXCompletionContext_ObjCProtocol) {
1681 fprintf(file, "Objective-C protocol\n");
1682 }
1683 if (contexts & CXCompletionContext_ObjCCategory) {
1684 fprintf(file, "Objective-C category\n");
1685 }
1686 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1687 fprintf(file, "Objective-C instance method\n");
1688 }
1689 if (contexts & CXCompletionContext_ObjCClassMessage) {
1690 fprintf(file, "Objective-C class method\n");
1691 }
1692 if (contexts & CXCompletionContext_ObjCSelectorName) {
1693 fprintf(file, "Objective-C selector name\n");
1694 }
1695 if (contexts & CXCompletionContext_MacroName) {
1696 fprintf(file, "Macro name\n");
1697 }
1698 if (contexts & CXCompletionContext_NaturalLanguage) {
1699 fprintf(file, "Natural language\n");
1700 }
1701}
1702
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001703int my_stricmp(const char *s1, const char *s2) {
1704 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001705 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001706 if (c1 < c2)
1707 return -1;
1708 else if (c1 > c2)
1709 return 1;
1710
1711 ++s1;
1712 ++s2;
1713 }
1714
1715 if (*s1)
1716 return 1;
1717 else if (*s2)
1718 return -1;
1719 return 0;
1720}
1721
Douglas Gregor1982c182010-07-12 18:38:41 +00001722int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001723 const char *input = argv[1];
1724 char *filename = 0;
1725 unsigned line;
1726 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001727 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001728 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001729 struct CXUnsavedFile *unsaved_files = 0;
1730 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001731 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001732 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001733 unsigned I, Repeats = 1;
1734 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1735
1736 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1737 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001738 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1739 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001740
Douglas Gregor1982c182010-07-12 18:38:41 +00001741 if (timing_only)
1742 input += strlen("-code-completion-timing=");
1743 else
1744 input += strlen("-code-completion-at=");
1745
Ted Kremeneke68fff62010-02-17 00:41:32 +00001746 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001747 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001748 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001749
Douglas Gregor735df882009-12-02 09:21:34 +00001750 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1751 return -1;
1752
Douglas Gregor32be4a52010-10-11 21:37:58 +00001753 CIdx = clang_createIndex(0, 0);
1754
1755 if (getenv("CINDEXTEST_EDITING"))
1756 Repeats = 5;
1757
1758 TU = clang_parseTranslationUnit(CIdx, 0,
1759 argv + num_unsaved_files + 2,
1760 argc - num_unsaved_files - 2,
1761 0, 0, getDefaultParsingOptions());
1762 if (!TU) {
1763 fprintf(stderr, "Unable to load translation unit!\n");
1764 return 1;
1765 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001766
1767 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1768 fprintf(stderr, "Unable to reparse translation init!\n");
1769 return 1;
1770 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001771
1772 for (I = 0; I != Repeats; ++I) {
1773 results = clang_codeCompleteAt(TU, filename, line, column,
1774 unsaved_files, num_unsaved_files,
1775 completionOptions);
1776 if (!results) {
1777 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001778 return 1;
1779 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001780 if (I != Repeats-1)
1781 clang_disposeCodeCompleteResults(results);
1782 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001783
Douglas Gregorec6762c2009-12-18 16:20:58 +00001784 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001785 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001786 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001787 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001788 CXString objCSelector;
1789 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001790 if (!timing_only) {
1791 /* Sort the code-completion results based on the typed text. */
1792 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1793
Douglas Gregor1982c182010-07-12 18:38:41 +00001794 for (i = 0; i != n; ++i)
1795 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001796 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001797 n = clang_codeCompleteGetNumDiagnostics(results);
1798 for (i = 0; i != n; ++i) {
1799 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1800 PrintDiagnostic(diag);
1801 clang_disposeDiagnostic(diag);
1802 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001803
1804 contexts = clang_codeCompleteGetContexts(results);
1805 print_completion_contexts(contexts, stdout);
1806
Douglas Gregor0a47d692011-07-26 15:24:30 +00001807 containerKind = clang_codeCompleteGetContainerKind(results,
1808 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001809
1810 if (containerKind != CXCursor_InvalidCode) {
1811 /* We have found a container */
1812 CXString containerUSR, containerKindSpelling;
1813 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1814 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1815 clang_disposeString(containerKindSpelling);
1816
1817 if (containerIsIncomplete) {
1818 printf("Container is incomplete\n");
1819 }
1820 else {
1821 printf("Container is complete\n");
1822 }
1823
1824 containerUSR = clang_codeCompleteGetContainerUSR(results);
1825 printf("Container USR: %s\n", clang_getCString(containerUSR));
1826 clang_disposeString(containerUSR);
1827 }
1828
Douglas Gregor0a47d692011-07-26 15:24:30 +00001829 objCSelector = clang_codeCompleteGetObjCSelector(results);
1830 selectorString = clang_getCString(objCSelector);
1831 if (selectorString && strlen(selectorString) > 0) {
1832 printf("Objective-C selector: %s\n", selectorString);
1833 }
1834 clang_disposeString(objCSelector);
1835
Douglas Gregorec6762c2009-12-18 16:20:58 +00001836 clang_disposeCodeCompleteResults(results);
1837 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001838 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001839 clang_disposeIndex(CIdx);
1840 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001841
Douglas Gregor735df882009-12-02 09:21:34 +00001842 free_remapped_files(unsaved_files, num_unsaved_files);
1843
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001844 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001845}
1846
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001847typedef struct {
1848 char *filename;
1849 unsigned line;
1850 unsigned column;
1851} CursorSourceLocation;
1852
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001853static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001854 CXIndex CIdx;
1855 int errorCode;
1856 struct CXUnsavedFile *unsaved_files = 0;
1857 int num_unsaved_files = 0;
1858 CXTranslationUnit TU;
1859 CXCursor Cursor;
1860 CursorSourceLocation *Locations = 0;
1861 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001862 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001863 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001864
Ted Kremeneke68fff62010-02-17 00:41:32 +00001865 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001866 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1867 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001868
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001869 /* Parse the locations. */
1870 assert(NumLocations > 0 && "Unable to count locations?");
1871 Locations = (CursorSourceLocation *)malloc(
1872 NumLocations * sizeof(CursorSourceLocation));
1873 for (Loc = 0; Loc < NumLocations; ++Loc) {
1874 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001875 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1876 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001877 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001878 return errorCode;
1879 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001880
1881 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001882 &num_unsaved_files))
1883 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001884
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001885 if (getenv("CINDEXTEST_EDITING"))
1886 Repeats = 5;
1887
1888 /* Parse the translation unit. When we're testing clang_getCursor() after
1889 reparsing, don't remap unsaved files until the second parse. */
1890 CIdx = clang_createIndex(1, 1);
1891 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1892 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001893 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001894 unsaved_files,
1895 Repeats > 1? 0 : num_unsaved_files,
1896 getDefaultParsingOptions());
1897
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001898 if (!TU) {
1899 fprintf(stderr, "unable to parse input\n");
1900 return -1;
1901 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001902
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001903 if (checkForErrors(TU) != 0)
1904 return -1;
1905
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001906 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001907 if (Repeats > 1 &&
1908 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1909 clang_defaultReparseOptions(TU))) {
1910 clang_disposeTranslationUnit(TU);
1911 return 1;
1912 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001913
1914 if (checkForErrors(TU) != 0)
1915 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001916
1917 for (Loc = 0; Loc < NumLocations; ++Loc) {
1918 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1919 if (!file)
1920 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001921
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001922 Cursor = clang_getCursor(TU,
1923 clang_getLocation(TU, file, Locations[Loc].line,
1924 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001925
1926 if (checkForErrors(TU) != 0)
1927 return -1;
1928
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001929 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001930 CXCompletionString completionString = clang_getCursorCompletionString(
1931 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001932 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1933 CXString Spelling;
1934 const char *cspell;
1935 unsigned line, column;
1936 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1937 printf("%d:%d ", line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001938 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001939 PrintCursorExtent(Cursor);
1940 Spelling = clang_getCursorSpelling(Cursor);
1941 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001942 if (cspell && strlen(cspell) != 0) {
1943 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001944 printf(" Spelling=%s (", cspell);
1945 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001946 CXSourceRange range =
1947 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001948 if (clang_Range_isNull(range))
1949 break;
1950 PrintRange(range, 0);
1951 }
1952 printf(")");
1953 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001954 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001955 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1956 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001957 if (clang_Cursor_isDynamicCall(Cursor))
1958 printf(" Dynamic-call");
1959
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001960 if (completionString != NULL) {
1961 printf("\nCompletion string: ");
1962 print_completion_string(completionString, stdout);
1963 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001964 printf("\n");
1965 free(Locations[Loc].filename);
1966 }
1967 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001968 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001969
Douglas Gregora88084b2010-02-18 18:08:43 +00001970 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001971 clang_disposeTranslationUnit(TU);
1972 clang_disposeIndex(CIdx);
1973 free(Locations);
1974 free_remapped_files(unsaved_files, num_unsaved_files);
1975 return 0;
1976}
1977
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001978static enum CXVisitorResult findFileRefsVisit(void *context,
1979 CXCursor cursor, CXSourceRange range) {
1980 if (clang_Range_isNull(range))
1981 return CXVisit_Continue;
1982
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001983 PrintCursor(cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001984 PrintRange(range, "");
1985 printf("\n");
1986 return CXVisit_Continue;
1987}
1988
1989static int find_file_refs_at(int argc, const char **argv) {
1990 CXIndex CIdx;
1991 int errorCode;
1992 struct CXUnsavedFile *unsaved_files = 0;
1993 int num_unsaved_files = 0;
1994 CXTranslationUnit TU;
1995 CXCursor Cursor;
1996 CursorSourceLocation *Locations = 0;
1997 unsigned NumLocations = 0, Loc;
1998 unsigned Repeats = 1;
1999 unsigned I;
2000
2001 /* Count the number of locations. */
2002 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2003 ++NumLocations;
2004
2005 /* Parse the locations. */
2006 assert(NumLocations > 0 && "Unable to count locations?");
2007 Locations = (CursorSourceLocation *)malloc(
2008 NumLocations * sizeof(CursorSourceLocation));
2009 for (Loc = 0; Loc < NumLocations; ++Loc) {
2010 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2011 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2012 &Locations[Loc].line,
2013 &Locations[Loc].column, 0, 0)))
2014 return errorCode;
2015 }
2016
2017 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2018 &num_unsaved_files))
2019 return -1;
2020
2021 if (getenv("CINDEXTEST_EDITING"))
2022 Repeats = 5;
2023
2024 /* Parse the translation unit. When we're testing clang_getCursor() after
2025 reparsing, don't remap unsaved files until the second parse. */
2026 CIdx = clang_createIndex(1, 1);
2027 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2028 argv + num_unsaved_files + 1 + NumLocations,
2029 argc - num_unsaved_files - 2 - NumLocations,
2030 unsaved_files,
2031 Repeats > 1? 0 : num_unsaved_files,
2032 getDefaultParsingOptions());
2033
2034 if (!TU) {
2035 fprintf(stderr, "unable to parse input\n");
2036 return -1;
2037 }
2038
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002039 if (checkForErrors(TU) != 0)
2040 return -1;
2041
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002042 for (I = 0; I != Repeats; ++I) {
2043 if (Repeats > 1 &&
2044 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2045 clang_defaultReparseOptions(TU))) {
2046 clang_disposeTranslationUnit(TU);
2047 return 1;
2048 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002049
2050 if (checkForErrors(TU) != 0)
2051 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002052
2053 for (Loc = 0; Loc < NumLocations; ++Loc) {
2054 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2055 if (!file)
2056 continue;
2057
2058 Cursor = clang_getCursor(TU,
2059 clang_getLocation(TU, file, Locations[Loc].line,
2060 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002061
2062 if (checkForErrors(TU) != 0)
2063 return -1;
2064
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002065 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002066 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002067 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002068 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002069 clang_findReferencesInFile(Cursor, file, visitor);
2070 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002071
2072 if (checkForErrors(TU) != 0)
2073 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002074 }
2075 }
2076 }
2077
2078 PrintDiagnostics(TU);
2079 clang_disposeTranslationUnit(TU);
2080 clang_disposeIndex(CIdx);
2081 free(Locations);
2082 free_remapped_files(unsaved_files, num_unsaved_files);
2083 return 0;
2084}
2085
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002086typedef struct {
2087 const char *check_prefix;
2088 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002089 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002090 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002091 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002092} IndexData;
2093
2094static void printCheck(IndexData *data) {
2095 if (data->check_prefix) {
2096 if (data->first_check_printed) {
2097 printf("// %s-NEXT: ", data->check_prefix);
2098 } else {
2099 printf("// %s : ", data->check_prefix);
2100 data->first_check_printed = 1;
2101 }
2102 }
2103}
2104
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002105static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002106 CXString filename = clang_getFileName((CXFile)file);
2107 printf("%s", clang_getCString(filename));
2108 clang_disposeString(filename);
2109}
2110
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002111static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2112 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002113 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002114 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002115 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002116 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002117 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002118
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002119 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002120 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2121 if (line == 0) {
2122 printf("<null loc>");
2123 return;
2124 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002125 if (!file) {
2126 printf("<no idxfile>");
2127 return;
2128 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002129 filename = clang_getFileName((CXFile)file);
2130 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002131 if (strcmp(cname, index_data->main_filename) == 0)
2132 isMainFile = 1;
2133 else
2134 isMainFile = 0;
2135 clang_disposeString(filename);
2136
2137 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002138 printCXIndexFile(file);
2139 printf(":");
2140 }
2141 printf("%d:%d", line, column);
2142}
2143
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002144static unsigned digitCount(unsigned val) {
2145 unsigned c = 1;
2146 while (1) {
2147 if (val < 10)
2148 return c;
2149 ++c;
2150 val /= 10;
2151 }
2152}
2153
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002154static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2155 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002156 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002157 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002158 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002159 unsigned line, column;
2160
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002161 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002162 if (!name)
2163 name = "<anon-tag>";
2164
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002165 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002166 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002167 newStr = (char *)malloc(strlen(name) +
2168 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002169 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002170 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002171}
2172
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002173static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2174 CXIdxClientContainer container;
2175 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002176 if (!container)
2177 printf("[<<NULL>>]");
2178 else
2179 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002180}
2181
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002182static const char *getEntityKindString(CXIdxEntityKind kind) {
2183 switch (kind) {
2184 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2185 case CXIdxEntity_Typedef: return "typedef";
2186 case CXIdxEntity_Function: return "function";
2187 case CXIdxEntity_Variable: return "variable";
2188 case CXIdxEntity_Field: return "field";
2189 case CXIdxEntity_EnumConstant: return "enumerator";
2190 case CXIdxEntity_ObjCClass: return "objc-class";
2191 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2192 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002193 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2194 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002195 case CXIdxEntity_ObjCProperty: return "objc-property";
2196 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2197 case CXIdxEntity_Enum: return "enum";
2198 case CXIdxEntity_Struct: return "struct";
2199 case CXIdxEntity_Union: return "union";
2200 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002201 case CXIdxEntity_CXXNamespace: return "namespace";
2202 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2203 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2204 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2205 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2206 case CXIdxEntity_CXXConstructor: return "constructor";
2207 case CXIdxEntity_CXXDestructor: return "destructor";
2208 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2209 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikie35adca02012-08-31 21:55:26 +00002210 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002211 }
2212 assert(0 && "Garbage entity kind");
2213 return 0;
2214}
2215
2216static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2217 switch (kind) {
2218 case CXIdxEntity_NonTemplate: return "";
2219 case CXIdxEntity_Template: return "-template";
2220 case CXIdxEntity_TemplatePartialSpecialization:
2221 return "-template-partial-spec";
2222 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002223 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002224 assert(0 && "Garbage entity kind");
2225 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002226}
2227
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002228static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2229 switch (kind) {
2230 case CXIdxEntityLang_None: return "<none>";
2231 case CXIdxEntityLang_C: return "C";
2232 case CXIdxEntityLang_ObjC: return "ObjC";
2233 case CXIdxEntityLang_CXX: return "C++";
2234 }
2235 assert(0 && "Garbage language kind");
2236 return 0;
2237}
2238
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002239static void printEntityInfo(const char *cb,
2240 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002241 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002242 const char *name;
2243 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002244 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002245 index_data = (IndexData *)client_data;
2246 printCheck(index_data);
2247
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002248 if (!info) {
2249 printf("%s: <<NULL>>", cb);
2250 return;
2251 }
2252
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002253 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002254 if (!name)
2255 name = "<anon-tag>";
2256
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002257 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2258 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002259 printf(" | name: %s", name);
2260 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002261 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002262
2263 for (i = 0; i != info->numAttributes; ++i) {
2264 const CXIdxAttrInfo *Attr = info->attributes[i];
2265 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002266 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002267 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002268}
2269
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002270static void printBaseClassInfo(CXClientData client_data,
2271 const CXIdxBaseClassInfo *info) {
2272 printEntityInfo(" <base>", client_data, info->base);
2273 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002274 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002275 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002276 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002277}
2278
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002279static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2280 CXClientData client_data) {
2281 unsigned i;
2282 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2283 printEntityInfo(" <protocol>", client_data,
2284 ProtoInfo->protocols[i]->protocol);
2285 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002286 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002287 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002288 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002289 printf("\n");
2290 }
2291}
2292
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002293static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002294 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002295 CXString str;
2296 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002297 unsigned numDiags, i;
2298 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002299 IndexData *index_data;
2300 index_data = (IndexData *)client_data;
2301 printCheck(index_data);
2302
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002303 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2304 for (i = 0; i != numDiags; ++i) {
2305 diag = clang_getDiagnosticInSet(diagSet, i);
2306 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2307 cstr = clang_getCString(str);
2308 printf("[diagnostic]: %s\n", cstr);
2309 clang_disposeString(str);
2310
2311 if (getenv("CINDEXTEST_FAILONERROR") &&
2312 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2313 index_data->fail_for_error = 1;
2314 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002315 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002316}
2317
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002318static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2319 CXFile file, void *reserved) {
2320 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002321 CXString filename;
2322
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002323 index_data = (IndexData *)client_data;
2324 printCheck(index_data);
2325
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002326 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002327 index_data->main_filename = clang_getCString(filename);
2328 clang_disposeString(filename);
2329
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002330 printf("[enteredMainFile]: ");
2331 printCXIndexFile((CXIdxClientFile)file);
2332 printf("\n");
2333
2334 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002335}
2336
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002337static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002338 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002339 IndexData *index_data;
2340 index_data = (IndexData *)client_data;
2341 printCheck(index_data);
2342
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002343 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002344 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002345 printf(" | name: \"%s\"", info->filename);
2346 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002347 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002348 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002349
2350 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002351}
2352
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002353static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002354 void *reserved) {
2355 IndexData *index_data;
2356 index_data = (IndexData *)client_data;
2357 printCheck(index_data);
2358
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002359 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002360 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002361}
2362
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002363static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002364 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002365 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002366 const CXIdxObjCCategoryDeclInfo *CatInfo;
2367 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002368 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002369 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002370 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002371 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002372 index_data = (IndexData *)client_data;
2373
2374 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2375 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002376 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002377 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002378 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002379 printf(" | semantic-container: ");
2380 printCXIndexContainer(info->semanticContainer);
2381 printf(" | lexical-container: ");
2382 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002383 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002384 printf(" | isDef: %d", info->isDefinition);
2385 printf(" | isContainer: %d", info->isContainer);
2386 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002387
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002388 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002389 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002390 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002391 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002392 printf("\n");
2393 }
2394
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002395 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2396 const char *kindName = 0;
2397 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2398 switch (K) {
2399 case CXIdxObjCContainer_ForwardRef:
2400 kindName = "forward-ref"; break;
2401 case CXIdxObjCContainer_Interface:
2402 kindName = "interface"; break;
2403 case CXIdxObjCContainer_Implementation:
2404 kindName = "implementation"; break;
2405 }
2406 printCheck(index_data);
2407 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2408 }
2409
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002410 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002411 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2412 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002413 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002414 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002415 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002416 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002417 printf("\n");
2418 }
2419
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002420 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2421 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002422 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002423 printf("\n");
2424 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002425 }
2426
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002427 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2428 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002429 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002430
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002431 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2432 if (PropInfo->getter) {
2433 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2434 printf("\n");
2435 }
2436 if (PropInfo->setter) {
2437 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2438 printf("\n");
2439 }
2440 }
2441
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002442 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2443 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2444 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2445 printf("\n");
2446 }
2447 }
2448
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002449 if (info->declAsContainer)
2450 clang_index_setClientContainer(info->declAsContainer,
2451 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002452}
2453
2454static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002455 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002456 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002457 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002458 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002459 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002460 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002461 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002462 printf(" | container: ");
2463 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002464 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002465 switch (info->kind) {
2466 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002467 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002468 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002469 printf("\n");
2470}
2471
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002472static int index_abortQuery(CXClientData client_data, void *reserved) {
2473 IndexData *index_data;
2474 index_data = (IndexData *)client_data;
2475 return index_data->abort;
2476}
2477
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002478static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002479 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002480 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002481 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002482 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002483 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002484 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002485 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002486 index_indexEntityReference
2487};
2488
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002489static unsigned getIndexOptions(void) {
2490 unsigned index_opts;
2491 index_opts = 0;
2492 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2493 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2494 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2495 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2496
2497 return index_opts;
2498}
2499
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002500static int index_file(int argc, const char **argv) {
2501 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002502 CXIndex Idx;
2503 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002504 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002505 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002506 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002507
2508 check_prefix = 0;
2509 if (argc > 0) {
2510 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2511 check_prefix = argv[0] + strlen("-check-prefix=");
2512 ++argv;
2513 --argc;
2514 }
2515 }
2516
2517 if (argc == 0) {
2518 fprintf(stderr, "no compiler arguments\n");
2519 return -1;
2520 }
2521
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002522 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2523 /* displayDiagnosics=*/1))) {
2524 fprintf(stderr, "Could not create Index\n");
2525 return 1;
2526 }
2527 idxAction = 0;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002528
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002529 index_data.check_prefix = check_prefix;
2530 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002531 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002532 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002533
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002534 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002535 idxAction = clang_IndexAction_create(Idx);
2536 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002537 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002538 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002539 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002540 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002541
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002542 clang_IndexAction_dispose(idxAction);
2543 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002544 return result;
2545}
2546
2547static int index_tu(int argc, const char **argv) {
2548 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002549 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002550 CXTranslationUnit TU;
2551 const char *check_prefix;
2552 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002553 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002554 int result;
2555
2556 check_prefix = 0;
2557 if (argc > 0) {
2558 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2559 check_prefix = argv[0] + strlen("-check-prefix=");
2560 ++argv;
2561 --argc;
2562 }
2563 }
2564
2565 if (argc == 0) {
2566 fprintf(stderr, "no ast file\n");
2567 return -1;
2568 }
2569
2570 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2571 /* displayDiagnosics=*/1))) {
2572 fprintf(stderr, "Could not create Index\n");
2573 return 1;
2574 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002575 idxAction = 0;
2576 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002577
2578 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002579 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002580
2581 index_data.check_prefix = check_prefix;
2582 index_data.first_check_printed = 0;
2583 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002584 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002585
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002586 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002587 idxAction = clang_IndexAction_create(Idx);
2588 result = clang_indexTranslationUnit(idxAction, &index_data,
2589 &IndexCB,sizeof(IndexCB),
2590 index_opts, TU);
2591 if (index_data.fail_for_error)
2592 goto finished;
2593
2594 finished:
2595 clang_IndexAction_dispose(idxAction);
2596 clang_disposeIndex(Idx);
2597
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002598 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002599}
2600
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002601int perform_token_annotation(int argc, const char **argv) {
2602 const char *input = argv[1];
2603 char *filename = 0;
2604 unsigned line, second_line;
2605 unsigned column, second_column;
2606 CXIndex CIdx;
2607 CXTranslationUnit TU = 0;
2608 int errorCode;
2609 struct CXUnsavedFile *unsaved_files = 0;
2610 int num_unsaved_files = 0;
2611 CXToken *tokens;
2612 unsigned num_tokens;
2613 CXSourceRange range;
2614 CXSourceLocation startLoc, endLoc;
2615 CXFile file = 0;
2616 CXCursor *cursors = 0;
2617 unsigned i;
2618
2619 input += strlen("-test-annotate-tokens=");
2620 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2621 &second_line, &second_column)))
2622 return errorCode;
2623
Richard Smithe07c5f82012-07-05 08:20:49 +00002624 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
2625 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002626 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00002627 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002628
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002629 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002630 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2631 argv + num_unsaved_files + 2,
2632 argc - num_unsaved_files - 3,
2633 unsaved_files,
2634 num_unsaved_files,
2635 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002636 if (!TU) {
2637 fprintf(stderr, "unable to parse input\n");
2638 clang_disposeIndex(CIdx);
2639 free(filename);
2640 free_remapped_files(unsaved_files, num_unsaved_files);
2641 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002642 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002643 errorCode = 0;
2644
Richard Smithe07c5f82012-07-05 08:20:49 +00002645 if (checkForErrors(TU) != 0) {
2646 errorCode = -1;
2647 goto teardown;
2648 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002649
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002650 if (getenv("CINDEXTEST_EDITING")) {
2651 for (i = 0; i < 5; ++i) {
2652 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2653 clang_defaultReparseOptions(TU))) {
2654 fprintf(stderr, "Unable to reparse translation unit!\n");
2655 errorCode = -1;
2656 goto teardown;
2657 }
2658 }
2659 }
2660
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002661 if (checkForErrors(TU) != 0) {
2662 errorCode = -1;
2663 goto teardown;
2664 }
2665
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002666 file = clang_getFile(TU, filename);
2667 if (!file) {
2668 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2669 errorCode = -1;
2670 goto teardown;
2671 }
2672
2673 startLoc = clang_getLocation(TU, file, line, column);
2674 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002675 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002676 column);
2677 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002678 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002679 }
2680
2681 endLoc = clang_getLocation(TU, file, second_line, second_column);
2682 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002683 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002684 second_line, second_column);
2685 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002686 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002687 }
2688
2689 range = clang_getRange(startLoc, endLoc);
2690 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002691
2692 if (checkForErrors(TU) != 0) {
2693 errorCode = -1;
2694 goto teardown;
2695 }
2696
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002697 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2698 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002699
2700 if (checkForErrors(TU) != 0) {
2701 errorCode = -1;
2702 goto teardown;
2703 }
2704
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002705 for (i = 0; i != num_tokens; ++i) {
2706 const char *kind = "<unknown>";
2707 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2708 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2709 unsigned start_line, start_column, end_line, end_column;
2710
2711 switch (clang_getTokenKind(tokens[i])) {
2712 case CXToken_Punctuation: kind = "Punctuation"; break;
2713 case CXToken_Keyword: kind = "Keyword"; break;
2714 case CXToken_Identifier: kind = "Identifier"; break;
2715 case CXToken_Literal: kind = "Literal"; break;
2716 case CXToken_Comment: kind = "Comment"; break;
2717 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002718 clang_getSpellingLocation(clang_getRangeStart(extent),
2719 0, &start_line, &start_column, 0);
2720 clang_getSpellingLocation(clang_getRangeEnd(extent),
2721 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002722 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002723 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002724 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002725 if (!clang_isInvalid(cursors[i].kind)) {
2726 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002727 PrintCursor(cursors[i], NULL);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002728 }
2729 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002730 }
2731 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002732 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002733
2734 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002735 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002736 clang_disposeTranslationUnit(TU);
2737 clang_disposeIndex(CIdx);
2738 free(filename);
2739 free_remapped_files(unsaved_files, num_unsaved_files);
2740 return errorCode;
2741}
2742
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002743static int
2744perform_test_compilation_db(const char *database, int argc, const char **argv) {
2745 CXCompilationDatabase db;
2746 CXCompileCommands CCmds;
2747 CXCompileCommand CCmd;
2748 CXCompilationDatabase_Error ec;
2749 CXString wd;
2750 CXString arg;
2751 int errorCode = 0;
2752 char *tmp;
2753 unsigned len;
2754 char *buildDir;
2755 int i, j, a, numCmds, numArgs;
2756
2757 len = strlen(database);
2758 tmp = (char *) malloc(len+1);
2759 memcpy(tmp, database, len+1);
2760 buildDir = dirname(tmp);
2761
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002762 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002763
2764 if (db) {
2765
2766 if (ec!=CXCompilationDatabase_NoError) {
2767 printf("unexpected error %d code while loading compilation database\n", ec);
2768 errorCode = -1;
2769 goto cdb_end;
2770 }
2771
2772 for (i=0; i<argc && errorCode==0; ) {
2773 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002774 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002775
2776 if (!CCmds) {
2777 printf("file %s not found in compilation db\n", argv[i+1]);
2778 errorCode = -1;
2779 break;
2780 }
2781
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002782 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002783
2784 if (numCmds==0) {
2785 fprintf(stderr, "should not get an empty compileCommand set for file"
2786 " '%s'\n", argv[i+1]);
2787 errorCode = -1;
2788 break;
2789 }
2790
2791 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002792 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002793
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002794 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002795 printf("workdir:'%s'", clang_getCString(wd));
2796 clang_disposeString(wd);
2797
2798 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002799 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002800 for (a=0; a<numArgs; ++a) {
2801 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002802 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002803 printf("%s", clang_getCString(arg));
2804 clang_disposeString(arg);
2805 }
2806 printf("'\n");
2807 }
2808
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002809 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002810
2811 i += 2;
2812 }
2813 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002814 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002815 } else {
2816 printf("database loading failed with error code %d.\n", ec);
2817 errorCode = -1;
2818 }
2819
2820cdb_end:
2821 free(tmp);
2822
2823 return errorCode;
2824}
2825
Ted Kremenek0d435192009-11-17 18:13:31 +00002826/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002827/* USR printing. */
2828/******************************************************************************/
2829
2830static int insufficient_usr(const char *kind, const char *usage) {
2831 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2832 return 1;
2833}
2834
2835static unsigned isUSR(const char *s) {
2836 return s[0] == 'c' && s[1] == ':';
2837}
2838
2839static int not_usr(const char *s, const char *arg) {
2840 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2841 return 1;
2842}
2843
2844static void print_usr(CXString usr) {
2845 const char *s = clang_getCString(usr);
2846 printf("%s\n", s);
2847 clang_disposeString(usr);
2848}
2849
2850static void display_usrs() {
2851 fprintf(stderr, "-print-usrs options:\n"
2852 " ObjCCategory <class name> <category name>\n"
2853 " ObjCClass <class name>\n"
2854 " ObjCIvar <ivar name> <class USR>\n"
2855 " ObjCMethod <selector> [0=class method|1=instance method] "
2856 "<class USR>\n"
2857 " ObjCProperty <property name> <class USR>\n"
2858 " ObjCProtocol <protocol name>\n");
2859}
2860
2861int print_usrs(const char **I, const char **E) {
2862 while (I != E) {
2863 const char *kind = *I;
2864 unsigned len = strlen(kind);
2865 switch (len) {
2866 case 8:
2867 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2868 if (I + 2 >= E)
2869 return insufficient_usr(kind, "<ivar name> <class USR>");
2870 if (!isUSR(I[2]))
2871 return not_usr("<class USR>", I[2]);
2872 else {
2873 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002874 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002875 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002876 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2877 }
2878
2879 I += 3;
2880 continue;
2881 }
2882 break;
2883 case 9:
2884 if (memcmp(kind, "ObjCClass", 9) == 0) {
2885 if (I + 1 >= E)
2886 return insufficient_usr(kind, "<class name>");
2887 print_usr(clang_constructUSR_ObjCClass(I[1]));
2888 I += 2;
2889 continue;
2890 }
2891 break;
2892 case 10:
2893 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2894 if (I + 3 >= E)
2895 return insufficient_usr(kind, "<method selector> "
2896 "[0=class method|1=instance method] <class USR>");
2897 if (!isUSR(I[3]))
2898 return not_usr("<class USR>", I[3]);
2899 else {
2900 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002901 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002902 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002903 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2904 }
2905 I += 4;
2906 continue;
2907 }
2908 break;
2909 case 12:
2910 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2911 if (I + 2 >= E)
2912 return insufficient_usr(kind, "<class name> <category name>");
2913 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2914 I += 3;
2915 continue;
2916 }
2917 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2918 if (I + 1 >= E)
2919 return insufficient_usr(kind, "<protocol name>");
2920 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2921 I += 2;
2922 continue;
2923 }
2924 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2925 if (I + 2 >= E)
2926 return insufficient_usr(kind, "<property name> <class USR>");
2927 if (!isUSR(I[2]))
2928 return not_usr("<class USR>", I[2]);
2929 else {
2930 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002931 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002932 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002933 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2934 }
2935 I += 3;
2936 continue;
2937 }
2938 break;
2939 default:
2940 break;
2941 }
2942 break;
2943 }
2944
2945 if (I != E) {
2946 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2947 display_usrs();
2948 return 1;
2949 }
2950 return 0;
2951}
2952
2953int print_usrs_file(const char *file_name) {
2954 char line[2048];
2955 const char *args[128];
2956 unsigned numChars = 0;
2957
2958 FILE *fp = fopen(file_name, "r");
2959 if (!fp) {
2960 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2961 return 1;
2962 }
2963
2964 /* This code is not really all that safe, but it works fine for testing. */
2965 while (!feof(fp)) {
2966 char c = fgetc(fp);
2967 if (c == '\n') {
2968 unsigned i = 0;
2969 const char *s = 0;
2970
2971 if (numChars == 0)
2972 continue;
2973
2974 line[numChars] = '\0';
2975 numChars = 0;
2976
2977 if (line[0] == '/' && line[1] == '/')
2978 continue;
2979
2980 s = strtok(line, " ");
2981 while (s) {
2982 args[i] = s;
2983 ++i;
2984 s = strtok(0, " ");
2985 }
2986 if (print_usrs(&args[0], &args[i]))
2987 return 1;
2988 }
2989 else
2990 line[numChars++] = c;
2991 }
2992
2993 fclose(fp);
2994 return 0;
2995}
2996
2997/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002998/* Command line processing. */
2999/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003000int write_pch_file(const char *filename, int argc, const char *argv[]) {
3001 CXIndex Idx;
3002 CXTranslationUnit TU;
3003 struct CXUnsavedFile *unsaved_files = 0;
3004 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00003005 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003006
3007 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
3008
3009 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3010 clang_disposeIndex(Idx);
3011 return -1;
3012 }
3013
3014 TU = clang_parseTranslationUnit(Idx, 0,
3015 argv + num_unsaved_files,
3016 argc - num_unsaved_files,
3017 unsaved_files,
3018 num_unsaved_files,
3019 CXTranslationUnit_Incomplete);
3020 if (!TU) {
3021 fprintf(stderr, "Unable to load translation unit!\n");
3022 free_remapped_files(unsaved_files, num_unsaved_files);
3023 clang_disposeIndex(Idx);
3024 return 1;
3025 }
3026
Douglas Gregor39c411f2011-07-06 16:43:36 +00003027 switch (clang_saveTranslationUnit(TU, filename,
3028 clang_defaultSaveOptions(TU))) {
3029 case CXSaveError_None:
3030 break;
3031
3032 case CXSaveError_TranslationErrors:
3033 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3034 filename);
3035 result = 2;
3036 break;
3037
3038 case CXSaveError_InvalidTU:
3039 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3040 filename);
3041 result = 3;
3042 break;
3043
3044 case CXSaveError_Unknown:
3045 default:
3046 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3047 result = 1;
3048 break;
3049 }
3050
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003051 clang_disposeTranslationUnit(TU);
3052 free_remapped_files(unsaved_files, num_unsaved_files);
3053 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00003054 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003055}
3056
3057/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00003058/* Serialized diagnostics. */
3059/******************************************************************************/
3060
3061static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3062 switch (error) {
3063 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3064 case CXLoadDiag_None: break;
3065 case CXLoadDiag_Unknown: return "Unknown";
3066 case CXLoadDiag_InvalidFile: return "Invalid File";
3067 }
3068 return "None";
3069}
3070
3071static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3072 switch (severity) {
3073 case CXDiagnostic_Note: return "note";
3074 case CXDiagnostic_Error: return "error";
3075 case CXDiagnostic_Fatal: return "fatal";
3076 case CXDiagnostic_Ignored: return "ignored";
3077 case CXDiagnostic_Warning: return "warning";
3078 }
3079 return "unknown";
3080}
3081
3082static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003083 if (indent == 0)
3084 return;
3085 fprintf(stderr, "+");
3086 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00003087 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003088 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00003089 --indent;
3090 }
3091}
3092
3093static void printLocation(CXSourceLocation L) {
3094 CXFile File;
3095 CXString FileName;
3096 unsigned line, column, offset;
3097
3098 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3099 FileName = clang_getFileName(File);
3100
3101 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3102 clang_disposeString(FileName);
3103}
3104
3105static void printRanges(CXDiagnostic D, unsigned indent) {
3106 unsigned i, n = clang_getDiagnosticNumRanges(D);
3107
3108 for (i = 0; i < n; ++i) {
3109 CXSourceLocation Start, End;
3110 CXSourceRange SR = clang_getDiagnosticRange(D, i);
3111 Start = clang_getRangeStart(SR);
3112 End = clang_getRangeEnd(SR);
3113
3114 printIndent(indent);
3115 fprintf(stderr, "Range: ");
3116 printLocation(Start);
3117 fprintf(stderr, " ");
3118 printLocation(End);
3119 fprintf(stderr, "\n");
3120 }
3121}
3122
3123static void printFixIts(CXDiagnostic D, unsigned indent) {
3124 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00003125 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00003126 for (i = 0 ; i < n; ++i) {
3127 CXSourceRange ReplacementRange;
3128 CXString text;
3129 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3130
3131 printIndent(indent);
3132 fprintf(stderr, "FIXIT: (");
3133 printLocation(clang_getRangeStart(ReplacementRange));
3134 fprintf(stderr, " - ");
3135 printLocation(clang_getRangeEnd(ReplacementRange));
3136 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3137 clang_disposeString(text);
3138 }
3139}
3140
3141static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003142 unsigned i, n;
3143
Ted Kremenek15322172011-11-10 08:43:12 +00003144 if (!Diags)
3145 return;
3146
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003147 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00003148 for (i = 0; i < n; ++i) {
3149 CXSourceLocation DiagLoc;
3150 CXDiagnostic D;
3151 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003152 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00003153 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003154 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00003155
3156 D = clang_getDiagnosticInSet(Diags, i);
3157 DiagLoc = clang_getDiagnosticLocation(D);
3158 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3159 FileName = clang_getFileName(File);
3160 DiagSpelling = clang_getDiagnosticSpelling(D);
3161
3162 printIndent(indent);
3163
3164 fprintf(stderr, "%s:%d:%d: %s: %s",
3165 clang_getCString(FileName),
3166 line,
3167 column,
3168 getSeverityString(clang_getDiagnosticSeverity(D)),
3169 clang_getCString(DiagSpelling));
3170
3171 DiagOption = clang_getDiagnosticOption(D, 0);
3172 DiagOptionStr = clang_getCString(DiagOption);
3173 if (DiagOptionStr) {
3174 fprintf(stderr, " [%s]", DiagOptionStr);
3175 }
3176
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003177 DiagCat = clang_getDiagnosticCategoryText(D);
3178 DiagCatStr = clang_getCString(DiagCat);
3179 if (DiagCatStr) {
3180 fprintf(stderr, " [%s]", DiagCatStr);
3181 }
3182
Ted Kremenek15322172011-11-10 08:43:12 +00003183 fprintf(stderr, "\n");
3184
3185 printRanges(D, indent);
3186 printFixIts(D, indent);
3187
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003188 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003189 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3190
3191 clang_disposeString(FileName);
3192 clang_disposeString(DiagSpelling);
3193 clang_disposeString(DiagOption);
3194 }
3195}
3196
3197static int read_diagnostics(const char *filename) {
3198 enum CXLoadDiag_Error error;
3199 CXString errorString;
3200 CXDiagnosticSet Diags = 0;
3201
3202 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3203 if (!Diags) {
3204 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3205 getDiagnosticCodeStr(error),
3206 clang_getCString(errorString));
3207 clang_disposeString(errorString);
3208 return 1;
3209 }
3210
3211 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003212 fprintf(stderr, "Number of diagnostics: %d\n",
3213 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003214 clang_disposeDiagnosticSet(Diags);
3215 return 0;
3216}
3217
3218/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003219/* Command line processing. */
3220/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003221
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003222static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003223 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003224 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003225 if (strcmp(s, "-usrs") == 0)
3226 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003227 if (strncmp(s, "-memory-usage", 13) == 0)
3228 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003229 return NULL;
3230}
3231
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003232static void print_usage(void) {
3233 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003234 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003235 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003236 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003237 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003238 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003239 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003240 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003241 "[FileCheck prefix]\n");
3242 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003243 " c-index-test -test-load-tu <AST file> <symbol filter> "
3244 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003245 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3246 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003247 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003248 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003249 " c-index-test -test-load-source-memory-usage "
3250 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003251 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3252 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003253 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003254 " c-index-test -test-load-source-usrs-memory-usage "
3255 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003256 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3257 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003258 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003259 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003260 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003261 " c-index-test -test-print-typekind {<args>}*\n"
3262 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003263 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003264 " c-index-test -write-pch <file> <compiler arguments>\n");
3265 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003266 " c-index-test -compilation-db [lookup <filename>] database\n");
3267 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003268 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003269 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003270 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003271 " all - load all symbols, including those from PCH\n"
3272 " local - load all symbols except those in PCH\n"
3273 " category - only load ObjC categories (non-PCH)\n"
3274 " interface - only load ObjC interfaces (non-PCH)\n"
3275 " protocol - only load ObjC protocols (non-PCH)\n"
3276 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003277 " typedef - only load typdefs (non-PCH)\n"
3278 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003279}
3280
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003281/***/
3282
3283int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003284 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003285 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3286 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003287 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003288 return perform_code_completion(argc, argv, 0);
3289 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3290 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003291 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3292 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003293 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3294 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003295 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
3296 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003297 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3298 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003299 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003300 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003301 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003302 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3303 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003304 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003305 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3306 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3307 if (I) {
3308 int trials = atoi(argv[2]);
3309 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3310 NULL);
3311 }
3312 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003313 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003314 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003315
3316 PostVisitTU postVisit = 0;
3317 if (strstr(argv[1], "-memory-usage"))
3318 postVisit = PrintMemoryUsage;
3319
Ted Kremenek7d405622010-01-12 23:34:26 +00003320 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003321 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3322 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003323 }
3324 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003325 return perform_file_scan(argv[2], argv[3],
3326 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003327 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3328 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003329 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3330 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3331 PrintInclusionStack);
3332 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3333 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3334 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003335 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3336 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3337 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003338 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
3339 return perform_test_load_source(argc - 2, argv + 2, "all",
3340 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003341 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3342 if (argc > 2)
3343 return print_usrs(argv + 2, argv + argc);
3344 else {
3345 display_usrs();
3346 return 1;
3347 }
3348 }
3349 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3350 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003351 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3352 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003353 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3354 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3355
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003356 print_usage();
3357 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003358}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003359
3360/***/
3361
3362/* We intentionally run in a separate thread to ensure we at least minimal
3363 * testing of a multithreaded environment (for example, having a reduced stack
3364 * size). */
3365
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003366typedef struct thread_info {
3367 int argc;
3368 const char **argv;
3369 int result;
3370} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003371void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003372 thread_info *client_data = client_data_v;
3373 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003374#ifdef __CYGWIN__
3375 fflush(stdout); /* stdout is not flushed on Cygwin. */
3376#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003377}
3378
3379int main(int argc, const char **argv) {
Benjamin Kramerd1a4f682012-08-10 10:06:13 +00003380 thread_info client_data;
3381
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003382#ifdef CLANG_HAVE_LIBXML
3383 LIBXML_TEST_VERSION
3384#endif
3385
Douglas Gregor61605982010-10-27 16:00:01 +00003386 if (getenv("CINDEXTEST_NOTHREADS"))
3387 return cindextest_main(argc, argv);
3388
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003389 client_data.argc = argc;
3390 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003391 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003392 return client_data.result;
3393}