blob: b8664ed75aa3fd6ad0cbd71dd5f0877d768e8ab6 [file] [log] [blame]
Steve Naroff2b8ee6c2009-09-01 15:55:40 +00001/* c-index-test.c */
Steve Naroff50398192009-08-28 15:28:48 +00002
3#include "clang-c/Index.h"
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00004#include "clang-c/CXCompilationDatabase.h"
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00005#include "llvm/Config/config.h"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00006#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00007#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00008#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00009#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +000010#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +000011
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +000012#ifdef CLANG_HAVE_LIBXML
13#include <libxml/parser.h>
14#include <libxml/relaxng.h>
15#include <libxml/xmlerror.h>
16#endif
17
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +000018#ifdef _WIN32
19# include <direct.h>
20#else
21# include <unistd.h>
22#endif
23
Ted Kremenek0d435192009-11-17 18:13:31 +000024/******************************************************************************/
25/* Utility functions. */
26/******************************************************************************/
27
John Thompson2e06fc82009-10-27 13:42:56 +000028#ifdef _MSC_VER
29char *basename(const char* path)
30{
31 char* base1 = (char*)strrchr(path, '/');
32 char* base2 = (char*)strrchr(path, '\\');
33 if (base1 && base2)
34 return((base1 > base2) ? base1 + 1 : base2 + 1);
35 else if (base1)
36 return(base1 + 1);
37 else if (base2)
38 return(base2 + 1);
39
40 return((char*)path);
41}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000042char *dirname(char* path)
43{
44 char* base1 = (char*)strrchr(path, '/');
45 char* base2 = (char*)strrchr(path, '\\');
46 if (base1 && base2)
47 if (base1 > base2)
48 *base1 = 0;
49 else
50 *base2 = 0;
51 else if (base1)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000052 *base1 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000053 else if (base2)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000054 *base2 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000055
56 return path;
57}
John Thompson2e06fc82009-10-27 13:42:56 +000058#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000059extern char *basename(const char *);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000060extern char *dirname(char *);
John Thompson2e06fc82009-10-27 13:42:56 +000061#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000062
Douglas Gregor45ba9a12010-07-25 17:39:21 +000063/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000064static unsigned getDefaultParsingOptions() {
65 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
66
67 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000068 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000069 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
70 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000071 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
72 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000073 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
74 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +000075 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
76 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregor44c181a2010-07-23 00:33:23 +000077
78 return options;
79}
80
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000081static int checkForErrors(CXTranslationUnit TU);
82
Daniel Dunbar51b058c2010-02-14 08:32:24 +000083static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
84 unsigned end_line, unsigned end_column) {
85 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000086 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000087}
88
Ted Kremenek1c6da172009-11-17 19:37:36 +000089static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
90 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000091
Douglas Gregora88084b2010-02-18 18:08:43 +000092 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000093 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000094 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
95 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000096 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000097 return 1;
98}
99
Douglas Gregor4db64a42010-01-23 00:14:00 +0000100void free_remapped_files(struct CXUnsavedFile *unsaved_files,
101 int num_unsaved_files) {
102 int i;
103 for (i = 0; i != num_unsaved_files; ++i) {
104 free((char *)unsaved_files[i].Filename);
105 free((char *)unsaved_files[i].Contents);
106 }
Douglas Gregor653a55f2010-08-19 20:50:29 +0000107 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000108}
109
110int parse_remapped_files(int argc, const char **argv, int start_arg,
111 struct CXUnsavedFile **unsaved_files,
112 int *num_unsaved_files) {
113 int i;
114 int arg;
115 int prefix_len = strlen("-remap-file=");
116 *unsaved_files = 0;
117 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000118
Douglas Gregor4db64a42010-01-23 00:14:00 +0000119 /* Count the number of remapped files. */
120 for (arg = start_arg; arg < argc; ++arg) {
121 if (strncmp(argv[arg], "-remap-file=", prefix_len))
122 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000123
Douglas Gregor4db64a42010-01-23 00:14:00 +0000124 ++*num_unsaved_files;
125 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000126
Douglas Gregor4db64a42010-01-23 00:14:00 +0000127 if (*num_unsaved_files == 0)
128 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000129
Douglas Gregor4db64a42010-01-23 00:14:00 +0000130 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +0000131 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
132 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000133 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
134 struct CXUnsavedFile *unsaved = *unsaved_files + i;
135 const char *arg_string = argv[arg] + prefix_len;
136 int filename_len;
137 char *filename;
138 char *contents;
139 FILE *to_file;
140 const char *semi = strchr(arg_string, ';');
141 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000142 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000143 "error: -remap-file=from;to argument is missing semicolon\n");
144 free_remapped_files(*unsaved_files, i);
145 *unsaved_files = 0;
146 *num_unsaved_files = 0;
147 return -1;
148 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000149
Douglas Gregor4db64a42010-01-23 00:14:00 +0000150 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000151 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000152 if (!to_file) {
153 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
154 semi + 1);
155 free_remapped_files(*unsaved_files, i);
156 *unsaved_files = 0;
157 *num_unsaved_files = 0;
158 return -1;
159 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000160
Douglas Gregor4db64a42010-01-23 00:14:00 +0000161 /* Determine the length of the file we're remapping to. */
162 fseek(to_file, 0, SEEK_END);
163 unsaved->Length = ftell(to_file);
164 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000165
Douglas Gregor4db64a42010-01-23 00:14:00 +0000166 /* Read the contents of the file we're remapping to. */
167 contents = (char *)malloc(unsaved->Length + 1);
168 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
169 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
170 (feof(to_file) ? "EOF" : "error"), semi + 1);
171 fclose(to_file);
172 free_remapped_files(*unsaved_files, i);
Richard Smithe07c5f82012-07-05 08:20:49 +0000173 free(contents);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000174 *unsaved_files = 0;
175 *num_unsaved_files = 0;
176 return -1;
177 }
178 contents[unsaved->Length] = 0;
179 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000180
Douglas Gregor4db64a42010-01-23 00:14:00 +0000181 /* Close the file. */
182 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000183
Douglas Gregor4db64a42010-01-23 00:14:00 +0000184 /* Copy the file name that we're remapping from. */
185 filename_len = semi - arg_string;
186 filename = (char *)malloc(filename_len + 1);
187 memcpy(filename, arg_string, filename_len);
188 filename[filename_len] = 0;
189 unsaved->Filename = filename;
190 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000191
Douglas Gregor4db64a42010-01-23 00:14:00 +0000192 return 0;
193}
194
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000195static const char *parse_comments_schema(int argc, const char **argv) {
196 const char *CommentsSchemaArg = "-comments-xml-schema=";
197 const char *CommentSchemaFile = NULL;
198
199 if (argc == 0)
200 return CommentSchemaFile;
201
202 if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg)))
203 CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg);
204
205 return CommentSchemaFile;
206}
207
Ted Kremenek0d435192009-11-17 18:13:31 +0000208/******************************************************************************/
209/* Pretty-printing. */
210/******************************************************************************/
211
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000212static const char *FileCheckPrefix = "CHECK";
213
214static void PrintCString(const char *CStr) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000215 if (CStr != NULL && CStr[0] != '\0') {
216 for ( ; *CStr; ++CStr) {
217 const char C = *CStr;
218 switch (C) {
219 case '\n': printf("\\n"); break;
220 case '\r': printf("\\r"); break;
221 case '\t': printf("\\t"); break;
222 case '\v': printf("\\v"); break;
223 case '\f': printf("\\f"); break;
224 default: putchar(C); break;
225 }
226 }
227 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000228}
229
230static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
231 printf(" %s=[", Prefix);
232 PrintCString(CStr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000233 printf("]");
234}
235
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000236static void PrintCXStringAndDispose(CXString Str) {
237 PrintCString(clang_getCString(Str));
238 clang_disposeString(Str);
239}
240
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000241static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) {
242 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
243}
244
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000245static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
246 CXString Str) {
247 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
248 clang_disposeString(Str);
249}
250
Douglas Gregor430d7a12011-07-25 17:48:11 +0000251static void PrintRange(CXSourceRange R, const char *str) {
252 CXFile begin_file, end_file;
253 unsigned begin_line, begin_column, end_line, end_column;
254
255 clang_getSpellingLocation(clang_getRangeStart(R),
256 &begin_file, &begin_line, &begin_column, 0);
257 clang_getSpellingLocation(clang_getRangeEnd(R),
258 &end_file, &end_line, &end_column, 0);
259 if (!begin_file || !end_file)
260 return;
261
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000262 if (str)
263 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000264 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
265}
266
Douglas Gregor358559d2010-10-02 22:49:11 +0000267int want_display_name = 0;
268
Douglas Gregorcc889662012-05-08 00:14:45 +0000269static void printVersion(const char *Prefix, CXVersion Version) {
270 if (Version.Major < 0)
271 return;
272 printf("%s%d", Prefix, Version.Major);
273
274 if (Version.Minor < 0)
275 return;
276 printf(".%d", Version.Minor);
277
278 if (Version.Subminor < 0)
279 return;
280 printf(".%d", Version.Subminor);
281}
282
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000283struct CommentASTDumpingContext {
284 int IndentLevel;
285};
286
287static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
288 CXComment Comment) {
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000289 unsigned i;
290 unsigned e;
291 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
292
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000293 Ctx->IndentLevel++;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000294 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000295 printf(" ");
296
297 printf("(");
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000298 switch (Kind) {
299 case CXComment_Null:
300 printf("CXComment_Null");
301 break;
302 case CXComment_Text:
303 printf("CXComment_Text");
304 PrintCXStringWithPrefixAndDispose("Text",
305 clang_TextComment_getText(Comment));
306 if (clang_Comment_isWhitespace(Comment))
307 printf(" IsWhitespace");
308 if (clang_InlineContentComment_hasTrailingNewline(Comment))
309 printf(" HasTrailingNewline");
310 break;
311 case CXComment_InlineCommand:
312 printf("CXComment_InlineCommand");
313 PrintCXStringWithPrefixAndDispose(
314 "CommandName",
315 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000316 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
317 case CXCommentInlineCommandRenderKind_Normal:
318 printf(" RenderNormal");
319 break;
320 case CXCommentInlineCommandRenderKind_Bold:
321 printf(" RenderBold");
322 break;
323 case CXCommentInlineCommandRenderKind_Monospaced:
324 printf(" RenderMonospaced");
325 break;
326 case CXCommentInlineCommandRenderKind_Emphasized:
327 printf(" RenderEmphasized");
328 break;
329 }
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000330 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000331 i != e; ++i) {
332 printf(" Arg[%u]=", i);
333 PrintCXStringAndDispose(
334 clang_InlineCommandComment_getArgText(Comment, i));
335 }
336 if (clang_InlineContentComment_hasTrailingNewline(Comment))
337 printf(" HasTrailingNewline");
338 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000339 case CXComment_HTMLStartTag: {
340 unsigned NumAttrs;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000341 printf("CXComment_HTMLStartTag");
342 PrintCXStringWithPrefixAndDispose(
343 "Name",
344 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000345 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000346 if (NumAttrs != 0) {
347 printf(" Attrs:");
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000348 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000349 printf(" ");
350 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
351 printf("=");
352 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
353 }
354 }
355 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
356 printf(" SelfClosing");
357 if (clang_InlineContentComment_hasTrailingNewline(Comment))
358 printf(" HasTrailingNewline");
359 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000360 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000361 case CXComment_HTMLEndTag:
362 printf("CXComment_HTMLEndTag");
363 PrintCXStringWithPrefixAndDispose(
364 "Name",
365 clang_HTMLTagComment_getTagName(Comment));
366 if (clang_InlineContentComment_hasTrailingNewline(Comment))
367 printf(" HasTrailingNewline");
368 break;
369 case CXComment_Paragraph:
370 printf("CXComment_Paragraph");
371 if (clang_Comment_isWhitespace(Comment))
372 printf(" IsWhitespace");
373 break;
374 case CXComment_BlockCommand:
375 printf("CXComment_BlockCommand");
376 PrintCXStringWithPrefixAndDispose(
377 "CommandName",
378 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000379 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000380 i != e; ++i) {
381 printf(" Arg[%u]=", i);
382 PrintCXStringAndDispose(
383 clang_BlockCommandComment_getArgText(Comment, i));
384 }
385 break;
386 case CXComment_ParamCommand:
387 printf("CXComment_ParamCommand");
388 switch (clang_ParamCommandComment_getDirection(Comment)) {
389 case CXCommentParamPassDirection_In:
390 printf(" in");
391 break;
392 case CXCommentParamPassDirection_Out:
393 printf(" out");
394 break;
395 case CXCommentParamPassDirection_InOut:
396 printf(" in,out");
397 break;
398 }
399 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
400 printf(" explicitly");
401 else
402 printf(" implicitly");
403 PrintCXStringWithPrefixAndDispose(
404 "ParamName",
405 clang_ParamCommandComment_getParamName(Comment));
406 if (clang_ParamCommandComment_isParamIndexValid(Comment))
407 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
408 else
409 printf(" ParamIndex=Invalid");
410 break;
Dmitri Gribenko96b09862012-07-31 22:37:06 +0000411 case CXComment_TParamCommand:
412 printf("CXComment_TParamCommand");
413 PrintCXStringWithPrefixAndDispose(
414 "ParamName",
415 clang_TParamCommandComment_getParamName(Comment));
416 if (clang_TParamCommandComment_isParamPositionValid(Comment)) {
417 printf(" ParamPosition={");
418 for (i = 0, e = clang_TParamCommandComment_getDepth(Comment);
419 i != e; ++i) {
420 printf("%u", clang_TParamCommandComment_getIndex(Comment, i));
421 if (i != e - 1)
422 printf(", ");
423 }
424 printf("}");
425 } else
426 printf(" ParamPosition=Invalid");
427 break;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000428 case CXComment_VerbatimBlockCommand:
429 printf("CXComment_VerbatimBlockCommand");
430 PrintCXStringWithPrefixAndDispose(
431 "CommandName",
432 clang_BlockCommandComment_getCommandName(Comment));
433 break;
434 case CXComment_VerbatimBlockLine:
435 printf("CXComment_VerbatimBlockLine");
436 PrintCXStringWithPrefixAndDispose(
437 "Text",
438 clang_VerbatimBlockLineComment_getText(Comment));
439 break;
440 case CXComment_VerbatimLine:
441 printf("CXComment_VerbatimLine");
442 PrintCXStringWithPrefixAndDispose(
443 "Text",
444 clang_VerbatimLineComment_getText(Comment));
445 break;
446 case CXComment_FullComment:
447 printf("CXComment_FullComment");
448 break;
449 }
450 if (Kind != CXComment_Null) {
451 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000452 unsigned i;
453 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000454 printf("\n// %s: ", FileCheckPrefix);
455 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
456 }
457 }
458 printf(")");
459 Ctx->IndentLevel--;
460}
461
462static void DumpCXComment(CXComment Comment) {
463 struct CommentASTDumpingContext Ctx;
464 Ctx.IndentLevel = 1;
465 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
466 DumpCXCommentInternal(&Ctx, Comment);
467 printf("]");
468}
469
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000470typedef struct {
471 const char *CommentSchemaFile;
472#ifdef CLANG_HAVE_LIBXML
473 xmlRelaxNGParserCtxtPtr RNGParser;
474 xmlRelaxNGPtr Schema;
475#endif
476} CommentXMLValidationData;
477
478static void ValidateCommentXML(const char *Str,
479 CommentXMLValidationData *ValidationData) {
480#ifdef CLANG_HAVE_LIBXML
481 xmlDocPtr Doc;
482 xmlRelaxNGValidCtxtPtr ValidationCtxt;
483 int status;
484
485 if (!ValidationData || !ValidationData->CommentSchemaFile)
486 return;
487
488 if (!ValidationData->RNGParser) {
489 ValidationData->RNGParser =
490 xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile);
491 ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser);
492 }
493 if (!ValidationData->RNGParser) {
494 printf(" libXMLError");
495 return;
496 }
497
498 Doc = xmlParseDoc((const xmlChar *) Str);
499
500 if (!Doc) {
501 xmlErrorPtr Error = xmlGetLastError();
502 printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message);
503 return;
504 }
505
506 ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->Schema);
507 status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc);
508 if (!status)
509 printf(" CommentXMLValid");
510 else if (status > 0) {
511 xmlErrorPtr Error = xmlGetLastError();
512 printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message);
513 } else
514 printf(" libXMLError");
515
516 xmlRelaxNGFreeValidCtxt(ValidationCtxt);
517 xmlFreeDoc(Doc);
518#endif
519}
520
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000521static void PrintCursorComments(CXCursor Cursor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000522 CommentXMLValidationData *ValidationData) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000523 {
524 CXString RawComment;
525 const char *RawCommentCString;
526 CXString BriefComment;
527 const char *BriefCommentCString;
528
529 RawComment = clang_Cursor_getRawCommentText(Cursor);
530 RawCommentCString = clang_getCString(RawComment);
531 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
532 PrintCStringWithPrefix("RawComment", RawCommentCString);
533 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
534
535 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
536 BriefCommentCString = clang_getCString(BriefComment);
537 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
538 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
539 clang_disposeString(BriefComment);
540 }
541 clang_disposeString(RawComment);
542 }
543
544 {
545 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
546 if (clang_Comment_getKind(Comment) != CXComment_Null) {
547 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
548 clang_FullComment_getAsHTML(Comment));
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000549 {
550 CXString XML;
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000551 XML = clang_FullComment_getAsXML(Comment);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000552 PrintCXStringWithPrefix("FullCommentAsXML", XML);
553 ValidateCommentXML(clang_getCString(XML), ValidationData);
554 clang_disposeString(XML);
555 }
556
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000557 DumpCXComment(Comment);
558 }
559 }
560}
561
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000562typedef struct {
563 unsigned line;
564 unsigned col;
565} LineCol;
566
567static int lineCol_cmp(const void *p1, const void *p2) {
568 const LineCol *lhs = p1;
569 const LineCol *rhs = p2;
570 if (lhs->line != rhs->line)
571 return (int)lhs->line - (int)rhs->line;
572 return (int)lhs->col - (int)rhs->col;
573}
574
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000575static void PrintCursor(CXCursor Cursor,
576 CommentXMLValidationData *ValidationData) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000577 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000578 if (clang_isInvalid(Cursor.kind)) {
579 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
580 printf("Invalid Cursor => %s", clang_getCString(ks));
581 clang_disposeString(ks);
582 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000583 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000584 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000585 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000586 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000587 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000588 CXCursor *overridden;
589 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000590 unsigned RefNameRangeNr;
591 CXSourceRange CursorExtent;
592 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000593 int AlwaysUnavailable;
594 int AlwaysDeprecated;
595 CXString UnavailableMessage;
596 CXString DeprecatedMessage;
597 CXPlatformAvailability PlatformAvailability[2];
598 int NumPlatformAvailability;
599 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000600
Ted Kremeneke68fff62010-02-17 00:41:32 +0000601 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000602 string = want_display_name? clang_getCursorDisplayName(Cursor)
603 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000604 printf("%s=%s", clang_getCString(ks),
605 clang_getCString(string));
606 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000607 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000608
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000609 Referenced = clang_getCursorReferenced(Cursor);
610 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000611 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
612 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
613 printf("[");
614 for (I = 0; I != N; ++I) {
615 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000616 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000617 if (I)
618 printf(", ");
619
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000620 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000621 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000622 printf("%d:%d", line, column);
623 }
624 printf("]");
625 } else {
626 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000627 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000628 printf(":%d:%d", line, column);
629 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000630 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000631
632 if (clang_isCursorDefinition(Cursor))
633 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000634
635 switch (clang_getCursorAvailability(Cursor)) {
636 case CXAvailability_Available:
637 break;
638
639 case CXAvailability_Deprecated:
640 printf(" (deprecated)");
641 break;
642
643 case CXAvailability_NotAvailable:
644 printf(" (unavailable)");
645 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000646
647 case CXAvailability_NotAccessible:
648 printf(" (inaccessible)");
649 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000650 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000651
Douglas Gregorcc889662012-05-08 00:14:45 +0000652 NumPlatformAvailability
653 = clang_getCursorPlatformAvailability(Cursor,
654 &AlwaysDeprecated,
655 &DeprecatedMessage,
656 &AlwaysUnavailable,
657 &UnavailableMessage,
658 PlatformAvailability, 2);
659 if (AlwaysUnavailable) {
660 printf(" (always unavailable: \"%s\")",
661 clang_getCString(UnavailableMessage));
662 } else if (AlwaysDeprecated) {
663 printf(" (always deprecated: \"%s\")",
664 clang_getCString(DeprecatedMessage));
665 } else {
666 for (I = 0; I != NumPlatformAvailability; ++I) {
667 if (I >= 2)
668 break;
669
670 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
671 if (PlatformAvailability[I].Unavailable)
672 printf(", unavailable");
673 else {
674 printVersion(", introduced=", PlatformAvailability[I].Introduced);
675 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
676 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
677 }
678 if (clang_getCString(PlatformAvailability[I].Message)[0])
679 printf(", message=\"%s\"",
680 clang_getCString(PlatformAvailability[I].Message));
681 printf(")");
682 }
683 }
684 for (I = 0; I != NumPlatformAvailability; ++I) {
685 if (I >= 2)
686 break;
687 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
688 }
689
690 clang_disposeString(DeprecatedMessage);
691 clang_disposeString(UnavailableMessage);
692
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000693 if (clang_CXXMethod_isStatic(Cursor))
694 printf(" (static)");
695 if (clang_CXXMethod_isVirtual(Cursor))
696 printf(" (virtual)");
697
Ted Kremenek95f33552010-08-26 01:42:22 +0000698 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
699 CXType T =
700 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
701 CXString S = clang_getTypeKindSpelling(T.kind);
702 printf(" [IBOutletCollection=%s]", clang_getCString(S));
703 clang_disposeString(S);
704 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000705
706 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
707 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
708 unsigned isVirtual = clang_isVirtualBase(Cursor);
709 const char *accessStr = 0;
710
711 switch (access) {
712 case CX_CXXInvalidAccessSpecifier:
713 accessStr = "invalid"; break;
714 case CX_CXXPublic:
715 accessStr = "public"; break;
716 case CX_CXXProtected:
717 accessStr = "protected"; break;
718 case CX_CXXPrivate:
719 accessStr = "private"; break;
720 }
721
722 printf(" [access=%s isVirtual=%s]", accessStr,
723 isVirtual ? "true" : "false");
724 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000725
726 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
727 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
728 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
729 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000730 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000731 printf(" [Specialization of %s:%d:%d]",
732 clang_getCString(Name), line, column);
733 clang_disposeString(Name);
734 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000735
736 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
737 if (num_overridden) {
738 unsigned I;
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000739 LineCol lineCols[50];
740 assert(num_overridden <= 50);
Douglas Gregor9f592342010-10-01 20:25:15 +0000741 printf(" [Overrides ");
742 for (I = 0; I != num_overridden; ++I) {
743 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000744 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000745 lineCols[I].line = line;
746 lineCols[I].col = column;
747 }
Michael Liao64221492012-08-30 00:45:32 +0000748 /* Make the order of the override list deterministic. */
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000749 qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp);
750 for (I = 0; I != num_overridden; ++I) {
Douglas Gregor9f592342010-10-01 20:25:15 +0000751 if (I)
752 printf(", ");
Argyrios Kyrtzidisb3dd9882012-08-22 23:15:52 +0000753 printf("@%d:%d", lineCols[I].line, lineCols[I].col);
Douglas Gregor9f592342010-10-01 20:25:15 +0000754 }
755 printf("]");
756 clang_disposeOverriddenCursors(overridden);
757 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000758
759 if (Cursor.kind == CXCursor_InclusionDirective) {
760 CXFile File = clang_getIncludedFile(Cursor);
761 CXString Included = clang_getFileName(File);
762 printf(" (%s)", clang_getCString(Included));
763 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000764
765 if (clang_isFileMultipleIncludeGuarded(TU, File))
766 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000767 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000768
769 CursorExtent = clang_getCursorExtent(Cursor);
770 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
771 CXNameRange_WantQualifier
772 | CXNameRange_WantSinglePiece
773 | CXNameRange_WantTemplateArgs,
774 0);
775 if (!clang_equalRanges(CursorExtent, RefNameRange))
776 PrintRange(RefNameRange, "SingleRefName");
777
778 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
779 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
780 CXNameRange_WantQualifier
781 | CXNameRange_WantTemplateArgs,
782 RefNameRangeNr);
783 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
784 break;
785 if (!clang_equalRanges(CursorExtent, RefNameRange))
786 PrintRange(RefNameRange, "RefName");
787 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000788
Dmitri Gribenkoe4330a32012-09-10 20:32:42 +0000789 PrintCursorComments(Cursor, ValidationData);
Argyrios Kyrtzidis9ee6a662013-04-18 22:15:49 +0000790
791 {
792 unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0);
793 if (PropAttrs != CXObjCPropertyAttr_noattr) {
794 printf(" [");
795 #define PRINT_PROP_ATTR(A) \
796 if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",")
797 PRINT_PROP_ATTR(readonly);
798 PRINT_PROP_ATTR(getter);
799 PRINT_PROP_ATTR(assign);
800 PRINT_PROP_ATTR(readwrite);
801 PRINT_PROP_ATTR(retain);
802 PRINT_PROP_ATTR(copy);
803 PRINT_PROP_ATTR(nonatomic);
804 PRINT_PROP_ATTR(setter);
805 PRINT_PROP_ATTR(atomic);
806 PRINT_PROP_ATTR(weak);
807 PRINT_PROP_ATTR(strong);
808 PRINT_PROP_ATTR(unsafe_unretained);
809 printf("]");
810 }
811 }
Argyrios Kyrtzidis38dbad22013-04-18 23:29:12 +0000812
813 {
814 unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor);
815 if (QT != CXObjCDeclQualifier_None) {
816 printf(" [");
817 #define PRINT_OBJC_QUAL(A) \
818 if (QT & CXObjCDeclQualifier_##A) printf(#A ",")
819 PRINT_OBJC_QUAL(In);
820 PRINT_OBJC_QUAL(Inout);
821 PRINT_OBJC_QUAL(Out);
822 PRINT_OBJC_QUAL(Bycopy);
823 PRINT_OBJC_QUAL(Byref);
824 PRINT_OBJC_QUAL(Oneway);
825 printf("]");
826 }
827 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000828 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000829}
Steve Naroff89922f82009-08-31 00:59:03 +0000830
Ted Kremeneke68fff62010-02-17 00:41:32 +0000831static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000832 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000833 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000834 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000835 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000836 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000837 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000838 clang_disposeString(source);
839 return "<invalid loc>";
840 }
841 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000842 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000843 clang_disposeString(source);
844 return b;
845 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000846}
847
Ted Kremenek0d435192009-11-17 18:13:31 +0000848/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000849/* Callbacks. */
850/******************************************************************************/
851
852typedef void (*PostVisitTU)(CXTranslationUnit);
853
Douglas Gregora88084b2010-02-18 18:08:43 +0000854void PrintDiagnostic(CXDiagnostic Diagnostic) {
855 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000856 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000857 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000858 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000859 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
860 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000861 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000862
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000863 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000864 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000865
Douglas Gregor274f1902010-02-22 23:17:23 +0000866 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
867 fprintf(stderr, "%s\n", clang_getCString(Msg));
868 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000869
Douglas Gregora9b06d42010-11-09 06:24:54 +0000870 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
871 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000872 if (!file)
873 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000874
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000875 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000876 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000877 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000878 CXSourceRange range;
879 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
880 CXSourceLocation start = clang_getRangeStart(range);
881 CXSourceLocation end = clang_getRangeEnd(range);
882 unsigned start_line, start_column, end_line, end_column;
883 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000884 clang_getSpellingLocation(start, &start_file, &start_line,
885 &start_column, 0);
886 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000887 if (clang_equalLocations(start, end)) {
888 /* Insertion. */
889 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000890 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000891 clang_getCString(insertion_text), start_line, start_column);
892 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
893 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000894 if (start_file == file && end_file == file) {
895 fprintf(out, "FIX-IT: Remove ");
896 PrintExtent(out, start_line, start_column, end_line, end_column);
897 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000898 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000899 } else {
900 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000901 if (start_file == end_file) {
902 fprintf(out, "FIX-IT: Replace ");
903 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000904 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000905 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000906 break;
907 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000908 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000909 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000910}
911
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000912void PrintDiagnosticSet(CXDiagnosticSet Set) {
913 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
914 for ( ; i != n ; ++i) {
915 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
916 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000917 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000918 if (ChildDiags)
919 PrintDiagnosticSet(ChildDiags);
920 }
921}
922
923void PrintDiagnostics(CXTranslationUnit TU) {
924 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
925 PrintDiagnosticSet(TUSet);
926 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000927}
928
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000929void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000930 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000931 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000932 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000933 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000934 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000935 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000936 unsigned long amount = usage.entries[i].amount;
937 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000938 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000939 ((double) amount)/(1024*1024));
940 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000941 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000942 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000943 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000944}
945
Ted Kremenekce2ae882010-01-26 17:59:48 +0000946/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000947/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000948/******************************************************************************/
949
Douglas Gregora7bde202010-01-19 00:34:46 +0000950static void PrintCursorExtent(CXCursor C) {
951 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000952 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000953}
954
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000955/* Data used by the visitors. */
956typedef struct {
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000957 CXTranslationUnit TU;
958 enum CXCursorKind *Filter;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000959 CommentXMLValidationData ValidationData;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000960} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000961
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000962
Ted Kremeneke68fff62010-02-17 00:41:32 +0000963enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000964 CXCursor Parent,
965 CXClientData ClientData) {
966 VisitorData *Data = (VisitorData *)ClientData;
967 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000968 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000969 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000970 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000971 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000972 GetCursorSource(Cursor), line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +0000973 PrintCursor(Cursor, &Data->ValidationData);
Douglas Gregora7bde202010-01-19 00:34:46 +0000974 PrintCursorExtent(Cursor);
Argyrios Kyrtzidis04b67482013-04-11 17:02:10 +0000975 if (clang_isDeclaration(Cursor.kind)) {
976 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
977 const char *accessStr = 0;
978
979 switch (access) {
980 case CX_CXXInvalidAccessSpecifier: break;
981 case CX_CXXPublic:
982 accessStr = "public"; break;
983 case CX_CXXProtected:
984 accessStr = "protected"; break;
985 case CX_CXXPrivate:
986 accessStr = "private"; break;
987 }
988
989 if (accessStr)
990 printf(" [access=%s]", accessStr);
991 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000992 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000993 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000994 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000995
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000996 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000997}
Steve Naroff50398192009-08-28 15:28:48 +0000998
Ted Kremeneke68fff62010-02-17 00:41:32 +0000999static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001000 CXCursor Parent,
1001 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001002 const char *startBuf, *endBuf;
1003 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
1004 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001005 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001006
Douglas Gregorb6998662010-01-19 19:34:47 +00001007 if (Cursor.kind != CXCursor_FunctionDecl ||
1008 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001009 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001010
1011 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
1012 &startLine, &startColumn,
1013 &endLine, &endColumn);
1014 /* Probe the entire body, looking for both decls and refs. */
1015 curLine = startLine;
1016 curColumn = startColumn;
1017
1018 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +00001019 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +00001020 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +00001021 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001022
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001023 if (*startBuf == '\n') {
1024 startBuf++;
1025 curLine++;
1026 curColumn = 1;
1027 } else if (*startBuf != '\t')
1028 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001029
Douglas Gregor98258af2010-01-18 22:46:11 +00001030 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +00001031 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001032
Douglas Gregor1db19de2010-01-19 21:36:55 +00001033 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001034 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +00001035 CXSourceLocation RefLoc
1036 = clang_getLocation(Data->TU, file, curLine, curColumn);
1037 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +00001038 if (Ref.kind == CXCursor_NoDeclFound) {
1039 /* Nothing found here; that's fine. */
1040 } else if (Ref.kind != CXCursor_FunctionDecl) {
1041 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
1042 curLine, curColumn);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001043 PrintCursor(Ref, &Data->ValidationData);
Douglas Gregor98258af2010-01-18 22:46:11 +00001044 printf("\n");
1045 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001046 }
Ted Kremenek74844072010-02-17 00:41:20 +00001047 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001048 startBuf++;
1049 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001050
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001051 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001052}
1053
Ted Kremenek7d405622010-01-12 23:34:26 +00001054/******************************************************************************/
1055/* USR testing. */
1056/******************************************************************************/
1057
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001058enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
1059 CXClientData ClientData) {
1060 VisitorData *Data = (VisitorData *)ClientData;
1061 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +00001062 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +00001063 const char *cstr = clang_getCString(USR);
1064 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +00001065 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +00001066 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +00001067 }
Ted Kremeneke542f772010-04-20 23:15:40 +00001068 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
1069
Douglas Gregora7bde202010-01-19 00:34:46 +00001070 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +00001071 printf("\n");
1072 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001073
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001074 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001075 }
1076
Douglas Gregore5b72ba2010-01-20 21:32:04 +00001077 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +00001078}
1079
1080/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +00001081/* Inclusion stack testing. */
1082/******************************************************************************/
1083
1084void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
1085 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001086
Ted Kremenek16b55a72010-01-26 19:31:51 +00001087 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +00001088 CXString fname;
1089
1090 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001091 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +00001092 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001093
Ted Kremenek16b55a72010-01-26 19:31:51 +00001094 for (i = 0; i < includeStackLen; ++i) {
1095 CXFile includingFile;
1096 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +00001097 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
1098 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +00001099 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001100 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +00001101 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001102 }
1103 printf("\n");
1104}
1105
1106void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001107 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +00001108}
1109
1110/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +00001111/* Linkage testing. */
1112/******************************************************************************/
1113
1114static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
1115 CXClientData d) {
1116 const char *linkage = 0;
1117
1118 if (clang_isInvalid(clang_getCursorKind(cursor)))
1119 return CXChildVisit_Recurse;
1120
1121 switch (clang_getCursorLinkage(cursor)) {
1122 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +00001123 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
1124 case CXLinkage_Internal: linkage = "Internal"; break;
1125 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
1126 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +00001127 }
1128
1129 if (linkage) {
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001130 PrintCursor(cursor, NULL);
Ted Kremenek3bed5272010-03-03 06:37:58 +00001131 printf("linkage=%s\n", linkage);
1132 }
1133
1134 return CXChildVisit_Recurse;
1135}
1136
1137/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001138/* Typekind testing. */
1139/******************************************************************************/
1140
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001141static void PrintTypeAndTypeKind(CXType T, const char *Format) {
1142 CXString TypeSpelling, TypeKindSpelling;
1143
1144 TypeSpelling = clang_getTypeSpelling(T);
1145 TypeKindSpelling = clang_getTypeKindSpelling(T.kind);
1146 printf(Format,
1147 clang_getCString(TypeSpelling),
1148 clang_getCString(TypeKindSpelling));
1149 clang_disposeString(TypeSpelling);
1150 clang_disposeString(TypeKindSpelling);
1151}
1152
1153static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p,
1154 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001155 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
1156 CXType T = clang_getCursorType(cursor);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001157 PrintCursor(cursor, NULL);
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001158 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
Douglas Gregore72fb6f2011-01-27 16:27:11 +00001159 if (clang_isConstQualifiedType(T))
1160 printf(" const");
1161 if (clang_isVolatileQualifiedType(T))
1162 printf(" volatile");
1163 if (clang_isRestrictQualifiedType(T))
1164 printf(" restrict");
Benjamin Kramere1403d22010-06-22 09:29:44 +00001165 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001166 {
1167 CXType CT = clang_getCanonicalType(T);
1168 if (!clang_equalTypes(T, CT)) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001169 PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]");
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001170 }
1171 }
Benjamin Kramere1403d22010-06-22 09:29:44 +00001172 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001173 {
Ted Kremenek9a140842010-06-21 20:48:56 +00001174 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001175 if (RT.kind != CXType_Invalid) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001176 PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]");
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001177 }
1178 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001179 /* Print the argument types if they exist. */
1180 {
1181 int numArgs = clang_Cursor_getNumArguments(cursor);
1182 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001183 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001184 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +00001185 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001186 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
1187 if (T.kind != CXType_Invalid) {
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00001188 PrintTypeAndTypeKind(T, " [%s] [%s]");
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +00001189 }
1190 }
1191 printf("]");
1192 }
1193 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +00001194 /* Print if this is a non-POD type. */
1195 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001196
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001197 printf("\n");
1198 }
1199 return CXChildVisit_Recurse;
1200}
1201
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00001202static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p,
1203 CXClientData d) {
1204 CXType T;
1205 enum CXCursorKind K = clang_getCursorKind(cursor);
1206 if (clang_isInvalid(K))
1207 return CXChildVisit_Recurse;
1208 T = clang_getCursorType(cursor);
1209 PrintCursor(cursor, NULL);
1210 PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]");
1211 /* Print the type sizeof if applicable. */
1212 {
1213 long long Size = clang_Type_getSizeOf(T);
1214 if (Size >= 0 || Size < -1 ) {
1215 printf(" [sizeof=%lld]", Size);
1216 }
1217 }
1218 /* Print the type alignof if applicable. */
1219 {
1220 long long Align = clang_Type_getAlignOf(T);
1221 if (Align >= 0 || Align < -1) {
1222 printf(" [alignof=%lld]", Align);
1223 }
1224 }
1225 /* Print the record field offset if applicable. */
1226 {
1227 const char *FieldName = clang_getCString(clang_getCursorSpelling(cursor));
1228 /* recurse to get the root anonymous record parent */
1229 CXCursor Parent, Root;
1230 if (clang_getCursorKind(cursor) == CXCursor_FieldDecl ) {
1231 const char *RootParentName;
1232 Root = Parent = p;
1233 do {
1234 Root = Parent;
1235 RootParentName = clang_getCString(clang_getCursorSpelling(Root));
1236 Parent = clang_getCursorSemanticParent(Root);
1237 } while ( clang_getCursorType(Parent).kind == CXType_Record &&
1238 !strcmp(RootParentName, "") );
1239 /* if RootParentName is "", record is anonymous. */
1240 {
1241 long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Root),
1242 FieldName);
1243 printf(" [offsetof=%lld]", Offset);
1244 }
1245 }
1246 }
1247 /* Print if its a bitfield */
1248 {
1249 int IsBitfield = clang_Cursor_isBitField(cursor);
1250 if (IsBitfield)
1251 printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor));
1252 }
1253 printf("\n");
1254 return CXChildVisit_Recurse;
1255}
1256
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001257/******************************************************************************/
1258/* Bitwidth testing. */
1259/******************************************************************************/
1260
1261static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p,
1262 CXClientData d) {
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001263 int Bitwidth;
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001264 if (clang_getCursorKind(cursor) != CXCursor_FieldDecl)
1265 return CXChildVisit_Recurse;
1266
NAKAMURA Takumi02c1b862012-12-04 15:32:03 +00001267 Bitwidth = clang_getFieldDeclBitWidth(cursor);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00001268 if (Bitwidth >= 0) {
1269 PrintCursor(cursor, NULL);
1270 printf(" bitwidth=%d\n", Bitwidth);
1271 }
1272
1273 return CXChildVisit_Recurse;
1274}
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001275
1276/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +00001277/* Loading ASTs/source. */
1278/******************************************************************************/
1279
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001280static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +00001281 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001282 CXCursorVisitor Visitor,
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001283 PostVisitTU PV,
1284 const char *CommentSchemaFile) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001285
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001286 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001287 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001288
1289 if (Visitor) {
1290 enum CXCursorKind K = CXCursor_NotImplemented;
1291 enum CXCursorKind *ck = &K;
1292 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001293
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001294 /* Perform some simple filtering. */
1295 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001296 else if (!strcmp(filter, "all-display") ||
1297 !strcmp(filter, "local-display")) {
1298 ck = NULL;
1299 want_display_name = 1;
1300 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001301 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001302 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1303 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1304 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1305 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1306 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1307 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1308 else {
1309 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1310 return 1;
1311 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001312
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001313 Data.TU = TU;
1314 Data.Filter = ck;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001315 Data.ValidationData.CommentSchemaFile = CommentSchemaFile;
1316#ifdef CLANG_HAVE_LIBXML
1317 Data.ValidationData.RNGParser = NULL;
1318 Data.ValidationData.Schema = NULL;
1319#endif
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001320 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001321 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001322
Ted Kremenekce2ae882010-01-26 17:59:48 +00001323 if (PV)
1324 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001325
Douglas Gregora88084b2010-02-18 18:08:43 +00001326 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001327 if (checkForErrors(TU) != 0) {
1328 clang_disposeTranslationUnit(TU);
1329 return -1;
1330 }
1331
Ted Kremenek0d435192009-11-17 18:13:31 +00001332 clang_disposeTranslationUnit(TU);
1333 return 0;
1334}
1335
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001336int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001337 const char *prefix, CXCursorVisitor Visitor,
1338 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001339 CXIndex Idx;
1340 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001341 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001342 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001343 !strcmp(filter, "local") ? 1 : 0,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00001344 /* displayDiagnostics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001345
Ted Kremenek020a0952010-02-11 07:41:25 +00001346 if (!CreateTranslationUnit(Idx, file, &TU)) {
1347 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001348 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001349 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001350
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001351 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL);
Ted Kremenek020a0952010-02-11 07:41:25 +00001352 clang_disposeIndex(Idx);
1353 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001354}
1355
Ted Kremenekce2ae882010-01-26 17:59:48 +00001356int perform_test_load_source(int argc, const char **argv,
1357 const char *filter, CXCursorVisitor Visitor,
1358 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001359 CXIndex Idx;
1360 CXTranslationUnit TU;
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001361 const char *CommentSchemaFile;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001362 struct CXUnsavedFile *unsaved_files = 0;
1363 int num_unsaved_files = 0;
1364 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001365
Daniel Dunbarada487d2009-12-01 02:03:10 +00001366 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001367 (!strcmp(filter, "local") ||
1368 !strcmp(filter, "local-display"))? 1 : 0,
Argyrios Kyrtzidiscd6dcb32013-04-09 20:29:24 +00001369 /* displayDiagnostics=*/1);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001370
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001371 if ((CommentSchemaFile = parse_comments_schema(argc, argv))) {
1372 argc--;
1373 argv++;
1374 }
1375
Ted Kremenek020a0952010-02-11 07:41:25 +00001376 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1377 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001378 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001379 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001380
Douglas Gregordca8ee82011-05-06 16:33:08 +00001381 TU = clang_parseTranslationUnit(Idx, 0,
1382 argv + num_unsaved_files,
1383 argc - num_unsaved_files,
1384 unsaved_files, num_unsaved_files,
1385 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001386 if (!TU) {
1387 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001388 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001389 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001390 return 1;
1391 }
1392
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001393 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV,
1394 CommentSchemaFile);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001395 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001396 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001397 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001398}
1399
Douglas Gregorabc563f2010-07-19 21:46:24 +00001400int perform_test_reparse_source(int argc, const char **argv, int trials,
1401 const char *filter, CXCursorVisitor Visitor,
1402 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001403 CXIndex Idx;
1404 CXTranslationUnit TU;
1405 struct CXUnsavedFile *unsaved_files = 0;
1406 int num_unsaved_files = 0;
1407 int result;
1408 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001409 int remap_after_trial = 0;
1410 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001411
1412 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1413 !strcmp(filter, "local") ? 1 : 0,
Argyrios Kyrtzidiscd6dcb32013-04-09 20:29:24 +00001414 /* displayDiagnostics=*/1);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001415
Douglas Gregorabc563f2010-07-19 21:46:24 +00001416 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1417 clang_disposeIndex(Idx);
1418 return -1;
1419 }
1420
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001421 /* Load the initial translation unit -- we do this without honoring remapped
1422 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001423 TU = clang_parseTranslationUnit(Idx, 0,
1424 argv + num_unsaved_files,
1425 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001426 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001427 if (!TU) {
1428 fprintf(stderr, "Unable to load translation unit!\n");
1429 free_remapped_files(unsaved_files, num_unsaved_files);
1430 clang_disposeIndex(Idx);
1431 return 1;
1432 }
1433
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001434 if (checkForErrors(TU) != 0)
1435 return -1;
1436
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001437 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1438 remap_after_trial =
1439 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1440 }
1441
Douglas Gregorabc563f2010-07-19 21:46:24 +00001442 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001443 if (clang_reparseTranslationUnit(TU,
1444 trial >= remap_after_trial ? num_unsaved_files : 0,
1445 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001446 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001447 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001448 clang_disposeTranslationUnit(TU);
1449 free_remapped_files(unsaved_files, num_unsaved_files);
1450 clang_disposeIndex(Idx);
1451 return -1;
1452 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001453
1454 if (checkForErrors(TU) != 0)
1455 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001456 }
1457
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001458 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001459
Douglas Gregorabc563f2010-07-19 21:46:24 +00001460 free_remapped_files(unsaved_files, num_unsaved_files);
1461 clang_disposeIndex(Idx);
1462 return result;
1463}
1464
Ted Kremenek0d435192009-11-17 18:13:31 +00001465/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001466/* Logic for testing clang_getCursor(). */
1467/******************************************************************************/
1468
Douglas Gregordd3e5542011-05-04 00:14:37 +00001469static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001470 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001471 unsigned end_line, unsigned end_col,
1472 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001473 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001474 if (prefix)
1475 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001476 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1477 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00001478 PrintCursor(cursor, NULL);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001479 printf("\n");
1480}
1481
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001482static int perform_file_scan(const char *ast_file, const char *source_file,
1483 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001484 CXIndex Idx;
1485 CXTranslationUnit TU;
1486 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001487 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001488 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001489 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001490 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001491
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001492 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00001493 /* displayDiagnostics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001494 fprintf(stderr, "Could not create Index\n");
1495 return 1;
1496 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001497
Ted Kremenek1c6da172009-11-17 19:37:36 +00001498 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1499 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001500
Ted Kremenek1c6da172009-11-17 19:37:36 +00001501 if ((fp = fopen(source_file, "r")) == NULL) {
1502 fprintf(stderr, "Could not open '%s'\n", source_file);
1503 return 1;
1504 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001505
Douglas Gregorb9790342010-01-22 21:44:22 +00001506 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001507 for (;;) {
1508 CXCursor cursor;
1509 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001510
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001511 if (c == '\n') {
1512 ++line;
1513 col = 1;
1514 } else
1515 ++col;
1516
1517 /* Check the cursor at this position, and dump the previous one if we have
1518 * found something new.
1519 */
1520 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1521 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1522 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001523 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001524 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001525 start_line = line;
1526 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001527 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001528 if (c == EOF)
1529 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001530
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001531 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001532 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001533
Ted Kremenek1c6da172009-11-17 19:37:36 +00001534 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001535 clang_disposeTranslationUnit(TU);
1536 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001537 return 0;
1538}
1539
1540/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001541/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001542/******************************************************************************/
1543
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001544/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1545 on failure. If successful, the pointer *filename will contain newly-allocated
1546 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001547int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001548 unsigned *column, unsigned *second_line,
1549 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001550 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001551 const char *last_colon = strrchr(input, ':');
1552 unsigned values[4], i;
1553 unsigned num_values = (second_line && second_column)? 4 : 2;
1554
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001555 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001556 if (!last_colon || last_colon == input) {
1557 if (num_values == 4)
1558 fprintf(stderr, "could not parse filename:line:column:line:column in "
1559 "'%s'\n", input);
1560 else
1561 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001562 return 1;
1563 }
1564
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001565 for (i = 0; i != num_values; ++i) {
1566 const char *prev_colon;
1567
1568 /* Parse the next line or column. */
1569 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1570 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001571 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001572 (i % 2 ? "column" : "line"), input);
1573 return 1;
1574 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001575
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001576 if (i + 1 == num_values)
1577 break;
1578
1579 /* Find the previous colon. */
1580 prev_colon = last_colon - 1;
1581 while (prev_colon != input && *prev_colon != ':')
1582 --prev_colon;
1583 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001584 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001585 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001586 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001587 }
1588
1589 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001590 }
1591
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001592 *line = values[0];
1593 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001594
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001595 if (second_line && second_column) {
1596 *second_line = values[2];
1597 *second_column = values[3];
1598 }
1599
Douglas Gregor88d23952009-11-09 18:19:57 +00001600 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001601 *filename = (char*)malloc(last_colon - input + 1);
1602 memcpy(*filename, input, last_colon - input);
1603 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001604 return 0;
1605}
1606
1607const char *
1608clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1609 switch (Kind) {
1610 case CXCompletionChunk_Optional: return "Optional";
1611 case CXCompletionChunk_TypedText: return "TypedText";
1612 case CXCompletionChunk_Text: return "Text";
1613 case CXCompletionChunk_Placeholder: return "Placeholder";
1614 case CXCompletionChunk_Informative: return "Informative";
1615 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1616 case CXCompletionChunk_LeftParen: return "LeftParen";
1617 case CXCompletionChunk_RightParen: return "RightParen";
1618 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1619 case CXCompletionChunk_RightBracket: return "RightBracket";
1620 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1621 case CXCompletionChunk_RightBrace: return "RightBrace";
1622 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1623 case CXCompletionChunk_RightAngle: return "RightAngle";
1624 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001625 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001626 case CXCompletionChunk_Colon: return "Colon";
1627 case CXCompletionChunk_SemiColon: return "SemiColon";
1628 case CXCompletionChunk_Equal: return "Equal";
1629 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1630 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001631 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001632
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001633 return "Unknown";
1634}
1635
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001636static int checkForErrors(CXTranslationUnit TU) {
1637 unsigned Num, i;
1638 CXDiagnostic Diag;
1639 CXString DiagStr;
1640
1641 if (!getenv("CINDEXTEST_FAILONERROR"))
1642 return 0;
1643
1644 Num = clang_getNumDiagnostics(TU);
1645 for (i = 0; i != Num; ++i) {
1646 Diag = clang_getDiagnostic(TU, i);
1647 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1648 DiagStr = clang_formatDiagnostic(Diag,
1649 clang_defaultDiagnosticDisplayOptions());
1650 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1651 clang_disposeString(DiagStr);
1652 clang_disposeDiagnostic(Diag);
1653 return -1;
1654 }
1655 clang_disposeDiagnostic(Diag);
1656 }
1657
1658 return 0;
1659}
1660
Douglas Gregor3ac73852009-11-09 16:04:45 +00001661void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001662 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001663
Douglas Gregor3ac73852009-11-09 16:04:45 +00001664 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001665 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001666 CXString text;
1667 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001668 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001669 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001670
Douglas Gregor3ac73852009-11-09 16:04:45 +00001671 if (Kind == CXCompletionChunk_Optional) {
1672 fprintf(file, "{Optional ");
1673 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001674 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001675 file);
1676 fprintf(file, "}");
1677 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001678 }
1679
1680 if (Kind == CXCompletionChunk_VerticalSpace) {
1681 fprintf(file, "{VerticalSpace }");
1682 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001683 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001684
Douglas Gregord5a20892009-11-09 17:05:28 +00001685 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001686 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001687 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001688 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001689 cstr ? cstr : "");
1690 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001691 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001692
Douglas Gregor3ac73852009-11-09 16:04:45 +00001693}
1694
1695void print_completion_result(CXCompletionResult *completion_result,
1696 CXClientData client_data) {
1697 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001698 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001699 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001700 enum CXCursorKind ParentKind;
1701 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001702 CXString BriefComment;
1703 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001704
Ted Kremeneke68fff62010-02-17 00:41:32 +00001705 fprintf(file, "%s:", clang_getCString(ks));
1706 clang_disposeString(ks);
1707
Douglas Gregor3ac73852009-11-09 16:04:45 +00001708 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001709 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001710 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001711 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1712 case CXAvailability_Available:
1713 break;
1714
1715 case CXAvailability_Deprecated:
1716 fprintf(file, " (deprecated)");
1717 break;
1718
1719 case CXAvailability_NotAvailable:
1720 fprintf(file, " (unavailable)");
1721 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001722
1723 case CXAvailability_NotAccessible:
1724 fprintf(file, " (inaccessible)");
1725 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001726 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001727
1728 annotationCount = clang_getCompletionNumAnnotations(
1729 completion_result->CompletionString);
1730 if (annotationCount) {
1731 unsigned i;
1732 fprintf(file, " (");
1733 for (i = 0; i < annotationCount; ++i) {
1734 if (i != 0)
1735 fprintf(file, ", ");
1736 fprintf(file, "\"%s\"",
1737 clang_getCString(clang_getCompletionAnnotation(
1738 completion_result->CompletionString, i)));
1739 }
1740 fprintf(file, ")");
1741 }
1742
Douglas Gregorba103062012-03-27 23:34:16 +00001743 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1744 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1745 &ParentKind);
1746 if (ParentKind != CXCursor_NotImplemented) {
1747 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1748 fprintf(file, " (parent: %s '%s')",
1749 clang_getCString(KindSpelling),
1750 clang_getCString(ParentName));
1751 clang_disposeString(KindSpelling);
1752 }
1753 clang_disposeString(ParentName);
1754 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001755
1756 BriefComment = clang_getCompletionBriefComment(
1757 completion_result->CompletionString);
1758 BriefCommentCString = clang_getCString(BriefComment);
1759 if (BriefCommentCString && *BriefCommentCString != '\0') {
1760 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1761 }
1762 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001763
Douglas Gregor58ddb602010-08-23 23:00:57 +00001764 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001765}
1766
Douglas Gregor3da626b2011-07-07 16:03:39 +00001767void print_completion_contexts(unsigned long long contexts, FILE *file) {
1768 fprintf(file, "Completion contexts:\n");
1769 if (contexts == CXCompletionContext_Unknown) {
1770 fprintf(file, "Unknown\n");
1771 }
1772 if (contexts & CXCompletionContext_AnyType) {
1773 fprintf(file, "Any type\n");
1774 }
1775 if (contexts & CXCompletionContext_AnyValue) {
1776 fprintf(file, "Any value\n");
1777 }
1778 if (contexts & CXCompletionContext_ObjCObjectValue) {
1779 fprintf(file, "Objective-C object value\n");
1780 }
1781 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1782 fprintf(file, "Objective-C selector value\n");
1783 }
1784 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1785 fprintf(file, "C++ class type value\n");
1786 }
1787 if (contexts & CXCompletionContext_DotMemberAccess) {
1788 fprintf(file, "Dot member access\n");
1789 }
1790 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1791 fprintf(file, "Arrow member access\n");
1792 }
1793 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1794 fprintf(file, "Objective-C property access\n");
1795 }
1796 if (contexts & CXCompletionContext_EnumTag) {
1797 fprintf(file, "Enum tag\n");
1798 }
1799 if (contexts & CXCompletionContext_UnionTag) {
1800 fprintf(file, "Union tag\n");
1801 }
1802 if (contexts & CXCompletionContext_StructTag) {
1803 fprintf(file, "Struct tag\n");
1804 }
1805 if (contexts & CXCompletionContext_ClassTag) {
1806 fprintf(file, "Class name\n");
1807 }
1808 if (contexts & CXCompletionContext_Namespace) {
1809 fprintf(file, "Namespace or namespace alias\n");
1810 }
1811 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1812 fprintf(file, "Nested name specifier\n");
1813 }
1814 if (contexts & CXCompletionContext_ObjCInterface) {
1815 fprintf(file, "Objective-C interface\n");
1816 }
1817 if (contexts & CXCompletionContext_ObjCProtocol) {
1818 fprintf(file, "Objective-C protocol\n");
1819 }
1820 if (contexts & CXCompletionContext_ObjCCategory) {
1821 fprintf(file, "Objective-C category\n");
1822 }
1823 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1824 fprintf(file, "Objective-C instance method\n");
1825 }
1826 if (contexts & CXCompletionContext_ObjCClassMessage) {
1827 fprintf(file, "Objective-C class method\n");
1828 }
1829 if (contexts & CXCompletionContext_ObjCSelectorName) {
1830 fprintf(file, "Objective-C selector name\n");
1831 }
1832 if (contexts & CXCompletionContext_MacroName) {
1833 fprintf(file, "Macro name\n");
1834 }
1835 if (contexts & CXCompletionContext_NaturalLanguage) {
1836 fprintf(file, "Natural language\n");
1837 }
1838}
1839
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001840int my_stricmp(const char *s1, const char *s2) {
1841 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001842 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001843 if (c1 < c2)
1844 return -1;
1845 else if (c1 > c2)
1846 return 1;
1847
1848 ++s1;
1849 ++s2;
1850 }
1851
1852 if (*s1)
1853 return 1;
1854 else if (*s2)
1855 return -1;
1856 return 0;
1857}
1858
Douglas Gregor1982c182010-07-12 18:38:41 +00001859int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001860 const char *input = argv[1];
1861 char *filename = 0;
1862 unsigned line;
1863 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001864 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001865 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001866 struct CXUnsavedFile *unsaved_files = 0;
1867 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001868 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001869 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001870 unsigned I, Repeats = 1;
1871 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1872
1873 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1874 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001875 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1876 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001877
Douglas Gregor1982c182010-07-12 18:38:41 +00001878 if (timing_only)
1879 input += strlen("-code-completion-timing=");
1880 else
1881 input += strlen("-code-completion-at=");
1882
Ted Kremeneke68fff62010-02-17 00:41:32 +00001883 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001884 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001885 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001886
Douglas Gregor735df882009-12-02 09:21:34 +00001887 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1888 return -1;
1889
Douglas Gregor32be4a52010-10-11 21:37:58 +00001890 CIdx = clang_createIndex(0, 0);
1891
1892 if (getenv("CINDEXTEST_EDITING"))
1893 Repeats = 5;
1894
1895 TU = clang_parseTranslationUnit(CIdx, 0,
1896 argv + num_unsaved_files + 2,
1897 argc - num_unsaved_files - 2,
1898 0, 0, getDefaultParsingOptions());
1899 if (!TU) {
1900 fprintf(stderr, "Unable to load translation unit!\n");
1901 return 1;
1902 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001903
1904 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1905 fprintf(stderr, "Unable to reparse translation init!\n");
1906 return 1;
1907 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001908
1909 for (I = 0; I != Repeats; ++I) {
1910 results = clang_codeCompleteAt(TU, filename, line, column,
1911 unsaved_files, num_unsaved_files,
1912 completionOptions);
1913 if (!results) {
1914 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001915 return 1;
1916 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001917 if (I != Repeats-1)
1918 clang_disposeCodeCompleteResults(results);
1919 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001920
Douglas Gregorec6762c2009-12-18 16:20:58 +00001921 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001922 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001923 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001924 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001925 CXString objCSelector;
1926 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001927 if (!timing_only) {
1928 /* Sort the code-completion results based on the typed text. */
1929 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1930
Douglas Gregor1982c182010-07-12 18:38:41 +00001931 for (i = 0; i != n; ++i)
1932 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001933 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001934 n = clang_codeCompleteGetNumDiagnostics(results);
1935 for (i = 0; i != n; ++i) {
1936 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1937 PrintDiagnostic(diag);
1938 clang_disposeDiagnostic(diag);
1939 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001940
1941 contexts = clang_codeCompleteGetContexts(results);
1942 print_completion_contexts(contexts, stdout);
1943
Douglas Gregor0a47d692011-07-26 15:24:30 +00001944 containerKind = clang_codeCompleteGetContainerKind(results,
1945 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001946
1947 if (containerKind != CXCursor_InvalidCode) {
1948 /* We have found a container */
1949 CXString containerUSR, containerKindSpelling;
1950 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1951 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1952 clang_disposeString(containerKindSpelling);
1953
1954 if (containerIsIncomplete) {
1955 printf("Container is incomplete\n");
1956 }
1957 else {
1958 printf("Container is complete\n");
1959 }
1960
1961 containerUSR = clang_codeCompleteGetContainerUSR(results);
1962 printf("Container USR: %s\n", clang_getCString(containerUSR));
1963 clang_disposeString(containerUSR);
1964 }
1965
Douglas Gregor0a47d692011-07-26 15:24:30 +00001966 objCSelector = clang_codeCompleteGetObjCSelector(results);
1967 selectorString = clang_getCString(objCSelector);
1968 if (selectorString && strlen(selectorString) > 0) {
1969 printf("Objective-C selector: %s\n", selectorString);
1970 }
1971 clang_disposeString(objCSelector);
1972
Douglas Gregorec6762c2009-12-18 16:20:58 +00001973 clang_disposeCodeCompleteResults(results);
1974 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001975 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001976 clang_disposeIndex(CIdx);
1977 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001978
Douglas Gregor735df882009-12-02 09:21:34 +00001979 free_remapped_files(unsaved_files, num_unsaved_files);
1980
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001981 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001982}
1983
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001984typedef struct {
1985 char *filename;
1986 unsigned line;
1987 unsigned column;
1988} CursorSourceLocation;
1989
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001990static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001991 CXIndex CIdx;
1992 int errorCode;
1993 struct CXUnsavedFile *unsaved_files = 0;
1994 int num_unsaved_files = 0;
1995 CXTranslationUnit TU;
1996 CXCursor Cursor;
1997 CursorSourceLocation *Locations = 0;
1998 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001999 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00002000 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002001
Ted Kremeneke68fff62010-02-17 00:41:32 +00002002 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002003 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
2004 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002005
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002006 /* Parse the locations. */
2007 assert(NumLocations > 0 && "Unable to count locations?");
2008 Locations = (CursorSourceLocation *)malloc(
2009 NumLocations * sizeof(CursorSourceLocation));
2010 for (Loc = 0; Loc < NumLocations; ++Loc) {
2011 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00002012 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2013 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002014 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002015 return errorCode;
2016 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002017
2018 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002019 &num_unsaved_files))
2020 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002021
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002022 if (getenv("CINDEXTEST_EDITING"))
2023 Repeats = 5;
2024
2025 /* Parse the translation unit. When we're testing clang_getCursor() after
2026 reparsing, don't remap unsaved files until the second parse. */
2027 CIdx = clang_createIndex(1, 1);
2028 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2029 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002030 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002031 unsaved_files,
2032 Repeats > 1? 0 : num_unsaved_files,
2033 getDefaultParsingOptions());
2034
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002035 if (!TU) {
2036 fprintf(stderr, "unable to parse input\n");
2037 return -1;
2038 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00002039
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002040 if (checkForErrors(TU) != 0)
2041 return -1;
2042
Douglas Gregorbdc4b362010-11-30 06:04:54 +00002043 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002044 if (Repeats > 1 &&
2045 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2046 clang_defaultReparseOptions(TU))) {
2047 clang_disposeTranslationUnit(TU);
2048 return 1;
2049 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002050
2051 if (checkForErrors(TU) != 0)
2052 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002053
2054 for (Loc = 0; Loc < NumLocations; ++Loc) {
2055 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2056 if (!file)
2057 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002058
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002059 Cursor = clang_getCursor(TU,
2060 clang_getLocation(TU, file, Locations[Loc].line,
2061 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002062
2063 if (checkForErrors(TU) != 0)
2064 return -1;
2065
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002066 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00002067 CXCompletionString completionString = clang_getCursorCompletionString(
2068 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002069 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
2070 CXString Spelling;
2071 const char *cspell;
2072 unsigned line, column;
2073 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
2074 printf("%d:%d ", line, column);
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002075 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002076 PrintCursorExtent(Cursor);
2077 Spelling = clang_getCursorSpelling(Cursor);
2078 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002079 if (cspell && strlen(cspell) != 0) {
2080 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002081 printf(" Spelling=%s (", cspell);
2082 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00002083 CXSourceRange range =
2084 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00002085 if (clang_Range_isNull(range))
2086 break;
2087 PrintRange(range, 0);
2088 }
2089 printf(")");
2090 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00002091 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00002092 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
2093 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00002094 if (clang_Cursor_isDynamicCall(Cursor))
2095 printf(" Dynamic-call");
Argyrios Kyrtzidise4a990f2012-11-01 02:01:34 +00002096 if (Cursor.kind == CXCursor_ObjCMessageExpr) {
2097 CXType T = clang_Cursor_getReceiverType(Cursor);
2098 CXString S = clang_getTypeKindSpelling(T.kind);
2099 printf(" Receiver-type=%s", clang_getCString(S));
2100 clang_disposeString(S);
2101 }
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00002102
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002103 {
2104 CXModule mod = clang_Cursor_getModule(Cursor);
2105 CXString name;
2106 unsigned i, numHeaders;
2107 if (mod) {
2108 name = clang_Module_getFullName(mod);
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002109 numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002110 printf(" ModuleName=%s Headers(%d):",
2111 clang_getCString(name), numHeaders);
2112 clang_disposeString(name);
2113 for (i = 0; i < numHeaders; ++i) {
Argyrios Kyrtzidisc1d22392013-03-13 21:13:43 +00002114 CXFile file = clang_Module_getTopLevelHeader(TU, mod, i);
Argyrios Kyrtzidis5d04b1a2012-10-05 00:22:37 +00002115 CXString filename = clang_getFileName(file);
2116 printf("\n%s", clang_getCString(filename));
2117 clang_disposeString(filename);
2118 }
2119 }
2120 }
2121
Douglas Gregor8fa0a802011-08-04 20:04:59 +00002122 if (completionString != NULL) {
2123 printf("\nCompletion string: ");
2124 print_completion_string(completionString, stdout);
2125 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002126 printf("\n");
2127 free(Locations[Loc].filename);
2128 }
2129 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002130 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00002131
Douglas Gregora88084b2010-02-18 18:08:43 +00002132 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00002133 clang_disposeTranslationUnit(TU);
2134 clang_disposeIndex(CIdx);
2135 free(Locations);
2136 free_remapped_files(unsaved_files, num_unsaved_files);
2137 return 0;
2138}
2139
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002140static enum CXVisitorResult findFileRefsVisit(void *context,
2141 CXCursor cursor, CXSourceRange range) {
2142 if (clang_Range_isNull(range))
2143 return CXVisit_Continue;
2144
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002145 PrintCursor(cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002146 PrintRange(range, "");
2147 printf("\n");
2148 return CXVisit_Continue;
2149}
2150
2151static int find_file_refs_at(int argc, const char **argv) {
2152 CXIndex CIdx;
2153 int errorCode;
2154 struct CXUnsavedFile *unsaved_files = 0;
2155 int num_unsaved_files = 0;
2156 CXTranslationUnit TU;
2157 CXCursor Cursor;
2158 CursorSourceLocation *Locations = 0;
2159 unsigned NumLocations = 0, Loc;
2160 unsigned Repeats = 1;
2161 unsigned I;
2162
2163 /* Count the number of locations. */
2164 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
2165 ++NumLocations;
2166
2167 /* Parse the locations. */
2168 assert(NumLocations > 0 && "Unable to count locations?");
2169 Locations = (CursorSourceLocation *)malloc(
2170 NumLocations * sizeof(CursorSourceLocation));
2171 for (Loc = 0; Loc < NumLocations; ++Loc) {
2172 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
2173 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
2174 &Locations[Loc].line,
2175 &Locations[Loc].column, 0, 0)))
2176 return errorCode;
2177 }
2178
2179 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
2180 &num_unsaved_files))
2181 return -1;
2182
2183 if (getenv("CINDEXTEST_EDITING"))
2184 Repeats = 5;
2185
2186 /* Parse the translation unit. When we're testing clang_getCursor() after
2187 reparsing, don't remap unsaved files until the second parse. */
2188 CIdx = clang_createIndex(1, 1);
2189 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2190 argv + num_unsaved_files + 1 + NumLocations,
2191 argc - num_unsaved_files - 2 - NumLocations,
2192 unsaved_files,
2193 Repeats > 1? 0 : num_unsaved_files,
2194 getDefaultParsingOptions());
2195
2196 if (!TU) {
2197 fprintf(stderr, "unable to parse input\n");
2198 return -1;
2199 }
2200
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002201 if (checkForErrors(TU) != 0)
2202 return -1;
2203
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002204 for (I = 0; I != Repeats; ++I) {
2205 if (Repeats > 1 &&
2206 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2207 clang_defaultReparseOptions(TU))) {
2208 clang_disposeTranslationUnit(TU);
2209 return 1;
2210 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002211
2212 if (checkForErrors(TU) != 0)
2213 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002214
2215 for (Loc = 0; Loc < NumLocations; ++Loc) {
2216 CXFile file = clang_getFile(TU, Locations[Loc].filename);
2217 if (!file)
2218 continue;
2219
2220 Cursor = clang_getCursor(TU,
2221 clang_getLocation(TU, file, Locations[Loc].line,
2222 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002223
2224 if (checkForErrors(TU) != 0)
2225 return -1;
2226
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002227 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00002228 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002229 PrintCursor(Cursor, NULL);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002230 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002231 clang_findReferencesInFile(Cursor, file, visitor);
2232 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002233
2234 if (checkForErrors(TU) != 0)
2235 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002236 }
2237 }
2238 }
2239
2240 PrintDiagnostics(TU);
2241 clang_disposeTranslationUnit(TU);
2242 clang_disposeIndex(CIdx);
2243 free(Locations);
2244 free_remapped_files(unsaved_files, num_unsaved_files);
2245 return 0;
2246}
2247
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00002248static enum CXVisitorResult findFileIncludesVisit(void *context,
2249 CXCursor cursor, CXSourceRange range) {
2250 PrintCursor(cursor, NULL);
2251 PrintRange(range, "");
2252 printf("\n");
2253 return CXVisit_Continue;
2254}
2255
2256static int find_file_includes_in(int argc, const char **argv) {
2257 CXIndex CIdx;
2258 struct CXUnsavedFile *unsaved_files = 0;
2259 int num_unsaved_files = 0;
2260 CXTranslationUnit TU;
2261 const char **Filenames = 0;
2262 unsigned NumFilenames = 0;
2263 unsigned Repeats = 1;
2264 unsigned I, FI;
2265
2266 /* Count the number of locations. */
2267 while (strstr(argv[NumFilenames+1], "-file-includes-in=") == argv[NumFilenames+1])
2268 ++NumFilenames;
2269
2270 /* Parse the locations. */
2271 assert(NumFilenames > 0 && "Unable to count filenames?");
2272 Filenames = (const char **)malloc(NumFilenames * sizeof(const char *));
2273 for (I = 0; I < NumFilenames; ++I) {
2274 const char *input = argv[I + 1] + strlen("-file-includes-in=");
2275 /* Copy the file name. */
2276 Filenames[I] = input;
2277 }
2278
2279 if (parse_remapped_files(argc, argv, NumFilenames + 1, &unsaved_files,
2280 &num_unsaved_files))
2281 return -1;
2282
2283 if (getenv("CINDEXTEST_EDITING"))
2284 Repeats = 2;
2285
2286 /* Parse the translation unit. When we're testing clang_getCursor() after
2287 reparsing, don't remap unsaved files until the second parse. */
2288 CIdx = clang_createIndex(1, 1);
2289 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2290 argv + num_unsaved_files + 1 + NumFilenames,
2291 argc - num_unsaved_files - 2 - NumFilenames,
2292 unsaved_files,
2293 Repeats > 1? 0 : num_unsaved_files,
2294 getDefaultParsingOptions());
2295
2296 if (!TU) {
2297 fprintf(stderr, "unable to parse input\n");
2298 return -1;
2299 }
2300
2301 if (checkForErrors(TU) != 0)
2302 return -1;
2303
2304 for (I = 0; I != Repeats; ++I) {
2305 if (Repeats > 1 &&
2306 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2307 clang_defaultReparseOptions(TU))) {
2308 clang_disposeTranslationUnit(TU);
2309 return 1;
2310 }
2311
2312 if (checkForErrors(TU) != 0)
2313 return -1;
2314
2315 for (FI = 0; FI < NumFilenames; ++FI) {
2316 CXFile file = clang_getFile(TU, Filenames[FI]);
2317 if (!file)
2318 continue;
2319
2320 if (checkForErrors(TU) != 0)
2321 return -1;
2322
2323 if (I + 1 == Repeats) {
2324 CXCursorAndRangeVisitor visitor = { 0, findFileIncludesVisit };
2325 clang_findIncludesInFile(TU, file, visitor);
2326
2327 if (checkForErrors(TU) != 0)
2328 return -1;
2329 }
2330 }
2331 }
2332
2333 PrintDiagnostics(TU);
2334 clang_disposeTranslationUnit(TU);
2335 clang_disposeIndex(CIdx);
Argyrios Kyrtzidis5256c1f2013-03-11 16:03:17 +00002336 free((void *)Filenames);
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00002337 free_remapped_files(unsaved_files, num_unsaved_files);
2338 return 0;
2339}
2340
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002341#define MAX_IMPORTED_ASTFILES 200
2342
2343typedef struct {
2344 char **filenames;
2345 unsigned num_files;
2346} ImportedASTFilesData;
2347
2348static ImportedASTFilesData *importedASTs_create() {
2349 ImportedASTFilesData *p;
2350 p = malloc(sizeof(ImportedASTFilesData));
2351 p->filenames = malloc(MAX_IMPORTED_ASTFILES * sizeof(const char *));
2352 p->num_files = 0;
2353 return p;
2354}
2355
2356static void importedASTs_dispose(ImportedASTFilesData *p) {
2357 unsigned i;
2358 if (!p)
2359 return;
2360
2361 for (i = 0; i < p->num_files; ++i)
2362 free(p->filenames[i]);
2363 free(p->filenames);
2364 free(p);
2365}
2366
2367static void importedASTS_insert(ImportedASTFilesData *p, const char *file) {
2368 unsigned i;
2369 assert(p && file);
2370 for (i = 0; i < p->num_files; ++i)
2371 if (strcmp(file, p->filenames[i]) == 0)
2372 return;
2373 assert(p->num_files + 1 < MAX_IMPORTED_ASTFILES);
2374 p->filenames[p->num_files++] = strdup(file);
2375}
2376
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002377typedef struct {
2378 const char *check_prefix;
2379 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002380 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002381 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002382 const char *main_filename;
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002383 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002384} IndexData;
2385
2386static void printCheck(IndexData *data) {
2387 if (data->check_prefix) {
2388 if (data->first_check_printed) {
2389 printf("// %s-NEXT: ", data->check_prefix);
2390 } else {
2391 printf("// %s : ", data->check_prefix);
2392 data->first_check_printed = 1;
2393 }
2394 }
2395}
2396
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002397static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002398 CXString filename = clang_getFileName((CXFile)file);
2399 printf("%s", clang_getCString(filename));
2400 clang_disposeString(filename);
2401}
2402
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002403static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
2404 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002405 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002406 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002407 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002408 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002409 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002410
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002411 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002412 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
2413 if (line == 0) {
Argyrios Kyrtzidis8003fd62012-10-11 19:00:44 +00002414 printf("<invalid>");
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002415 return;
2416 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002417 if (!file) {
2418 printf("<no idxfile>");
2419 return;
2420 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002421 filename = clang_getFileName((CXFile)file);
2422 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002423 if (strcmp(cname, index_data->main_filename) == 0)
2424 isMainFile = 1;
2425 else
2426 isMainFile = 0;
2427 clang_disposeString(filename);
2428
2429 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002430 printCXIndexFile(file);
2431 printf(":");
2432 }
2433 printf("%d:%d", line, column);
2434}
2435
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002436static unsigned digitCount(unsigned val) {
2437 unsigned c = 1;
2438 while (1) {
2439 if (val < 10)
2440 return c;
2441 ++c;
2442 val /= 10;
2443 }
2444}
2445
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002446static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2447 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002448 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002449 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002450 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002451 unsigned line, column;
2452
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002453 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002454 if (!name)
2455 name = "<anon-tag>";
2456
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002457 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002458 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002459 newStr = (char *)malloc(strlen(name) +
2460 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002461 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002462 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002463}
2464
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002465static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2466 CXIdxClientContainer container;
2467 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002468 if (!container)
2469 printf("[<<NULL>>]");
2470 else
2471 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002472}
2473
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002474static const char *getEntityKindString(CXIdxEntityKind kind) {
2475 switch (kind) {
2476 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2477 case CXIdxEntity_Typedef: return "typedef";
2478 case CXIdxEntity_Function: return "function";
2479 case CXIdxEntity_Variable: return "variable";
2480 case CXIdxEntity_Field: return "field";
2481 case CXIdxEntity_EnumConstant: return "enumerator";
2482 case CXIdxEntity_ObjCClass: return "objc-class";
2483 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2484 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002485 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2486 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002487 case CXIdxEntity_ObjCProperty: return "objc-property";
2488 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2489 case CXIdxEntity_Enum: return "enum";
2490 case CXIdxEntity_Struct: return "struct";
2491 case CXIdxEntity_Union: return "union";
2492 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002493 case CXIdxEntity_CXXNamespace: return "namespace";
2494 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2495 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2496 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2497 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2498 case CXIdxEntity_CXXConstructor: return "constructor";
2499 case CXIdxEntity_CXXDestructor: return "destructor";
2500 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2501 case CXIdxEntity_CXXTypeAlias: return "type-alias";
David Blaikie35adca02012-08-31 21:55:26 +00002502 case CXIdxEntity_CXXInterface: return "c++-__interface";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002503 }
2504 assert(0 && "Garbage entity kind");
2505 return 0;
2506}
2507
2508static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2509 switch (kind) {
2510 case CXIdxEntity_NonTemplate: return "";
2511 case CXIdxEntity_Template: return "-template";
2512 case CXIdxEntity_TemplatePartialSpecialization:
2513 return "-template-partial-spec";
2514 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002515 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002516 assert(0 && "Garbage entity kind");
2517 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002518}
2519
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002520static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2521 switch (kind) {
2522 case CXIdxEntityLang_None: return "<none>";
2523 case CXIdxEntityLang_C: return "C";
2524 case CXIdxEntityLang_ObjC: return "ObjC";
2525 case CXIdxEntityLang_CXX: return "C++";
2526 }
2527 assert(0 && "Garbage language kind");
2528 return 0;
2529}
2530
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002531static void printEntityInfo(const char *cb,
2532 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002533 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002534 const char *name;
2535 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002536 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002537 index_data = (IndexData *)client_data;
2538 printCheck(index_data);
2539
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002540 if (!info) {
2541 printf("%s: <<NULL>>", cb);
2542 return;
2543 }
2544
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002545 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002546 if (!name)
2547 name = "<anon-tag>";
2548
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002549 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2550 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002551 printf(" | name: %s", name);
2552 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002553 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002554
2555 for (i = 0; i != info->numAttributes; ++i) {
2556 const CXIdxAttrInfo *Attr = info->attributes[i];
2557 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002558 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002559 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002560}
2561
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002562static void printBaseClassInfo(CXClientData client_data,
2563 const CXIdxBaseClassInfo *info) {
2564 printEntityInfo(" <base>", client_data, info->base);
2565 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002566 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002567 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002568 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002569}
2570
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002571static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2572 CXClientData client_data) {
2573 unsigned i;
2574 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2575 printEntityInfo(" <protocol>", client_data,
2576 ProtoInfo->protocols[i]->protocol);
2577 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002578 PrintCursor(ProtoInfo->protocols[i]->cursor, NULL);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002579 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002580 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002581 printf("\n");
2582 }
2583}
2584
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002585static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002586 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002587 CXString str;
2588 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002589 unsigned numDiags, i;
2590 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002591 IndexData *index_data;
2592 index_data = (IndexData *)client_data;
2593 printCheck(index_data);
2594
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002595 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2596 for (i = 0; i != numDiags; ++i) {
2597 diag = clang_getDiagnosticInSet(diagSet, i);
2598 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2599 cstr = clang_getCString(str);
2600 printf("[diagnostic]: %s\n", cstr);
2601 clang_disposeString(str);
2602
2603 if (getenv("CINDEXTEST_FAILONERROR") &&
2604 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2605 index_data->fail_for_error = 1;
2606 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002607 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002608}
2609
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002610static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2611 CXFile file, void *reserved) {
2612 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002613 CXString filename;
2614
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002615 index_data = (IndexData *)client_data;
2616 printCheck(index_data);
2617
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002618 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002619 index_data->main_filename = clang_getCString(filename);
2620 clang_disposeString(filename);
2621
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002622 printf("[enteredMainFile]: ");
2623 printCXIndexFile((CXIdxClientFile)file);
2624 printf("\n");
2625
2626 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002627}
2628
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002629static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002630 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002631 IndexData *index_data;
2632 index_data = (IndexData *)client_data;
2633 printCheck(index_data);
2634
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002635 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002636 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002637 printf(" | name: \"%s\"", info->filename);
2638 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002639 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis8d7a24e2012-10-18 00:17:05 +00002640 printf(" | isImport: %d | isAngled: %d | isModule: %d\n",
2641 info->isImport, info->isAngled, info->isModuleImport);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002642
2643 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002644}
2645
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002646static CXIdxClientFile index_importedASTFile(CXClientData client_data,
2647 const CXIdxImportedASTFileInfo *info) {
2648 IndexData *index_data;
2649 index_data = (IndexData *)client_data;
2650 printCheck(index_data);
2651
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002652 if (index_data->importedASTs) {
2653 CXString filename = clang_getFileName(info->file);
2654 importedASTS_insert(index_data->importedASTs, clang_getCString(filename));
2655 clang_disposeString(filename);
2656 }
2657
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002658 printf("[importedASTFile]: ");
2659 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002660 if (info->module) {
2661 CXString name = clang_Module_getFullName(info->module);
2662 printf(" | loc: ");
2663 printCXIndexLoc(info->loc, client_data);
2664 printf(" | name: \"%s\"", clang_getCString(name));
2665 printf(" | isImplicit: %d\n", info->isImplicit);
2666 clang_disposeString(name);
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002667 } else {
NAKAMURA Takumi3c5527e2012-10-12 14:25:52 +00002668 /* PCH file, the rest are not relevant. */
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00002669 printf("\n");
Argyrios Kyrtzidis134d1e8a2012-10-05 00:22:40 +00002670 }
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002671
2672 return (CXIdxClientFile)info->file;
2673}
2674
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002675static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002676 void *reserved) {
2677 IndexData *index_data;
2678 index_data = (IndexData *)client_data;
2679 printCheck(index_data);
2680
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002681 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002682 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002683}
2684
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002685static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002686 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002687 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002688 const CXIdxObjCCategoryDeclInfo *CatInfo;
2689 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002690 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002691 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002692 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002693 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002694 index_data = (IndexData *)client_data;
2695
2696 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2697 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002698 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002699 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002700 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002701 printf(" | semantic-container: ");
2702 printCXIndexContainer(info->semanticContainer);
2703 printf(" | lexical-container: ");
2704 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002705 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002706 printf(" | isDef: %d", info->isDefinition);
Argyrios Kyrtzidis838eb7e2012-12-06 19:41:16 +00002707 if (info->flags & CXIdxDeclFlag_Skipped) {
2708 assert(!info->isContainer);
2709 printf(" | isContainer: skipped");
2710 } else {
2711 printf(" | isContainer: %d", info->isContainer);
2712 }
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002713 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002714
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002715 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002716 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002717 printf(" <attribute>: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002718 PrintCursor(Attr->cursor, NULL);
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002719 printf("\n");
2720 }
2721
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002722 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2723 const char *kindName = 0;
2724 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2725 switch (K) {
2726 case CXIdxObjCContainer_ForwardRef:
2727 kindName = "forward-ref"; break;
2728 case CXIdxObjCContainer_Interface:
2729 kindName = "interface"; break;
2730 case CXIdxObjCContainer_Implementation:
2731 kindName = "implementation"; break;
2732 }
2733 printCheck(index_data);
2734 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2735 }
2736
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002737 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002738 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2739 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002740 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002741 PrintCursor(CatInfo->classCursor, NULL);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002742 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002743 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002744 printf("\n");
2745 }
2746
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002747 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2748 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002749 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002750 printf("\n");
2751 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002752 }
2753
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002754 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2755 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002756 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002757
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002758 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2759 if (PropInfo->getter) {
2760 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2761 printf("\n");
2762 }
2763 if (PropInfo->setter) {
2764 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2765 printf("\n");
2766 }
2767 }
2768
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002769 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2770 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2771 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2772 printf("\n");
2773 }
2774 }
2775
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002776 if (info->declAsContainer)
2777 clang_index_setClientContainer(info->declAsContainer,
2778 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002779}
2780
2781static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002782 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002783 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002784 printf(" | cursor: ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00002785 PrintCursor(info->cursor, NULL);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002786 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002787 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002788 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002789 printf(" | container: ");
2790 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002791 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002792 switch (info->kind) {
2793 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002794 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002795 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002796 printf("\n");
2797}
2798
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002799static int index_abortQuery(CXClientData client_data, void *reserved) {
2800 IndexData *index_data;
2801 index_data = (IndexData *)client_data;
2802 return index_data->abort;
2803}
2804
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002805static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002806 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002807 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002808 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002809 index_ppIncludedFile,
Argyrios Kyrtzidis2c3e05c2012-10-02 16:10:38 +00002810 index_importedASTFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002811 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002812 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002813 index_indexEntityReference
2814};
2815
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002816static unsigned getIndexOptions(void) {
2817 unsigned index_opts;
2818 index_opts = 0;
2819 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2820 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2821 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2822 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
Argyrios Kyrtzidis838eb7e2012-12-06 19:41:16 +00002823 if (!getenv("CINDEXTEST_DISABLE_SKIPPARSEDBODIES"))
2824 index_opts |= CXIndexOpt_SkipParsedBodiesInSession;
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002825
2826 return index_opts;
2827}
2828
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002829static int index_compile_args(int num_args, const char **args,
2830 CXIndexAction idxAction,
2831 ImportedASTFilesData *importedASTs,
2832 const char *check_prefix) {
2833 IndexData index_data;
2834 unsigned index_opts;
2835 int result;
2836
2837 if (num_args == 0) {
2838 fprintf(stderr, "no compiler arguments\n");
2839 return -1;
2840 }
2841
2842 index_data.check_prefix = check_prefix;
2843 index_data.first_check_printed = 0;
2844 index_data.fail_for_error = 0;
2845 index_data.abort = 0;
2846 index_data.main_filename = "";
2847 index_data.importedASTs = importedASTs;
2848
2849 index_opts = getIndexOptions();
2850 result = clang_indexSourceFile(idxAction, &index_data,
2851 &IndexCB,sizeof(IndexCB), index_opts,
2852 0, args, num_args, 0, 0, 0,
2853 getDefaultParsingOptions());
2854 if (index_data.fail_for_error)
2855 result = -1;
2856
2857 return result;
2858}
2859
2860static int index_ast_file(const char *ast_file,
2861 CXIndex Idx,
2862 CXIndexAction idxAction,
2863 ImportedASTFilesData *importedASTs,
2864 const char *check_prefix) {
2865 CXTranslationUnit TU;
2866 IndexData index_data;
2867 unsigned index_opts;
2868 int result;
2869
2870 if (!CreateTranslationUnit(Idx, ast_file, &TU))
2871 return -1;
2872
2873 index_data.check_prefix = check_prefix;
2874 index_data.first_check_printed = 0;
2875 index_data.fail_for_error = 0;
2876 index_data.abort = 0;
2877 index_data.main_filename = "";
2878 index_data.importedASTs = importedASTs;
2879
2880 index_opts = getIndexOptions();
2881 result = clang_indexTranslationUnit(idxAction, &index_data,
2882 &IndexCB,sizeof(IndexCB),
2883 index_opts, TU);
2884 if (index_data.fail_for_error)
2885 result = -1;
2886
2887 clang_disposeTranslationUnit(TU);
2888 return result;
2889}
2890
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002891static int index_file(int argc, const char **argv, int full) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002892 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002893 CXIndex Idx;
2894 CXIndexAction idxAction;
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002895 ImportedASTFilesData *importedASTs;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002896 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002897
2898 check_prefix = 0;
2899 if (argc > 0) {
2900 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2901 check_prefix = argv[0] + strlen("-check-prefix=");
2902 ++argv;
2903 --argc;
2904 }
2905 }
2906
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002907 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002908 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002909 fprintf(stderr, "Could not create Index\n");
2910 return 1;
2911 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002912 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002913 importedASTs = 0;
2914 if (full)
2915 importedASTs = importedASTs_create();
2916
2917 result = index_compile_args(argc, argv, idxAction, importedASTs, check_prefix);
2918 if (result != 0)
2919 goto finished;
2920
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002921 if (full) {
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002922 unsigned i;
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002923 for (i = 0; i < importedASTs->num_files && result == 0; ++i) {
2924 result = index_ast_file(importedASTs->filenames[i], Idx, idxAction,
2925 importedASTs, check_prefix);
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002926 }
2927 }
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002928
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00002929finished:
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002930 importedASTs_dispose(importedASTs);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002931 clang_IndexAction_dispose(idxAction);
2932 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002933 return result;
2934}
2935
2936static int index_tu(int argc, const char **argv) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002937 const char *check_prefix;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002938 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002939 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002940 int result;
2941
2942 check_prefix = 0;
2943 if (argc > 0) {
2944 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2945 check_prefix = argv[0] + strlen("-check-prefix=");
2946 ++argv;
2947 --argc;
2948 }
2949 }
2950
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002951 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002952 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002953 fprintf(stderr, "Could not create Index\n");
2954 return 1;
2955 }
2956 idxAction = clang_IndexAction_create(Idx);
2957
2958 result = index_ast_file(argv[0], Idx, idxAction,
2959 /*importedASTs=*/0, check_prefix);
2960
2961 clang_IndexAction_dispose(idxAction);
2962 clang_disposeIndex(Idx);
2963 return result;
2964}
2965
2966static int index_compile_db(int argc, const char **argv) {
2967 const char *check_prefix;
2968 CXIndex Idx;
2969 CXIndexAction idxAction;
2970 int errorCode = 0;
2971
2972 check_prefix = 0;
2973 if (argc > 0) {
2974 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2975 check_prefix = argv[0] + strlen("-check-prefix=");
2976 ++argv;
2977 --argc;
2978 }
2979 }
2980
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002981 if (argc == 0) {
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002982 fprintf(stderr, "no compilation database\n");
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002983 return -1;
2984 }
2985
2986 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
Stefanus Du Toitfc093362013-03-01 21:41:22 +00002987 /* displayDiagnostics=*/1))) {
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002988 fprintf(stderr, "Could not create Index\n");
2989 return 1;
2990 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002991 idxAction = clang_IndexAction_create(Idx);
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002992
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00002993 {
2994 const char *database = argv[0];
2995 CXCompilationDatabase db = 0;
2996 CXCompileCommands CCmds = 0;
2997 CXCompileCommand CCmd;
2998 CXCompilationDatabase_Error ec;
2999 CXString wd;
3000#define MAX_COMPILE_ARGS 512
3001 CXString cxargs[MAX_COMPILE_ARGS];
3002 const char *args[MAX_COMPILE_ARGS];
3003 char *tmp;
3004 unsigned len;
3005 char *buildDir;
3006 int i, a, numCmds, numArgs;
3007
3008 len = strlen(database);
3009 tmp = (char *) malloc(len+1);
3010 memcpy(tmp, database, len+1);
3011 buildDir = dirname(tmp);
3012
3013 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
3014
3015 if (db) {
3016
3017 if (ec!=CXCompilationDatabase_NoError) {
3018 printf("unexpected error %d code while loading compilation database\n", ec);
3019 errorCode = -1;
3020 goto cdb_end;
3021 }
3022
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003023 if (chdir(buildDir) != 0) {
3024 printf("Could not chdir to %s\n", buildDir);
3025 errorCode = -1;
3026 goto cdb_end;
3027 }
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003028
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003029 CCmds = clang_CompilationDatabase_getAllCompileCommands(db);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003030 if (!CCmds) {
3031 printf("compilation db is empty\n");
3032 errorCode = -1;
3033 goto cdb_end;
3034 }
3035
3036 numCmds = clang_CompileCommands_getSize(CCmds);
3037
3038 if (numCmds==0) {
3039 fprintf(stderr, "should not get an empty compileCommand set\n");
3040 errorCode = -1;
3041 goto cdb_end;
3042 }
3043
3044 for (i=0; i<numCmds && errorCode == 0; ++i) {
3045 CCmd = clang_CompileCommands_getCommand(CCmds, i);
3046
3047 wd = clang_CompileCommand_getDirectory(CCmd);
Argyrios Kyrtzidis2bff7e52012-12-17 20:19:56 +00003048 if (chdir(clang_getCString(wd)) != 0) {
3049 printf("Could not chdir to %s\n", clang_getCString(wd));
3050 errorCode = -1;
3051 goto cdb_end;
3052 }
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003053 clang_disposeString(wd);
3054
3055 numArgs = clang_CompileCommand_getNumArgs(CCmd);
3056 if (numArgs > MAX_COMPILE_ARGS){
3057 fprintf(stderr, "got more compile arguments than maximum\n");
3058 errorCode = -1;
3059 goto cdb_end;
3060 }
3061 for (a=0; a<numArgs; ++a) {
3062 cxargs[a] = clang_CompileCommand_getArg(CCmd, a);
3063 args[a] = clang_getCString(cxargs[a]);
3064 }
3065
3066 errorCode = index_compile_args(numArgs, args, idxAction,
3067 /*importedASTs=*/0, check_prefix);
3068
3069 for (a=0; a<numArgs; ++a)
3070 clang_disposeString(cxargs[a]);
3071 }
3072 } else {
3073 printf("database loading failed with error code %d.\n", ec);
3074 errorCode = -1;
3075 }
3076
3077 cdb_end:
3078 clang_CompileCommands_dispose(CCmds);
3079 clang_CompilationDatabase_dispose(db);
3080 free(tmp);
3081
3082 }
3083
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00003084 clang_IndexAction_dispose(idxAction);
3085 clang_disposeIndex(Idx);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003086 return errorCode;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003087}
3088
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003089int perform_token_annotation(int argc, const char **argv) {
3090 const char *input = argv[1];
3091 char *filename = 0;
3092 unsigned line, second_line;
3093 unsigned column, second_column;
3094 CXIndex CIdx;
3095 CXTranslationUnit TU = 0;
3096 int errorCode;
3097 struct CXUnsavedFile *unsaved_files = 0;
3098 int num_unsaved_files = 0;
3099 CXToken *tokens;
3100 unsigned num_tokens;
3101 CXSourceRange range;
3102 CXSourceLocation startLoc, endLoc;
3103 CXFile file = 0;
3104 CXCursor *cursors = 0;
3105 unsigned i;
3106
3107 input += strlen("-test-annotate-tokens=");
3108 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
3109 &second_line, &second_column)))
3110 return errorCode;
3111
Richard Smithe07c5f82012-07-05 08:20:49 +00003112 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
3113 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003114 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00003115 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003116
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003117 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00003118 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
3119 argv + num_unsaved_files + 2,
3120 argc - num_unsaved_files - 3,
3121 unsaved_files,
3122 num_unsaved_files,
3123 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003124 if (!TU) {
3125 fprintf(stderr, "unable to parse input\n");
3126 clang_disposeIndex(CIdx);
3127 free(filename);
3128 free_remapped_files(unsaved_files, num_unsaved_files);
3129 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003130 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003131 errorCode = 0;
3132
Richard Smithe07c5f82012-07-05 08:20:49 +00003133 if (checkForErrors(TU) != 0) {
3134 errorCode = -1;
3135 goto teardown;
3136 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003137
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00003138 if (getenv("CINDEXTEST_EDITING")) {
3139 for (i = 0; i < 5; ++i) {
3140 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
3141 clang_defaultReparseOptions(TU))) {
3142 fprintf(stderr, "Unable to reparse translation unit!\n");
3143 errorCode = -1;
3144 goto teardown;
3145 }
3146 }
3147 }
3148
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003149 if (checkForErrors(TU) != 0) {
3150 errorCode = -1;
3151 goto teardown;
3152 }
3153
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003154 file = clang_getFile(TU, filename);
3155 if (!file) {
3156 fprintf(stderr, "file %s is not in this translation unit\n", filename);
3157 errorCode = -1;
3158 goto teardown;
3159 }
3160
3161 startLoc = clang_getLocation(TU, file, line, column);
3162 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003163 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003164 column);
3165 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003166 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003167 }
3168
3169 endLoc = clang_getLocation(TU, file, second_line, second_column);
3170 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00003171 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003172 second_line, second_column);
3173 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00003174 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003175 }
3176
3177 range = clang_getRange(startLoc, endLoc);
3178 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003179
3180 if (checkForErrors(TU) != 0) {
3181 errorCode = -1;
3182 goto teardown;
3183 }
3184
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003185 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
3186 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00003187
3188 if (checkForErrors(TU) != 0) {
3189 errorCode = -1;
3190 goto teardown;
3191 }
3192
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003193 for (i = 0; i != num_tokens; ++i) {
3194 const char *kind = "<unknown>";
3195 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
3196 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
3197 unsigned start_line, start_column, end_line, end_column;
3198
3199 switch (clang_getTokenKind(tokens[i])) {
3200 case CXToken_Punctuation: kind = "Punctuation"; break;
3201 case CXToken_Keyword: kind = "Keyword"; break;
3202 case CXToken_Identifier: kind = "Identifier"; break;
3203 case CXToken_Literal: kind = "Literal"; break;
3204 case CXToken_Comment: kind = "Comment"; break;
3205 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00003206 clang_getSpellingLocation(clang_getRangeStart(extent),
3207 0, &start_line, &start_column, 0);
3208 clang_getSpellingLocation(clang_getRangeEnd(extent),
3209 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00003210 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00003211 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00003212 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003213 if (!clang_isInvalid(cursors[i].kind)) {
3214 printf(" ");
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003215 PrintCursor(cursors[i], NULL);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00003216 }
3217 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003218 }
3219 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00003220 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003221
3222 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00003223 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003224 clang_disposeTranslationUnit(TU);
3225 clang_disposeIndex(CIdx);
3226 free(filename);
3227 free_remapped_files(unsaved_files, num_unsaved_files);
3228 return errorCode;
3229}
3230
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003231static int
3232perform_test_compilation_db(const char *database, int argc, const char **argv) {
3233 CXCompilationDatabase db;
3234 CXCompileCommands CCmds;
3235 CXCompileCommand CCmd;
3236 CXCompilationDatabase_Error ec;
3237 CXString wd;
3238 CXString arg;
3239 int errorCode = 0;
3240 char *tmp;
3241 unsigned len;
3242 char *buildDir;
3243 int i, j, a, numCmds, numArgs;
3244
3245 len = strlen(database);
3246 tmp = (char *) malloc(len+1);
3247 memcpy(tmp, database, len+1);
3248 buildDir = dirname(tmp);
3249
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003250 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003251
3252 if (db) {
3253
3254 if (ec!=CXCompilationDatabase_NoError) {
3255 printf("unexpected error %d code while loading compilation database\n", ec);
3256 errorCode = -1;
3257 goto cdb_end;
3258 }
3259
3260 for (i=0; i<argc && errorCode==0; ) {
3261 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003262 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003263
3264 if (!CCmds) {
3265 printf("file %s not found in compilation db\n", argv[i+1]);
3266 errorCode = -1;
3267 break;
3268 }
3269
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003270 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003271
3272 if (numCmds==0) {
3273 fprintf(stderr, "should not get an empty compileCommand set for file"
3274 " '%s'\n", argv[i+1]);
3275 errorCode = -1;
3276 break;
3277 }
3278
3279 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003280 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003281
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003282 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003283 printf("workdir:'%s'", clang_getCString(wd));
3284 clang_disposeString(wd);
3285
3286 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003287 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003288 for (a=0; a<numArgs; ++a) {
3289 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003290 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003291 printf("%s", clang_getCString(arg));
3292 clang_disposeString(arg);
3293 }
3294 printf("'\n");
3295 }
3296
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003297 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003298
3299 i += 2;
3300 }
3301 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00003302 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003303 } else {
3304 printf("database loading failed with error code %d.\n", ec);
3305 errorCode = -1;
3306 }
3307
3308cdb_end:
3309 free(tmp);
3310
3311 return errorCode;
3312}
3313
Ted Kremenek0d435192009-11-17 18:13:31 +00003314/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003315/* USR printing. */
3316/******************************************************************************/
3317
3318static int insufficient_usr(const char *kind, const char *usage) {
3319 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
3320 return 1;
3321}
3322
3323static unsigned isUSR(const char *s) {
3324 return s[0] == 'c' && s[1] == ':';
3325}
3326
3327static int not_usr(const char *s, const char *arg) {
3328 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
3329 return 1;
3330}
3331
3332static void print_usr(CXString usr) {
3333 const char *s = clang_getCString(usr);
3334 printf("%s\n", s);
3335 clang_disposeString(usr);
3336}
3337
3338static void display_usrs() {
3339 fprintf(stderr, "-print-usrs options:\n"
3340 " ObjCCategory <class name> <category name>\n"
3341 " ObjCClass <class name>\n"
3342 " ObjCIvar <ivar name> <class USR>\n"
3343 " ObjCMethod <selector> [0=class method|1=instance method] "
3344 "<class USR>\n"
3345 " ObjCProperty <property name> <class USR>\n"
3346 " ObjCProtocol <protocol name>\n");
3347}
3348
3349int print_usrs(const char **I, const char **E) {
3350 while (I != E) {
3351 const char *kind = *I;
3352 unsigned len = strlen(kind);
3353 switch (len) {
3354 case 8:
3355 if (memcmp(kind, "ObjCIvar", 8) == 0) {
3356 if (I + 2 >= E)
3357 return insufficient_usr(kind, "<ivar name> <class USR>");
3358 if (!isUSR(I[2]))
3359 return not_usr("<class USR>", I[2]);
3360 else {
3361 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003362 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003363 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003364 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
3365 }
3366
3367 I += 3;
3368 continue;
3369 }
3370 break;
3371 case 9:
3372 if (memcmp(kind, "ObjCClass", 9) == 0) {
3373 if (I + 1 >= E)
3374 return insufficient_usr(kind, "<class name>");
3375 print_usr(clang_constructUSR_ObjCClass(I[1]));
3376 I += 2;
3377 continue;
3378 }
3379 break;
3380 case 10:
3381 if (memcmp(kind, "ObjCMethod", 10) == 0) {
3382 if (I + 3 >= E)
3383 return insufficient_usr(kind, "<method selector> "
3384 "[0=class method|1=instance method] <class USR>");
3385 if (!isUSR(I[3]))
3386 return not_usr("<class USR>", I[3]);
3387 else {
3388 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003389 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00003390 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003391 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
3392 }
3393 I += 4;
3394 continue;
3395 }
3396 break;
3397 case 12:
3398 if (memcmp(kind, "ObjCCategory", 12) == 0) {
3399 if (I + 2 >= E)
3400 return insufficient_usr(kind, "<class name> <category name>");
3401 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
3402 I += 3;
3403 continue;
3404 }
3405 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
3406 if (I + 1 >= E)
3407 return insufficient_usr(kind, "<protocol name>");
3408 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
3409 I += 2;
3410 continue;
3411 }
3412 if (memcmp(kind, "ObjCProperty", 12) == 0) {
3413 if (I + 2 >= E)
3414 return insufficient_usr(kind, "<property name> <class USR>");
3415 if (!isUSR(I[2]))
3416 return not_usr("<class USR>", I[2]);
3417 else {
3418 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00003419 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00003420 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003421 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
3422 }
3423 I += 3;
3424 continue;
3425 }
3426 break;
3427 default:
3428 break;
3429 }
3430 break;
3431 }
3432
3433 if (I != E) {
3434 fprintf(stderr, "Invalid USR kind: %s\n", *I);
3435 display_usrs();
3436 return 1;
3437 }
3438 return 0;
3439}
3440
3441int print_usrs_file(const char *file_name) {
3442 char line[2048];
3443 const char *args[128];
3444 unsigned numChars = 0;
3445
3446 FILE *fp = fopen(file_name, "r");
3447 if (!fp) {
3448 fprintf(stderr, "error: cannot open '%s'\n", file_name);
3449 return 1;
3450 }
3451
3452 /* This code is not really all that safe, but it works fine for testing. */
3453 while (!feof(fp)) {
3454 char c = fgetc(fp);
3455 if (c == '\n') {
3456 unsigned i = 0;
3457 const char *s = 0;
3458
3459 if (numChars == 0)
3460 continue;
3461
3462 line[numChars] = '\0';
3463 numChars = 0;
3464
3465 if (line[0] == '/' && line[1] == '/')
3466 continue;
3467
3468 s = strtok(line, " ");
3469 while (s) {
3470 args[i] = s;
3471 ++i;
3472 s = strtok(0, " ");
3473 }
3474 if (print_usrs(&args[0], &args[i]))
3475 return 1;
3476 }
3477 else
3478 line[numChars++] = c;
3479 }
3480
3481 fclose(fp);
3482 return 0;
3483}
3484
3485/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00003486/* Command line processing. */
3487/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003488int write_pch_file(const char *filename, int argc, const char *argv[]) {
3489 CXIndex Idx;
3490 CXTranslationUnit TU;
3491 struct CXUnsavedFile *unsaved_files = 0;
3492 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00003493 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003494
Stefanus Du Toitfc093362013-03-01 21:41:22 +00003495 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnostics=*/1);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003496
3497 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
3498 clang_disposeIndex(Idx);
3499 return -1;
3500 }
3501
3502 TU = clang_parseTranslationUnit(Idx, 0,
3503 argv + num_unsaved_files,
3504 argc - num_unsaved_files,
3505 unsaved_files,
3506 num_unsaved_files,
Argyrios Kyrtzidis900ab952012-10-11 16:05:00 +00003507 CXTranslationUnit_Incomplete |
3508 CXTranslationUnit_ForSerialization);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003509 if (!TU) {
3510 fprintf(stderr, "Unable to load translation unit!\n");
3511 free_remapped_files(unsaved_files, num_unsaved_files);
3512 clang_disposeIndex(Idx);
3513 return 1;
3514 }
3515
Douglas Gregor39c411f2011-07-06 16:43:36 +00003516 switch (clang_saveTranslationUnit(TU, filename,
3517 clang_defaultSaveOptions(TU))) {
3518 case CXSaveError_None:
3519 break;
3520
3521 case CXSaveError_TranslationErrors:
3522 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
3523 filename);
3524 result = 2;
3525 break;
3526
3527 case CXSaveError_InvalidTU:
3528 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
3529 filename);
3530 result = 3;
3531 break;
3532
3533 case CXSaveError_Unknown:
3534 default:
3535 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
3536 result = 1;
3537 break;
3538 }
3539
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003540 clang_disposeTranslationUnit(TU);
3541 free_remapped_files(unsaved_files, num_unsaved_files);
3542 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00003543 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003544}
3545
3546/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00003547/* Serialized diagnostics. */
3548/******************************************************************************/
3549
3550static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
3551 switch (error) {
3552 case CXLoadDiag_CannotLoad: return "Cannot Load File";
3553 case CXLoadDiag_None: break;
3554 case CXLoadDiag_Unknown: return "Unknown";
3555 case CXLoadDiag_InvalidFile: return "Invalid File";
3556 }
3557 return "None";
3558}
3559
3560static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
3561 switch (severity) {
3562 case CXDiagnostic_Note: return "note";
3563 case CXDiagnostic_Error: return "error";
3564 case CXDiagnostic_Fatal: return "fatal";
3565 case CXDiagnostic_Ignored: return "ignored";
3566 case CXDiagnostic_Warning: return "warning";
3567 }
3568 return "unknown";
3569}
3570
3571static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003572 if (indent == 0)
3573 return;
3574 fprintf(stderr, "+");
3575 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00003576 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003577 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00003578 --indent;
3579 }
3580}
3581
3582static void printLocation(CXSourceLocation L) {
3583 CXFile File;
3584 CXString FileName;
3585 unsigned line, column, offset;
3586
3587 clang_getExpansionLocation(L, &File, &line, &column, &offset);
3588 FileName = clang_getFileName(File);
3589
3590 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
3591 clang_disposeString(FileName);
3592}
3593
3594static void printRanges(CXDiagnostic D, unsigned indent) {
3595 unsigned i, n = clang_getDiagnosticNumRanges(D);
3596
3597 for (i = 0; i < n; ++i) {
3598 CXSourceLocation Start, End;
3599 CXSourceRange SR = clang_getDiagnosticRange(D, i);
3600 Start = clang_getRangeStart(SR);
3601 End = clang_getRangeEnd(SR);
3602
3603 printIndent(indent);
3604 fprintf(stderr, "Range: ");
3605 printLocation(Start);
3606 fprintf(stderr, " ");
3607 printLocation(End);
3608 fprintf(stderr, "\n");
3609 }
3610}
3611
3612static void printFixIts(CXDiagnostic D, unsigned indent) {
3613 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00003614 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00003615 for (i = 0 ; i < n; ++i) {
3616 CXSourceRange ReplacementRange;
3617 CXString text;
3618 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
3619
3620 printIndent(indent);
3621 fprintf(stderr, "FIXIT: (");
3622 printLocation(clang_getRangeStart(ReplacementRange));
3623 fprintf(stderr, " - ");
3624 printLocation(clang_getRangeEnd(ReplacementRange));
3625 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
3626 clang_disposeString(text);
3627 }
3628}
3629
3630static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003631 unsigned i, n;
3632
Ted Kremenek15322172011-11-10 08:43:12 +00003633 if (!Diags)
3634 return;
3635
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003636 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00003637 for (i = 0; i < n; ++i) {
3638 CXSourceLocation DiagLoc;
3639 CXDiagnostic D;
3640 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003641 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00003642 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003643 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00003644
3645 D = clang_getDiagnosticInSet(Diags, i);
3646 DiagLoc = clang_getDiagnosticLocation(D);
3647 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3648 FileName = clang_getFileName(File);
3649 DiagSpelling = clang_getDiagnosticSpelling(D);
3650
3651 printIndent(indent);
3652
3653 fprintf(stderr, "%s:%d:%d: %s: %s",
3654 clang_getCString(FileName),
3655 line,
3656 column,
3657 getSeverityString(clang_getDiagnosticSeverity(D)),
3658 clang_getCString(DiagSpelling));
3659
3660 DiagOption = clang_getDiagnosticOption(D, 0);
3661 DiagOptionStr = clang_getCString(DiagOption);
3662 if (DiagOptionStr) {
3663 fprintf(stderr, " [%s]", DiagOptionStr);
3664 }
3665
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003666 DiagCat = clang_getDiagnosticCategoryText(D);
3667 DiagCatStr = clang_getCString(DiagCat);
3668 if (DiagCatStr) {
3669 fprintf(stderr, " [%s]", DiagCatStr);
3670 }
3671
Ted Kremenek15322172011-11-10 08:43:12 +00003672 fprintf(stderr, "\n");
3673
3674 printRanges(D, indent);
3675 printFixIts(D, indent);
3676
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003677 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003678 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3679
3680 clang_disposeString(FileName);
3681 clang_disposeString(DiagSpelling);
3682 clang_disposeString(DiagOption);
3683 }
3684}
3685
3686static int read_diagnostics(const char *filename) {
3687 enum CXLoadDiag_Error error;
3688 CXString errorString;
3689 CXDiagnosticSet Diags = 0;
3690
3691 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3692 if (!Diags) {
3693 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3694 getDiagnosticCodeStr(error),
3695 clang_getCString(errorString));
3696 clang_disposeString(errorString);
3697 return 1;
3698 }
3699
3700 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003701 fprintf(stderr, "Number of diagnostics: %d\n",
3702 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003703 clang_disposeDiagnosticSet(Diags);
3704 return 0;
3705}
3706
3707/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003708/* Command line processing. */
3709/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003710
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003711static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003712 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003713 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003714 if (strcmp(s, "-usrs") == 0)
3715 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003716 if (strncmp(s, "-memory-usage", 13) == 0)
3717 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003718 return NULL;
3719}
3720
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003721static void print_usage(void) {
3722 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003723 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003724 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003725 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00003726 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
3727 " c-index-test -file-includes-in=<filename> <compiler arguments>\n");
NAKAMURA Takumi35849722012-10-24 22:52:04 +00003728 fprintf(stderr,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003729 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003730 " c-index-test -index-file-full [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003731 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003732 " c-index-test -index-compile-db [-check-prefix=<FileCheck prefix>] <compilation database>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003733 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003734 "[FileCheck prefix]\n");
3735 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003736 " c-index-test -test-load-tu <AST file> <symbol filter> "
3737 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003738 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3739 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003740 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003741 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003742 " c-index-test -test-load-source-memory-usage "
3743 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003744 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3745 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003746 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003747 " c-index-test -test-load-source-usrs-memory-usage "
3748 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003749 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3750 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003751 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003752 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003753 " c-index-test -test-print-linkage-source {<args>}*\n"
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003754 " c-index-test -test-print-type {<args>}*\n"
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00003755 " c-index-test -test-print-type-size {<args>}*\n"
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003756 " c-index-test -test-print-bitwidth {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003757 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003758 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003759 " c-index-test -write-pch <file> <compiler arguments>\n");
3760 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003761 " c-index-test -compilation-db [lookup <filename>] database\n");
3762 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003763 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003764 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003765 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003766 " all - load all symbols, including those from PCH\n"
3767 " local - load all symbols except those in PCH\n"
3768 " category - only load ObjC categories (non-PCH)\n"
3769 " interface - only load ObjC interfaces (non-PCH)\n"
3770 " protocol - only load ObjC protocols (non-PCH)\n"
3771 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003772 " typedef - only load typdefs (non-PCH)\n"
3773 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003774}
3775
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003776/***/
3777
3778int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003779 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003780 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3781 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003782 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003783 return perform_code_completion(argc, argv, 0);
3784 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3785 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003786 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3787 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003788 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3789 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidisee2d5fd2013-03-08 02:32:34 +00003790 if (argc > 2 && strstr(argv[1], "-file-includes-in=") == argv[1])
3791 return find_file_includes_in(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003792 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
Argyrios Kyrtzidis11db1822012-10-24 18:29:15 +00003793 return index_file(argc - 2, argv + 2, /*full=*/0);
3794 if (argc > 2 && strcmp(argv[1], "-index-file-full") == 0)
3795 return index_file(argc - 2, argv + 2, /*full=*/1);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003796 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3797 return index_tu(argc - 2, argv + 2);
Argyrios Kyrtzidisd10682f2012-12-05 21:53:37 +00003798 if (argc > 2 && strcmp(argv[1], "-index-compile-db") == 0)
3799 return index_compile_db(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003800 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003801 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003802 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003803 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3804 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003805 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003806 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3807 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3808 if (I) {
3809 int trials = atoi(argv[2]);
3810 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3811 NULL);
3812 }
3813 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003814 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003815 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003816
3817 PostVisitTU postVisit = 0;
3818 if (strstr(argv[1], "-memory-usage"))
3819 postVisit = PrintMemoryUsage;
3820
Ted Kremenek7d405622010-01-12 23:34:26 +00003821 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003822 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3823 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003824 }
3825 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003826 return perform_file_scan(argv[2], argv[3],
3827 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003828 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3829 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003830 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3831 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3832 PrintInclusionStack);
3833 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3834 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3835 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003836 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3837 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3838 NULL);
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003839 else if (argc > 2 && strcmp(argv[1], "-test-print-type") == 0)
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003840 return perform_test_load_source(argc - 2, argv + 2, "all",
Dmitri Gribenkoae03d8e2013-02-15 21:15:49 +00003841 PrintType, 0);
Argyrios Kyrtzidis411d33a2013-04-11 01:20:11 +00003842 else if (argc > 2 && strcmp(argv[1], "-test-print-type-size") == 0)
3843 return perform_test_load_source(argc - 2, argv + 2, "all",
3844 PrintTypeSize, 0);
Dmitri Gribenko1eb60822012-12-04 15:13:46 +00003845 else if (argc > 2 && strcmp(argv[1], "-test-print-bitwidth") == 0)
3846 return perform_test_load_source(argc - 2, argv + 2, "all",
3847 PrintBitWidth, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003848 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3849 if (argc > 2)
3850 return print_usrs(argv + 2, argv + argc);
3851 else {
3852 display_usrs();
3853 return 1;
3854 }
3855 }
3856 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3857 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003858 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3859 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003860 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3861 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3862
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003863 print_usage();
3864 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003865}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003866
3867/***/
3868
3869/* We intentionally run in a separate thread to ensure we at least minimal
3870 * testing of a multithreaded environment (for example, having a reduced stack
3871 * size). */
3872
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003873typedef struct thread_info {
3874 int argc;
3875 const char **argv;
3876 int result;
3877} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003878void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003879 thread_info *client_data = client_data_v;
3880 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003881#ifdef __CYGWIN__
3882 fflush(stdout); /* stdout is not flushed on Cygwin. */
3883#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003884}
3885
3886int main(int argc, const char **argv) {
Benjamin Kramerd1a4f682012-08-10 10:06:13 +00003887 thread_info client_data;
3888
Dmitri Gribenkof303d4c2012-08-07 17:54:38 +00003889#ifdef CLANG_HAVE_LIBXML
3890 LIBXML_TEST_VERSION
3891#endif
3892
Douglas Gregor61605982010-10-27 16:00:01 +00003893 if (getenv("CINDEXTEST_NOTHREADS"))
3894 return cindextest_main(argc, argv);
3895
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003896 client_data.argc = argc;
3897 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003898 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003899 return client_data.result;
3900}