blob: 9f270f0151cb61ad723a5035ac5bdeef6bcc2890 [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"
Douglas Gregor1e5e6682010-08-26 13:48:20 +00005#include <ctype.h>
Douglas Gregor0c8296d2009-11-07 00:00:49 +00006#include <stdlib.h>
Steve Naroff89922f82009-08-31 00:59:03 +00007#include <stdio.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +00008#include <string.h>
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00009#include <assert.h>
Steve Naroffaf08ddc2009-09-03 15:49:00 +000010
Ted Kremenek0d435192009-11-17 18:13:31 +000011/******************************************************************************/
12/* Utility functions. */
13/******************************************************************************/
14
John Thompson2e06fc82009-10-27 13:42:56 +000015#ifdef _MSC_VER
16char *basename(const char* path)
17{
18 char* base1 = (char*)strrchr(path, '/');
19 char* base2 = (char*)strrchr(path, '\\');
20 if (base1 && base2)
21 return((base1 > base2) ? base1 + 1 : base2 + 1);
22 else if (base1)
23 return(base1 + 1);
24 else if (base2)
25 return(base2 + 1);
26
27 return((char*)path);
28}
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000029char *dirname(char* path)
30{
31 char* base1 = (char*)strrchr(path, '/');
32 char* base2 = (char*)strrchr(path, '\\');
33 if (base1 && base2)
34 if (base1 > base2)
35 *base1 = 0;
36 else
37 *base2 = 0;
38 else if (base1)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000039 *base1 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000040 else if (base2)
NAKAMURA Takumi0fb474a2012-06-30 11:47:18 +000041 *base2 = 0;
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000042
43 return path;
44}
John Thompson2e06fc82009-10-27 13:42:56 +000045#else
Steve Naroffff9e18c2009-09-24 20:03:06 +000046extern char *basename(const char *);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +000047extern char *dirname(char *);
John Thompson2e06fc82009-10-27 13:42:56 +000048#endif
Steve Naroffff9e18c2009-09-24 20:03:06 +000049
Douglas Gregor45ba9a12010-07-25 17:39:21 +000050/** \brief Return the default parsing options. */
Douglas Gregor44c181a2010-07-23 00:33:23 +000051static unsigned getDefaultParsingOptions() {
52 unsigned options = CXTranslationUnit_DetailedPreprocessingRecord;
53
54 if (getenv("CINDEXTEST_EDITING"))
Douglas Gregorb1c031b2010-08-09 22:28:58 +000055 options |= clang_defaultEditingTranslationUnitOptions();
Douglas Gregor87c08a52010-08-13 22:48:40 +000056 if (getenv("CINDEXTEST_COMPLETION_CACHING"))
57 options |= CXTranslationUnit_CacheCompletionResults;
Argyrios Kyrtzidisdcaca012011-11-03 02:20:25 +000058 if (getenv("CINDEXTEST_COMPLETION_NO_CACHING"))
59 options &= ~CXTranslationUnit_CacheCompletionResults;
Erik Verbruggen6a91d382012-04-12 10:11:59 +000060 if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES"))
61 options |= CXTranslationUnit_SkipFunctionBodies;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +000062 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
63 options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion;
Douglas Gregor44c181a2010-07-23 00:33:23 +000064
65 return options;
66}
67
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +000068static int checkForErrors(CXTranslationUnit TU);
69
Daniel Dunbar51b058c2010-02-14 08:32:24 +000070static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column,
71 unsigned end_line, unsigned end_column) {
72 fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column,
Daniel Dunbard52864b2010-02-14 10:02:57 +000073 end_line, end_column);
Daniel Dunbar51b058c2010-02-14 08:32:24 +000074}
75
Ted Kremenek1c6da172009-11-17 19:37:36 +000076static unsigned CreateTranslationUnit(CXIndex Idx, const char *file,
77 CXTranslationUnit *TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +000078
Douglas Gregora88084b2010-02-18 18:08:43 +000079 *TU = clang_createTranslationUnit(Idx, file);
Dan Gohman6be2a222010-07-26 21:44:15 +000080 if (!*TU) {
Ted Kremenek1c6da172009-11-17 19:37:36 +000081 fprintf(stderr, "Unable to load translation unit from '%s'!\n", file);
82 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +000083 }
Ted Kremenek1c6da172009-11-17 19:37:36 +000084 return 1;
85}
86
Douglas Gregor4db64a42010-01-23 00:14:00 +000087void free_remapped_files(struct CXUnsavedFile *unsaved_files,
88 int num_unsaved_files) {
89 int i;
90 for (i = 0; i != num_unsaved_files; ++i) {
91 free((char *)unsaved_files[i].Filename);
92 free((char *)unsaved_files[i].Contents);
93 }
Douglas Gregor653a55f2010-08-19 20:50:29 +000094 free(unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +000095}
96
97int parse_remapped_files(int argc, const char **argv, int start_arg,
98 struct CXUnsavedFile **unsaved_files,
99 int *num_unsaved_files) {
100 int i;
101 int arg;
102 int prefix_len = strlen("-remap-file=");
103 *unsaved_files = 0;
104 *num_unsaved_files = 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000105
Douglas Gregor4db64a42010-01-23 00:14:00 +0000106 /* Count the number of remapped files. */
107 for (arg = start_arg; arg < argc; ++arg) {
108 if (strncmp(argv[arg], "-remap-file=", prefix_len))
109 break;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000110
Douglas Gregor4db64a42010-01-23 00:14:00 +0000111 ++*num_unsaved_files;
112 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000113
Douglas Gregor4db64a42010-01-23 00:14:00 +0000114 if (*num_unsaved_files == 0)
115 return 0;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000116
Douglas Gregor4db64a42010-01-23 00:14:00 +0000117 *unsaved_files
Douglas Gregor653a55f2010-08-19 20:50:29 +0000118 = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) *
119 *num_unsaved_files);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000120 for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) {
121 struct CXUnsavedFile *unsaved = *unsaved_files + i;
122 const char *arg_string = argv[arg] + prefix_len;
123 int filename_len;
124 char *filename;
125 char *contents;
126 FILE *to_file;
127 const char *semi = strchr(arg_string, ';');
128 if (!semi) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000129 fprintf(stderr,
Douglas Gregor4db64a42010-01-23 00:14:00 +0000130 "error: -remap-file=from;to argument is missing semicolon\n");
131 free_remapped_files(*unsaved_files, i);
132 *unsaved_files = 0;
133 *num_unsaved_files = 0;
134 return -1;
135 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000136
Douglas Gregor4db64a42010-01-23 00:14:00 +0000137 /* Open the file that we're remapping to. */
Francois Pichetc44fe4b2010-10-12 01:01:43 +0000138 to_file = fopen(semi + 1, "rb");
Douglas Gregor4db64a42010-01-23 00:14:00 +0000139 if (!to_file) {
140 fprintf(stderr, "error: cannot open file %s that we are remapping to\n",
141 semi + 1);
142 free_remapped_files(*unsaved_files, i);
143 *unsaved_files = 0;
144 *num_unsaved_files = 0;
145 return -1;
146 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000147
Douglas Gregor4db64a42010-01-23 00:14:00 +0000148 /* Determine the length of the file we're remapping to. */
149 fseek(to_file, 0, SEEK_END);
150 unsaved->Length = ftell(to_file);
151 fseek(to_file, 0, SEEK_SET);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000152
Douglas Gregor4db64a42010-01-23 00:14:00 +0000153 /* Read the contents of the file we're remapping to. */
154 contents = (char *)malloc(unsaved->Length + 1);
155 if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) {
156 fprintf(stderr, "error: unexpected %s reading 'to' file %s\n",
157 (feof(to_file) ? "EOF" : "error"), semi + 1);
158 fclose(to_file);
159 free_remapped_files(*unsaved_files, i);
Richard Smithe07c5f82012-07-05 08:20:49 +0000160 free(contents);
Douglas Gregor4db64a42010-01-23 00:14:00 +0000161 *unsaved_files = 0;
162 *num_unsaved_files = 0;
163 return -1;
164 }
165 contents[unsaved->Length] = 0;
166 unsaved->Contents = contents;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000167
Douglas Gregor4db64a42010-01-23 00:14:00 +0000168 /* Close the file. */
169 fclose(to_file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000170
Douglas Gregor4db64a42010-01-23 00:14:00 +0000171 /* Copy the file name that we're remapping from. */
172 filename_len = semi - arg_string;
173 filename = (char *)malloc(filename_len + 1);
174 memcpy(filename, arg_string, filename_len);
175 filename[filename_len] = 0;
176 unsaved->Filename = filename;
177 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000178
Douglas Gregor4db64a42010-01-23 00:14:00 +0000179 return 0;
180}
181
Ted Kremenek0d435192009-11-17 18:13:31 +0000182/******************************************************************************/
183/* Pretty-printing. */
184/******************************************************************************/
185
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000186static const char *FileCheckPrefix = "CHECK";
187
188static void PrintCString(const char *CStr) {
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000189 if (CStr != NULL && CStr[0] != '\0') {
190 for ( ; *CStr; ++CStr) {
191 const char C = *CStr;
192 switch (C) {
193 case '\n': printf("\\n"); break;
194 case '\r': printf("\\r"); break;
195 case '\t': printf("\\t"); break;
196 case '\v': printf("\\v"); break;
197 case '\f': printf("\\f"); break;
198 default: putchar(C); break;
199 }
200 }
201 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000202}
203
204static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) {
205 printf(" %s=[", Prefix);
206 PrintCString(CStr);
Dmitri Gribenko2d44d772012-06-26 20:39:18 +0000207 printf("]");
208}
209
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000210static void PrintCXStringAndDispose(CXString Str) {
211 PrintCString(clang_getCString(Str));
212 clang_disposeString(Str);
213}
214
215static void PrintCXStringWithPrefixAndDispose(const char *Prefix,
216 CXString Str) {
217 PrintCStringWithPrefix(Prefix, clang_getCString(Str));
218 clang_disposeString(Str);
219}
220
Douglas Gregor430d7a12011-07-25 17:48:11 +0000221static void PrintRange(CXSourceRange R, const char *str) {
222 CXFile begin_file, end_file;
223 unsigned begin_line, begin_column, end_line, end_column;
224
225 clang_getSpellingLocation(clang_getRangeStart(R),
226 &begin_file, &begin_line, &begin_column, 0);
227 clang_getSpellingLocation(clang_getRangeEnd(R),
228 &end_file, &end_line, &end_column, 0);
229 if (!begin_file || !end_file)
230 return;
231
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +0000232 if (str)
233 printf(" %s=", str);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000234 PrintExtent(stdout, begin_line, begin_column, end_line, end_column);
235}
236
Douglas Gregor358559d2010-10-02 22:49:11 +0000237int want_display_name = 0;
238
Douglas Gregorcc889662012-05-08 00:14:45 +0000239static void printVersion(const char *Prefix, CXVersion Version) {
240 if (Version.Major < 0)
241 return;
242 printf("%s%d", Prefix, Version.Major);
243
244 if (Version.Minor < 0)
245 return;
246 printf(".%d", Version.Minor);
247
248 if (Version.Subminor < 0)
249 return;
250 printf(".%d", Version.Subminor);
251}
252
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000253struct CommentASTDumpingContext {
254 int IndentLevel;
255};
256
257static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx,
258 CXComment Comment) {
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000259 unsigned i;
260 unsigned e;
261 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
262
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000263 Ctx->IndentLevel++;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000264 for (i = 0, e = Ctx->IndentLevel; i != e; ++i)
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000265 printf(" ");
266
267 printf("(");
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000268 switch (Kind) {
269 case CXComment_Null:
270 printf("CXComment_Null");
271 break;
272 case CXComment_Text:
273 printf("CXComment_Text");
274 PrintCXStringWithPrefixAndDispose("Text",
275 clang_TextComment_getText(Comment));
276 if (clang_Comment_isWhitespace(Comment))
277 printf(" IsWhitespace");
278 if (clang_InlineContentComment_hasTrailingNewline(Comment))
279 printf(" HasTrailingNewline");
280 break;
281 case CXComment_InlineCommand:
282 printf("CXComment_InlineCommand");
283 PrintCXStringWithPrefixAndDispose(
284 "CommandName",
285 clang_InlineCommandComment_getCommandName(Comment));
Dmitri Gribenko2d66a502012-07-23 16:43:01 +0000286 switch (clang_InlineCommandComment_getRenderKind(Comment)) {
287 case CXCommentInlineCommandRenderKind_Normal:
288 printf(" RenderNormal");
289 break;
290 case CXCommentInlineCommandRenderKind_Bold:
291 printf(" RenderBold");
292 break;
293 case CXCommentInlineCommandRenderKind_Monospaced:
294 printf(" RenderMonospaced");
295 break;
296 case CXCommentInlineCommandRenderKind_Emphasized:
297 printf(" RenderEmphasized");
298 break;
299 }
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000300 for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000301 i != e; ++i) {
302 printf(" Arg[%u]=", i);
303 PrintCXStringAndDispose(
304 clang_InlineCommandComment_getArgText(Comment, i));
305 }
306 if (clang_InlineContentComment_hasTrailingNewline(Comment))
307 printf(" HasTrailingNewline");
308 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000309 case CXComment_HTMLStartTag: {
310 unsigned NumAttrs;
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000311 printf("CXComment_HTMLStartTag");
312 PrintCXStringWithPrefixAndDispose(
313 "Name",
314 clang_HTMLTagComment_getTagName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000315 NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000316 if (NumAttrs != 0) {
317 printf(" Attrs:");
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000318 for (i = 0; i != NumAttrs; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000319 printf(" ");
320 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
321 printf("=");
322 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
323 }
324 }
325 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
326 printf(" SelfClosing");
327 if (clang_InlineContentComment_hasTrailingNewline(Comment))
328 printf(" HasTrailingNewline");
329 break;
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000330 }
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000331 case CXComment_HTMLEndTag:
332 printf("CXComment_HTMLEndTag");
333 PrintCXStringWithPrefixAndDispose(
334 "Name",
335 clang_HTMLTagComment_getTagName(Comment));
336 if (clang_InlineContentComment_hasTrailingNewline(Comment))
337 printf(" HasTrailingNewline");
338 break;
339 case CXComment_Paragraph:
340 printf("CXComment_Paragraph");
341 if (clang_Comment_isWhitespace(Comment))
342 printf(" IsWhitespace");
343 break;
344 case CXComment_BlockCommand:
345 printf("CXComment_BlockCommand");
346 PrintCXStringWithPrefixAndDispose(
347 "CommandName",
348 clang_BlockCommandComment_getCommandName(Comment));
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000349 for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000350 i != e; ++i) {
351 printf(" Arg[%u]=", i);
352 PrintCXStringAndDispose(
353 clang_BlockCommandComment_getArgText(Comment, i));
354 }
355 break;
356 case CXComment_ParamCommand:
357 printf("CXComment_ParamCommand");
358 switch (clang_ParamCommandComment_getDirection(Comment)) {
359 case CXCommentParamPassDirection_In:
360 printf(" in");
361 break;
362 case CXCommentParamPassDirection_Out:
363 printf(" out");
364 break;
365 case CXCommentParamPassDirection_InOut:
366 printf(" in,out");
367 break;
368 }
369 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
370 printf(" explicitly");
371 else
372 printf(" implicitly");
373 PrintCXStringWithPrefixAndDispose(
374 "ParamName",
375 clang_ParamCommandComment_getParamName(Comment));
376 if (clang_ParamCommandComment_isParamIndexValid(Comment))
377 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
378 else
379 printf(" ParamIndex=Invalid");
380 break;
381 case CXComment_VerbatimBlockCommand:
382 printf("CXComment_VerbatimBlockCommand");
383 PrintCXStringWithPrefixAndDispose(
384 "CommandName",
385 clang_BlockCommandComment_getCommandName(Comment));
386 break;
387 case CXComment_VerbatimBlockLine:
388 printf("CXComment_VerbatimBlockLine");
389 PrintCXStringWithPrefixAndDispose(
390 "Text",
391 clang_VerbatimBlockLineComment_getText(Comment));
392 break;
393 case CXComment_VerbatimLine:
394 printf("CXComment_VerbatimLine");
395 PrintCXStringWithPrefixAndDispose(
396 "Text",
397 clang_VerbatimLineComment_getText(Comment));
398 break;
399 case CXComment_FullComment:
400 printf("CXComment_FullComment");
401 break;
402 }
403 if (Kind != CXComment_Null) {
404 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
Dmitri Gribenko5ef6ea52012-07-20 22:00:35 +0000405 unsigned i;
406 for (i = 0; i != NumChildren; ++i) {
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000407 printf("\n// %s: ", FileCheckPrefix);
408 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
409 }
410 }
411 printf(")");
412 Ctx->IndentLevel--;
413}
414
415static void DumpCXComment(CXComment Comment) {
416 struct CommentASTDumpingContext Ctx;
417 Ctx.IndentLevel = 1;
418 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
419 DumpCXCommentInternal(&Ctx, Comment);
420 printf("]");
421}
422
423static void PrintCursorComments(CXCursor Cursor) {
424 {
425 CXString RawComment;
426 const char *RawCommentCString;
427 CXString BriefComment;
428 const char *BriefCommentCString;
429
430 RawComment = clang_Cursor_getRawCommentText(Cursor);
431 RawCommentCString = clang_getCString(RawComment);
432 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
433 PrintCStringWithPrefix("RawComment", RawCommentCString);
434 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
435
436 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
437 BriefCommentCString = clang_getCString(BriefComment);
438 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
439 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
440 clang_disposeString(BriefComment);
441 }
442 clang_disposeString(RawComment);
443 }
444
445 {
446 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
447 if (clang_Comment_getKind(Comment) != CXComment_Null) {
448 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
449 clang_FullComment_getAsHTML(Comment));
450 DumpCXComment(Comment);
451 }
452 }
453}
454
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000455static void PrintCursor(CXCursor Cursor) {
456 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000457 if (clang_isInvalid(Cursor.kind)) {
458 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
459 printf("Invalid Cursor => %s", clang_getCString(ks));
460 clang_disposeString(ks);
461 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000462 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000463 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000464 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000465 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000466 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000467 CXCursor *overridden;
468 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000469 unsigned RefNameRangeNr;
470 CXSourceRange CursorExtent;
471 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000472 int AlwaysUnavailable;
473 int AlwaysDeprecated;
474 CXString UnavailableMessage;
475 CXString DeprecatedMessage;
476 CXPlatformAvailability PlatformAvailability[2];
477 int NumPlatformAvailability;
478 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000479
Ted Kremeneke68fff62010-02-17 00:41:32 +0000480 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000481 string = want_display_name? clang_getCursorDisplayName(Cursor)
482 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000483 printf("%s=%s", clang_getCString(ks),
484 clang_getCString(string));
485 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000486 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000487
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000488 Referenced = clang_getCursorReferenced(Cursor);
489 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000490 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
491 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
492 printf("[");
493 for (I = 0; I != N; ++I) {
494 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000495 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000496 if (I)
497 printf(", ");
498
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000499 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000500 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000501 printf("%d:%d", line, column);
502 }
503 printf("]");
504 } else {
505 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000506 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000507 printf(":%d:%d", line, column);
508 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000509 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000510
511 if (clang_isCursorDefinition(Cursor))
512 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000513
514 switch (clang_getCursorAvailability(Cursor)) {
515 case CXAvailability_Available:
516 break;
517
518 case CXAvailability_Deprecated:
519 printf(" (deprecated)");
520 break;
521
522 case CXAvailability_NotAvailable:
523 printf(" (unavailable)");
524 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000525
526 case CXAvailability_NotAccessible:
527 printf(" (inaccessible)");
528 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000529 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000530
Douglas Gregorcc889662012-05-08 00:14:45 +0000531 NumPlatformAvailability
532 = clang_getCursorPlatformAvailability(Cursor,
533 &AlwaysDeprecated,
534 &DeprecatedMessage,
535 &AlwaysUnavailable,
536 &UnavailableMessage,
537 PlatformAvailability, 2);
538 if (AlwaysUnavailable) {
539 printf(" (always unavailable: \"%s\")",
540 clang_getCString(UnavailableMessage));
541 } else if (AlwaysDeprecated) {
542 printf(" (always deprecated: \"%s\")",
543 clang_getCString(DeprecatedMessage));
544 } else {
545 for (I = 0; I != NumPlatformAvailability; ++I) {
546 if (I >= 2)
547 break;
548
549 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
550 if (PlatformAvailability[I].Unavailable)
551 printf(", unavailable");
552 else {
553 printVersion(", introduced=", PlatformAvailability[I].Introduced);
554 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
555 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
556 }
557 if (clang_getCString(PlatformAvailability[I].Message)[0])
558 printf(", message=\"%s\"",
559 clang_getCString(PlatformAvailability[I].Message));
560 printf(")");
561 }
562 }
563 for (I = 0; I != NumPlatformAvailability; ++I) {
564 if (I >= 2)
565 break;
566 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
567 }
568
569 clang_disposeString(DeprecatedMessage);
570 clang_disposeString(UnavailableMessage);
571
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000572 if (clang_CXXMethod_isStatic(Cursor))
573 printf(" (static)");
574 if (clang_CXXMethod_isVirtual(Cursor))
575 printf(" (virtual)");
576
Ted Kremenek95f33552010-08-26 01:42:22 +0000577 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
578 CXType T =
579 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
580 CXString S = clang_getTypeKindSpelling(T.kind);
581 printf(" [IBOutletCollection=%s]", clang_getCString(S));
582 clang_disposeString(S);
583 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000584
585 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
586 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
587 unsigned isVirtual = clang_isVirtualBase(Cursor);
588 const char *accessStr = 0;
589
590 switch (access) {
591 case CX_CXXInvalidAccessSpecifier:
592 accessStr = "invalid"; break;
593 case CX_CXXPublic:
594 accessStr = "public"; break;
595 case CX_CXXProtected:
596 accessStr = "protected"; break;
597 case CX_CXXPrivate:
598 accessStr = "private"; break;
599 }
600
601 printf(" [access=%s isVirtual=%s]", accessStr,
602 isVirtual ? "true" : "false");
603 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000604
605 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
606 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
607 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
608 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000609 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000610 printf(" [Specialization of %s:%d:%d]",
611 clang_getCString(Name), line, column);
612 clang_disposeString(Name);
613 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000614
615 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
616 if (num_overridden) {
617 unsigned I;
618 printf(" [Overrides ");
619 for (I = 0; I != num_overridden; ++I) {
620 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000621 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000622 if (I)
623 printf(", ");
624 printf("@%d:%d", line, column);
625 }
626 printf("]");
627 clang_disposeOverriddenCursors(overridden);
628 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000629
630 if (Cursor.kind == CXCursor_InclusionDirective) {
631 CXFile File = clang_getIncludedFile(Cursor);
632 CXString Included = clang_getFileName(File);
633 printf(" (%s)", clang_getCString(Included));
634 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000635
636 if (clang_isFileMultipleIncludeGuarded(TU, File))
637 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000638 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000639
640 CursorExtent = clang_getCursorExtent(Cursor);
641 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
642 CXNameRange_WantQualifier
643 | CXNameRange_WantSinglePiece
644 | CXNameRange_WantTemplateArgs,
645 0);
646 if (!clang_equalRanges(CursorExtent, RefNameRange))
647 PrintRange(RefNameRange, "SingleRefName");
648
649 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
650 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
651 CXNameRange_WantQualifier
652 | CXNameRange_WantTemplateArgs,
653 RefNameRangeNr);
654 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
655 break;
656 if (!clang_equalRanges(CursorExtent, RefNameRange))
657 PrintRange(RefNameRange, "RefName");
658 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000659
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000660 PrintCursorComments(Cursor);
Steve Naroff699a07d2009-09-25 21:32:34 +0000661 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000662}
Steve Naroff89922f82009-08-31 00:59:03 +0000663
Ted Kremeneke68fff62010-02-17 00:41:32 +0000664static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000665 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000666 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000667 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000668 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000669 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000670 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000671 clang_disposeString(source);
672 return "<invalid loc>";
673 }
674 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000675 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000676 clang_disposeString(source);
677 return b;
678 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000679}
680
Ted Kremenek0d435192009-11-17 18:13:31 +0000681/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000682/* Callbacks. */
683/******************************************************************************/
684
685typedef void (*PostVisitTU)(CXTranslationUnit);
686
Douglas Gregora88084b2010-02-18 18:08:43 +0000687void PrintDiagnostic(CXDiagnostic Diagnostic) {
688 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000689 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000690 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000691 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000692 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
693 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000694 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000695
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000696 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000697 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000698
Douglas Gregor274f1902010-02-22 23:17:23 +0000699 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
700 fprintf(stderr, "%s\n", clang_getCString(Msg));
701 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000702
Douglas Gregora9b06d42010-11-09 06:24:54 +0000703 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
704 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000705 if (!file)
706 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000707
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000708 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000709 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000710 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000711 CXSourceRange range;
712 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
713 CXSourceLocation start = clang_getRangeStart(range);
714 CXSourceLocation end = clang_getRangeEnd(range);
715 unsigned start_line, start_column, end_line, end_column;
716 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000717 clang_getSpellingLocation(start, &start_file, &start_line,
718 &start_column, 0);
719 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000720 if (clang_equalLocations(start, end)) {
721 /* Insertion. */
722 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000723 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000724 clang_getCString(insertion_text), start_line, start_column);
725 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
726 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000727 if (start_file == file && end_file == file) {
728 fprintf(out, "FIX-IT: Remove ");
729 PrintExtent(out, start_line, start_column, end_line, end_column);
730 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000731 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000732 } else {
733 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000734 if (start_file == end_file) {
735 fprintf(out, "FIX-IT: Replace ");
736 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000737 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000738 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000739 break;
740 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000741 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000742 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000743}
744
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000745void PrintDiagnosticSet(CXDiagnosticSet Set) {
746 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
747 for ( ; i != n ; ++i) {
748 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
749 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000750 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000751 if (ChildDiags)
752 PrintDiagnosticSet(ChildDiags);
753 }
754}
755
756void PrintDiagnostics(CXTranslationUnit TU) {
757 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
758 PrintDiagnosticSet(TUSet);
759 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000760}
761
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000762void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000763 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000764 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000765 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000766 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000767 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000768 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000769 unsigned long amount = usage.entries[i].amount;
770 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000771 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000772 ((double) amount)/(1024*1024));
773 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000774 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000775 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000776 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000777}
778
Ted Kremenekce2ae882010-01-26 17:59:48 +0000779/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000780/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000781/******************************************************************************/
782
Douglas Gregora7bde202010-01-19 00:34:46 +0000783static void PrintCursorExtent(CXCursor C) {
784 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000785 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000786}
787
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000788/* Data used by all of the visitors. */
789typedef struct {
790 CXTranslationUnit TU;
791 enum CXCursorKind *Filter;
792} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000793
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000794
Ted Kremeneke68fff62010-02-17 00:41:32 +0000795enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000796 CXCursor Parent,
797 CXClientData ClientData) {
798 VisitorData *Data = (VisitorData *)ClientData;
799 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000800 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000801 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000802 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000803 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000804 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000805 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000806 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000807 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000808 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000809 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000810
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000811 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000812}
Steve Naroff50398192009-08-28 15:28:48 +0000813
Ted Kremeneke68fff62010-02-17 00:41:32 +0000814static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000815 CXCursor Parent,
816 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000817 const char *startBuf, *endBuf;
818 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
819 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000820 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000821
Douglas Gregorb6998662010-01-19 19:34:47 +0000822 if (Cursor.kind != CXCursor_FunctionDecl ||
823 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000824 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000825
826 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
827 &startLine, &startColumn,
828 &endLine, &endColumn);
829 /* Probe the entire body, looking for both decls and refs. */
830 curLine = startLine;
831 curColumn = startColumn;
832
833 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000834 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000835 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000836 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000837
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000838 if (*startBuf == '\n') {
839 startBuf++;
840 curLine++;
841 curColumn = 1;
842 } else if (*startBuf != '\t')
843 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000844
Douglas Gregor98258af2010-01-18 22:46:11 +0000845 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000846 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000847
Douglas Gregor1db19de2010-01-19 21:36:55 +0000848 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000849 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000850 CXSourceLocation RefLoc
851 = clang_getLocation(Data->TU, file, curLine, curColumn);
852 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000853 if (Ref.kind == CXCursor_NoDeclFound) {
854 /* Nothing found here; that's fine. */
855 } else if (Ref.kind != CXCursor_FunctionDecl) {
856 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
857 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000858 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000859 printf("\n");
860 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000861 }
Ted Kremenek74844072010-02-17 00:41:20 +0000862 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000863 startBuf++;
864 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000865
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000866 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000867}
868
Ted Kremenek7d405622010-01-12 23:34:26 +0000869/******************************************************************************/
870/* USR testing. */
871/******************************************************************************/
872
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000873enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
874 CXClientData ClientData) {
875 VisitorData *Data = (VisitorData *)ClientData;
876 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000877 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000878 const char *cstr = clang_getCString(USR);
879 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000880 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000881 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000882 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000883 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
884
Douglas Gregora7bde202010-01-19 00:34:46 +0000885 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000886 printf("\n");
887 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000888
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000889 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000890 }
891
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000892 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000893}
894
895/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000896/* Inclusion stack testing. */
897/******************************************************************************/
898
899void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
900 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000901
Ted Kremenek16b55a72010-01-26 19:31:51 +0000902 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000903 CXString fname;
904
905 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000906 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000907 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000908
Ted Kremenek16b55a72010-01-26 19:31:51 +0000909 for (i = 0; i < includeStackLen; ++i) {
910 CXFile includingFile;
911 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000912 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
913 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000914 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000915 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000916 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000917 }
918 printf("\n");
919}
920
921void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000922 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000923}
924
925/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000926/* Linkage testing. */
927/******************************************************************************/
928
929static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
930 CXClientData d) {
931 const char *linkage = 0;
932
933 if (clang_isInvalid(clang_getCursorKind(cursor)))
934 return CXChildVisit_Recurse;
935
936 switch (clang_getCursorLinkage(cursor)) {
937 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000938 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
939 case CXLinkage_Internal: linkage = "Internal"; break;
940 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
941 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000942 }
943
944 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000945 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000946 printf("linkage=%s\n", linkage);
947 }
948
949 return CXChildVisit_Recurse;
950}
951
952/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000953/* Typekind testing. */
954/******************************************************************************/
955
956static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
957 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000958 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
959 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000960 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000961 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000962 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000963 if (clang_isConstQualifiedType(T))
964 printf(" const");
965 if (clang_isVolatileQualifiedType(T))
966 printf(" volatile");
967 if (clang_isRestrictQualifiedType(T))
968 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000969 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000970 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000971 {
972 CXType CT = clang_getCanonicalType(T);
973 if (!clang_equalTypes(T, CT)) {
974 CXString CS = clang_getTypeKindSpelling(CT.kind);
975 printf(" [canonical=%s]", clang_getCString(CS));
976 clang_disposeString(CS);
977 }
978 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000979 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000980 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000981 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000982 if (RT.kind != CXType_Invalid) {
983 CXString RS = clang_getTypeKindSpelling(RT.kind);
984 printf(" [result=%s]", clang_getCString(RS));
985 clang_disposeString(RS);
986 }
987 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000988 /* Print the argument types if they exist. */
989 {
990 int numArgs = clang_Cursor_getNumArguments(cursor);
991 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000992 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000993 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000994 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000995 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
996 if (T.kind != CXType_Invalid) {
997 CXString S = clang_getTypeKindSpelling(T.kind);
998 printf(" %s", clang_getCString(S));
999 clang_disposeString(S);
1000 }
1001 }
1002 printf("]");
1003 }
1004 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +00001005 /* Print if this is a non-POD type. */
1006 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +00001007
Ted Kremenek8e0ac172010-05-14 21:29:26 +00001008 printf("\n");
1009 }
1010 return CXChildVisit_Recurse;
1011}
1012
1013
1014/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +00001015/* Loading ASTs/source. */
1016/******************************************************************************/
1017
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001018static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +00001019 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001020 CXCursorVisitor Visitor,
1021 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001022
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001023 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001024 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001025
1026 if (Visitor) {
1027 enum CXCursorKind K = CXCursor_NotImplemented;
1028 enum CXCursorKind *ck = &K;
1029 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001030
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001031 /* Perform some simple filtering. */
1032 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001033 else if (!strcmp(filter, "all-display") ||
1034 !strcmp(filter, "local-display")) {
1035 ck = NULL;
1036 want_display_name = 1;
1037 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001038 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001039 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1040 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1041 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1042 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1043 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1044 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1045 else {
1046 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1047 return 1;
1048 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001049
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001050 Data.TU = TU;
1051 Data.Filter = ck;
1052 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001053 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001054
Ted Kremenekce2ae882010-01-26 17:59:48 +00001055 if (PV)
1056 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001057
Douglas Gregora88084b2010-02-18 18:08:43 +00001058 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001059 if (checkForErrors(TU) != 0) {
1060 clang_disposeTranslationUnit(TU);
1061 return -1;
1062 }
1063
Ted Kremenek0d435192009-11-17 18:13:31 +00001064 clang_disposeTranslationUnit(TU);
1065 return 0;
1066}
1067
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001068int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001069 const char *prefix, CXCursorVisitor Visitor,
1070 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001071 CXIndex Idx;
1072 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001073 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001074 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001075 !strcmp(filter, "local") ? 1 : 0,
1076 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001077
Ted Kremenek020a0952010-02-11 07:41:25 +00001078 if (!CreateTranslationUnit(Idx, file, &TU)) {
1079 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001080 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001081 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001082
Ted Kremenek020a0952010-02-11 07:41:25 +00001083 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
1084 clang_disposeIndex(Idx);
1085 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001086}
1087
Ted Kremenekce2ae882010-01-26 17:59:48 +00001088int perform_test_load_source(int argc, const char **argv,
1089 const char *filter, CXCursorVisitor Visitor,
1090 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001091 CXIndex Idx;
1092 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001093 struct CXUnsavedFile *unsaved_files = 0;
1094 int num_unsaved_files = 0;
1095 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001096
Daniel Dunbarada487d2009-12-01 02:03:10 +00001097 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001098 (!strcmp(filter, "local") ||
1099 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +00001100 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001101
Ted Kremenek020a0952010-02-11 07:41:25 +00001102 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1103 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001104 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001105 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001106
Douglas Gregordca8ee82011-05-06 16:33:08 +00001107 TU = clang_parseTranslationUnit(Idx, 0,
1108 argv + num_unsaved_files,
1109 argc - num_unsaved_files,
1110 unsaved_files, num_unsaved_files,
1111 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001112 if (!TU) {
1113 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001114 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001115 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001116 return 1;
1117 }
1118
Ted Kremenekce2ae882010-01-26 17:59:48 +00001119 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001120 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001121 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001122 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001123}
1124
Douglas Gregorabc563f2010-07-19 21:46:24 +00001125int perform_test_reparse_source(int argc, const char **argv, int trials,
1126 const char *filter, CXCursorVisitor Visitor,
1127 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001128 CXIndex Idx;
1129 CXTranslationUnit TU;
1130 struct CXUnsavedFile *unsaved_files = 0;
1131 int num_unsaved_files = 0;
1132 int result;
1133 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001134 int remap_after_trial = 0;
1135 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001136
1137 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1138 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +00001139 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001140
Douglas Gregorabc563f2010-07-19 21:46:24 +00001141 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1142 clang_disposeIndex(Idx);
1143 return -1;
1144 }
1145
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001146 /* Load the initial translation unit -- we do this without honoring remapped
1147 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001148 TU = clang_parseTranslationUnit(Idx, 0,
1149 argv + num_unsaved_files,
1150 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001151 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001152 if (!TU) {
1153 fprintf(stderr, "Unable to load translation unit!\n");
1154 free_remapped_files(unsaved_files, num_unsaved_files);
1155 clang_disposeIndex(Idx);
1156 return 1;
1157 }
1158
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001159 if (checkForErrors(TU) != 0)
1160 return -1;
1161
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001162 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1163 remap_after_trial =
1164 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1165 }
1166
Douglas Gregorabc563f2010-07-19 21:46:24 +00001167 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001168 if (clang_reparseTranslationUnit(TU,
1169 trial >= remap_after_trial ? num_unsaved_files : 0,
1170 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001171 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001172 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001173 clang_disposeTranslationUnit(TU);
1174 free_remapped_files(unsaved_files, num_unsaved_files);
1175 clang_disposeIndex(Idx);
1176 return -1;
1177 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001178
1179 if (checkForErrors(TU) != 0)
1180 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001181 }
1182
1183 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001184
Douglas Gregorabc563f2010-07-19 21:46:24 +00001185 free_remapped_files(unsaved_files, num_unsaved_files);
1186 clang_disposeIndex(Idx);
1187 return result;
1188}
1189
Ted Kremenek0d435192009-11-17 18:13:31 +00001190/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001191/* Logic for testing clang_getCursor(). */
1192/******************************************************************************/
1193
Douglas Gregordd3e5542011-05-04 00:14:37 +00001194static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001195 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001196 unsigned end_line, unsigned end_col,
1197 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001198 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001199 if (prefix)
1200 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001201 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1202 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001203 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001204 printf("\n");
1205}
1206
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001207static int perform_file_scan(const char *ast_file, const char *source_file,
1208 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001209 CXIndex Idx;
1210 CXTranslationUnit TU;
1211 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001212 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001213 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001214 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001215 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001216
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001217 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1218 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001219 fprintf(stderr, "Could not create Index\n");
1220 return 1;
1221 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001222
Ted Kremenek1c6da172009-11-17 19:37:36 +00001223 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1224 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001225
Ted Kremenek1c6da172009-11-17 19:37:36 +00001226 if ((fp = fopen(source_file, "r")) == NULL) {
1227 fprintf(stderr, "Could not open '%s'\n", source_file);
1228 return 1;
1229 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001230
Douglas Gregorb9790342010-01-22 21:44:22 +00001231 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001232 for (;;) {
1233 CXCursor cursor;
1234 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001235
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001236 if (c == '\n') {
1237 ++line;
1238 col = 1;
1239 } else
1240 ++col;
1241
1242 /* Check the cursor at this position, and dump the previous one if we have
1243 * found something new.
1244 */
1245 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1246 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1247 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001248 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001249 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001250 start_line = line;
1251 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001252 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001253 if (c == EOF)
1254 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001255
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001256 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001257 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001258
Ted Kremenek1c6da172009-11-17 19:37:36 +00001259 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001260 clang_disposeTranslationUnit(TU);
1261 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001262 return 0;
1263}
1264
1265/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001266/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001267/******************************************************************************/
1268
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001269/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1270 on failure. If successful, the pointer *filename will contain newly-allocated
1271 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001272int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001273 unsigned *column, unsigned *second_line,
1274 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001275 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001276 const char *last_colon = strrchr(input, ':');
1277 unsigned values[4], i;
1278 unsigned num_values = (second_line && second_column)? 4 : 2;
1279
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001280 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001281 if (!last_colon || last_colon == input) {
1282 if (num_values == 4)
1283 fprintf(stderr, "could not parse filename:line:column:line:column in "
1284 "'%s'\n", input);
1285 else
1286 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001287 return 1;
1288 }
1289
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001290 for (i = 0; i != num_values; ++i) {
1291 const char *prev_colon;
1292
1293 /* Parse the next line or column. */
1294 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1295 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001296 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001297 (i % 2 ? "column" : "line"), input);
1298 return 1;
1299 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001300
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001301 if (i + 1 == num_values)
1302 break;
1303
1304 /* Find the previous colon. */
1305 prev_colon = last_colon - 1;
1306 while (prev_colon != input && *prev_colon != ':')
1307 --prev_colon;
1308 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001309 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001310 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001311 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001312 }
1313
1314 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001315 }
1316
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001317 *line = values[0];
1318 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001319
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001320 if (second_line && second_column) {
1321 *second_line = values[2];
1322 *second_column = values[3];
1323 }
1324
Douglas Gregor88d23952009-11-09 18:19:57 +00001325 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001326 *filename = (char*)malloc(last_colon - input + 1);
1327 memcpy(*filename, input, last_colon - input);
1328 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001329 return 0;
1330}
1331
1332const char *
1333clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1334 switch (Kind) {
1335 case CXCompletionChunk_Optional: return "Optional";
1336 case CXCompletionChunk_TypedText: return "TypedText";
1337 case CXCompletionChunk_Text: return "Text";
1338 case CXCompletionChunk_Placeholder: return "Placeholder";
1339 case CXCompletionChunk_Informative: return "Informative";
1340 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1341 case CXCompletionChunk_LeftParen: return "LeftParen";
1342 case CXCompletionChunk_RightParen: return "RightParen";
1343 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1344 case CXCompletionChunk_RightBracket: return "RightBracket";
1345 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1346 case CXCompletionChunk_RightBrace: return "RightBrace";
1347 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1348 case CXCompletionChunk_RightAngle: return "RightAngle";
1349 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001350 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001351 case CXCompletionChunk_Colon: return "Colon";
1352 case CXCompletionChunk_SemiColon: return "SemiColon";
1353 case CXCompletionChunk_Equal: return "Equal";
1354 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1355 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001356 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001357
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001358 return "Unknown";
1359}
1360
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001361static int checkForErrors(CXTranslationUnit TU) {
1362 unsigned Num, i;
1363 CXDiagnostic Diag;
1364 CXString DiagStr;
1365
1366 if (!getenv("CINDEXTEST_FAILONERROR"))
1367 return 0;
1368
1369 Num = clang_getNumDiagnostics(TU);
1370 for (i = 0; i != Num; ++i) {
1371 Diag = clang_getDiagnostic(TU, i);
1372 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1373 DiagStr = clang_formatDiagnostic(Diag,
1374 clang_defaultDiagnosticDisplayOptions());
1375 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1376 clang_disposeString(DiagStr);
1377 clang_disposeDiagnostic(Diag);
1378 return -1;
1379 }
1380 clang_disposeDiagnostic(Diag);
1381 }
1382
1383 return 0;
1384}
1385
Douglas Gregor3ac73852009-11-09 16:04:45 +00001386void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001387 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001388
Douglas Gregor3ac73852009-11-09 16:04:45 +00001389 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001390 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001391 CXString text;
1392 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001393 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001394 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001395
Douglas Gregor3ac73852009-11-09 16:04:45 +00001396 if (Kind == CXCompletionChunk_Optional) {
1397 fprintf(file, "{Optional ");
1398 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001399 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001400 file);
1401 fprintf(file, "}");
1402 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001403 }
1404
1405 if (Kind == CXCompletionChunk_VerticalSpace) {
1406 fprintf(file, "{VerticalSpace }");
1407 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001408 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001409
Douglas Gregord5a20892009-11-09 17:05:28 +00001410 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001411 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001412 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001413 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001414 cstr ? cstr : "");
1415 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001416 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001417
Douglas Gregor3ac73852009-11-09 16:04:45 +00001418}
1419
1420void print_completion_result(CXCompletionResult *completion_result,
1421 CXClientData client_data) {
1422 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001423 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001424 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001425 enum CXCursorKind ParentKind;
1426 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001427 CXString BriefComment;
1428 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001429
Ted Kremeneke68fff62010-02-17 00:41:32 +00001430 fprintf(file, "%s:", clang_getCString(ks));
1431 clang_disposeString(ks);
1432
Douglas Gregor3ac73852009-11-09 16:04:45 +00001433 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001434 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001435 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001436 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1437 case CXAvailability_Available:
1438 break;
1439
1440 case CXAvailability_Deprecated:
1441 fprintf(file, " (deprecated)");
1442 break;
1443
1444 case CXAvailability_NotAvailable:
1445 fprintf(file, " (unavailable)");
1446 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001447
1448 case CXAvailability_NotAccessible:
1449 fprintf(file, " (inaccessible)");
1450 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001451 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001452
1453 annotationCount = clang_getCompletionNumAnnotations(
1454 completion_result->CompletionString);
1455 if (annotationCount) {
1456 unsigned i;
1457 fprintf(file, " (");
1458 for (i = 0; i < annotationCount; ++i) {
1459 if (i != 0)
1460 fprintf(file, ", ");
1461 fprintf(file, "\"%s\"",
1462 clang_getCString(clang_getCompletionAnnotation(
1463 completion_result->CompletionString, i)));
1464 }
1465 fprintf(file, ")");
1466 }
1467
Douglas Gregorba103062012-03-27 23:34:16 +00001468 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1469 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1470 &ParentKind);
1471 if (ParentKind != CXCursor_NotImplemented) {
1472 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1473 fprintf(file, " (parent: %s '%s')",
1474 clang_getCString(KindSpelling),
1475 clang_getCString(ParentName));
1476 clang_disposeString(KindSpelling);
1477 }
1478 clang_disposeString(ParentName);
1479 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001480
1481 BriefComment = clang_getCompletionBriefComment(
1482 completion_result->CompletionString);
1483 BriefCommentCString = clang_getCString(BriefComment);
1484 if (BriefCommentCString && *BriefCommentCString != '\0') {
1485 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1486 }
1487 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001488
Douglas Gregor58ddb602010-08-23 23:00:57 +00001489 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001490}
1491
Douglas Gregor3da626b2011-07-07 16:03:39 +00001492void print_completion_contexts(unsigned long long contexts, FILE *file) {
1493 fprintf(file, "Completion contexts:\n");
1494 if (contexts == CXCompletionContext_Unknown) {
1495 fprintf(file, "Unknown\n");
1496 }
1497 if (contexts & CXCompletionContext_AnyType) {
1498 fprintf(file, "Any type\n");
1499 }
1500 if (contexts & CXCompletionContext_AnyValue) {
1501 fprintf(file, "Any value\n");
1502 }
1503 if (contexts & CXCompletionContext_ObjCObjectValue) {
1504 fprintf(file, "Objective-C object value\n");
1505 }
1506 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1507 fprintf(file, "Objective-C selector value\n");
1508 }
1509 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1510 fprintf(file, "C++ class type value\n");
1511 }
1512 if (contexts & CXCompletionContext_DotMemberAccess) {
1513 fprintf(file, "Dot member access\n");
1514 }
1515 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1516 fprintf(file, "Arrow member access\n");
1517 }
1518 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1519 fprintf(file, "Objective-C property access\n");
1520 }
1521 if (contexts & CXCompletionContext_EnumTag) {
1522 fprintf(file, "Enum tag\n");
1523 }
1524 if (contexts & CXCompletionContext_UnionTag) {
1525 fprintf(file, "Union tag\n");
1526 }
1527 if (contexts & CXCompletionContext_StructTag) {
1528 fprintf(file, "Struct tag\n");
1529 }
1530 if (contexts & CXCompletionContext_ClassTag) {
1531 fprintf(file, "Class name\n");
1532 }
1533 if (contexts & CXCompletionContext_Namespace) {
1534 fprintf(file, "Namespace or namespace alias\n");
1535 }
1536 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1537 fprintf(file, "Nested name specifier\n");
1538 }
1539 if (contexts & CXCompletionContext_ObjCInterface) {
1540 fprintf(file, "Objective-C interface\n");
1541 }
1542 if (contexts & CXCompletionContext_ObjCProtocol) {
1543 fprintf(file, "Objective-C protocol\n");
1544 }
1545 if (contexts & CXCompletionContext_ObjCCategory) {
1546 fprintf(file, "Objective-C category\n");
1547 }
1548 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1549 fprintf(file, "Objective-C instance method\n");
1550 }
1551 if (contexts & CXCompletionContext_ObjCClassMessage) {
1552 fprintf(file, "Objective-C class method\n");
1553 }
1554 if (contexts & CXCompletionContext_ObjCSelectorName) {
1555 fprintf(file, "Objective-C selector name\n");
1556 }
1557 if (contexts & CXCompletionContext_MacroName) {
1558 fprintf(file, "Macro name\n");
1559 }
1560 if (contexts & CXCompletionContext_NaturalLanguage) {
1561 fprintf(file, "Natural language\n");
1562 }
1563}
1564
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001565int my_stricmp(const char *s1, const char *s2) {
1566 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001567 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001568 if (c1 < c2)
1569 return -1;
1570 else if (c1 > c2)
1571 return 1;
1572
1573 ++s1;
1574 ++s2;
1575 }
1576
1577 if (*s1)
1578 return 1;
1579 else if (*s2)
1580 return -1;
1581 return 0;
1582}
1583
Douglas Gregor1982c182010-07-12 18:38:41 +00001584int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001585 const char *input = argv[1];
1586 char *filename = 0;
1587 unsigned line;
1588 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001589 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001590 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001591 struct CXUnsavedFile *unsaved_files = 0;
1592 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001593 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001594 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001595 unsigned I, Repeats = 1;
1596 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1597
1598 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1599 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001600 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1601 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001602
Douglas Gregor1982c182010-07-12 18:38:41 +00001603 if (timing_only)
1604 input += strlen("-code-completion-timing=");
1605 else
1606 input += strlen("-code-completion-at=");
1607
Ted Kremeneke68fff62010-02-17 00:41:32 +00001608 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001609 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001610 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001611
Douglas Gregor735df882009-12-02 09:21:34 +00001612 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1613 return -1;
1614
Douglas Gregor32be4a52010-10-11 21:37:58 +00001615 CIdx = clang_createIndex(0, 0);
1616
1617 if (getenv("CINDEXTEST_EDITING"))
1618 Repeats = 5;
1619
1620 TU = clang_parseTranslationUnit(CIdx, 0,
1621 argv + num_unsaved_files + 2,
1622 argc - num_unsaved_files - 2,
1623 0, 0, getDefaultParsingOptions());
1624 if (!TU) {
1625 fprintf(stderr, "Unable to load translation unit!\n");
1626 return 1;
1627 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001628
1629 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1630 fprintf(stderr, "Unable to reparse translation init!\n");
1631 return 1;
1632 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001633
1634 for (I = 0; I != Repeats; ++I) {
1635 results = clang_codeCompleteAt(TU, filename, line, column,
1636 unsaved_files, num_unsaved_files,
1637 completionOptions);
1638 if (!results) {
1639 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001640 return 1;
1641 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001642 if (I != Repeats-1)
1643 clang_disposeCodeCompleteResults(results);
1644 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001645
Douglas Gregorec6762c2009-12-18 16:20:58 +00001646 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001647 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001648 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001649 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001650 CXString objCSelector;
1651 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001652 if (!timing_only) {
1653 /* Sort the code-completion results based on the typed text. */
1654 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1655
Douglas Gregor1982c182010-07-12 18:38:41 +00001656 for (i = 0; i != n; ++i)
1657 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001658 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001659 n = clang_codeCompleteGetNumDiagnostics(results);
1660 for (i = 0; i != n; ++i) {
1661 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1662 PrintDiagnostic(diag);
1663 clang_disposeDiagnostic(diag);
1664 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001665
1666 contexts = clang_codeCompleteGetContexts(results);
1667 print_completion_contexts(contexts, stdout);
1668
Douglas Gregor0a47d692011-07-26 15:24:30 +00001669 containerKind = clang_codeCompleteGetContainerKind(results,
1670 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001671
1672 if (containerKind != CXCursor_InvalidCode) {
1673 /* We have found a container */
1674 CXString containerUSR, containerKindSpelling;
1675 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1676 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1677 clang_disposeString(containerKindSpelling);
1678
1679 if (containerIsIncomplete) {
1680 printf("Container is incomplete\n");
1681 }
1682 else {
1683 printf("Container is complete\n");
1684 }
1685
1686 containerUSR = clang_codeCompleteGetContainerUSR(results);
1687 printf("Container USR: %s\n", clang_getCString(containerUSR));
1688 clang_disposeString(containerUSR);
1689 }
1690
Douglas Gregor0a47d692011-07-26 15:24:30 +00001691 objCSelector = clang_codeCompleteGetObjCSelector(results);
1692 selectorString = clang_getCString(objCSelector);
1693 if (selectorString && strlen(selectorString) > 0) {
1694 printf("Objective-C selector: %s\n", selectorString);
1695 }
1696 clang_disposeString(objCSelector);
1697
Douglas Gregorec6762c2009-12-18 16:20:58 +00001698 clang_disposeCodeCompleteResults(results);
1699 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001700 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001701 clang_disposeIndex(CIdx);
1702 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001703
Douglas Gregor735df882009-12-02 09:21:34 +00001704 free_remapped_files(unsaved_files, num_unsaved_files);
1705
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001706 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001707}
1708
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001709typedef struct {
1710 char *filename;
1711 unsigned line;
1712 unsigned column;
1713} CursorSourceLocation;
1714
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001715static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001716 CXIndex CIdx;
1717 int errorCode;
1718 struct CXUnsavedFile *unsaved_files = 0;
1719 int num_unsaved_files = 0;
1720 CXTranslationUnit TU;
1721 CXCursor Cursor;
1722 CursorSourceLocation *Locations = 0;
1723 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001724 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001725 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001726
Ted Kremeneke68fff62010-02-17 00:41:32 +00001727 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001728 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1729 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001730
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001731 /* Parse the locations. */
1732 assert(NumLocations > 0 && "Unable to count locations?");
1733 Locations = (CursorSourceLocation *)malloc(
1734 NumLocations * sizeof(CursorSourceLocation));
1735 for (Loc = 0; Loc < NumLocations; ++Loc) {
1736 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001737 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1738 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001739 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001740 return errorCode;
1741 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001742
1743 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001744 &num_unsaved_files))
1745 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001746
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001747 if (getenv("CINDEXTEST_EDITING"))
1748 Repeats = 5;
1749
1750 /* Parse the translation unit. When we're testing clang_getCursor() after
1751 reparsing, don't remap unsaved files until the second parse. */
1752 CIdx = clang_createIndex(1, 1);
1753 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1754 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001755 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001756 unsaved_files,
1757 Repeats > 1? 0 : num_unsaved_files,
1758 getDefaultParsingOptions());
1759
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001760 if (!TU) {
1761 fprintf(stderr, "unable to parse input\n");
1762 return -1;
1763 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001764
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001765 if (checkForErrors(TU) != 0)
1766 return -1;
1767
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001768 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001769 if (Repeats > 1 &&
1770 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1771 clang_defaultReparseOptions(TU))) {
1772 clang_disposeTranslationUnit(TU);
1773 return 1;
1774 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001775
1776 if (checkForErrors(TU) != 0)
1777 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001778
1779 for (Loc = 0; Loc < NumLocations; ++Loc) {
1780 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1781 if (!file)
1782 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001783
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001784 Cursor = clang_getCursor(TU,
1785 clang_getLocation(TU, file, Locations[Loc].line,
1786 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001787
1788 if (checkForErrors(TU) != 0)
1789 return -1;
1790
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001791 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001792 CXCompletionString completionString = clang_getCursorCompletionString(
1793 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001794 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1795 CXString Spelling;
1796 const char *cspell;
1797 unsigned line, column;
1798 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1799 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001800 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001801 PrintCursorExtent(Cursor);
1802 Spelling = clang_getCursorSpelling(Cursor);
1803 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001804 if (cspell && strlen(cspell) != 0) {
1805 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001806 printf(" Spelling=%s (", cspell);
1807 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001808 CXSourceRange range =
1809 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001810 if (clang_Range_isNull(range))
1811 break;
1812 PrintRange(range, 0);
1813 }
1814 printf(")");
1815 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001816 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001817 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1818 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001819 if (clang_Cursor_isDynamicCall(Cursor))
1820 printf(" Dynamic-call");
1821
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001822 if (completionString != NULL) {
1823 printf("\nCompletion string: ");
1824 print_completion_string(completionString, stdout);
1825 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001826 printf("\n");
1827 free(Locations[Loc].filename);
1828 }
1829 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001830 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001831
Douglas Gregora88084b2010-02-18 18:08:43 +00001832 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001833 clang_disposeTranslationUnit(TU);
1834 clang_disposeIndex(CIdx);
1835 free(Locations);
1836 free_remapped_files(unsaved_files, num_unsaved_files);
1837 return 0;
1838}
1839
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001840static enum CXVisitorResult findFileRefsVisit(void *context,
1841 CXCursor cursor, CXSourceRange range) {
1842 if (clang_Range_isNull(range))
1843 return CXVisit_Continue;
1844
1845 PrintCursor(cursor);
1846 PrintRange(range, "");
1847 printf("\n");
1848 return CXVisit_Continue;
1849}
1850
1851static int find_file_refs_at(int argc, const char **argv) {
1852 CXIndex CIdx;
1853 int errorCode;
1854 struct CXUnsavedFile *unsaved_files = 0;
1855 int num_unsaved_files = 0;
1856 CXTranslationUnit TU;
1857 CXCursor Cursor;
1858 CursorSourceLocation *Locations = 0;
1859 unsigned NumLocations = 0, Loc;
1860 unsigned Repeats = 1;
1861 unsigned I;
1862
1863 /* Count the number of locations. */
1864 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1865 ++NumLocations;
1866
1867 /* Parse the locations. */
1868 assert(NumLocations > 0 && "Unable to count locations?");
1869 Locations = (CursorSourceLocation *)malloc(
1870 NumLocations * sizeof(CursorSourceLocation));
1871 for (Loc = 0; Loc < NumLocations; ++Loc) {
1872 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1873 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1874 &Locations[Loc].line,
1875 &Locations[Loc].column, 0, 0)))
1876 return errorCode;
1877 }
1878
1879 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1880 &num_unsaved_files))
1881 return -1;
1882
1883 if (getenv("CINDEXTEST_EDITING"))
1884 Repeats = 5;
1885
1886 /* Parse the translation unit. When we're testing clang_getCursor() after
1887 reparsing, don't remap unsaved files until the second parse. */
1888 CIdx = clang_createIndex(1, 1);
1889 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1890 argv + num_unsaved_files + 1 + NumLocations,
1891 argc - num_unsaved_files - 2 - NumLocations,
1892 unsaved_files,
1893 Repeats > 1? 0 : num_unsaved_files,
1894 getDefaultParsingOptions());
1895
1896 if (!TU) {
1897 fprintf(stderr, "unable to parse input\n");
1898 return -1;
1899 }
1900
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001901 if (checkForErrors(TU) != 0)
1902 return -1;
1903
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001904 for (I = 0; I != Repeats; ++I) {
1905 if (Repeats > 1 &&
1906 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1907 clang_defaultReparseOptions(TU))) {
1908 clang_disposeTranslationUnit(TU);
1909 return 1;
1910 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001911
1912 if (checkForErrors(TU) != 0)
1913 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001914
1915 for (Loc = 0; Loc < NumLocations; ++Loc) {
1916 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1917 if (!file)
1918 continue;
1919
1920 Cursor = clang_getCursor(TU,
1921 clang_getLocation(TU, file, Locations[Loc].line,
1922 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001923
1924 if (checkForErrors(TU) != 0)
1925 return -1;
1926
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001927 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001928 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001929 PrintCursor(Cursor);
1930 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001931 clang_findReferencesInFile(Cursor, file, visitor);
1932 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001933
1934 if (checkForErrors(TU) != 0)
1935 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001936 }
1937 }
1938 }
1939
1940 PrintDiagnostics(TU);
1941 clang_disposeTranslationUnit(TU);
1942 clang_disposeIndex(CIdx);
1943 free(Locations);
1944 free_remapped_files(unsaved_files, num_unsaved_files);
1945 return 0;
1946}
1947
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001948typedef struct {
1949 const char *check_prefix;
1950 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001951 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001952 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001953 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001954} IndexData;
1955
1956static void printCheck(IndexData *data) {
1957 if (data->check_prefix) {
1958 if (data->first_check_printed) {
1959 printf("// %s-NEXT: ", data->check_prefix);
1960 } else {
1961 printf("// %s : ", data->check_prefix);
1962 data->first_check_printed = 1;
1963 }
1964 }
1965}
1966
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001967static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001968 CXString filename = clang_getFileName((CXFile)file);
1969 printf("%s", clang_getCString(filename));
1970 clang_disposeString(filename);
1971}
1972
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001973static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1974 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001975 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001976 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001977 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001978 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001979 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001980
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001981 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001982 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1983 if (line == 0) {
1984 printf("<null loc>");
1985 return;
1986 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001987 if (!file) {
1988 printf("<no idxfile>");
1989 return;
1990 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001991 filename = clang_getFileName((CXFile)file);
1992 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001993 if (strcmp(cname, index_data->main_filename) == 0)
1994 isMainFile = 1;
1995 else
1996 isMainFile = 0;
1997 clang_disposeString(filename);
1998
1999 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002000 printCXIndexFile(file);
2001 printf(":");
2002 }
2003 printf("%d:%d", line, column);
2004}
2005
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002006static unsigned digitCount(unsigned val) {
2007 unsigned c = 1;
2008 while (1) {
2009 if (val < 10)
2010 return c;
2011 ++c;
2012 val /= 10;
2013 }
2014}
2015
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002016static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
2017 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002018 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002019 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002020 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002021 unsigned line, column;
2022
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002023 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002024 if (!name)
2025 name = "<anon-tag>";
2026
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002027 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002028 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002029 newStr = (char *)malloc(strlen(name) +
2030 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002031 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002032 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002033}
2034
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002035static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2036 CXIdxClientContainer container;
2037 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002038 if (!container)
2039 printf("[<<NULL>>]");
2040 else
2041 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002042}
2043
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002044static const char *getEntityKindString(CXIdxEntityKind kind) {
2045 switch (kind) {
2046 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2047 case CXIdxEntity_Typedef: return "typedef";
2048 case CXIdxEntity_Function: return "function";
2049 case CXIdxEntity_Variable: return "variable";
2050 case CXIdxEntity_Field: return "field";
2051 case CXIdxEntity_EnumConstant: return "enumerator";
2052 case CXIdxEntity_ObjCClass: return "objc-class";
2053 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2054 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002055 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2056 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002057 case CXIdxEntity_ObjCProperty: return "objc-property";
2058 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2059 case CXIdxEntity_Enum: return "enum";
2060 case CXIdxEntity_Struct: return "struct";
2061 case CXIdxEntity_Union: return "union";
2062 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002063 case CXIdxEntity_CXXNamespace: return "namespace";
2064 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2065 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2066 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2067 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2068 case CXIdxEntity_CXXConstructor: return "constructor";
2069 case CXIdxEntity_CXXDestructor: return "destructor";
2070 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2071 case CXIdxEntity_CXXTypeAlias: return "type-alias";
2072 }
2073 assert(0 && "Garbage entity kind");
2074 return 0;
2075}
2076
2077static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2078 switch (kind) {
2079 case CXIdxEntity_NonTemplate: return "";
2080 case CXIdxEntity_Template: return "-template";
2081 case CXIdxEntity_TemplatePartialSpecialization:
2082 return "-template-partial-spec";
2083 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002084 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002085 assert(0 && "Garbage entity kind");
2086 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002087}
2088
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002089static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2090 switch (kind) {
2091 case CXIdxEntityLang_None: return "<none>";
2092 case CXIdxEntityLang_C: return "C";
2093 case CXIdxEntityLang_ObjC: return "ObjC";
2094 case CXIdxEntityLang_CXX: return "C++";
2095 }
2096 assert(0 && "Garbage language kind");
2097 return 0;
2098}
2099
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002100static void printEntityInfo(const char *cb,
2101 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002102 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002103 const char *name;
2104 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002105 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002106 index_data = (IndexData *)client_data;
2107 printCheck(index_data);
2108
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002109 if (!info) {
2110 printf("%s: <<NULL>>", cb);
2111 return;
2112 }
2113
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002114 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002115 if (!name)
2116 name = "<anon-tag>";
2117
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002118 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2119 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002120 printf(" | name: %s", name);
2121 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002122 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002123
2124 for (i = 0; i != info->numAttributes; ++i) {
2125 const CXIdxAttrInfo *Attr = info->attributes[i];
2126 printf(" <attribute>: ");
2127 PrintCursor(Attr->cursor);
2128 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002129}
2130
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002131static void printBaseClassInfo(CXClientData client_data,
2132 const CXIdxBaseClassInfo *info) {
2133 printEntityInfo(" <base>", client_data, info->base);
2134 printf(" | cursor: ");
2135 PrintCursor(info->cursor);
2136 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002137 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002138}
2139
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002140static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2141 CXClientData client_data) {
2142 unsigned i;
2143 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2144 printEntityInfo(" <protocol>", client_data,
2145 ProtoInfo->protocols[i]->protocol);
2146 printf(" | cursor: ");
2147 PrintCursor(ProtoInfo->protocols[i]->cursor);
2148 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002149 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002150 printf("\n");
2151 }
2152}
2153
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002154static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002155 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002156 CXString str;
2157 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002158 unsigned numDiags, i;
2159 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002160 IndexData *index_data;
2161 index_data = (IndexData *)client_data;
2162 printCheck(index_data);
2163
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002164 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2165 for (i = 0; i != numDiags; ++i) {
2166 diag = clang_getDiagnosticInSet(diagSet, i);
2167 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2168 cstr = clang_getCString(str);
2169 printf("[diagnostic]: %s\n", cstr);
2170 clang_disposeString(str);
2171
2172 if (getenv("CINDEXTEST_FAILONERROR") &&
2173 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2174 index_data->fail_for_error = 1;
2175 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002176 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002177}
2178
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002179static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2180 CXFile file, void *reserved) {
2181 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002182 CXString filename;
2183
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002184 index_data = (IndexData *)client_data;
2185 printCheck(index_data);
2186
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002187 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002188 index_data->main_filename = clang_getCString(filename);
2189 clang_disposeString(filename);
2190
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002191 printf("[enteredMainFile]: ");
2192 printCXIndexFile((CXIdxClientFile)file);
2193 printf("\n");
2194
2195 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002196}
2197
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002198static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002199 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002200 IndexData *index_data;
2201 index_data = (IndexData *)client_data;
2202 printCheck(index_data);
2203
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002204 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002205 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002206 printf(" | name: \"%s\"", info->filename);
2207 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002208 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002209 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002210
2211 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002212}
2213
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002214static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002215 void *reserved) {
2216 IndexData *index_data;
2217 index_data = (IndexData *)client_data;
2218 printCheck(index_data);
2219
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002220 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002221 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002222}
2223
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002224static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002225 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002226 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002227 const CXIdxObjCCategoryDeclInfo *CatInfo;
2228 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002229 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002230 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002231 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002232 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002233 index_data = (IndexData *)client_data;
2234
2235 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2236 printf(" | cursor: ");
2237 PrintCursor(info->cursor);
2238 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002239 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002240 printf(" | semantic-container: ");
2241 printCXIndexContainer(info->semanticContainer);
2242 printf(" | lexical-container: ");
2243 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002244 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002245 printf(" | isDef: %d", info->isDefinition);
2246 printf(" | isContainer: %d", info->isContainer);
2247 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002248
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002249 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002250 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002251 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002252 PrintCursor(Attr->cursor);
2253 printf("\n");
2254 }
2255
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002256 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2257 const char *kindName = 0;
2258 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2259 switch (K) {
2260 case CXIdxObjCContainer_ForwardRef:
2261 kindName = "forward-ref"; break;
2262 case CXIdxObjCContainer_Interface:
2263 kindName = "interface"; break;
2264 case CXIdxObjCContainer_Implementation:
2265 kindName = "implementation"; break;
2266 }
2267 printCheck(index_data);
2268 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2269 }
2270
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002271 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002272 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2273 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002274 printf(" | cursor: ");
2275 PrintCursor(CatInfo->classCursor);
2276 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002277 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002278 printf("\n");
2279 }
2280
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002281 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2282 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002283 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002284 printf("\n");
2285 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002286 }
2287
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002288 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2289 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002290 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002291
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002292 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2293 if (PropInfo->getter) {
2294 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2295 printf("\n");
2296 }
2297 if (PropInfo->setter) {
2298 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2299 printf("\n");
2300 }
2301 }
2302
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002303 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2304 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2305 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2306 printf("\n");
2307 }
2308 }
2309
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002310 if (info->declAsContainer)
2311 clang_index_setClientContainer(info->declAsContainer,
2312 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002313}
2314
2315static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002316 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002317 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002318 printf(" | cursor: ");
2319 PrintCursor(info->cursor);
2320 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002321 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002322 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002323 printf(" | container: ");
2324 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002325 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002326 switch (info->kind) {
2327 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002328 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002329 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002330 printf("\n");
2331}
2332
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002333static int index_abortQuery(CXClientData client_data, void *reserved) {
2334 IndexData *index_data;
2335 index_data = (IndexData *)client_data;
2336 return index_data->abort;
2337}
2338
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002339static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002340 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002341 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002342 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002343 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002344 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002345 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002346 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002347 index_indexEntityReference
2348};
2349
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002350static unsigned getIndexOptions(void) {
2351 unsigned index_opts;
2352 index_opts = 0;
2353 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2354 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2355 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2356 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2357
2358 return index_opts;
2359}
2360
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002361static int index_file(int argc, const char **argv) {
2362 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002363 CXIndex Idx;
2364 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002365 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002366 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002367 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002368
2369 check_prefix = 0;
2370 if (argc > 0) {
2371 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2372 check_prefix = argv[0] + strlen("-check-prefix=");
2373 ++argv;
2374 --argc;
2375 }
2376 }
2377
2378 if (argc == 0) {
2379 fprintf(stderr, "no compiler arguments\n");
2380 return -1;
2381 }
2382
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002383 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2384 /* displayDiagnosics=*/1))) {
2385 fprintf(stderr, "Could not create Index\n");
2386 return 1;
2387 }
2388 idxAction = 0;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002389
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002390 index_data.check_prefix = check_prefix;
2391 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002392 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002393 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002394
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002395 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002396 idxAction = clang_IndexAction_create(Idx);
2397 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002398 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002399 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002400 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002401 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002402
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002403 clang_IndexAction_dispose(idxAction);
2404 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002405 return result;
2406}
2407
2408static int index_tu(int argc, const char **argv) {
2409 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002410 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002411 CXTranslationUnit TU;
2412 const char *check_prefix;
2413 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002414 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002415 int result;
2416
2417 check_prefix = 0;
2418 if (argc > 0) {
2419 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2420 check_prefix = argv[0] + strlen("-check-prefix=");
2421 ++argv;
2422 --argc;
2423 }
2424 }
2425
2426 if (argc == 0) {
2427 fprintf(stderr, "no ast file\n");
2428 return -1;
2429 }
2430
2431 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2432 /* displayDiagnosics=*/1))) {
2433 fprintf(stderr, "Could not create Index\n");
2434 return 1;
2435 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002436 idxAction = 0;
2437 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002438
2439 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002440 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002441
2442 index_data.check_prefix = check_prefix;
2443 index_data.first_check_printed = 0;
2444 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002445 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002446
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002447 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002448 idxAction = clang_IndexAction_create(Idx);
2449 result = clang_indexTranslationUnit(idxAction, &index_data,
2450 &IndexCB,sizeof(IndexCB),
2451 index_opts, TU);
2452 if (index_data.fail_for_error)
2453 goto finished;
2454
2455 finished:
2456 clang_IndexAction_dispose(idxAction);
2457 clang_disposeIndex(Idx);
2458
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002459 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002460}
2461
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002462int perform_token_annotation(int argc, const char **argv) {
2463 const char *input = argv[1];
2464 char *filename = 0;
2465 unsigned line, second_line;
2466 unsigned column, second_column;
2467 CXIndex CIdx;
2468 CXTranslationUnit TU = 0;
2469 int errorCode;
2470 struct CXUnsavedFile *unsaved_files = 0;
2471 int num_unsaved_files = 0;
2472 CXToken *tokens;
2473 unsigned num_tokens;
2474 CXSourceRange range;
2475 CXSourceLocation startLoc, endLoc;
2476 CXFile file = 0;
2477 CXCursor *cursors = 0;
2478 unsigned i;
2479
2480 input += strlen("-test-annotate-tokens=");
2481 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2482 &second_line, &second_column)))
2483 return errorCode;
2484
Richard Smithe07c5f82012-07-05 08:20:49 +00002485 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
2486 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002487 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00002488 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002489
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002490 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002491 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2492 argv + num_unsaved_files + 2,
2493 argc - num_unsaved_files - 3,
2494 unsaved_files,
2495 num_unsaved_files,
2496 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002497 if (!TU) {
2498 fprintf(stderr, "unable to parse input\n");
2499 clang_disposeIndex(CIdx);
2500 free(filename);
2501 free_remapped_files(unsaved_files, num_unsaved_files);
2502 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002503 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002504 errorCode = 0;
2505
Richard Smithe07c5f82012-07-05 08:20:49 +00002506 if (checkForErrors(TU) != 0) {
2507 errorCode = -1;
2508 goto teardown;
2509 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002510
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002511 if (getenv("CINDEXTEST_EDITING")) {
2512 for (i = 0; i < 5; ++i) {
2513 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2514 clang_defaultReparseOptions(TU))) {
2515 fprintf(stderr, "Unable to reparse translation unit!\n");
2516 errorCode = -1;
2517 goto teardown;
2518 }
2519 }
2520 }
2521
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002522 if (checkForErrors(TU) != 0) {
2523 errorCode = -1;
2524 goto teardown;
2525 }
2526
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002527 file = clang_getFile(TU, filename);
2528 if (!file) {
2529 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2530 errorCode = -1;
2531 goto teardown;
2532 }
2533
2534 startLoc = clang_getLocation(TU, file, line, column);
2535 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002536 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002537 column);
2538 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002539 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002540 }
2541
2542 endLoc = clang_getLocation(TU, file, second_line, second_column);
2543 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002544 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002545 second_line, second_column);
2546 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002547 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002548 }
2549
2550 range = clang_getRange(startLoc, endLoc);
2551 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002552
2553 if (checkForErrors(TU) != 0) {
2554 errorCode = -1;
2555 goto teardown;
2556 }
2557
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002558 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2559 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002560
2561 if (checkForErrors(TU) != 0) {
2562 errorCode = -1;
2563 goto teardown;
2564 }
2565
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002566 for (i = 0; i != num_tokens; ++i) {
2567 const char *kind = "<unknown>";
2568 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2569 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2570 unsigned start_line, start_column, end_line, end_column;
2571
2572 switch (clang_getTokenKind(tokens[i])) {
2573 case CXToken_Punctuation: kind = "Punctuation"; break;
2574 case CXToken_Keyword: kind = "Keyword"; break;
2575 case CXToken_Identifier: kind = "Identifier"; break;
2576 case CXToken_Literal: kind = "Literal"; break;
2577 case CXToken_Comment: kind = "Comment"; break;
2578 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002579 clang_getSpellingLocation(clang_getRangeStart(extent),
2580 0, &start_line, &start_column, 0);
2581 clang_getSpellingLocation(clang_getRangeEnd(extent),
2582 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002583 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002584 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002585 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002586 if (!clang_isInvalid(cursors[i].kind)) {
2587 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002588 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002589 }
2590 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002591 }
2592 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002593 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002594
2595 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002596 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002597 clang_disposeTranslationUnit(TU);
2598 clang_disposeIndex(CIdx);
2599 free(filename);
2600 free_remapped_files(unsaved_files, num_unsaved_files);
2601 return errorCode;
2602}
2603
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002604static int
2605perform_test_compilation_db(const char *database, int argc, const char **argv) {
2606 CXCompilationDatabase db;
2607 CXCompileCommands CCmds;
2608 CXCompileCommand CCmd;
2609 CXCompilationDatabase_Error ec;
2610 CXString wd;
2611 CXString arg;
2612 int errorCode = 0;
2613 char *tmp;
2614 unsigned len;
2615 char *buildDir;
2616 int i, j, a, numCmds, numArgs;
2617
2618 len = strlen(database);
2619 tmp = (char *) malloc(len+1);
2620 memcpy(tmp, database, len+1);
2621 buildDir = dirname(tmp);
2622
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002623 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002624
2625 if (db) {
2626
2627 if (ec!=CXCompilationDatabase_NoError) {
2628 printf("unexpected error %d code while loading compilation database\n", ec);
2629 errorCode = -1;
2630 goto cdb_end;
2631 }
2632
2633 for (i=0; i<argc && errorCode==0; ) {
2634 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002635 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002636
2637 if (!CCmds) {
2638 printf("file %s not found in compilation db\n", argv[i+1]);
2639 errorCode = -1;
2640 break;
2641 }
2642
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002643 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002644
2645 if (numCmds==0) {
2646 fprintf(stderr, "should not get an empty compileCommand set for file"
2647 " '%s'\n", argv[i+1]);
2648 errorCode = -1;
2649 break;
2650 }
2651
2652 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002653 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002654
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002655 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002656 printf("workdir:'%s'", clang_getCString(wd));
2657 clang_disposeString(wd);
2658
2659 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002660 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002661 for (a=0; a<numArgs; ++a) {
2662 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002663 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002664 printf("%s", clang_getCString(arg));
2665 clang_disposeString(arg);
2666 }
2667 printf("'\n");
2668 }
2669
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002670 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002671
2672 i += 2;
2673 }
2674 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002675 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002676 } else {
2677 printf("database loading failed with error code %d.\n", ec);
2678 errorCode = -1;
2679 }
2680
2681cdb_end:
2682 free(tmp);
2683
2684 return errorCode;
2685}
2686
Ted Kremenek0d435192009-11-17 18:13:31 +00002687/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002688/* USR printing. */
2689/******************************************************************************/
2690
2691static int insufficient_usr(const char *kind, const char *usage) {
2692 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2693 return 1;
2694}
2695
2696static unsigned isUSR(const char *s) {
2697 return s[0] == 'c' && s[1] == ':';
2698}
2699
2700static int not_usr(const char *s, const char *arg) {
2701 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2702 return 1;
2703}
2704
2705static void print_usr(CXString usr) {
2706 const char *s = clang_getCString(usr);
2707 printf("%s\n", s);
2708 clang_disposeString(usr);
2709}
2710
2711static void display_usrs() {
2712 fprintf(stderr, "-print-usrs options:\n"
2713 " ObjCCategory <class name> <category name>\n"
2714 " ObjCClass <class name>\n"
2715 " ObjCIvar <ivar name> <class USR>\n"
2716 " ObjCMethod <selector> [0=class method|1=instance method] "
2717 "<class USR>\n"
2718 " ObjCProperty <property name> <class USR>\n"
2719 " ObjCProtocol <protocol name>\n");
2720}
2721
2722int print_usrs(const char **I, const char **E) {
2723 while (I != E) {
2724 const char *kind = *I;
2725 unsigned len = strlen(kind);
2726 switch (len) {
2727 case 8:
2728 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2729 if (I + 2 >= E)
2730 return insufficient_usr(kind, "<ivar name> <class USR>");
2731 if (!isUSR(I[2]))
2732 return not_usr("<class USR>", I[2]);
2733 else {
2734 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002735 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002736 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002737 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2738 }
2739
2740 I += 3;
2741 continue;
2742 }
2743 break;
2744 case 9:
2745 if (memcmp(kind, "ObjCClass", 9) == 0) {
2746 if (I + 1 >= E)
2747 return insufficient_usr(kind, "<class name>");
2748 print_usr(clang_constructUSR_ObjCClass(I[1]));
2749 I += 2;
2750 continue;
2751 }
2752 break;
2753 case 10:
2754 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2755 if (I + 3 >= E)
2756 return insufficient_usr(kind, "<method selector> "
2757 "[0=class method|1=instance method] <class USR>");
2758 if (!isUSR(I[3]))
2759 return not_usr("<class USR>", I[3]);
2760 else {
2761 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002762 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002763 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002764 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2765 }
2766 I += 4;
2767 continue;
2768 }
2769 break;
2770 case 12:
2771 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2772 if (I + 2 >= E)
2773 return insufficient_usr(kind, "<class name> <category name>");
2774 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2775 I += 3;
2776 continue;
2777 }
2778 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2779 if (I + 1 >= E)
2780 return insufficient_usr(kind, "<protocol name>");
2781 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2782 I += 2;
2783 continue;
2784 }
2785 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2786 if (I + 2 >= E)
2787 return insufficient_usr(kind, "<property name> <class USR>");
2788 if (!isUSR(I[2]))
2789 return not_usr("<class USR>", I[2]);
2790 else {
2791 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002792 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002793 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002794 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2795 }
2796 I += 3;
2797 continue;
2798 }
2799 break;
2800 default:
2801 break;
2802 }
2803 break;
2804 }
2805
2806 if (I != E) {
2807 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2808 display_usrs();
2809 return 1;
2810 }
2811 return 0;
2812}
2813
2814int print_usrs_file(const char *file_name) {
2815 char line[2048];
2816 const char *args[128];
2817 unsigned numChars = 0;
2818
2819 FILE *fp = fopen(file_name, "r");
2820 if (!fp) {
2821 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2822 return 1;
2823 }
2824
2825 /* This code is not really all that safe, but it works fine for testing. */
2826 while (!feof(fp)) {
2827 char c = fgetc(fp);
2828 if (c == '\n') {
2829 unsigned i = 0;
2830 const char *s = 0;
2831
2832 if (numChars == 0)
2833 continue;
2834
2835 line[numChars] = '\0';
2836 numChars = 0;
2837
2838 if (line[0] == '/' && line[1] == '/')
2839 continue;
2840
2841 s = strtok(line, " ");
2842 while (s) {
2843 args[i] = s;
2844 ++i;
2845 s = strtok(0, " ");
2846 }
2847 if (print_usrs(&args[0], &args[i]))
2848 return 1;
2849 }
2850 else
2851 line[numChars++] = c;
2852 }
2853
2854 fclose(fp);
2855 return 0;
2856}
2857
2858/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002859/* Command line processing. */
2860/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002861int write_pch_file(const char *filename, int argc, const char *argv[]) {
2862 CXIndex Idx;
2863 CXTranslationUnit TU;
2864 struct CXUnsavedFile *unsaved_files = 0;
2865 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002866 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002867
2868 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2869
2870 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2871 clang_disposeIndex(Idx);
2872 return -1;
2873 }
2874
2875 TU = clang_parseTranslationUnit(Idx, 0,
2876 argv + num_unsaved_files,
2877 argc - num_unsaved_files,
2878 unsaved_files,
2879 num_unsaved_files,
2880 CXTranslationUnit_Incomplete);
2881 if (!TU) {
2882 fprintf(stderr, "Unable to load translation unit!\n");
2883 free_remapped_files(unsaved_files, num_unsaved_files);
2884 clang_disposeIndex(Idx);
2885 return 1;
2886 }
2887
Douglas Gregor39c411f2011-07-06 16:43:36 +00002888 switch (clang_saveTranslationUnit(TU, filename,
2889 clang_defaultSaveOptions(TU))) {
2890 case CXSaveError_None:
2891 break;
2892
2893 case CXSaveError_TranslationErrors:
2894 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2895 filename);
2896 result = 2;
2897 break;
2898
2899 case CXSaveError_InvalidTU:
2900 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2901 filename);
2902 result = 3;
2903 break;
2904
2905 case CXSaveError_Unknown:
2906 default:
2907 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2908 result = 1;
2909 break;
2910 }
2911
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002912 clang_disposeTranslationUnit(TU);
2913 free_remapped_files(unsaved_files, num_unsaved_files);
2914 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002915 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002916}
2917
2918/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002919/* Serialized diagnostics. */
2920/******************************************************************************/
2921
2922static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2923 switch (error) {
2924 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2925 case CXLoadDiag_None: break;
2926 case CXLoadDiag_Unknown: return "Unknown";
2927 case CXLoadDiag_InvalidFile: return "Invalid File";
2928 }
2929 return "None";
2930}
2931
2932static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2933 switch (severity) {
2934 case CXDiagnostic_Note: return "note";
2935 case CXDiagnostic_Error: return "error";
2936 case CXDiagnostic_Fatal: return "fatal";
2937 case CXDiagnostic_Ignored: return "ignored";
2938 case CXDiagnostic_Warning: return "warning";
2939 }
2940 return "unknown";
2941}
2942
2943static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002944 if (indent == 0)
2945 return;
2946 fprintf(stderr, "+");
2947 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002948 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002949 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002950 --indent;
2951 }
2952}
2953
2954static void printLocation(CXSourceLocation L) {
2955 CXFile File;
2956 CXString FileName;
2957 unsigned line, column, offset;
2958
2959 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2960 FileName = clang_getFileName(File);
2961
2962 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2963 clang_disposeString(FileName);
2964}
2965
2966static void printRanges(CXDiagnostic D, unsigned indent) {
2967 unsigned i, n = clang_getDiagnosticNumRanges(D);
2968
2969 for (i = 0; i < n; ++i) {
2970 CXSourceLocation Start, End;
2971 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2972 Start = clang_getRangeStart(SR);
2973 End = clang_getRangeEnd(SR);
2974
2975 printIndent(indent);
2976 fprintf(stderr, "Range: ");
2977 printLocation(Start);
2978 fprintf(stderr, " ");
2979 printLocation(End);
2980 fprintf(stderr, "\n");
2981 }
2982}
2983
2984static void printFixIts(CXDiagnostic D, unsigned indent) {
2985 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002986 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002987 for (i = 0 ; i < n; ++i) {
2988 CXSourceRange ReplacementRange;
2989 CXString text;
2990 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2991
2992 printIndent(indent);
2993 fprintf(stderr, "FIXIT: (");
2994 printLocation(clang_getRangeStart(ReplacementRange));
2995 fprintf(stderr, " - ");
2996 printLocation(clang_getRangeEnd(ReplacementRange));
2997 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2998 clang_disposeString(text);
2999 }
3000}
3001
3002static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003003 unsigned i, n;
3004
Ted Kremenek15322172011-11-10 08:43:12 +00003005 if (!Diags)
3006 return;
3007
NAKAMURA Takumi91909432011-11-10 09:30:15 +00003008 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00003009 for (i = 0; i < n; ++i) {
3010 CXSourceLocation DiagLoc;
3011 CXDiagnostic D;
3012 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003013 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00003014 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003015 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00003016
3017 D = clang_getDiagnosticInSet(Diags, i);
3018 DiagLoc = clang_getDiagnosticLocation(D);
3019 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3020 FileName = clang_getFileName(File);
3021 DiagSpelling = clang_getDiagnosticSpelling(D);
3022
3023 printIndent(indent);
3024
3025 fprintf(stderr, "%s:%d:%d: %s: %s",
3026 clang_getCString(FileName),
3027 line,
3028 column,
3029 getSeverityString(clang_getDiagnosticSeverity(D)),
3030 clang_getCString(DiagSpelling));
3031
3032 DiagOption = clang_getDiagnosticOption(D, 0);
3033 DiagOptionStr = clang_getCString(DiagOption);
3034 if (DiagOptionStr) {
3035 fprintf(stderr, " [%s]", DiagOptionStr);
3036 }
3037
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003038 DiagCat = clang_getDiagnosticCategoryText(D);
3039 DiagCatStr = clang_getCString(DiagCat);
3040 if (DiagCatStr) {
3041 fprintf(stderr, " [%s]", DiagCatStr);
3042 }
3043
Ted Kremenek15322172011-11-10 08:43:12 +00003044 fprintf(stderr, "\n");
3045
3046 printRanges(D, indent);
3047 printFixIts(D, indent);
3048
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003049 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003050 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3051
3052 clang_disposeString(FileName);
3053 clang_disposeString(DiagSpelling);
3054 clang_disposeString(DiagOption);
3055 }
3056}
3057
3058static int read_diagnostics(const char *filename) {
3059 enum CXLoadDiag_Error error;
3060 CXString errorString;
3061 CXDiagnosticSet Diags = 0;
3062
3063 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3064 if (!Diags) {
3065 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3066 getDiagnosticCodeStr(error),
3067 clang_getCString(errorString));
3068 clang_disposeString(errorString);
3069 return 1;
3070 }
3071
3072 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003073 fprintf(stderr, "Number of diagnostics: %d\n",
3074 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003075 clang_disposeDiagnosticSet(Diags);
3076 return 0;
3077}
3078
3079/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003080/* Command line processing. */
3081/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003082
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003083static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003084 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003085 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003086 if (strcmp(s, "-usrs") == 0)
3087 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003088 if (strncmp(s, "-memory-usage", 13) == 0)
3089 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003090 return NULL;
3091}
3092
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003093static void print_usage(void) {
3094 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003095 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003096 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003097 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003098 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003099 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003100 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003101 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003102 "[FileCheck prefix]\n");
3103 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003104 " c-index-test -test-load-tu <AST file> <symbol filter> "
3105 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003106 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3107 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003108 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003109 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003110 " c-index-test -test-load-source-memory-usage "
3111 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003112 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3113 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003114 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003115 " c-index-test -test-load-source-usrs-memory-usage "
3116 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003117 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3118 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003119 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003120 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003121 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003122 " c-index-test -test-print-typekind {<args>}*\n"
3123 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003124 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003125 " c-index-test -write-pch <file> <compiler arguments>\n");
3126 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003127 " c-index-test -compilation-db [lookup <filename>] database\n");
3128 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003129 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003130 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003131 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003132 " all - load all symbols, including those from PCH\n"
3133 " local - load all symbols except those in PCH\n"
3134 " category - only load ObjC categories (non-PCH)\n"
3135 " interface - only load ObjC interfaces (non-PCH)\n"
3136 " protocol - only load ObjC protocols (non-PCH)\n"
3137 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003138 " typedef - only load typdefs (non-PCH)\n"
3139 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003140}
3141
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003142/***/
3143
3144int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003145 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003146 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3147 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003148 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003149 return perform_code_completion(argc, argv, 0);
3150 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3151 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003152 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3153 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003154 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3155 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003156 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
3157 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003158 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3159 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003160 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003161 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003162 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003163 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3164 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003165 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003166 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3167 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3168 if (I) {
3169 int trials = atoi(argv[2]);
3170 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3171 NULL);
3172 }
3173 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003174 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003175 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003176
3177 PostVisitTU postVisit = 0;
3178 if (strstr(argv[1], "-memory-usage"))
3179 postVisit = PrintMemoryUsage;
3180
Ted Kremenek7d405622010-01-12 23:34:26 +00003181 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003182 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3183 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003184 }
3185 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003186 return perform_file_scan(argv[2], argv[3],
3187 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003188 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3189 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003190 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3191 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3192 PrintInclusionStack);
3193 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3194 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3195 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003196 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3197 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3198 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003199 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
3200 return perform_test_load_source(argc - 2, argv + 2, "all",
3201 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003202 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3203 if (argc > 2)
3204 return print_usrs(argv + 2, argv + argc);
3205 else {
3206 display_usrs();
3207 return 1;
3208 }
3209 }
3210 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3211 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003212 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3213 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003214 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3215 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3216
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003217 print_usage();
3218 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003219}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003220
3221/***/
3222
3223/* We intentionally run in a separate thread to ensure we at least minimal
3224 * testing of a multithreaded environment (for example, having a reduced stack
3225 * size). */
3226
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003227typedef struct thread_info {
3228 int argc;
3229 const char **argv;
3230 int result;
3231} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003232void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003233 thread_info *client_data = client_data_v;
3234 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003235#ifdef __CYGWIN__
3236 fflush(stdout); /* stdout is not flushed on Cygwin. */
3237#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003238}
3239
3240int main(int argc, const char **argv) {
3241 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003242
Douglas Gregor61605982010-10-27 16:00:01 +00003243 if (getenv("CINDEXTEST_NOTHREADS"))
3244 return cindextest_main(argc, argv);
3245
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003246 client_data.argc = argc;
3247 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003248 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003249 return client_data.result;
3250}