blob: bcfbece3ffdef2f37029ccb67b8dd4ff27b635cd [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) {
259 Ctx->IndentLevel++;
260 for (unsigned i = 0, e = Ctx->IndentLevel; i != e; ++i)
261 printf(" ");
262
263 printf("(");
264 enum CXCommentKind Kind = clang_Comment_getKind(Comment);
265 switch (Kind) {
266 case CXComment_Null:
267 printf("CXComment_Null");
268 break;
269 case CXComment_Text:
270 printf("CXComment_Text");
271 PrintCXStringWithPrefixAndDispose("Text",
272 clang_TextComment_getText(Comment));
273 if (clang_Comment_isWhitespace(Comment))
274 printf(" IsWhitespace");
275 if (clang_InlineContentComment_hasTrailingNewline(Comment))
276 printf(" HasTrailingNewline");
277 break;
278 case CXComment_InlineCommand:
279 printf("CXComment_InlineCommand");
280 PrintCXStringWithPrefixAndDispose(
281 "CommandName",
282 clang_InlineCommandComment_getCommandName(Comment));
283 for (unsigned i = 0, e = clang_InlineCommandComment_getNumArgs(Comment);
284 i != e; ++i) {
285 printf(" Arg[%u]=", i);
286 PrintCXStringAndDispose(
287 clang_InlineCommandComment_getArgText(Comment, i));
288 }
289 if (clang_InlineContentComment_hasTrailingNewline(Comment))
290 printf(" HasTrailingNewline");
291 break;
292 case CXComment_HTMLStartTag:
293 printf("CXComment_HTMLStartTag");
294 PrintCXStringWithPrefixAndDispose(
295 "Name",
296 clang_HTMLTagComment_getTagName(Comment));
297 const unsigned NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment);
298 if (NumAttrs != 0) {
299 printf(" Attrs:");
300 for (unsigned i = 0; i != NumAttrs; ++i) {
301 printf(" ");
302 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i));
303 printf("=");
304 PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i));
305 }
306 }
307 if (clang_HTMLStartTagComment_isSelfClosing(Comment))
308 printf(" SelfClosing");
309 if (clang_InlineContentComment_hasTrailingNewline(Comment))
310 printf(" HasTrailingNewline");
311 break;
312 case CXComment_HTMLEndTag:
313 printf("CXComment_HTMLEndTag");
314 PrintCXStringWithPrefixAndDispose(
315 "Name",
316 clang_HTMLTagComment_getTagName(Comment));
317 if (clang_InlineContentComment_hasTrailingNewline(Comment))
318 printf(" HasTrailingNewline");
319 break;
320 case CXComment_Paragraph:
321 printf("CXComment_Paragraph");
322 if (clang_Comment_isWhitespace(Comment))
323 printf(" IsWhitespace");
324 break;
325 case CXComment_BlockCommand:
326 printf("CXComment_BlockCommand");
327 PrintCXStringWithPrefixAndDispose(
328 "CommandName",
329 clang_BlockCommandComment_getCommandName(Comment));
330 for (unsigned i = 0, e = clang_BlockCommandComment_getNumArgs(Comment);
331 i != e; ++i) {
332 printf(" Arg[%u]=", i);
333 PrintCXStringAndDispose(
334 clang_BlockCommandComment_getArgText(Comment, i));
335 }
336 break;
337 case CXComment_ParamCommand:
338 printf("CXComment_ParamCommand");
339 switch (clang_ParamCommandComment_getDirection(Comment)) {
340 case CXCommentParamPassDirection_In:
341 printf(" in");
342 break;
343 case CXCommentParamPassDirection_Out:
344 printf(" out");
345 break;
346 case CXCommentParamPassDirection_InOut:
347 printf(" in,out");
348 break;
349 }
350 if (clang_ParamCommandComment_isDirectionExplicit(Comment))
351 printf(" explicitly");
352 else
353 printf(" implicitly");
354 PrintCXStringWithPrefixAndDispose(
355 "ParamName",
356 clang_ParamCommandComment_getParamName(Comment));
357 if (clang_ParamCommandComment_isParamIndexValid(Comment))
358 printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment));
359 else
360 printf(" ParamIndex=Invalid");
361 break;
362 case CXComment_VerbatimBlockCommand:
363 printf("CXComment_VerbatimBlockCommand");
364 PrintCXStringWithPrefixAndDispose(
365 "CommandName",
366 clang_BlockCommandComment_getCommandName(Comment));
367 break;
368 case CXComment_VerbatimBlockLine:
369 printf("CXComment_VerbatimBlockLine");
370 PrintCXStringWithPrefixAndDispose(
371 "Text",
372 clang_VerbatimBlockLineComment_getText(Comment));
373 break;
374 case CXComment_VerbatimLine:
375 printf("CXComment_VerbatimLine");
376 PrintCXStringWithPrefixAndDispose(
377 "Text",
378 clang_VerbatimLineComment_getText(Comment));
379 break;
380 case CXComment_FullComment:
381 printf("CXComment_FullComment");
382 break;
383 }
384 if (Kind != CXComment_Null) {
385 const unsigned NumChildren = clang_Comment_getNumChildren(Comment);
386 for (unsigned i = 0; i != NumChildren; ++i) {
387 printf("\n// %s: ", FileCheckPrefix);
388 DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i));
389 }
390 }
391 printf(")");
392 Ctx->IndentLevel--;
393}
394
395static void DumpCXComment(CXComment Comment) {
396 struct CommentASTDumpingContext Ctx;
397 Ctx.IndentLevel = 1;
398 printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix);
399 DumpCXCommentInternal(&Ctx, Comment);
400 printf("]");
401}
402
403static void PrintCursorComments(CXCursor Cursor) {
404 {
405 CXString RawComment;
406 const char *RawCommentCString;
407 CXString BriefComment;
408 const char *BriefCommentCString;
409
410 RawComment = clang_Cursor_getRawCommentText(Cursor);
411 RawCommentCString = clang_getCString(RawComment);
412 if (RawCommentCString != NULL && RawCommentCString[0] != '\0') {
413 PrintCStringWithPrefix("RawComment", RawCommentCString);
414 PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange");
415
416 BriefComment = clang_Cursor_getBriefCommentText(Cursor);
417 BriefCommentCString = clang_getCString(BriefComment);
418 if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0')
419 PrintCStringWithPrefix("BriefComment", BriefCommentCString);
420 clang_disposeString(BriefComment);
421 }
422 clang_disposeString(RawComment);
423 }
424
425 {
426 CXComment Comment = clang_Cursor_getParsedComment(Cursor);
427 if (clang_Comment_getKind(Comment) != CXComment_Null) {
428 PrintCXStringWithPrefixAndDispose("FullCommentAsHTML",
429 clang_FullComment_getAsHTML(Comment));
430 DumpCXComment(Comment);
431 }
432 }
433}
434
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000435static void PrintCursor(CXCursor Cursor) {
436 CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000437 if (clang_isInvalid(Cursor.kind)) {
438 CXString ks = clang_getCursorKindSpelling(Cursor.kind);
439 printf("Invalid Cursor => %s", clang_getCString(ks));
440 clang_disposeString(ks);
441 }
Steve Naroff699a07d2009-09-25 21:32:34 +0000442 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000443 CXString string, ks;
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000444 CXCursor Referenced;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000445 unsigned line, column;
Douglas Gregore0329ac2010-09-02 00:07:54 +0000446 CXCursor SpecializationOf;
Douglas Gregor9f592342010-10-01 20:25:15 +0000447 CXCursor *overridden;
448 unsigned num_overridden;
Douglas Gregor430d7a12011-07-25 17:48:11 +0000449 unsigned RefNameRangeNr;
450 CXSourceRange CursorExtent;
451 CXSourceRange RefNameRange;
Douglas Gregorcc889662012-05-08 00:14:45 +0000452 int AlwaysUnavailable;
453 int AlwaysDeprecated;
454 CXString UnavailableMessage;
455 CXString DeprecatedMessage;
456 CXPlatformAvailability PlatformAvailability[2];
457 int NumPlatformAvailability;
458 int I;
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000459
Ted Kremeneke68fff62010-02-17 00:41:32 +0000460 ks = clang_getCursorKindSpelling(Cursor.kind);
Douglas Gregor358559d2010-10-02 22:49:11 +0000461 string = want_display_name? clang_getCursorDisplayName(Cursor)
462 : clang_getCursorSpelling(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000463 printf("%s=%s", clang_getCString(ks),
464 clang_getCString(string));
465 clang_disposeString(ks);
Steve Naroffef0cef62009-11-09 17:45:52 +0000466 clang_disposeString(string);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000467
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000468 Referenced = clang_getCursorReferenced(Cursor);
469 if (!clang_equalCursors(Referenced, clang_getNullCursor())) {
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000470 if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) {
471 unsigned I, N = clang_getNumOverloadedDecls(Referenced);
472 printf("[");
473 for (I = 0; I != N; ++I) {
474 CXCursor Ovl = clang_getOverloadedDecl(Referenced, I);
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000475 CXSourceLocation Loc;
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000476 if (I)
477 printf(", ");
478
Douglas Gregor1f6206e2010-09-14 00:20:32 +0000479 Loc = clang_getCursorLocation(Ovl);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000480 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000481 printf("%d:%d", line, column);
482 }
483 printf("]");
484 } else {
485 CXSourceLocation Loc = clang_getCursorLocation(Referenced);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000486 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor1f60d9e2010-09-13 22:52:57 +0000487 printf(":%d:%d", line, column);
488 }
Douglas Gregorc5d1e932010-01-19 01:20:04 +0000489 }
Douglas Gregorb6998662010-01-19 19:34:47 +0000490
491 if (clang_isCursorDefinition(Cursor))
492 printf(" (Definition)");
Douglas Gregor58ddb602010-08-23 23:00:57 +0000493
494 switch (clang_getCursorAvailability(Cursor)) {
495 case CXAvailability_Available:
496 break;
497
498 case CXAvailability_Deprecated:
499 printf(" (deprecated)");
500 break;
501
502 case CXAvailability_NotAvailable:
503 printf(" (unavailable)");
504 break;
Erik Verbruggend1205962011-10-06 07:27:49 +0000505
506 case CXAvailability_NotAccessible:
507 printf(" (inaccessible)");
508 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +0000509 }
Ted Kremenek95f33552010-08-26 01:42:22 +0000510
Douglas Gregorcc889662012-05-08 00:14:45 +0000511 NumPlatformAvailability
512 = clang_getCursorPlatformAvailability(Cursor,
513 &AlwaysDeprecated,
514 &DeprecatedMessage,
515 &AlwaysUnavailable,
516 &UnavailableMessage,
517 PlatformAvailability, 2);
518 if (AlwaysUnavailable) {
519 printf(" (always unavailable: \"%s\")",
520 clang_getCString(UnavailableMessage));
521 } else if (AlwaysDeprecated) {
522 printf(" (always deprecated: \"%s\")",
523 clang_getCString(DeprecatedMessage));
524 } else {
525 for (I = 0; I != NumPlatformAvailability; ++I) {
526 if (I >= 2)
527 break;
528
529 printf(" (%s", clang_getCString(PlatformAvailability[I].Platform));
530 if (PlatformAvailability[I].Unavailable)
531 printf(", unavailable");
532 else {
533 printVersion(", introduced=", PlatformAvailability[I].Introduced);
534 printVersion(", deprecated=", PlatformAvailability[I].Deprecated);
535 printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted);
536 }
537 if (clang_getCString(PlatformAvailability[I].Message)[0])
538 printf(", message=\"%s\"",
539 clang_getCString(PlatformAvailability[I].Message));
540 printf(")");
541 }
542 }
543 for (I = 0; I != NumPlatformAvailability; ++I) {
544 if (I >= 2)
545 break;
546 clang_disposeCXPlatformAvailability(PlatformAvailability + I);
547 }
548
549 clang_disposeString(DeprecatedMessage);
550 clang_disposeString(UnavailableMessage);
551
Douglas Gregorb83d4d72011-05-13 15:54:42 +0000552 if (clang_CXXMethod_isStatic(Cursor))
553 printf(" (static)");
554 if (clang_CXXMethod_isVirtual(Cursor))
555 printf(" (virtual)");
556
Ted Kremenek95f33552010-08-26 01:42:22 +0000557 if (Cursor.kind == CXCursor_IBOutletCollectionAttr) {
558 CXType T =
559 clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor));
560 CXString S = clang_getTypeKindSpelling(T.kind);
561 printf(" [IBOutletCollection=%s]", clang_getCString(S));
562 clang_disposeString(S);
563 }
Ted Kremenek3064ef92010-08-27 21:34:58 +0000564
565 if (Cursor.kind == CXCursor_CXXBaseSpecifier) {
566 enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor);
567 unsigned isVirtual = clang_isVirtualBase(Cursor);
568 const char *accessStr = 0;
569
570 switch (access) {
571 case CX_CXXInvalidAccessSpecifier:
572 accessStr = "invalid"; break;
573 case CX_CXXPublic:
574 accessStr = "public"; break;
575 case CX_CXXProtected:
576 accessStr = "protected"; break;
577 case CX_CXXPrivate:
578 accessStr = "private"; break;
579 }
580
581 printf(" [access=%s isVirtual=%s]", accessStr,
582 isVirtual ? "true" : "false");
583 }
Douglas Gregore0329ac2010-09-02 00:07:54 +0000584
585 SpecializationOf = clang_getSpecializedCursorTemplate(Cursor);
586 if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) {
587 CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf);
588 CXString Name = clang_getCursorSpelling(SpecializationOf);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000589 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregore0329ac2010-09-02 00:07:54 +0000590 printf(" [Specialization of %s:%d:%d]",
591 clang_getCString(Name), line, column);
592 clang_disposeString(Name);
593 }
Douglas Gregor9f592342010-10-01 20:25:15 +0000594
595 clang_getOverriddenCursors(Cursor, &overridden, &num_overridden);
596 if (num_overridden) {
597 unsigned I;
598 printf(" [Overrides ");
599 for (I = 0; I != num_overridden; ++I) {
600 CXSourceLocation Loc = clang_getCursorLocation(overridden[I]);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000601 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Douglas Gregor9f592342010-10-01 20:25:15 +0000602 if (I)
603 printf(", ");
604 printf("@%d:%d", line, column);
605 }
606 printf("]");
607 clang_disposeOverriddenCursors(overridden);
608 }
Douglas Gregorecdcb882010-10-20 22:00:55 +0000609
610 if (Cursor.kind == CXCursor_InclusionDirective) {
611 CXFile File = clang_getIncludedFile(Cursor);
612 CXString Included = clang_getFileName(File);
613 printf(" (%s)", clang_getCString(Included));
614 clang_disposeString(Included);
Douglas Gregordd3e5542011-05-04 00:14:37 +0000615
616 if (clang_isFileMultipleIncludeGuarded(TU, File))
617 printf(" [multi-include guarded]");
Douglas Gregorecdcb882010-10-20 22:00:55 +0000618 }
Douglas Gregor430d7a12011-07-25 17:48:11 +0000619
620 CursorExtent = clang_getCursorExtent(Cursor);
621 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
622 CXNameRange_WantQualifier
623 | CXNameRange_WantSinglePiece
624 | CXNameRange_WantTemplateArgs,
625 0);
626 if (!clang_equalRanges(CursorExtent, RefNameRange))
627 PrintRange(RefNameRange, "SingleRefName");
628
629 for (RefNameRangeNr = 0; 1; RefNameRangeNr++) {
630 RefNameRange = clang_getCursorReferenceNameRange(Cursor,
631 CXNameRange_WantQualifier
632 | CXNameRange_WantTemplateArgs,
633 RefNameRangeNr);
634 if (clang_equalRanges(clang_getNullRange(), RefNameRange))
635 break;
636 if (!clang_equalRanges(CursorExtent, RefNameRange))
637 PrintRange(RefNameRange, "RefName");
638 }
Dmitri Gribenkoaa0cd852012-06-20 00:34:58 +0000639
Dmitri Gribenkoae99b752012-07-20 21:34:34 +0000640 PrintCursorComments(Cursor);
Steve Naroff699a07d2009-09-25 21:32:34 +0000641 }
Steve Naroffaf08ddc2009-09-03 15:49:00 +0000642}
Steve Naroff89922f82009-08-31 00:59:03 +0000643
Ted Kremeneke68fff62010-02-17 00:41:32 +0000644static const char* GetCursorSource(CXCursor Cursor) {
Douglas Gregor1db19de2010-01-19 21:36:55 +0000645 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Ted Kremenek74844072010-02-17 00:41:20 +0000646 CXString source;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000647 CXFile file;
Argyrios Kyrtzidisb4efaa02011-11-03 02:20:36 +0000648 clang_getExpansionLocation(Loc, &file, 0, 0, 0);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000649 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000650 if (!clang_getCString(source)) {
Ted Kremenek74844072010-02-17 00:41:20 +0000651 clang_disposeString(source);
652 return "<invalid loc>";
653 }
654 else {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000655 const char *b = basename(clang_getCString(source));
Ted Kremenek74844072010-02-17 00:41:20 +0000656 clang_disposeString(source);
657 return b;
658 }
Ted Kremenek9298cfc2009-11-17 05:31:58 +0000659}
660
Ted Kremenek0d435192009-11-17 18:13:31 +0000661/******************************************************************************/
Ted Kremenekce2ae882010-01-26 17:59:48 +0000662/* Callbacks. */
663/******************************************************************************/
664
665typedef void (*PostVisitTU)(CXTranslationUnit);
666
Douglas Gregora88084b2010-02-18 18:08:43 +0000667void PrintDiagnostic(CXDiagnostic Diagnostic) {
668 FILE *out = stderr;
Douglas Gregor5352ac02010-01-28 00:27:43 +0000669 CXFile file;
Douglas Gregor274f1902010-02-22 23:17:23 +0000670 CXString Msg;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000671 unsigned display_opts = CXDiagnostic_DisplaySourceLocation
Douglas Gregoraa5f1352010-11-19 16:18:16 +0000672 | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges
673 | CXDiagnostic_DisplayOption;
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000674 unsigned i, num_fixits;
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000675
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000676 if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored)
Douglas Gregor5352ac02010-01-28 00:27:43 +0000677 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000678
Douglas Gregor274f1902010-02-22 23:17:23 +0000679 Msg = clang_formatDiagnostic(Diagnostic, display_opts);
680 fprintf(stderr, "%s\n", clang_getCString(Msg));
681 clang_disposeString(Msg);
Ted Kremenekf7b714d2010-03-25 02:00:39 +0000682
Douglas Gregora9b06d42010-11-09 06:24:54 +0000683 clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic),
684 &file, 0, 0, 0);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000685 if (!file)
686 return;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000687
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000688 num_fixits = clang_getDiagnosticNumFixIts(Diagnostic);
Ted Kremenek3739b322012-03-20 20:49:45 +0000689 fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits);
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000690 for (i = 0; i != num_fixits; ++i) {
Douglas Gregor473d7012010-02-19 18:16:06 +0000691 CXSourceRange range;
692 CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range);
693 CXSourceLocation start = clang_getRangeStart(range);
694 CXSourceLocation end = clang_getRangeEnd(range);
695 unsigned start_line, start_column, end_line, end_column;
696 CXFile start_file, end_file;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000697 clang_getSpellingLocation(start, &start_file, &start_line,
698 &start_column, 0);
699 clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0);
Douglas Gregor473d7012010-02-19 18:16:06 +0000700 if (clang_equalLocations(start, end)) {
701 /* Insertion. */
702 if (start_file == file)
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000703 fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n",
Douglas Gregor473d7012010-02-19 18:16:06 +0000704 clang_getCString(insertion_text), start_line, start_column);
705 } else if (strcmp(clang_getCString(insertion_text), "") == 0) {
706 /* Removal. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000707 if (start_file == file && end_file == file) {
708 fprintf(out, "FIX-IT: Remove ");
709 PrintExtent(out, start_line, start_column, end_line, end_column);
710 fprintf(out, "\n");
Douglas Gregor51c6d382010-01-29 00:41:11 +0000711 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000712 } else {
713 /* Replacement. */
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000714 if (start_file == end_file) {
715 fprintf(out, "FIX-IT: Replace ");
716 PrintExtent(out, start_line, start_column, end_line, end_column);
Douglas Gregor473d7012010-02-19 18:16:06 +0000717 fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text));
Douglas Gregor436f3f02010-02-18 22:27:07 +0000718 }
Douglas Gregor0a812cf2010-02-18 23:07:20 +0000719 break;
720 }
Douglas Gregor473d7012010-02-19 18:16:06 +0000721 clang_disposeString(insertion_text);
Douglas Gregor51c6d382010-01-29 00:41:11 +0000722 }
Douglas Gregor5352ac02010-01-28 00:27:43 +0000723}
724
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000725void PrintDiagnosticSet(CXDiagnosticSet Set) {
726 int i = 0, n = clang_getNumDiagnosticsInSet(Set);
727 for ( ; i != n ; ++i) {
728 CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i);
729 CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag);
Douglas Gregora88084b2010-02-18 18:08:43 +0000730 PrintDiagnostic(Diag);
Ted Kremenek7473b1c2012-02-14 02:46:03 +0000731 if (ChildDiags)
732 PrintDiagnosticSet(ChildDiags);
733 }
734}
735
736void PrintDiagnostics(CXTranslationUnit TU) {
737 CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU);
738 PrintDiagnosticSet(TUSet);
739 clang_disposeDiagnosticSet(TUSet);
Douglas Gregora88084b2010-02-18 18:08:43 +0000740}
741
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000742void PrintMemoryUsage(CXTranslationUnit TU) {
Matt Beaumont-Gayb2273232011-08-29 16:37:29 +0000743 unsigned long total = 0;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000744 unsigned i = 0;
Ted Kremenekf7870022011-04-20 16:41:07 +0000745 CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU);
Francois Pichet3c683362011-04-18 23:33:22 +0000746 fprintf(stderr, "Memory usage:\n");
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000747 for (i = 0 ; i != usage.numEntries; ++i) {
Ted Kremenekf7870022011-04-20 16:41:07 +0000748 const char *name = clang_getTUResourceUsageName(usage.entries[i].kind);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000749 unsigned long amount = usage.entries[i].amount;
750 total += amount;
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000751 fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000752 ((double) amount)/(1024*1024));
753 }
Ted Kremenek4e6a3f72011-04-18 23:42:53 +0000754 fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total,
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000755 ((double) total)/(1024*1024));
Ted Kremenekf7870022011-04-20 16:41:07 +0000756 clang_disposeCXTUResourceUsage(usage);
Ted Kremenek59fc1e52011-04-18 22:47:10 +0000757}
758
Ted Kremenekce2ae882010-01-26 17:59:48 +0000759/******************************************************************************/
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000760/* Logic for testing traversal. */
Ted Kremenek0d435192009-11-17 18:13:31 +0000761/******************************************************************************/
762
Douglas Gregora7bde202010-01-19 00:34:46 +0000763static void PrintCursorExtent(CXCursor C) {
764 CXSourceRange extent = clang_getCursorExtent(C);
Douglas Gregor430d7a12011-07-25 17:48:11 +0000765 PrintRange(extent, "Extent");
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000766}
767
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000768/* Data used by all of the visitors. */
769typedef struct {
770 CXTranslationUnit TU;
771 enum CXCursorKind *Filter;
772} VisitorData;
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000773
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000774
Ted Kremeneke68fff62010-02-17 00:41:32 +0000775enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000776 CXCursor Parent,
777 CXClientData ClientData) {
778 VisitorData *Data = (VisitorData *)ClientData;
779 if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000780 CXSourceLocation Loc = clang_getCursorLocation(Cursor);
Douglas Gregor1db19de2010-01-19 21:36:55 +0000781 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000782 clang_getSpellingLocation(Loc, 0, &line, &column, 0);
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +0000783 printf("// %s: %s:%d:%d: ", FileCheckPrefix,
Douglas Gregor1db19de2010-01-19 21:36:55 +0000784 GetCursorSource(Cursor), line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000785 PrintCursor(Cursor);
Douglas Gregora7bde202010-01-19 00:34:46 +0000786 PrintCursorExtent(Cursor);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000787 printf("\n");
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000788 return CXChildVisit_Recurse;
Steve Naroff2d4d6292009-08-31 14:26:51 +0000789 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000790
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000791 return CXChildVisit_Continue;
Steve Naroff89922f82009-08-31 00:59:03 +0000792}
Steve Naroff50398192009-08-28 15:28:48 +0000793
Ted Kremeneke68fff62010-02-17 00:41:32 +0000794static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor,
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000795 CXCursor Parent,
796 CXClientData ClientData) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000797 const char *startBuf, *endBuf;
798 unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn;
799 CXCursor Ref;
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000800 VisitorData *Data = (VisitorData *)ClientData;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000801
Douglas Gregorb6998662010-01-19 19:34:47 +0000802 if (Cursor.kind != CXCursor_FunctionDecl ||
803 !clang_isCursorDefinition(Cursor))
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000804 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000805
806 clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf,
807 &startLine, &startColumn,
808 &endLine, &endColumn);
809 /* Probe the entire body, looking for both decls and refs. */
810 curLine = startLine;
811 curColumn = startColumn;
812
813 while (startBuf < endBuf) {
Douglas Gregor98258af2010-01-18 22:46:11 +0000814 CXSourceLocation Loc;
Douglas Gregor1db19de2010-01-19 21:36:55 +0000815 CXFile file;
Ted Kremenek74844072010-02-17 00:41:20 +0000816 CXString source;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000817
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000818 if (*startBuf == '\n') {
819 startBuf++;
820 curLine++;
821 curColumn = 1;
822 } else if (*startBuf != '\t')
823 curColumn++;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000824
Douglas Gregor98258af2010-01-18 22:46:11 +0000825 Loc = clang_getCursorLocation(Cursor);
Douglas Gregora9b06d42010-11-09 06:24:54 +0000826 clang_getSpellingLocation(Loc, &file, 0, 0, 0);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000827
Douglas Gregor1db19de2010-01-19 21:36:55 +0000828 source = clang_getFileName(file);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000829 if (clang_getCString(source)) {
Douglas Gregorb9790342010-01-22 21:44:22 +0000830 CXSourceLocation RefLoc
831 = clang_getLocation(Data->TU, file, curLine, curColumn);
832 Ref = clang_getCursor(Data->TU, RefLoc);
Douglas Gregor98258af2010-01-18 22:46:11 +0000833 if (Ref.kind == CXCursor_NoDeclFound) {
834 /* Nothing found here; that's fine. */
835 } else if (Ref.kind != CXCursor_FunctionDecl) {
836 printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref),
837 curLine, curColumn);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000838 PrintCursor(Ref);
Douglas Gregor98258af2010-01-18 22:46:11 +0000839 printf("\n");
840 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000841 }
Ted Kremenek74844072010-02-17 00:41:20 +0000842 clang_disposeString(source);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000843 startBuf++;
844 }
Ted Kremeneke68fff62010-02-17 00:41:32 +0000845
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000846 return CXChildVisit_Continue;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000847}
848
Ted Kremenek7d405622010-01-12 23:34:26 +0000849/******************************************************************************/
850/* USR testing. */
851/******************************************************************************/
852
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000853enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent,
854 CXClientData ClientData) {
855 VisitorData *Data = (VisitorData *)ClientData;
856 if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) {
Ted Kremenekcf84aa42010-01-18 20:23:29 +0000857 CXString USR = clang_getCursorUSR(C);
Ted Kremeneke542f772010-04-20 23:15:40 +0000858 const char *cstr = clang_getCString(USR);
859 if (!cstr || cstr[0] == '\0') {
Ted Kremenek7d405622010-01-12 23:34:26 +0000860 clang_disposeString(USR);
Ted Kremeneke74ef122010-04-16 21:31:52 +0000861 return CXChildVisit_Recurse;
Ted Kremenek7d405622010-01-12 23:34:26 +0000862 }
Ted Kremeneke542f772010-04-20 23:15:40 +0000863 printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr);
864
Douglas Gregora7bde202010-01-19 00:34:46 +0000865 PrintCursorExtent(C);
Ted Kremenek7d405622010-01-12 23:34:26 +0000866 printf("\n");
867 clang_disposeString(USR);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000868
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000869 return CXChildVisit_Recurse;
Ted Kremeneke68fff62010-02-17 00:41:32 +0000870 }
871
Douglas Gregore5b72ba2010-01-20 21:32:04 +0000872 return CXChildVisit_Continue;
Ted Kremenek7d405622010-01-12 23:34:26 +0000873}
874
875/******************************************************************************/
Ted Kremenek16b55a72010-01-26 19:31:51 +0000876/* Inclusion stack testing. */
877/******************************************************************************/
878
879void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack,
880 unsigned includeStackLen, CXClientData data) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000881
Ted Kremenek16b55a72010-01-26 19:31:51 +0000882 unsigned i;
Ted Kremenek74844072010-02-17 00:41:20 +0000883 CXString fname;
884
885 fname = clang_getFileName(includedFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000886 printf("file: %s\nincluded by:\n", clang_getCString(fname));
Ted Kremenek74844072010-02-17 00:41:20 +0000887 clang_disposeString(fname);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000888
Ted Kremenek16b55a72010-01-26 19:31:51 +0000889 for (i = 0; i < includeStackLen; ++i) {
890 CXFile includingFile;
891 unsigned line, column;
Douglas Gregora9b06d42010-11-09 06:24:54 +0000892 clang_getSpellingLocation(includeStack[i], &includingFile, &line,
893 &column, 0);
Ted Kremenek74844072010-02-17 00:41:20 +0000894 fname = clang_getFileName(includingFile);
Ted Kremeneke68fff62010-02-17 00:41:32 +0000895 printf(" %s:%d:%d\n", clang_getCString(fname), line, column);
Ted Kremenek74844072010-02-17 00:41:20 +0000896 clang_disposeString(fname);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000897 }
898 printf("\n");
899}
900
901void PrintInclusionStack(CXTranslationUnit TU) {
Ted Kremeneke68fff62010-02-17 00:41:32 +0000902 clang_getInclusions(TU, InclusionVisitor, NULL);
Ted Kremenek16b55a72010-01-26 19:31:51 +0000903}
904
905/******************************************************************************/
Ted Kremenek3bed5272010-03-03 06:37:58 +0000906/* Linkage testing. */
907/******************************************************************************/
908
909static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p,
910 CXClientData d) {
911 const char *linkage = 0;
912
913 if (clang_isInvalid(clang_getCursorKind(cursor)))
914 return CXChildVisit_Recurse;
915
916 switch (clang_getCursorLinkage(cursor)) {
917 case CXLinkage_Invalid: break;
Douglas Gregorc2a2b3c2010-03-04 19:36:27 +0000918 case CXLinkage_NoLinkage: linkage = "NoLinkage"; break;
919 case CXLinkage_Internal: linkage = "Internal"; break;
920 case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break;
921 case CXLinkage_External: linkage = "External"; break;
Ted Kremenek3bed5272010-03-03 06:37:58 +0000922 }
923
924 if (linkage) {
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000925 PrintCursor(cursor);
Ted Kremenek3bed5272010-03-03 06:37:58 +0000926 printf("linkage=%s\n", linkage);
927 }
928
929 return CXChildVisit_Recurse;
930}
931
932/******************************************************************************/
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000933/* Typekind testing. */
934/******************************************************************************/
935
936static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p,
937 CXClientData d) {
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000938 if (!clang_isInvalid(clang_getCursorKind(cursor))) {
939 CXType T = clang_getCursorType(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000940 CXString S = clang_getTypeKindSpelling(T.kind);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +0000941 PrintCursor(cursor);
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000942 printf(" typekind=%s", clang_getCString(S));
Douglas Gregore72fb6f2011-01-27 16:27:11 +0000943 if (clang_isConstQualifiedType(T))
944 printf(" const");
945 if (clang_isVolatileQualifiedType(T))
946 printf(" volatile");
947 if (clang_isRestrictQualifiedType(T))
948 printf(" restrict");
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000949 clang_disposeString(S);
Benjamin Kramere1403d22010-06-22 09:29:44 +0000950 /* Print the canonical type if it is different. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000951 {
952 CXType CT = clang_getCanonicalType(T);
953 if (!clang_equalTypes(T, CT)) {
954 CXString CS = clang_getTypeKindSpelling(CT.kind);
955 printf(" [canonical=%s]", clang_getCString(CS));
956 clang_disposeString(CS);
957 }
958 }
Benjamin Kramere1403d22010-06-22 09:29:44 +0000959 /* Print the return type if it exists. */
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000960 {
Ted Kremenek9a140842010-06-21 20:48:56 +0000961 CXType RT = clang_getCursorResultType(cursor);
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000962 if (RT.kind != CXType_Invalid) {
963 CXString RS = clang_getTypeKindSpelling(RT.kind);
964 printf(" [result=%s]", clang_getCString(RS));
965 clang_disposeString(RS);
966 }
967 }
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000968 /* Print the argument types if they exist. */
969 {
970 int numArgs = clang_Cursor_getNumArguments(cursor);
971 if (numArgs != -1 && numArgs != 0) {
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000972 int i;
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000973 printf(" [args=");
Argyrios Kyrtzidis47f11652012-04-11 19:54:09 +0000974 for (i = 0; i < numArgs; ++i) {
Argyrios Kyrtzidisd98ef9a2012-04-11 19:32:19 +0000975 CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i));
976 if (T.kind != CXType_Invalid) {
977 CXString S = clang_getTypeKindSpelling(T.kind);
978 printf(" %s", clang_getCString(S));
979 clang_disposeString(S);
980 }
981 }
982 printf("]");
983 }
984 }
Ted Kremenek3ce9e7d2010-07-30 00:14:11 +0000985 /* Print if this is a non-POD type. */
986 printf(" [isPOD=%d]", clang_isPODType(T));
Ted Kremenek04c3cf32010-06-21 20:15:39 +0000987
Ted Kremenek8e0ac172010-05-14 21:29:26 +0000988 printf("\n");
989 }
990 return CXChildVisit_Recurse;
991}
992
993
994/******************************************************************************/
Ted Kremenek7d405622010-01-12 23:34:26 +0000995/* Loading ASTs/source. */
996/******************************************************************************/
997
Daniel Dunbar625e4ef2009-12-01 02:35:37 +0000998static int perform_test_load(CXIndex Idx, CXTranslationUnit TU,
Ted Kremenek98271562010-01-12 18:53:15 +0000999 const char *filter, const char *prefix,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001000 CXCursorVisitor Visitor,
1001 PostVisitTU PV) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001002
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001003 if (prefix)
Ted Kremeneke68fff62010-02-17 00:41:32 +00001004 FileCheckPrefix = prefix;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001005
1006 if (Visitor) {
1007 enum CXCursorKind K = CXCursor_NotImplemented;
1008 enum CXCursorKind *ck = &K;
1009 VisitorData Data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001010
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001011 /* Perform some simple filtering. */
1012 if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL;
Douglas Gregor358559d2010-10-02 22:49:11 +00001013 else if (!strcmp(filter, "all-display") ||
1014 !strcmp(filter, "local-display")) {
1015 ck = NULL;
1016 want_display_name = 1;
1017 }
Daniel Dunbarb1ffee62010-02-10 20:42:40 +00001018 else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0;
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001019 else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl;
1020 else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl;
1021 else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl;
1022 else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl;
1023 else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl;
1024 else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor;
1025 else {
1026 fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter);
1027 return 1;
1028 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001029
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001030 Data.TU = TU;
1031 Data.Filter = ck;
1032 clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data);
Ted Kremenek0d435192009-11-17 18:13:31 +00001033 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001034
Ted Kremenekce2ae882010-01-26 17:59:48 +00001035 if (PV)
1036 PV(TU);
Ted Kremeneke3ee02a2010-01-26 17:55:33 +00001037
Douglas Gregora88084b2010-02-18 18:08:43 +00001038 PrintDiagnostics(TU);
Argyrios Kyrtzidis16ac8be2011-11-13 23:39:14 +00001039 if (checkForErrors(TU) != 0) {
1040 clang_disposeTranslationUnit(TU);
1041 return -1;
1042 }
1043
Ted Kremenek0d435192009-11-17 18:13:31 +00001044 clang_disposeTranslationUnit(TU);
1045 return 0;
1046}
1047
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00001048int perform_test_load_tu(const char *file, const char *filter,
Ted Kremenekce2ae882010-01-26 17:59:48 +00001049 const char *prefix, CXCursorVisitor Visitor,
1050 PostVisitTU PV) {
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001051 CXIndex Idx;
1052 CXTranslationUnit TU;
Ted Kremenek020a0952010-02-11 07:41:25 +00001053 int result;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001054 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001055 !strcmp(filter, "local") ? 1 : 0,
1056 /* displayDiagnosics=*/1);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001057
Ted Kremenek020a0952010-02-11 07:41:25 +00001058 if (!CreateTranslationUnit(Idx, file, &TU)) {
1059 clang_disposeIndex(Idx);
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001060 return 1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001061 }
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001062
Ted Kremenek020a0952010-02-11 07:41:25 +00001063 result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV);
1064 clang_disposeIndex(Idx);
1065 return result;
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00001066}
1067
Ted Kremenekce2ae882010-01-26 17:59:48 +00001068int perform_test_load_source(int argc, const char **argv,
1069 const char *filter, CXCursorVisitor Visitor,
1070 PostVisitTU PV) {
Daniel Dunbarada487d2009-12-01 02:03:10 +00001071 CXIndex Idx;
1072 CXTranslationUnit TU;
Douglas Gregor4db64a42010-01-23 00:14:00 +00001073 struct CXUnsavedFile *unsaved_files = 0;
1074 int num_unsaved_files = 0;
1075 int result;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001076
Daniel Dunbarada487d2009-12-01 02:03:10 +00001077 Idx = clang_createIndex(/* excludeDeclsFromPCH */
Douglas Gregor358559d2010-10-02 22:49:11 +00001078 (!strcmp(filter, "local") ||
1079 !strcmp(filter, "local-display"))? 1 : 0,
Douglas Gregor4814fb52011-02-03 23:41:12 +00001080 /* displayDiagnosics=*/0);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001081
Ted Kremenek020a0952010-02-11 07:41:25 +00001082 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1083 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001084 return -1;
Ted Kremenek020a0952010-02-11 07:41:25 +00001085 }
Douglas Gregor4db64a42010-01-23 00:14:00 +00001086
Douglas Gregordca8ee82011-05-06 16:33:08 +00001087 TU = clang_parseTranslationUnit(Idx, 0,
1088 argv + num_unsaved_files,
1089 argc - num_unsaved_files,
1090 unsaved_files, num_unsaved_files,
1091 getDefaultParsingOptions());
Daniel Dunbarada487d2009-12-01 02:03:10 +00001092 if (!TU) {
1093 fprintf(stderr, "Unable to load translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001094 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001095 clang_disposeIndex(Idx);
Daniel Dunbarada487d2009-12-01 02:03:10 +00001096 return 1;
1097 }
1098
Ted Kremenekce2ae882010-01-26 17:59:48 +00001099 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001100 free_remapped_files(unsaved_files, num_unsaved_files);
Ted Kremenek020a0952010-02-11 07:41:25 +00001101 clang_disposeIndex(Idx);
Douglas Gregor4db64a42010-01-23 00:14:00 +00001102 return result;
Daniel Dunbarada487d2009-12-01 02:03:10 +00001103}
1104
Douglas Gregorabc563f2010-07-19 21:46:24 +00001105int perform_test_reparse_source(int argc, const char **argv, int trials,
1106 const char *filter, CXCursorVisitor Visitor,
1107 PostVisitTU PV) {
Douglas Gregorabc563f2010-07-19 21:46:24 +00001108 CXIndex Idx;
1109 CXTranslationUnit TU;
1110 struct CXUnsavedFile *unsaved_files = 0;
1111 int num_unsaved_files = 0;
1112 int result;
1113 int trial;
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001114 int remap_after_trial = 0;
1115 char *endptr = 0;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001116
1117 Idx = clang_createIndex(/* excludeDeclsFromPCH */
1118 !strcmp(filter, "local") ? 1 : 0,
Douglas Gregor1aa27302011-01-27 18:02:58 +00001119 /* displayDiagnosics=*/0);
Douglas Gregorabc563f2010-07-19 21:46:24 +00001120
Douglas Gregorabc563f2010-07-19 21:46:24 +00001121 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
1122 clang_disposeIndex(Idx);
1123 return -1;
1124 }
1125
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001126 /* Load the initial translation unit -- we do this without honoring remapped
1127 * files, so that we have a way to test results after changing the source. */
Douglas Gregor44c181a2010-07-23 00:33:23 +00001128 TU = clang_parseTranslationUnit(Idx, 0,
1129 argv + num_unsaved_files,
1130 argc - num_unsaved_files,
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001131 0, 0, getDefaultParsingOptions());
Douglas Gregorabc563f2010-07-19 21:46:24 +00001132 if (!TU) {
1133 fprintf(stderr, "Unable to load translation unit!\n");
1134 free_remapped_files(unsaved_files, num_unsaved_files);
1135 clang_disposeIndex(Idx);
1136 return 1;
1137 }
1138
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001139 if (checkForErrors(TU) != 0)
1140 return -1;
1141
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001142 if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) {
1143 remap_after_trial =
1144 strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10);
1145 }
1146
Douglas Gregorabc563f2010-07-19 21:46:24 +00001147 for (trial = 0; trial < trials; ++trial) {
Argyrios Kyrtzidis40098e82011-09-12 18:09:31 +00001148 if (clang_reparseTranslationUnit(TU,
1149 trial >= remap_after_trial ? num_unsaved_files : 0,
1150 trial >= remap_after_trial ? unsaved_files : 0,
Douglas Gregore1e13bf2010-08-11 15:58:42 +00001151 clang_defaultReparseOptions(TU))) {
Daniel Dunbarc8a61802010-08-18 23:09:16 +00001152 fprintf(stderr, "Unable to reparse translation unit!\n");
Douglas Gregorabc563f2010-07-19 21:46:24 +00001153 clang_disposeTranslationUnit(TU);
1154 free_remapped_files(unsaved_files, num_unsaved_files);
1155 clang_disposeIndex(Idx);
1156 return -1;
1157 }
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001158
1159 if (checkForErrors(TU) != 0)
1160 return -1;
Douglas Gregorabc563f2010-07-19 21:46:24 +00001161 }
1162
1163 result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV);
Argyrios Kyrtzidisbda536d2011-11-13 22:08:33 +00001164
Douglas Gregorabc563f2010-07-19 21:46:24 +00001165 free_remapped_files(unsaved_files, num_unsaved_files);
1166 clang_disposeIndex(Idx);
1167 return result;
1168}
1169
Ted Kremenek0d435192009-11-17 18:13:31 +00001170/******************************************************************************/
Ted Kremenek1c6da172009-11-17 19:37:36 +00001171/* Logic for testing clang_getCursor(). */
1172/******************************************************************************/
1173
Douglas Gregordd3e5542011-05-04 00:14:37 +00001174static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor,
Ted Kremenek1c6da172009-11-17 19:37:36 +00001175 unsigned start_line, unsigned start_col,
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001176 unsigned end_line, unsigned end_col,
1177 const char *prefix) {
Ted Kremenek9096a202010-01-07 01:17:12 +00001178 printf("// %s: ", FileCheckPrefix);
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001179 if (prefix)
1180 printf("-%s", prefix);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00001181 PrintExtent(stdout, start_line, start_col, end_line, end_col);
1182 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001183 PrintCursor(cursor);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001184 printf("\n");
1185}
1186
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00001187static int perform_file_scan(const char *ast_file, const char *source_file,
1188 const char *prefix) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001189 CXIndex Idx;
1190 CXTranslationUnit TU;
1191 FILE *fp;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001192 CXCursor prevCursor = clang_getNullCursor();
Douglas Gregorb9790342010-01-22 21:44:22 +00001193 CXFile file;
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001194 unsigned line = 1, col = 1;
Daniel Dunbar8f0bf812010-02-14 08:32:51 +00001195 unsigned start_line = 1, start_col = 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001196
Douglas Gregor0a812cf2010-02-18 23:07:20 +00001197 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
1198 /* displayDiagnosics=*/1))) {
Ted Kremenek1c6da172009-11-17 19:37:36 +00001199 fprintf(stderr, "Could not create Index\n");
1200 return 1;
1201 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001202
Ted Kremenek1c6da172009-11-17 19:37:36 +00001203 if (!CreateTranslationUnit(Idx, ast_file, &TU))
1204 return 1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001205
Ted Kremenek1c6da172009-11-17 19:37:36 +00001206 if ((fp = fopen(source_file, "r")) == NULL) {
1207 fprintf(stderr, "Could not open '%s'\n", source_file);
1208 return 1;
1209 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001210
Douglas Gregorb9790342010-01-22 21:44:22 +00001211 file = clang_getFile(TU, source_file);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001212 for (;;) {
1213 CXCursor cursor;
1214 int c = fgetc(fp);
Benjamin Kramera9933b92009-11-17 20:51:40 +00001215
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001216 if (c == '\n') {
1217 ++line;
1218 col = 1;
1219 } else
1220 ++col;
1221
1222 /* Check the cursor at this position, and dump the previous one if we have
1223 * found something new.
1224 */
1225 cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col));
1226 if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) &&
1227 prevCursor.kind != CXCursor_InvalidFile) {
Douglas Gregordd3e5542011-05-04 00:14:37 +00001228 print_cursor_file_scan(TU, prevCursor, start_line, start_col,
Daniel Dunbard52864b2010-02-14 10:02:57 +00001229 line, col, prefix);
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001230 start_line = line;
1231 start_col = col;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001232 }
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001233 if (c == EOF)
1234 break;
Benjamin Kramera9933b92009-11-17 20:51:40 +00001235
Daniel Dunbar2389eff2010-02-14 08:32:32 +00001236 prevCursor = cursor;
Ted Kremenek1c6da172009-11-17 19:37:36 +00001237 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001238
Ted Kremenek1c6da172009-11-17 19:37:36 +00001239 fclose(fp);
Douglas Gregor4f5e21e2011-01-31 22:04:05 +00001240 clang_disposeTranslationUnit(TU);
1241 clang_disposeIndex(Idx);
Ted Kremenek1c6da172009-11-17 19:37:36 +00001242 return 0;
1243}
1244
1245/******************************************************************************/
Douglas Gregor32be4a52010-10-11 21:37:58 +00001246/* Logic for testing clang code completion. */
Ted Kremenek0d435192009-11-17 18:13:31 +00001247/******************************************************************************/
1248
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001249/* Parse file:line:column from the input string. Returns 0 on success, non-zero
1250 on failure. If successful, the pointer *filename will contain newly-allocated
1251 memory (that will be owned by the caller) to store the file name. */
Ted Kremeneke68fff62010-02-17 00:41:32 +00001252int parse_file_line_column(const char *input, char **filename, unsigned *line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001253 unsigned *column, unsigned *second_line,
1254 unsigned *second_column) {
Douglas Gregor88d23952009-11-09 18:19:57 +00001255 /* Find the second colon. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001256 const char *last_colon = strrchr(input, ':');
1257 unsigned values[4], i;
1258 unsigned num_values = (second_line && second_column)? 4 : 2;
1259
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001260 char *endptr = 0;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001261 if (!last_colon || last_colon == input) {
1262 if (num_values == 4)
1263 fprintf(stderr, "could not parse filename:line:column:line:column in "
1264 "'%s'\n", input);
1265 else
1266 fprintf(stderr, "could not parse filename:line:column in '%s'\n", input);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001267 return 1;
1268 }
1269
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001270 for (i = 0; i != num_values; ++i) {
1271 const char *prev_colon;
1272
1273 /* Parse the next line or column. */
1274 values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10);
1275 if (*endptr != 0 && *endptr != ':') {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001276 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001277 (i % 2 ? "column" : "line"), input);
1278 return 1;
1279 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001280
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001281 if (i + 1 == num_values)
1282 break;
1283
1284 /* Find the previous colon. */
1285 prev_colon = last_colon - 1;
1286 while (prev_colon != input && *prev_colon != ':')
1287 --prev_colon;
1288 if (prev_colon == input) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00001289 fprintf(stderr, "could not parse %s in '%s'\n",
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001290 (i % 2 == 0? "column" : "line"), input);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001291 return 1;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001292 }
1293
1294 last_colon = prev_colon;
Douglas Gregor88d23952009-11-09 18:19:57 +00001295 }
1296
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001297 *line = values[0];
1298 *column = values[1];
Ted Kremeneke68fff62010-02-17 00:41:32 +00001299
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001300 if (second_line && second_column) {
1301 *second_line = values[2];
1302 *second_column = values[3];
1303 }
1304
Douglas Gregor88d23952009-11-09 18:19:57 +00001305 /* Copy the file name. */
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001306 *filename = (char*)malloc(last_colon - input + 1);
1307 memcpy(*filename, input, last_colon - input);
1308 (*filename)[last_colon - input] = 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001309 return 0;
1310}
1311
1312const char *
1313clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) {
1314 switch (Kind) {
1315 case CXCompletionChunk_Optional: return "Optional";
1316 case CXCompletionChunk_TypedText: return "TypedText";
1317 case CXCompletionChunk_Text: return "Text";
1318 case CXCompletionChunk_Placeholder: return "Placeholder";
1319 case CXCompletionChunk_Informative: return "Informative";
1320 case CXCompletionChunk_CurrentParameter: return "CurrentParameter";
1321 case CXCompletionChunk_LeftParen: return "LeftParen";
1322 case CXCompletionChunk_RightParen: return "RightParen";
1323 case CXCompletionChunk_LeftBracket: return "LeftBracket";
1324 case CXCompletionChunk_RightBracket: return "RightBracket";
1325 case CXCompletionChunk_LeftBrace: return "LeftBrace";
1326 case CXCompletionChunk_RightBrace: return "RightBrace";
1327 case CXCompletionChunk_LeftAngle: return "LeftAngle";
1328 case CXCompletionChunk_RightAngle: return "RightAngle";
1329 case CXCompletionChunk_Comma: return "Comma";
Douglas Gregorff5ce6e2009-12-18 18:53:37 +00001330 case CXCompletionChunk_ResultType: return "ResultType";
Douglas Gregor01dfea02010-01-10 23:08:15 +00001331 case CXCompletionChunk_Colon: return "Colon";
1332 case CXCompletionChunk_SemiColon: return "SemiColon";
1333 case CXCompletionChunk_Equal: return "Equal";
1334 case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace";
1335 case CXCompletionChunk_VerticalSpace: return "VerticalSpace";
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001336 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001337
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001338 return "Unknown";
1339}
1340
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001341static int checkForErrors(CXTranslationUnit TU) {
1342 unsigned Num, i;
1343 CXDiagnostic Diag;
1344 CXString DiagStr;
1345
1346 if (!getenv("CINDEXTEST_FAILONERROR"))
1347 return 0;
1348
1349 Num = clang_getNumDiagnostics(TU);
1350 for (i = 0; i != Num; ++i) {
1351 Diag = clang_getDiagnostic(TU, i);
1352 if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) {
1353 DiagStr = clang_formatDiagnostic(Diag,
1354 clang_defaultDiagnosticDisplayOptions());
1355 fprintf(stderr, "%s\n", clang_getCString(DiagStr));
1356 clang_disposeString(DiagStr);
1357 clang_disposeDiagnostic(Diag);
1358 return -1;
1359 }
1360 clang_disposeDiagnostic(Diag);
1361 }
1362
1363 return 0;
1364}
1365
Douglas Gregor3ac73852009-11-09 16:04:45 +00001366void print_completion_string(CXCompletionString completion_string, FILE *file) {
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001367 int I, N;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001368
Douglas Gregor3ac73852009-11-09 16:04:45 +00001369 N = clang_getNumCompletionChunks(completion_string);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001370 for (I = 0; I != N; ++I) {
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001371 CXString text;
1372 const char *cstr;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001373 enum CXCompletionChunkKind Kind
Douglas Gregor3ac73852009-11-09 16:04:45 +00001374 = clang_getCompletionChunkKind(completion_string, I);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001375
Douglas Gregor3ac73852009-11-09 16:04:45 +00001376 if (Kind == CXCompletionChunk_Optional) {
1377 fprintf(file, "{Optional ");
1378 print_completion_string(
Ted Kremeneke68fff62010-02-17 00:41:32 +00001379 clang_getCompletionChunkCompletionString(completion_string, I),
Douglas Gregor3ac73852009-11-09 16:04:45 +00001380 file);
1381 fprintf(file, "}");
1382 continue;
Douglas Gregor5a9c0bc2010-10-08 20:39:29 +00001383 }
1384
1385 if (Kind == CXCompletionChunk_VerticalSpace) {
1386 fprintf(file, "{VerticalSpace }");
1387 continue;
Douglas Gregor3ac73852009-11-09 16:04:45 +00001388 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001389
Douglas Gregord5a20892009-11-09 17:05:28 +00001390 text = clang_getCompletionChunkText(completion_string, I);
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001391 cstr = clang_getCString(text);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001392 fprintf(file, "{%s %s}",
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001393 clang_getCompletionChunkKindSpelling(Kind),
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001394 cstr ? cstr : "");
1395 clang_disposeString(text);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001396 }
Ted Kremenek2ef6f8f2010-02-17 01:42:24 +00001397
Douglas Gregor3ac73852009-11-09 16:04:45 +00001398}
1399
1400void print_completion_result(CXCompletionResult *completion_result,
1401 CXClientData client_data) {
1402 FILE *file = (FILE *)client_data;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001403 CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind);
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001404 unsigned annotationCount;
Douglas Gregorba103062012-03-27 23:34:16 +00001405 enum CXCursorKind ParentKind;
1406 CXString ParentName;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001407 CXString BriefComment;
1408 const char *BriefCommentCString;
Douglas Gregorba103062012-03-27 23:34:16 +00001409
Ted Kremeneke68fff62010-02-17 00:41:32 +00001410 fprintf(file, "%s:", clang_getCString(ks));
1411 clang_disposeString(ks);
1412
Douglas Gregor3ac73852009-11-09 16:04:45 +00001413 print_completion_string(completion_result->CompletionString, file);
Douglas Gregor58ddb602010-08-23 23:00:57 +00001414 fprintf(file, " (%u)",
Douglas Gregor12e13132010-05-26 22:00:08 +00001415 clang_getCompletionPriority(completion_result->CompletionString));
Douglas Gregor58ddb602010-08-23 23:00:57 +00001416 switch (clang_getCompletionAvailability(completion_result->CompletionString)){
1417 case CXAvailability_Available:
1418 break;
1419
1420 case CXAvailability_Deprecated:
1421 fprintf(file, " (deprecated)");
1422 break;
1423
1424 case CXAvailability_NotAvailable:
1425 fprintf(file, " (unavailable)");
1426 break;
Erik Verbruggend1205962011-10-06 07:27:49 +00001427
1428 case CXAvailability_NotAccessible:
1429 fprintf(file, " (inaccessible)");
1430 break;
Douglas Gregor58ddb602010-08-23 23:00:57 +00001431 }
Erik Verbruggen6164ea12011-10-14 15:31:08 +00001432
1433 annotationCount = clang_getCompletionNumAnnotations(
1434 completion_result->CompletionString);
1435 if (annotationCount) {
1436 unsigned i;
1437 fprintf(file, " (");
1438 for (i = 0; i < annotationCount; ++i) {
1439 if (i != 0)
1440 fprintf(file, ", ");
1441 fprintf(file, "\"%s\"",
1442 clang_getCString(clang_getCompletionAnnotation(
1443 completion_result->CompletionString, i)));
1444 }
1445 fprintf(file, ")");
1446 }
1447
Douglas Gregorba103062012-03-27 23:34:16 +00001448 if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) {
1449 ParentName = clang_getCompletionParent(completion_result->CompletionString,
1450 &ParentKind);
1451 if (ParentKind != CXCursor_NotImplemented) {
1452 CXString KindSpelling = clang_getCursorKindSpelling(ParentKind);
1453 fprintf(file, " (parent: %s '%s')",
1454 clang_getCString(KindSpelling),
1455 clang_getCString(ParentName));
1456 clang_disposeString(KindSpelling);
1457 }
1458 clang_disposeString(ParentName);
1459 }
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001460
1461 BriefComment = clang_getCompletionBriefComment(
1462 completion_result->CompletionString);
1463 BriefCommentCString = clang_getCString(BriefComment);
1464 if (BriefCommentCString && *BriefCommentCString != '\0') {
1465 fprintf(file, "(brief comment: %s)", BriefCommentCString);
1466 }
1467 clang_disposeString(BriefComment);
Douglas Gregorba103062012-03-27 23:34:16 +00001468
Douglas Gregor58ddb602010-08-23 23:00:57 +00001469 fprintf(file, "\n");
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001470}
1471
Douglas Gregor3da626b2011-07-07 16:03:39 +00001472void print_completion_contexts(unsigned long long contexts, FILE *file) {
1473 fprintf(file, "Completion contexts:\n");
1474 if (contexts == CXCompletionContext_Unknown) {
1475 fprintf(file, "Unknown\n");
1476 }
1477 if (contexts & CXCompletionContext_AnyType) {
1478 fprintf(file, "Any type\n");
1479 }
1480 if (contexts & CXCompletionContext_AnyValue) {
1481 fprintf(file, "Any value\n");
1482 }
1483 if (contexts & CXCompletionContext_ObjCObjectValue) {
1484 fprintf(file, "Objective-C object value\n");
1485 }
1486 if (contexts & CXCompletionContext_ObjCSelectorValue) {
1487 fprintf(file, "Objective-C selector value\n");
1488 }
1489 if (contexts & CXCompletionContext_CXXClassTypeValue) {
1490 fprintf(file, "C++ class type value\n");
1491 }
1492 if (contexts & CXCompletionContext_DotMemberAccess) {
1493 fprintf(file, "Dot member access\n");
1494 }
1495 if (contexts & CXCompletionContext_ArrowMemberAccess) {
1496 fprintf(file, "Arrow member access\n");
1497 }
1498 if (contexts & CXCompletionContext_ObjCPropertyAccess) {
1499 fprintf(file, "Objective-C property access\n");
1500 }
1501 if (contexts & CXCompletionContext_EnumTag) {
1502 fprintf(file, "Enum tag\n");
1503 }
1504 if (contexts & CXCompletionContext_UnionTag) {
1505 fprintf(file, "Union tag\n");
1506 }
1507 if (contexts & CXCompletionContext_StructTag) {
1508 fprintf(file, "Struct tag\n");
1509 }
1510 if (contexts & CXCompletionContext_ClassTag) {
1511 fprintf(file, "Class name\n");
1512 }
1513 if (contexts & CXCompletionContext_Namespace) {
1514 fprintf(file, "Namespace or namespace alias\n");
1515 }
1516 if (contexts & CXCompletionContext_NestedNameSpecifier) {
1517 fprintf(file, "Nested name specifier\n");
1518 }
1519 if (contexts & CXCompletionContext_ObjCInterface) {
1520 fprintf(file, "Objective-C interface\n");
1521 }
1522 if (contexts & CXCompletionContext_ObjCProtocol) {
1523 fprintf(file, "Objective-C protocol\n");
1524 }
1525 if (contexts & CXCompletionContext_ObjCCategory) {
1526 fprintf(file, "Objective-C category\n");
1527 }
1528 if (contexts & CXCompletionContext_ObjCInstanceMessage) {
1529 fprintf(file, "Objective-C instance method\n");
1530 }
1531 if (contexts & CXCompletionContext_ObjCClassMessage) {
1532 fprintf(file, "Objective-C class method\n");
1533 }
1534 if (contexts & CXCompletionContext_ObjCSelectorName) {
1535 fprintf(file, "Objective-C selector name\n");
1536 }
1537 if (contexts & CXCompletionContext_MacroName) {
1538 fprintf(file, "Macro name\n");
1539 }
1540 if (contexts & CXCompletionContext_NaturalLanguage) {
1541 fprintf(file, "Natural language\n");
1542 }
1543}
1544
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001545int my_stricmp(const char *s1, const char *s2) {
1546 while (*s1 && *s2) {
NAKAMURA Takumi6d555212011-03-09 03:02:28 +00001547 int c1 = tolower((unsigned char)*s1), c2 = tolower((unsigned char)*s2);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001548 if (c1 < c2)
1549 return -1;
1550 else if (c1 > c2)
1551 return 1;
1552
1553 ++s1;
1554 ++s2;
1555 }
1556
1557 if (*s1)
1558 return 1;
1559 else if (*s2)
1560 return -1;
1561 return 0;
1562}
1563
Douglas Gregor1982c182010-07-12 18:38:41 +00001564int perform_code_completion(int argc, const char **argv, int timing_only) {
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001565 const char *input = argv[1];
1566 char *filename = 0;
1567 unsigned line;
1568 unsigned column;
Daniel Dunbarf8297f12009-11-07 18:34:24 +00001569 CXIndex CIdx;
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001570 int errorCode;
Douglas Gregor735df882009-12-02 09:21:34 +00001571 struct CXUnsavedFile *unsaved_files = 0;
1572 int num_unsaved_files = 0;
Douglas Gregorec6762c2009-12-18 16:20:58 +00001573 CXCodeCompleteResults *results = 0;
Dawn Perchik25d9b002010-09-30 22:26:05 +00001574 CXTranslationUnit TU = 0;
Douglas Gregor32be4a52010-10-11 21:37:58 +00001575 unsigned I, Repeats = 1;
1576 unsigned completionOptions = clang_defaultCodeCompleteOptions();
1577
1578 if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS"))
1579 completionOptions |= CXCodeComplete_IncludeCodePatterns;
Dmitri Gribenkod99ef532012-07-02 17:35:10 +00001580 if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS"))
1581 completionOptions |= CXCodeComplete_IncludeBriefComments;
Douglas Gregordf95a132010-08-09 20:45:32 +00001582
Douglas Gregor1982c182010-07-12 18:38:41 +00001583 if (timing_only)
1584 input += strlen("-code-completion-timing=");
1585 else
1586 input += strlen("-code-completion-at=");
1587
Ted Kremeneke68fff62010-02-17 00:41:32 +00001588 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001589 0, 0)))
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001590 return errorCode;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001591
Douglas Gregor735df882009-12-02 09:21:34 +00001592 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files))
1593 return -1;
1594
Douglas Gregor32be4a52010-10-11 21:37:58 +00001595 CIdx = clang_createIndex(0, 0);
1596
1597 if (getenv("CINDEXTEST_EDITING"))
1598 Repeats = 5;
1599
1600 TU = clang_parseTranslationUnit(CIdx, 0,
1601 argv + num_unsaved_files + 2,
1602 argc - num_unsaved_files - 2,
1603 0, 0, getDefaultParsingOptions());
1604 if (!TU) {
1605 fprintf(stderr, "Unable to load translation unit!\n");
1606 return 1;
1607 }
Douglas Gregor08bb4c62010-11-15 23:00:34 +00001608
1609 if (clang_reparseTranslationUnit(TU, 0, 0, clang_defaultReparseOptions(TU))) {
1610 fprintf(stderr, "Unable to reparse translation init!\n");
1611 return 1;
1612 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001613
1614 for (I = 0; I != Repeats; ++I) {
1615 results = clang_codeCompleteAt(TU, filename, line, column,
1616 unsaved_files, num_unsaved_files,
1617 completionOptions);
1618 if (!results) {
1619 fprintf(stderr, "Unable to perform code completion!\n");
Daniel Dunbar2de41c92010-08-19 23:44:06 +00001620 return 1;
1621 }
Douglas Gregor32be4a52010-10-11 21:37:58 +00001622 if (I != Repeats-1)
1623 clang_disposeCodeCompleteResults(results);
1624 }
Douglas Gregor936ea3b2010-01-28 00:56:43 +00001625
Douglas Gregorec6762c2009-12-18 16:20:58 +00001626 if (results) {
Douglas Gregore081a612011-07-21 01:05:26 +00001627 unsigned i, n = results->NumResults, containerIsIncomplete = 0;
Douglas Gregor3da626b2011-07-07 16:03:39 +00001628 unsigned long long contexts;
Douglas Gregore081a612011-07-21 01:05:26 +00001629 enum CXCursorKind containerKind;
Douglas Gregor0a47d692011-07-26 15:24:30 +00001630 CXString objCSelector;
1631 const char *selectorString;
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001632 if (!timing_only) {
1633 /* Sort the code-completion results based on the typed text. */
1634 clang_sortCodeCompletionResults(results->Results, results->NumResults);
1635
Douglas Gregor1982c182010-07-12 18:38:41 +00001636 for (i = 0; i != n; ++i)
1637 print_completion_result(results->Results + i, stdout);
Douglas Gregor1e5e6682010-08-26 13:48:20 +00001638 }
Douglas Gregora88084b2010-02-18 18:08:43 +00001639 n = clang_codeCompleteGetNumDiagnostics(results);
1640 for (i = 0; i != n; ++i) {
1641 CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i);
1642 PrintDiagnostic(diag);
1643 clang_disposeDiagnostic(diag);
1644 }
Douglas Gregor3da626b2011-07-07 16:03:39 +00001645
1646 contexts = clang_codeCompleteGetContexts(results);
1647 print_completion_contexts(contexts, stdout);
1648
Douglas Gregor0a47d692011-07-26 15:24:30 +00001649 containerKind = clang_codeCompleteGetContainerKind(results,
1650 &containerIsIncomplete);
Douglas Gregore081a612011-07-21 01:05:26 +00001651
1652 if (containerKind != CXCursor_InvalidCode) {
1653 /* We have found a container */
1654 CXString containerUSR, containerKindSpelling;
1655 containerKindSpelling = clang_getCursorKindSpelling(containerKind);
1656 printf("Container Kind: %s\n", clang_getCString(containerKindSpelling));
1657 clang_disposeString(containerKindSpelling);
1658
1659 if (containerIsIncomplete) {
1660 printf("Container is incomplete\n");
1661 }
1662 else {
1663 printf("Container is complete\n");
1664 }
1665
1666 containerUSR = clang_codeCompleteGetContainerUSR(results);
1667 printf("Container USR: %s\n", clang_getCString(containerUSR));
1668 clang_disposeString(containerUSR);
1669 }
1670
Douglas Gregor0a47d692011-07-26 15:24:30 +00001671 objCSelector = clang_codeCompleteGetObjCSelector(results);
1672 selectorString = clang_getCString(objCSelector);
1673 if (selectorString && strlen(selectorString) > 0) {
1674 printf("Objective-C selector: %s\n", selectorString);
1675 }
1676 clang_disposeString(objCSelector);
1677
Douglas Gregorec6762c2009-12-18 16:20:58 +00001678 clang_disposeCodeCompleteResults(results);
1679 }
Douglas Gregordf95a132010-08-09 20:45:32 +00001680 clang_disposeTranslationUnit(TU);
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001681 clang_disposeIndex(CIdx);
1682 free(filename);
Ted Kremeneke68fff62010-02-17 00:41:32 +00001683
Douglas Gregor735df882009-12-02 09:21:34 +00001684 free_remapped_files(unsaved_files, num_unsaved_files);
1685
Ted Kremenekf5d9c932009-11-17 18:09:14 +00001686 return 0;
Douglas Gregor0c8296d2009-11-07 00:00:49 +00001687}
1688
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001689typedef struct {
1690 char *filename;
1691 unsigned line;
1692 unsigned column;
1693} CursorSourceLocation;
1694
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001695static int inspect_cursor_at(int argc, const char **argv) {
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001696 CXIndex CIdx;
1697 int errorCode;
1698 struct CXUnsavedFile *unsaved_files = 0;
1699 int num_unsaved_files = 0;
1700 CXTranslationUnit TU;
1701 CXCursor Cursor;
1702 CursorSourceLocation *Locations = 0;
1703 unsigned NumLocations = 0, Loc;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001704 unsigned Repeats = 1;
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001705 unsigned I;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001706
Ted Kremeneke68fff62010-02-17 00:41:32 +00001707 /* Count the number of locations. */
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001708 while (strstr(argv[NumLocations+1], "-cursor-at=") == argv[NumLocations+1])
1709 ++NumLocations;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001710
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001711 /* Parse the locations. */
1712 assert(NumLocations > 0 && "Unable to count locations?");
1713 Locations = (CursorSourceLocation *)malloc(
1714 NumLocations * sizeof(CursorSourceLocation));
1715 for (Loc = 0; Loc < NumLocations; ++Loc) {
1716 const char *input = argv[Loc + 1] + strlen("-cursor-at=");
Ted Kremeneke68fff62010-02-17 00:41:32 +00001717 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1718 &Locations[Loc].line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00001719 &Locations[Loc].column, 0, 0)))
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001720 return errorCode;
1721 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001722
1723 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001724 &num_unsaved_files))
1725 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001726
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001727 if (getenv("CINDEXTEST_EDITING"))
1728 Repeats = 5;
1729
1730 /* Parse the translation unit. When we're testing clang_getCursor() after
1731 reparsing, don't remap unsaved files until the second parse. */
1732 CIdx = clang_createIndex(1, 1);
1733 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1734 argv + num_unsaved_files + 1 + NumLocations,
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001735 argc - num_unsaved_files - 2 - NumLocations,
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001736 unsaved_files,
1737 Repeats > 1? 0 : num_unsaved_files,
1738 getDefaultParsingOptions());
1739
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001740 if (!TU) {
1741 fprintf(stderr, "unable to parse input\n");
1742 return -1;
1743 }
Ted Kremeneke68fff62010-02-17 00:41:32 +00001744
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001745 if (checkForErrors(TU) != 0)
1746 return -1;
1747
Douglas Gregorbdc4b362010-11-30 06:04:54 +00001748 for (I = 0; I != Repeats; ++I) {
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001749 if (Repeats > 1 &&
1750 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1751 clang_defaultReparseOptions(TU))) {
1752 clang_disposeTranslationUnit(TU);
1753 return 1;
1754 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001755
1756 if (checkForErrors(TU) != 0)
1757 return -1;
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001758
1759 for (Loc = 0; Loc < NumLocations; ++Loc) {
1760 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1761 if (!file)
1762 continue;
Ted Kremeneke68fff62010-02-17 00:41:32 +00001763
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001764 Cursor = clang_getCursor(TU,
1765 clang_getLocation(TU, file, Locations[Loc].line,
1766 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001767
1768 if (checkForErrors(TU) != 0)
1769 return -1;
1770
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001771 if (I + 1 == Repeats) {
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001772 CXCompletionString completionString = clang_getCursorCompletionString(
1773 Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001774 CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor);
1775 CXString Spelling;
1776 const char *cspell;
1777 unsigned line, column;
1778 clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0);
1779 printf("%d:%d ", line, column);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001780 PrintCursor(Cursor);
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001781 PrintCursorExtent(Cursor);
1782 Spelling = clang_getCursorSpelling(Cursor);
1783 cspell = clang_getCString(Spelling);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001784 if (cspell && strlen(cspell) != 0) {
1785 unsigned pieceIndex;
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001786 printf(" Spelling=%s (", cspell);
1787 for (pieceIndex = 0; ; ++pieceIndex) {
Benjamin Kramer6c235bc2012-03-31 10:23:28 +00001788 CXSourceRange range =
1789 clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0);
Argyrios Kyrtzidisba1da142012-03-30 20:58:35 +00001790 if (clang_Range_isNull(range))
1791 break;
1792 PrintRange(range, 0);
1793 }
1794 printf(")");
1795 }
Argyrios Kyrtzidis66373dd2012-03-30 00:19:05 +00001796 clang_disposeString(Spelling);
Argyrios Kyrtzidis34ebe1e2012-03-30 22:15:48 +00001797 if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1)
1798 printf(" Selector index=%d",clang_Cursor_getObjCSelectorIndex(Cursor));
Argyrios Kyrtzidisf39a7ae2012-07-02 23:54:36 +00001799 if (clang_Cursor_isDynamicCall(Cursor))
1800 printf(" Dynamic-call");
1801
Douglas Gregor8fa0a802011-08-04 20:04:59 +00001802 if (completionString != NULL) {
1803 printf("\nCompletion string: ");
1804 print_completion_string(completionString, stdout);
1805 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001806 printf("\n");
1807 free(Locations[Loc].filename);
1808 }
1809 }
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001810 }
Douglas Gregor8e08dec2010-11-30 05:52:55 +00001811
Douglas Gregora88084b2010-02-18 18:08:43 +00001812 PrintDiagnostics(TU);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00001813 clang_disposeTranslationUnit(TU);
1814 clang_disposeIndex(CIdx);
1815 free(Locations);
1816 free_remapped_files(unsaved_files, num_unsaved_files);
1817 return 0;
1818}
1819
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001820static enum CXVisitorResult findFileRefsVisit(void *context,
1821 CXCursor cursor, CXSourceRange range) {
1822 if (clang_Range_isNull(range))
1823 return CXVisit_Continue;
1824
1825 PrintCursor(cursor);
1826 PrintRange(range, "");
1827 printf("\n");
1828 return CXVisit_Continue;
1829}
1830
1831static int find_file_refs_at(int argc, const char **argv) {
1832 CXIndex CIdx;
1833 int errorCode;
1834 struct CXUnsavedFile *unsaved_files = 0;
1835 int num_unsaved_files = 0;
1836 CXTranslationUnit TU;
1837 CXCursor Cursor;
1838 CursorSourceLocation *Locations = 0;
1839 unsigned NumLocations = 0, Loc;
1840 unsigned Repeats = 1;
1841 unsigned I;
1842
1843 /* Count the number of locations. */
1844 while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1])
1845 ++NumLocations;
1846
1847 /* Parse the locations. */
1848 assert(NumLocations > 0 && "Unable to count locations?");
1849 Locations = (CursorSourceLocation *)malloc(
1850 NumLocations * sizeof(CursorSourceLocation));
1851 for (Loc = 0; Loc < NumLocations; ++Loc) {
1852 const char *input = argv[Loc + 1] + strlen("-file-refs-at=");
1853 if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename,
1854 &Locations[Loc].line,
1855 &Locations[Loc].column, 0, 0)))
1856 return errorCode;
1857 }
1858
1859 if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files,
1860 &num_unsaved_files))
1861 return -1;
1862
1863 if (getenv("CINDEXTEST_EDITING"))
1864 Repeats = 5;
1865
1866 /* Parse the translation unit. When we're testing clang_getCursor() after
1867 reparsing, don't remap unsaved files until the second parse. */
1868 CIdx = clang_createIndex(1, 1);
1869 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
1870 argv + num_unsaved_files + 1 + NumLocations,
1871 argc - num_unsaved_files - 2 - NumLocations,
1872 unsaved_files,
1873 Repeats > 1? 0 : num_unsaved_files,
1874 getDefaultParsingOptions());
1875
1876 if (!TU) {
1877 fprintf(stderr, "unable to parse input\n");
1878 return -1;
1879 }
1880
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001881 if (checkForErrors(TU) != 0)
1882 return -1;
1883
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001884 for (I = 0; I != Repeats; ++I) {
1885 if (Repeats > 1 &&
1886 clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
1887 clang_defaultReparseOptions(TU))) {
1888 clang_disposeTranslationUnit(TU);
1889 return 1;
1890 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001891
1892 if (checkForErrors(TU) != 0)
1893 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001894
1895 for (Loc = 0; Loc < NumLocations; ++Loc) {
1896 CXFile file = clang_getFile(TU, Locations[Loc].filename);
1897 if (!file)
1898 continue;
1899
1900 Cursor = clang_getCursor(TU,
1901 clang_getLocation(TU, file, Locations[Loc].line,
1902 Locations[Loc].column));
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001903
1904 if (checkForErrors(TU) != 0)
1905 return -1;
1906
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001907 if (I + 1 == Repeats) {
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00001908 CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit };
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001909 PrintCursor(Cursor);
1910 printf("\n");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001911 clang_findReferencesInFile(Cursor, file, visitor);
1912 free(Locations[Loc].filename);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001913
1914 if (checkForErrors(TU) != 0)
1915 return -1;
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00001916 }
1917 }
1918 }
1919
1920 PrintDiagnostics(TU);
1921 clang_disposeTranslationUnit(TU);
1922 clang_disposeIndex(CIdx);
1923 free(Locations);
1924 free_remapped_files(unsaved_files, num_unsaved_files);
1925 return 0;
1926}
1927
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001928typedef struct {
1929 const char *check_prefix;
1930 int first_check_printed;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00001931 int fail_for_error;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00001932 int abort;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001933 const char *main_filename;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001934} IndexData;
1935
1936static void printCheck(IndexData *data) {
1937 if (data->check_prefix) {
1938 if (data->first_check_printed) {
1939 printf("// %s-NEXT: ", data->check_prefix);
1940 } else {
1941 printf("// %s : ", data->check_prefix);
1942 data->first_check_printed = 1;
1943 }
1944 }
1945}
1946
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001947static void printCXIndexFile(CXIdxClientFile file) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001948 CXString filename = clang_getFileName((CXFile)file);
1949 printf("%s", clang_getCString(filename));
1950 clang_disposeString(filename);
1951}
1952
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001953static void printCXIndexLoc(CXIdxLoc loc, CXClientData client_data) {
1954 IndexData *index_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001955 CXString filename;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001956 const char *cname;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00001957 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001958 unsigned line, column;
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001959 int isMainFile;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001960
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001961 index_data = (IndexData *)client_data;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001962 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
1963 if (line == 0) {
1964 printf("<null loc>");
1965 return;
1966 }
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00001967 if (!file) {
1968 printf("<no idxfile>");
1969 return;
1970 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001971 filename = clang_getFileName((CXFile)file);
1972 cname = clang_getCString(filename);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001973 if (strcmp(cname, index_data->main_filename) == 0)
1974 isMainFile = 1;
1975 else
1976 isMainFile = 0;
1977 clang_disposeString(filename);
1978
1979 if (!isMainFile) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001980 printCXIndexFile(file);
1981 printf(":");
1982 }
1983 printf("%d:%d", line, column);
1984}
1985
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00001986static unsigned digitCount(unsigned val) {
1987 unsigned c = 1;
1988 while (1) {
1989 if (val < 10)
1990 return c;
1991 ++c;
1992 val /= 10;
1993 }
1994}
1995
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00001996static CXIdxClientContainer makeClientContainer(const CXIdxEntityInfo *info,
1997 CXIdxLoc loc) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001998 const char *name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00001999 char *newStr;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002000 CXIdxClientFile file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002001 unsigned line, column;
2002
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002003 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002004 if (!name)
2005 name = "<anon-tag>";
2006
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002007 clang_indexLoc_getFileLocation(loc, &file, 0, &line, &column, 0);
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002008 /* FIXME: free these.*/
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002009 newStr = (char *)malloc(strlen(name) +
2010 digitCount(line) + digitCount(column) + 3);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002011 sprintf(newStr, "%s:%d:%d", name, line, column);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002012 return (CXIdxClientContainer)newStr;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002013}
2014
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002015static void printCXIndexContainer(const CXIdxContainerInfo *info) {
2016 CXIdxClientContainer container;
2017 container = clang_index_getClientContainer(info);
Argyrios Kyrtzidis3e340a62011-11-16 02:35:05 +00002018 if (!container)
2019 printf("[<<NULL>>]");
2020 else
2021 printf("[%s]", (const char *)container);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002022}
2023
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002024static const char *getEntityKindString(CXIdxEntityKind kind) {
2025 switch (kind) {
2026 case CXIdxEntity_Unexposed: return "<<UNEXPOSED>>";
2027 case CXIdxEntity_Typedef: return "typedef";
2028 case CXIdxEntity_Function: return "function";
2029 case CXIdxEntity_Variable: return "variable";
2030 case CXIdxEntity_Field: return "field";
2031 case CXIdxEntity_EnumConstant: return "enumerator";
2032 case CXIdxEntity_ObjCClass: return "objc-class";
2033 case CXIdxEntity_ObjCProtocol: return "objc-protocol";
2034 case CXIdxEntity_ObjCCategory: return "objc-category";
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002035 case CXIdxEntity_ObjCInstanceMethod: return "objc-instance-method";
2036 case CXIdxEntity_ObjCClassMethod: return "objc-class-method";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002037 case CXIdxEntity_ObjCProperty: return "objc-property";
2038 case CXIdxEntity_ObjCIvar: return "objc-ivar";
2039 case CXIdxEntity_Enum: return "enum";
2040 case CXIdxEntity_Struct: return "struct";
2041 case CXIdxEntity_Union: return "union";
2042 case CXIdxEntity_CXXClass: return "c++-class";
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002043 case CXIdxEntity_CXXNamespace: return "namespace";
2044 case CXIdxEntity_CXXNamespaceAlias: return "namespace-alias";
2045 case CXIdxEntity_CXXStaticVariable: return "c++-static-var";
2046 case CXIdxEntity_CXXStaticMethod: return "c++-static-method";
2047 case CXIdxEntity_CXXInstanceMethod: return "c++-instance-method";
2048 case CXIdxEntity_CXXConstructor: return "constructor";
2049 case CXIdxEntity_CXXDestructor: return "destructor";
2050 case CXIdxEntity_CXXConversionFunction: return "conversion-func";
2051 case CXIdxEntity_CXXTypeAlias: return "type-alias";
2052 }
2053 assert(0 && "Garbage entity kind");
2054 return 0;
2055}
2056
2057static const char *getEntityTemplateKindString(CXIdxEntityCXXTemplateKind kind) {
2058 switch (kind) {
2059 case CXIdxEntity_NonTemplate: return "";
2060 case CXIdxEntity_Template: return "-template";
2061 case CXIdxEntity_TemplatePartialSpecialization:
2062 return "-template-partial-spec";
2063 case CXIdxEntity_TemplateSpecialization: return "-template-spec";
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002064 }
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002065 assert(0 && "Garbage entity kind");
2066 return 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002067}
2068
Argyrios Kyrtzidis838d3c22011-12-07 20:44:12 +00002069static const char *getEntityLanguageString(CXIdxEntityLanguage kind) {
2070 switch (kind) {
2071 case CXIdxEntityLang_None: return "<none>";
2072 case CXIdxEntityLang_C: return "C";
2073 case CXIdxEntityLang_ObjC: return "ObjC";
2074 case CXIdxEntityLang_CXX: return "C++";
2075 }
2076 assert(0 && "Garbage language kind");
2077 return 0;
2078}
2079
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002080static void printEntityInfo(const char *cb,
2081 CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002082 const CXIdxEntityInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002083 const char *name;
2084 IndexData *index_data;
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002085 unsigned i;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002086 index_data = (IndexData *)client_data;
2087 printCheck(index_data);
2088
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002089 if (!info) {
2090 printf("%s: <<NULL>>", cb);
2091 return;
2092 }
2093
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002094 name = info->name;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002095 if (!name)
2096 name = "<anon-tag>";
2097
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002098 printf("%s: kind: %s%s", cb, getEntityKindString(info->kind),
2099 getEntityTemplateKindString(info->templateKind));
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002100 printf(" | name: %s", name);
2101 printf(" | USR: %s", info->USR);
Argyrios Kyrtzidisc2be04e2011-12-13 18:47:35 +00002102 printf(" | lang: %s", getEntityLanguageString(info->lang));
Argyrios Kyrtzidis643d3ce2011-12-15 00:05:00 +00002103
2104 for (i = 0; i != info->numAttributes; ++i) {
2105 const CXIdxAttrInfo *Attr = info->attributes[i];
2106 printf(" <attribute>: ");
2107 PrintCursor(Attr->cursor);
2108 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002109}
2110
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002111static void printBaseClassInfo(CXClientData client_data,
2112 const CXIdxBaseClassInfo *info) {
2113 printEntityInfo(" <base>", client_data, info->base);
2114 printf(" | cursor: ");
2115 PrintCursor(info->cursor);
2116 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002117 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002118}
2119
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002120static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo,
2121 CXClientData client_data) {
2122 unsigned i;
2123 for (i = 0; i < ProtoInfo->numProtocols; ++i) {
2124 printEntityInfo(" <protocol>", client_data,
2125 ProtoInfo->protocols[i]->protocol);
2126 printf(" | cursor: ");
2127 PrintCursor(ProtoInfo->protocols[i]->cursor);
2128 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002129 printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002130 printf("\n");
2131 }
2132}
2133
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002134static void index_diagnostic(CXClientData client_data,
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002135 CXDiagnosticSet diagSet, void *reserved) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002136 CXString str;
2137 const char *cstr;
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002138 unsigned numDiags, i;
2139 CXDiagnostic diag;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002140 IndexData *index_data;
2141 index_data = (IndexData *)client_data;
2142 printCheck(index_data);
2143
Argyrios Kyrtzidis996e6e52011-12-01 02:42:50 +00002144 numDiags = clang_getNumDiagnosticsInSet(diagSet);
2145 for (i = 0; i != numDiags; ++i) {
2146 diag = clang_getDiagnosticInSet(diagSet, i);
2147 str = clang_formatDiagnostic(diag, clang_defaultDiagnosticDisplayOptions());
2148 cstr = clang_getCString(str);
2149 printf("[diagnostic]: %s\n", cstr);
2150 clang_disposeString(str);
2151
2152 if (getenv("CINDEXTEST_FAILONERROR") &&
2153 clang_getDiagnosticSeverity(diag) >= CXDiagnostic_Error) {
2154 index_data->fail_for_error = 1;
2155 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002156 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002157}
2158
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002159static CXIdxClientFile index_enteredMainFile(CXClientData client_data,
2160 CXFile file, void *reserved) {
2161 IndexData *index_data;
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002162 CXString filename;
2163
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002164 index_data = (IndexData *)client_data;
2165 printCheck(index_data);
2166
Argyrios Kyrtzidis62d7fea2012-03-15 18:48:52 +00002167 filename = clang_getFileName(file);
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002168 index_data->main_filename = clang_getCString(filename);
2169 clang_disposeString(filename);
2170
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002171 printf("[enteredMainFile]: ");
2172 printCXIndexFile((CXIdxClientFile)file);
2173 printf("\n");
2174
2175 return (CXIdxClientFile)file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002176}
2177
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002178static CXIdxClientFile index_ppIncludedFile(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002179 const CXIdxIncludedFileInfo *info) {
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002180 IndexData *index_data;
2181 index_data = (IndexData *)client_data;
2182 printCheck(index_data);
2183
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002184 printf("[ppIncludedFile]: ");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002185 printCXIndexFile((CXIdxClientFile)info->file);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002186 printf(" | name: \"%s\"", info->filename);
2187 printf(" | hash loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002188 printCXIndexLoc(info->hashLoc, client_data);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002189 printf(" | isImport: %d | isAngled: %d\n", info->isImport, info->isAngled);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002190
2191 return (CXIdxClientFile)info->file;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002192}
2193
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002194static CXIdxClientContainer index_startedTranslationUnit(CXClientData client_data,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002195 void *reserved) {
2196 IndexData *index_data;
2197 index_data = (IndexData *)client_data;
2198 printCheck(index_data);
2199
Argyrios Kyrtzidis66042b32011-11-05 04:03:35 +00002200 printf("[startedTranslationUnit]\n");
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002201 return (CXIdxClientContainer)"TU";
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002202}
2203
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002204static void index_indexDeclaration(CXClientData client_data,
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002205 const CXIdxDeclInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002206 IndexData *index_data;
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002207 const CXIdxObjCCategoryDeclInfo *CatInfo;
2208 const CXIdxObjCInterfaceDeclInfo *InterInfo;
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002209 const CXIdxObjCProtocolRefListInfo *ProtoInfo;
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002210 const CXIdxObjCPropertyDeclInfo *PropInfo;
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002211 const CXIdxCXXClassDeclInfo *CXXClassInfo;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002212 unsigned i;
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002213 index_data = (IndexData *)client_data;
2214
2215 printEntityInfo("[indexDeclaration]", client_data, info->entityInfo);
2216 printf(" | cursor: ");
2217 PrintCursor(info->cursor);
2218 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002219 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisb1febb62011-12-07 20:44:19 +00002220 printf(" | semantic-container: ");
2221 printCXIndexContainer(info->semanticContainer);
2222 printf(" | lexical-container: ");
2223 printCXIndexContainer(info->lexicalContainer);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002224 printf(" | isRedecl: %d", info->isRedeclaration);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002225 printf(" | isDef: %d", info->isDefinition);
2226 printf(" | isContainer: %d", info->isContainer);
2227 printf(" | isImplicit: %d\n", info->isImplicit);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002228
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002229 for (i = 0; i != info->numAttributes; ++i) {
NAKAMURA Takumi87adb0b2011-11-18 00:51:03 +00002230 const CXIdxAttrInfo *Attr = info->attributes[i];
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002231 printf(" <attribute>: ");
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002232 PrintCursor(Attr->cursor);
2233 printf("\n");
2234 }
2235
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002236 if (clang_index_isEntityObjCContainerKind(info->entityInfo->kind)) {
2237 const char *kindName = 0;
2238 CXIdxObjCContainerKind K = clang_index_getObjCContainerDeclInfo(info)->kind;
2239 switch (K) {
2240 case CXIdxObjCContainer_ForwardRef:
2241 kindName = "forward-ref"; break;
2242 case CXIdxObjCContainer_Interface:
2243 kindName = "interface"; break;
2244 case CXIdxObjCContainer_Implementation:
2245 kindName = "implementation"; break;
2246 }
2247 printCheck(index_data);
2248 printf(" <ObjCContainerInfo>: kind: %s\n", kindName);
2249 }
2250
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002251 if ((CatInfo = clang_index_getObjCCategoryDeclInfo(info))) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002252 printEntityInfo(" <ObjCCategoryInfo>: class", client_data,
2253 CatInfo->objcClass);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002254 printf(" | cursor: ");
2255 PrintCursor(CatInfo->classCursor);
2256 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002257 printCXIndexLoc(CatInfo->classLoc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002258 printf("\n");
2259 }
2260
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002261 if ((InterInfo = clang_index_getObjCInterfaceDeclInfo(info))) {
2262 if (InterInfo->superInfo) {
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002263 printBaseClassInfo(client_data, InterInfo->superInfo);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002264 printf("\n");
2265 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002266 }
2267
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002268 if ((ProtoInfo = clang_index_getObjCProtocolRefListInfo(info))) {
2269 printProtocolList(ProtoInfo, client_data);
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002270 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002271
Argyrios Kyrtzidis792db262012-02-28 17:50:33 +00002272 if ((PropInfo = clang_index_getObjCPropertyDeclInfo(info))) {
2273 if (PropInfo->getter) {
2274 printEntityInfo(" <getter>", client_data, PropInfo->getter);
2275 printf("\n");
2276 }
2277 if (PropInfo->setter) {
2278 printEntityInfo(" <setter>", client_data, PropInfo->setter);
2279 printf("\n");
2280 }
2281 }
2282
Argyrios Kyrtzidisb526a872011-12-07 20:44:15 +00002283 if ((CXXClassInfo = clang_index_getCXXClassDeclInfo(info))) {
2284 for (i = 0; i != CXXClassInfo->numBases; ++i) {
2285 printBaseClassInfo(client_data, CXXClassInfo->bases[i]);
2286 printf("\n");
2287 }
2288 }
2289
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002290 if (info->declAsContainer)
2291 clang_index_setClientContainer(info->declAsContainer,
2292 makeClientContainer(info->entityInfo, info->loc));
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002293}
2294
2295static void index_indexEntityReference(CXClientData client_data,
Argyrios Kyrtzidis6ec43ad2011-11-12 02:16:30 +00002296 const CXIdxEntityRefInfo *info) {
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002297 printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002298 printf(" | cursor: ");
2299 PrintCursor(info->cursor);
2300 printf(" | loc: ");
Argyrios Kyrtzidis13c20a72012-03-15 18:07:22 +00002301 printCXIndexLoc(info->loc, client_data);
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002302 printEntityInfo(" | <parent>:", client_data, info->parentEntity);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002303 printf(" | container: ");
2304 printCXIndexContainer(info->container);
Argyrios Kyrtzidisc71d5542011-11-14 22:39:19 +00002305 printf(" | refkind: ");
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002306 switch (info->kind) {
2307 case CXIdxEntityRef_Direct: printf("direct"); break;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002308 case CXIdxEntityRef_Implicit: printf("implicit"); break;
Argyrios Kyrtzidisaca19be2011-10-18 15:50:50 +00002309 }
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002310 printf("\n");
2311}
2312
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002313static int index_abortQuery(CXClientData client_data, void *reserved) {
2314 IndexData *index_data;
2315 index_data = (IndexData *)client_data;
2316 return index_data->abort;
2317}
2318
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002319static IndexerCallbacks IndexCB = {
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002320 index_abortQuery,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002321 index_diagnostic,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002322 index_enteredMainFile,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002323 index_ppIncludedFile,
Argyrios Kyrtzidisf89bc052011-10-20 17:21:46 +00002324 0, /*importedASTFile*/
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002325 index_startedTranslationUnit,
Argyrios Kyrtzidisdd93c592011-11-11 00:23:36 +00002326 index_indexDeclaration,
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002327 index_indexEntityReference
2328};
2329
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002330static unsigned getIndexOptions(void) {
2331 unsigned index_opts;
2332 index_opts = 0;
2333 if (getenv("CINDEXTEST_SUPPRESSREFS"))
2334 index_opts |= CXIndexOpt_SuppressRedundantRefs;
2335 if (getenv("CINDEXTEST_INDEXLOCALSYMBOLS"))
2336 index_opts |= CXIndexOpt_IndexFunctionLocalSymbols;
2337
2338 return index_opts;
2339}
2340
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002341static int index_file(int argc, const char **argv) {
2342 const char *check_prefix;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002343 CXIndex Idx;
2344 CXIndexAction idxAction;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002345 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002346 unsigned index_opts;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002347 int result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002348
2349 check_prefix = 0;
2350 if (argc > 0) {
2351 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2352 check_prefix = argv[0] + strlen("-check-prefix=");
2353 ++argv;
2354 --argc;
2355 }
2356 }
2357
2358 if (argc == 0) {
2359 fprintf(stderr, "no compiler arguments\n");
2360 return -1;
2361 }
2362
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002363 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2364 /* displayDiagnosics=*/1))) {
2365 fprintf(stderr, "Could not create Index\n");
2366 return 1;
2367 }
2368 idxAction = 0;
2369 result = 1;
2370
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002371 index_data.check_prefix = check_prefix;
2372 index_data.first_check_printed = 0;
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002373 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002374 index_data.abort = 0;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002375
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002376 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002377 idxAction = clang_IndexAction_create(Idx);
2378 result = clang_indexSourceFile(idxAction, &index_data,
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002379 &IndexCB,sizeof(IndexCB), index_opts,
Argyrios Kyrtzidisc6b4a502011-11-16 02:34:59 +00002380 0, argv, argc, 0, 0, 0, 0);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002381 if (index_data.fail_for_error)
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002382 result = -1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002383
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002384 clang_IndexAction_dispose(idxAction);
2385 clang_disposeIndex(Idx);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002386 return result;
2387}
2388
2389static int index_tu(int argc, const char **argv) {
2390 CXIndex Idx;
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002391 CXIndexAction idxAction;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002392 CXTranslationUnit TU;
2393 const char *check_prefix;
2394 IndexData index_data;
Argyrios Kyrtzidisb395c632011-11-18 00:26:51 +00002395 unsigned index_opts;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002396 int result;
2397
2398 check_prefix = 0;
2399 if (argc > 0) {
2400 if (strstr(argv[0], "-check-prefix=") == argv[0]) {
2401 check_prefix = argv[0] + strlen("-check-prefix=");
2402 ++argv;
2403 --argc;
2404 }
2405 }
2406
2407 if (argc == 0) {
2408 fprintf(stderr, "no ast file\n");
2409 return -1;
2410 }
2411
2412 if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1,
2413 /* displayDiagnosics=*/1))) {
2414 fprintf(stderr, "Could not create Index\n");
2415 return 1;
2416 }
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002417 idxAction = 0;
2418 result = 1;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002419
2420 if (!CreateTranslationUnit(Idx, argv[0], &TU))
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002421 goto finished;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002422
2423 index_data.check_prefix = check_prefix;
2424 index_data.first_check_printed = 0;
2425 index_data.fail_for_error = 0;
Argyrios Kyrtzidis6f3ce972011-11-28 04:56:00 +00002426 index_data.abort = 0;
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00002427
Argyrios Kyrtzidis22490742012-01-14 00:11:49 +00002428 index_opts = getIndexOptions();
Argyrios Kyrtzidis2957e6f2011-11-22 07:24:51 +00002429 idxAction = clang_IndexAction_create(Idx);
2430 result = clang_indexTranslationUnit(idxAction, &index_data,
2431 &IndexCB,sizeof(IndexCB),
2432 index_opts, TU);
2433 if (index_data.fail_for_error)
2434 goto finished;
2435
2436 finished:
2437 clang_IndexAction_dispose(idxAction);
2438 clang_disposeIndex(Idx);
2439
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002440 return result;
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00002441}
2442
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002443int perform_token_annotation(int argc, const char **argv) {
2444 const char *input = argv[1];
2445 char *filename = 0;
2446 unsigned line, second_line;
2447 unsigned column, second_column;
2448 CXIndex CIdx;
2449 CXTranslationUnit TU = 0;
2450 int errorCode;
2451 struct CXUnsavedFile *unsaved_files = 0;
2452 int num_unsaved_files = 0;
2453 CXToken *tokens;
2454 unsigned num_tokens;
2455 CXSourceRange range;
2456 CXSourceLocation startLoc, endLoc;
2457 CXFile file = 0;
2458 CXCursor *cursors = 0;
2459 unsigned i;
2460
2461 input += strlen("-test-annotate-tokens=");
2462 if ((errorCode = parse_file_line_column(input, &filename, &line, &column,
2463 &second_line, &second_column)))
2464 return errorCode;
2465
Richard Smithe07c5f82012-07-05 08:20:49 +00002466 if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) {
2467 free(filename);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002468 return -1;
Richard Smithe07c5f82012-07-05 08:20:49 +00002469 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002470
Douglas Gregor0a812cf2010-02-18 23:07:20 +00002471 CIdx = clang_createIndex(0, 1);
Douglas Gregordca8ee82011-05-06 16:33:08 +00002472 TU = clang_parseTranslationUnit(CIdx, argv[argc - 1],
2473 argv + num_unsaved_files + 2,
2474 argc - num_unsaved_files - 3,
2475 unsaved_files,
2476 num_unsaved_files,
2477 getDefaultParsingOptions());
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002478 if (!TU) {
2479 fprintf(stderr, "unable to parse input\n");
2480 clang_disposeIndex(CIdx);
2481 free(filename);
2482 free_remapped_files(unsaved_files, num_unsaved_files);
2483 return -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002484 }
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002485 errorCode = 0;
2486
Richard Smithe07c5f82012-07-05 08:20:49 +00002487 if (checkForErrors(TU) != 0) {
2488 errorCode = -1;
2489 goto teardown;
2490 }
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002491
Argyrios Kyrtzidisee0f84f2011-09-26 08:01:41 +00002492 if (getenv("CINDEXTEST_EDITING")) {
2493 for (i = 0; i < 5; ++i) {
2494 if (clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files,
2495 clang_defaultReparseOptions(TU))) {
2496 fprintf(stderr, "Unable to reparse translation unit!\n");
2497 errorCode = -1;
2498 goto teardown;
2499 }
2500 }
2501 }
2502
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002503 if (checkForErrors(TU) != 0) {
2504 errorCode = -1;
2505 goto teardown;
2506 }
2507
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002508 file = clang_getFile(TU, filename);
2509 if (!file) {
2510 fprintf(stderr, "file %s is not in this translation unit\n", filename);
2511 errorCode = -1;
2512 goto teardown;
2513 }
2514
2515 startLoc = clang_getLocation(TU, file, line, column);
2516 if (clang_equalLocations(clang_getNullLocation(), startLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002517 fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002518 column);
2519 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002520 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002521 }
2522
2523 endLoc = clang_getLocation(TU, file, second_line, second_column);
2524 if (clang_equalLocations(clang_getNullLocation(), endLoc)) {
Ted Kremeneke68fff62010-02-17 00:41:32 +00002525 fprintf(stderr, "invalid source location %s:%d:%d\n", filename,
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002526 second_line, second_column);
2527 errorCode = -1;
Ted Kremeneke68fff62010-02-17 00:41:32 +00002528 goto teardown;
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002529 }
2530
2531 range = clang_getRange(startLoc, endLoc);
2532 clang_tokenize(TU, range, &tokens, &num_tokens);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002533
2534 if (checkForErrors(TU) != 0) {
2535 errorCode = -1;
2536 goto teardown;
2537 }
2538
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002539 cursors = (CXCursor *)malloc(num_tokens * sizeof(CXCursor));
2540 clang_annotateTokens(TU, tokens, num_tokens, cursors);
Argyrios Kyrtzidisdfca64d2011-10-28 22:54:36 +00002541
2542 if (checkForErrors(TU) != 0) {
2543 errorCode = -1;
2544 goto teardown;
2545 }
2546
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002547 for (i = 0; i != num_tokens; ++i) {
2548 const char *kind = "<unknown>";
2549 CXString spelling = clang_getTokenSpelling(TU, tokens[i]);
2550 CXSourceRange extent = clang_getTokenExtent(TU, tokens[i]);
2551 unsigned start_line, start_column, end_line, end_column;
2552
2553 switch (clang_getTokenKind(tokens[i])) {
2554 case CXToken_Punctuation: kind = "Punctuation"; break;
2555 case CXToken_Keyword: kind = "Keyword"; break;
2556 case CXToken_Identifier: kind = "Identifier"; break;
2557 case CXToken_Literal: kind = "Literal"; break;
2558 case CXToken_Comment: kind = "Comment"; break;
2559 }
Douglas Gregora9b06d42010-11-09 06:24:54 +00002560 clang_getSpellingLocation(clang_getRangeStart(extent),
2561 0, &start_line, &start_column, 0);
2562 clang_getSpellingLocation(clang_getRangeEnd(extent),
2563 0, &end_line, &end_column, 0);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002564 printf("%s: \"%s\" ", kind, clang_getCString(spelling));
Benjamin Kramer342742a2012-04-14 09:11:51 +00002565 clang_disposeString(spelling);
Daniel Dunbar51b058c2010-02-14 08:32:24 +00002566 PrintExtent(stdout, start_line, start_column, end_line, end_column);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002567 if (!clang_isInvalid(cursors[i].kind)) {
2568 printf(" ");
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00002569 PrintCursor(cursors[i]);
Douglas Gregor0045e9f2010-01-26 18:31:56 +00002570 }
2571 printf("\n");
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002572 }
2573 free(cursors);
Ted Kremenek93f5e6a2010-10-20 21:22:15 +00002574 clang_disposeTokens(TU, tokens, num_tokens);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002575
2576 teardown:
Douglas Gregora88084b2010-02-18 18:08:43 +00002577 PrintDiagnostics(TU);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00002578 clang_disposeTranslationUnit(TU);
2579 clang_disposeIndex(CIdx);
2580 free(filename);
2581 free_remapped_files(unsaved_files, num_unsaved_files);
2582 return errorCode;
2583}
2584
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002585static int
2586perform_test_compilation_db(const char *database, int argc, const char **argv) {
2587 CXCompilationDatabase db;
2588 CXCompileCommands CCmds;
2589 CXCompileCommand CCmd;
2590 CXCompilationDatabase_Error ec;
2591 CXString wd;
2592 CXString arg;
2593 int errorCode = 0;
2594 char *tmp;
2595 unsigned len;
2596 char *buildDir;
2597 int i, j, a, numCmds, numArgs;
2598
2599 len = strlen(database);
2600 tmp = (char *) malloc(len+1);
2601 memcpy(tmp, database, len+1);
2602 buildDir = dirname(tmp);
2603
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002604 db = clang_CompilationDatabase_fromDirectory(buildDir, &ec);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002605
2606 if (db) {
2607
2608 if (ec!=CXCompilationDatabase_NoError) {
2609 printf("unexpected error %d code while loading compilation database\n", ec);
2610 errorCode = -1;
2611 goto cdb_end;
2612 }
2613
2614 for (i=0; i<argc && errorCode==0; ) {
2615 if (strcmp(argv[i],"lookup")==0){
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002616 CCmds = clang_CompilationDatabase_getCompileCommands(db, argv[i+1]);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002617
2618 if (!CCmds) {
2619 printf("file %s not found in compilation db\n", argv[i+1]);
2620 errorCode = -1;
2621 break;
2622 }
2623
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002624 numCmds = clang_CompileCommands_getSize(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002625
2626 if (numCmds==0) {
2627 fprintf(stderr, "should not get an empty compileCommand set for file"
2628 " '%s'\n", argv[i+1]);
2629 errorCode = -1;
2630 break;
2631 }
2632
2633 for (j=0; j<numCmds; ++j) {
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002634 CCmd = clang_CompileCommands_getCommand(CCmds, j);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002635
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002636 wd = clang_CompileCommand_getDirectory(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002637 printf("workdir:'%s'", clang_getCString(wd));
2638 clang_disposeString(wd);
2639
2640 printf(" cmdline:'");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002641 numArgs = clang_CompileCommand_getNumArgs(CCmd);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002642 for (a=0; a<numArgs; ++a) {
2643 if (a) printf(" ");
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002644 arg = clang_CompileCommand_getArg(CCmd, a);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002645 printf("%s", clang_getCString(arg));
2646 clang_disposeString(arg);
2647 }
2648 printf("'\n");
2649 }
2650
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002651 clang_CompileCommands_dispose(CCmds);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002652
2653 i += 2;
2654 }
2655 }
Arnaud A. de Grandmaisonc70851b2012-07-03 20:38:12 +00002656 clang_CompilationDatabase_dispose(db);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00002657 } else {
2658 printf("database loading failed with error code %d.\n", ec);
2659 errorCode = -1;
2660 }
2661
2662cdb_end:
2663 free(tmp);
2664
2665 return errorCode;
2666}
2667
Ted Kremenek0d435192009-11-17 18:13:31 +00002668/******************************************************************************/
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002669/* USR printing. */
2670/******************************************************************************/
2671
2672static int insufficient_usr(const char *kind, const char *usage) {
2673 fprintf(stderr, "USR for '%s' requires: %s\n", kind, usage);
2674 return 1;
2675}
2676
2677static unsigned isUSR(const char *s) {
2678 return s[0] == 'c' && s[1] == ':';
2679}
2680
2681static int not_usr(const char *s, const char *arg) {
2682 fprintf(stderr, "'%s' argument ('%s') is not a USR\n", s, arg);
2683 return 1;
2684}
2685
2686static void print_usr(CXString usr) {
2687 const char *s = clang_getCString(usr);
2688 printf("%s\n", s);
2689 clang_disposeString(usr);
2690}
2691
2692static void display_usrs() {
2693 fprintf(stderr, "-print-usrs options:\n"
2694 " ObjCCategory <class name> <category name>\n"
2695 " ObjCClass <class name>\n"
2696 " ObjCIvar <ivar name> <class USR>\n"
2697 " ObjCMethod <selector> [0=class method|1=instance method] "
2698 "<class USR>\n"
2699 " ObjCProperty <property name> <class USR>\n"
2700 " ObjCProtocol <protocol name>\n");
2701}
2702
2703int print_usrs(const char **I, const char **E) {
2704 while (I != E) {
2705 const char *kind = *I;
2706 unsigned len = strlen(kind);
2707 switch (len) {
2708 case 8:
2709 if (memcmp(kind, "ObjCIvar", 8) == 0) {
2710 if (I + 2 >= E)
2711 return insufficient_usr(kind, "<ivar name> <class USR>");
2712 if (!isUSR(I[2]))
2713 return not_usr("<class USR>", I[2]);
2714 else {
2715 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002716 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002717 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002718 print_usr(clang_constructUSR_ObjCIvar(I[1], x));
2719 }
2720
2721 I += 3;
2722 continue;
2723 }
2724 break;
2725 case 9:
2726 if (memcmp(kind, "ObjCClass", 9) == 0) {
2727 if (I + 1 >= E)
2728 return insufficient_usr(kind, "<class name>");
2729 print_usr(clang_constructUSR_ObjCClass(I[1]));
2730 I += 2;
2731 continue;
2732 }
2733 break;
2734 case 10:
2735 if (memcmp(kind, "ObjCMethod", 10) == 0) {
2736 if (I + 3 >= E)
2737 return insufficient_usr(kind, "<method selector> "
2738 "[0=class method|1=instance method] <class USR>");
2739 if (!isUSR(I[3]))
2740 return not_usr("<class USR>", I[3]);
2741 else {
2742 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002743 x.data = (void*) I[3];
Ted Kremeneked122732010-11-16 01:56:27 +00002744 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002745 print_usr(clang_constructUSR_ObjCMethod(I[1], atoi(I[2]), x));
2746 }
2747 I += 4;
2748 continue;
2749 }
2750 break;
2751 case 12:
2752 if (memcmp(kind, "ObjCCategory", 12) == 0) {
2753 if (I + 2 >= E)
2754 return insufficient_usr(kind, "<class name> <category name>");
2755 print_usr(clang_constructUSR_ObjCCategory(I[1], I[2]));
2756 I += 3;
2757 continue;
2758 }
2759 if (memcmp(kind, "ObjCProtocol", 12) == 0) {
2760 if (I + 1 >= E)
2761 return insufficient_usr(kind, "<protocol name>");
2762 print_usr(clang_constructUSR_ObjCProtocol(I[1]));
2763 I += 2;
2764 continue;
2765 }
2766 if (memcmp(kind, "ObjCProperty", 12) == 0) {
2767 if (I + 2 >= E)
2768 return insufficient_usr(kind, "<property name> <class USR>");
2769 if (!isUSR(I[2]))
2770 return not_usr("<class USR>", I[2]);
2771 else {
2772 CXString x;
Ted Kremeneka60ed472010-11-16 08:15:36 +00002773 x.data = (void*) I[2];
Ted Kremeneked122732010-11-16 01:56:27 +00002774 x.private_flags = 0;
Ted Kremenekf7b714d2010-03-25 02:00:39 +00002775 print_usr(clang_constructUSR_ObjCProperty(I[1], x));
2776 }
2777 I += 3;
2778 continue;
2779 }
2780 break;
2781 default:
2782 break;
2783 }
2784 break;
2785 }
2786
2787 if (I != E) {
2788 fprintf(stderr, "Invalid USR kind: %s\n", *I);
2789 display_usrs();
2790 return 1;
2791 }
2792 return 0;
2793}
2794
2795int print_usrs_file(const char *file_name) {
2796 char line[2048];
2797 const char *args[128];
2798 unsigned numChars = 0;
2799
2800 FILE *fp = fopen(file_name, "r");
2801 if (!fp) {
2802 fprintf(stderr, "error: cannot open '%s'\n", file_name);
2803 return 1;
2804 }
2805
2806 /* This code is not really all that safe, but it works fine for testing. */
2807 while (!feof(fp)) {
2808 char c = fgetc(fp);
2809 if (c == '\n') {
2810 unsigned i = 0;
2811 const char *s = 0;
2812
2813 if (numChars == 0)
2814 continue;
2815
2816 line[numChars] = '\0';
2817 numChars = 0;
2818
2819 if (line[0] == '/' && line[1] == '/')
2820 continue;
2821
2822 s = strtok(line, " ");
2823 while (s) {
2824 args[i] = s;
2825 ++i;
2826 s = strtok(0, " ");
2827 }
2828 if (print_usrs(&args[0], &args[i]))
2829 return 1;
2830 }
2831 else
2832 line[numChars++] = c;
2833 }
2834
2835 fclose(fp);
2836 return 0;
2837}
2838
2839/******************************************************************************/
Ted Kremenek0d435192009-11-17 18:13:31 +00002840/* Command line processing. */
2841/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002842int write_pch_file(const char *filename, int argc, const char *argv[]) {
2843 CXIndex Idx;
2844 CXTranslationUnit TU;
2845 struct CXUnsavedFile *unsaved_files = 0;
2846 int num_unsaved_files = 0;
Francois Pichet08aa6222011-07-06 22:09:44 +00002847 int result = 0;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002848
2849 Idx = clang_createIndex(/* excludeDeclsFromPCH */1, /* displayDiagnosics=*/1);
2850
2851 if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) {
2852 clang_disposeIndex(Idx);
2853 return -1;
2854 }
2855
2856 TU = clang_parseTranslationUnit(Idx, 0,
2857 argv + num_unsaved_files,
2858 argc - num_unsaved_files,
2859 unsaved_files,
2860 num_unsaved_files,
2861 CXTranslationUnit_Incomplete);
2862 if (!TU) {
2863 fprintf(stderr, "Unable to load translation unit!\n");
2864 free_remapped_files(unsaved_files, num_unsaved_files);
2865 clang_disposeIndex(Idx);
2866 return 1;
2867 }
2868
Douglas Gregor39c411f2011-07-06 16:43:36 +00002869 switch (clang_saveTranslationUnit(TU, filename,
2870 clang_defaultSaveOptions(TU))) {
2871 case CXSaveError_None:
2872 break;
2873
2874 case CXSaveError_TranslationErrors:
2875 fprintf(stderr, "Unable to write PCH file %s: translation errors\n",
2876 filename);
2877 result = 2;
2878 break;
2879
2880 case CXSaveError_InvalidTU:
2881 fprintf(stderr, "Unable to write PCH file %s: invalid translation unit\n",
2882 filename);
2883 result = 3;
2884 break;
2885
2886 case CXSaveError_Unknown:
2887 default:
2888 fprintf(stderr, "Unable to write PCH file %s: unknown error \n", filename);
2889 result = 1;
2890 break;
2891 }
2892
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002893 clang_disposeTranslationUnit(TU);
2894 free_remapped_files(unsaved_files, num_unsaved_files);
2895 clang_disposeIndex(Idx);
Douglas Gregor39c411f2011-07-06 16:43:36 +00002896 return result;
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00002897}
2898
2899/******************************************************************************/
Ted Kremenek15322172011-11-10 08:43:12 +00002900/* Serialized diagnostics. */
2901/******************************************************************************/
2902
2903static const char *getDiagnosticCodeStr(enum CXLoadDiag_Error error) {
2904 switch (error) {
2905 case CXLoadDiag_CannotLoad: return "Cannot Load File";
2906 case CXLoadDiag_None: break;
2907 case CXLoadDiag_Unknown: return "Unknown";
2908 case CXLoadDiag_InvalidFile: return "Invalid File";
2909 }
2910 return "None";
2911}
2912
2913static const char *getSeverityString(enum CXDiagnosticSeverity severity) {
2914 switch (severity) {
2915 case CXDiagnostic_Note: return "note";
2916 case CXDiagnostic_Error: return "error";
2917 case CXDiagnostic_Fatal: return "fatal";
2918 case CXDiagnostic_Ignored: return "ignored";
2919 case CXDiagnostic_Warning: return "warning";
2920 }
2921 return "unknown";
2922}
2923
2924static void printIndent(unsigned indent) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002925 if (indent == 0)
2926 return;
2927 fprintf(stderr, "+");
2928 --indent;
Ted Kremenek15322172011-11-10 08:43:12 +00002929 while (indent > 0) {
Ted Kremeneka7e8a832011-11-11 00:46:43 +00002930 fprintf(stderr, "-");
Ted Kremenek15322172011-11-10 08:43:12 +00002931 --indent;
2932 }
2933}
2934
2935static void printLocation(CXSourceLocation L) {
2936 CXFile File;
2937 CXString FileName;
2938 unsigned line, column, offset;
2939
2940 clang_getExpansionLocation(L, &File, &line, &column, &offset);
2941 FileName = clang_getFileName(File);
2942
2943 fprintf(stderr, "%s:%d:%d", clang_getCString(FileName), line, column);
2944 clang_disposeString(FileName);
2945}
2946
2947static void printRanges(CXDiagnostic D, unsigned indent) {
2948 unsigned i, n = clang_getDiagnosticNumRanges(D);
2949
2950 for (i = 0; i < n; ++i) {
2951 CXSourceLocation Start, End;
2952 CXSourceRange SR = clang_getDiagnosticRange(D, i);
2953 Start = clang_getRangeStart(SR);
2954 End = clang_getRangeEnd(SR);
2955
2956 printIndent(indent);
2957 fprintf(stderr, "Range: ");
2958 printLocation(Start);
2959 fprintf(stderr, " ");
2960 printLocation(End);
2961 fprintf(stderr, "\n");
2962 }
2963}
2964
2965static void printFixIts(CXDiagnostic D, unsigned indent) {
2966 unsigned i, n = clang_getDiagnosticNumFixIts(D);
Ted Kremenek3739b322012-03-20 20:49:45 +00002967 fprintf(stderr, "Number FIXITs = %d\n", n);
Ted Kremenek15322172011-11-10 08:43:12 +00002968 for (i = 0 ; i < n; ++i) {
2969 CXSourceRange ReplacementRange;
2970 CXString text;
2971 text = clang_getDiagnosticFixIt(D, i, &ReplacementRange);
2972
2973 printIndent(indent);
2974 fprintf(stderr, "FIXIT: (");
2975 printLocation(clang_getRangeStart(ReplacementRange));
2976 fprintf(stderr, " - ");
2977 printLocation(clang_getRangeEnd(ReplacementRange));
2978 fprintf(stderr, "): \"%s\"\n", clang_getCString(text));
2979 clang_disposeString(text);
2980 }
2981}
2982
2983static void printDiagnosticSet(CXDiagnosticSet Diags, unsigned indent) {
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002984 unsigned i, n;
2985
Ted Kremenek15322172011-11-10 08:43:12 +00002986 if (!Diags)
2987 return;
2988
NAKAMURA Takumi91909432011-11-10 09:30:15 +00002989 n = clang_getNumDiagnosticsInSet(Diags);
Ted Kremenek15322172011-11-10 08:43:12 +00002990 for (i = 0; i < n; ++i) {
2991 CXSourceLocation DiagLoc;
2992 CXDiagnostic D;
2993 CXFile File;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002994 CXString FileName, DiagSpelling, DiagOption, DiagCat;
Ted Kremenek15322172011-11-10 08:43:12 +00002995 unsigned line, column, offset;
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00002996 const char *DiagOptionStr = 0, *DiagCatStr = 0;
Ted Kremenek15322172011-11-10 08:43:12 +00002997
2998 D = clang_getDiagnosticInSet(Diags, i);
2999 DiagLoc = clang_getDiagnosticLocation(D);
3000 clang_getExpansionLocation(DiagLoc, &File, &line, &column, &offset);
3001 FileName = clang_getFileName(File);
3002 DiagSpelling = clang_getDiagnosticSpelling(D);
3003
3004 printIndent(indent);
3005
3006 fprintf(stderr, "%s:%d:%d: %s: %s",
3007 clang_getCString(FileName),
3008 line,
3009 column,
3010 getSeverityString(clang_getDiagnosticSeverity(D)),
3011 clang_getCString(DiagSpelling));
3012
3013 DiagOption = clang_getDiagnosticOption(D, 0);
3014 DiagOptionStr = clang_getCString(DiagOption);
3015 if (DiagOptionStr) {
3016 fprintf(stderr, " [%s]", DiagOptionStr);
3017 }
3018
Ted Kremenek78d5d3b2012-04-12 00:03:31 +00003019 DiagCat = clang_getDiagnosticCategoryText(D);
3020 DiagCatStr = clang_getCString(DiagCat);
3021 if (DiagCatStr) {
3022 fprintf(stderr, " [%s]", DiagCatStr);
3023 }
3024
Ted Kremenek15322172011-11-10 08:43:12 +00003025 fprintf(stderr, "\n");
3026
3027 printRanges(D, indent);
3028 printFixIts(D, indent);
3029
NAKAMURA Takumia4ca95a2011-11-10 10:07:57 +00003030 /* Print subdiagnostics. */
Ted Kremenek15322172011-11-10 08:43:12 +00003031 printDiagnosticSet(clang_getChildDiagnostics(D), indent+2);
3032
3033 clang_disposeString(FileName);
3034 clang_disposeString(DiagSpelling);
3035 clang_disposeString(DiagOption);
3036 }
3037}
3038
3039static int read_diagnostics(const char *filename) {
3040 enum CXLoadDiag_Error error;
3041 CXString errorString;
3042 CXDiagnosticSet Diags = 0;
3043
3044 Diags = clang_loadDiagnostics(filename, &error, &errorString);
3045 if (!Diags) {
3046 fprintf(stderr, "Trouble deserializing file (%s): %s\n",
3047 getDiagnosticCodeStr(error),
3048 clang_getCString(errorString));
3049 clang_disposeString(errorString);
3050 return 1;
3051 }
3052
3053 printDiagnosticSet(Diags, 0);
Ted Kremeneka7e8a832011-11-11 00:46:43 +00003054 fprintf(stderr, "Number of diagnostics: %d\n",
3055 clang_getNumDiagnosticsInSet(Diags));
Ted Kremenek15322172011-11-10 08:43:12 +00003056 clang_disposeDiagnosticSet(Diags);
3057 return 0;
3058}
3059
3060/******************************************************************************/
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003061/* Command line processing. */
3062/******************************************************************************/
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003063
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003064static CXCursorVisitor GetVisitor(const char *s) {
Ted Kremenek7d405622010-01-12 23:34:26 +00003065 if (s[0] == '\0')
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003066 return FilteredPrintingVisitor;
Ted Kremenek7d405622010-01-12 23:34:26 +00003067 if (strcmp(s, "-usrs") == 0)
3068 return USRVisitor;
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003069 if (strncmp(s, "-memory-usage", 13) == 0)
3070 return GetVisitor(s + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003071 return NULL;
3072}
3073
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003074static void print_usage(void) {
3075 fprintf(stderr,
Ted Kremenek0d435192009-11-17 18:13:31 +00003076 "usage: c-index-test -code-completion-at=<site> <compiler arguments>\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003077 " c-index-test -code-completion-timing=<site> <compiler arguments>\n"
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003078 " c-index-test -cursor-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003079 " c-index-test -file-refs-at=<site> <compiler arguments>\n"
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003080 " c-index-test -index-file [-check-prefix=<FileCheck prefix>] <compiler arguments>\n"
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003081 " c-index-test -index-tu [-check-prefix=<FileCheck prefix>] <AST file>\n"
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003082 " c-index-test -test-file-scan <AST file> <source file> "
Erik Verbruggen26fc0f92011-10-06 11:38:08 +00003083 "[FileCheck prefix]\n");
3084 fprintf(stderr,
Ted Kremenekfe6fd3d2010-01-05 23:18:49 +00003085 " c-index-test -test-load-tu <AST file> <symbol filter> "
3086 "[FileCheck prefix]\n"
Ted Kremenek7d405622010-01-12 23:34:26 +00003087 " c-index-test -test-load-tu-usrs <AST file> <symbol filter> "
3088 "[FileCheck prefix]\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003089 " c-index-test -test-load-source <symbol filter> {<args>}*\n");
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003090 fprintf(stderr,
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003091 " c-index-test -test-load-source-memory-usage "
3092 "<symbol filter> {<args>}*\n"
Douglas Gregorabc563f2010-07-19 21:46:24 +00003093 " c-index-test -test-load-source-reparse <trials> <symbol filter> "
3094 " {<args>}*\n"
Douglas Gregor1982c182010-07-12 18:38:41 +00003095 " c-index-test -test-load-source-usrs <symbol filter> {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003096 " c-index-test -test-load-source-usrs-memory-usage "
3097 "<symbol filter> {<args>}*\n"
Ted Kremenek16b55a72010-01-26 19:31:51 +00003098 " c-index-test -test-annotate-tokens=<range> {<args>}*\n"
3099 " c-index-test -test-inclusion-stack-source {<args>}*\n"
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003100 " c-index-test -test-inclusion-stack-tu <AST file>\n");
Chandler Carruth53513d22010-07-22 06:29:13 +00003101 fprintf(stderr,
Ted Kremenek4e6a3f72011-04-18 23:42:53 +00003102 " c-index-test -test-print-linkage-source {<args>}*\n"
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003103 " c-index-test -test-print-typekind {<args>}*\n"
3104 " c-index-test -print-usr [<CursorKind> {<args>}]*\n"
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003105 " c-index-test -print-usr-file <file>\n"
Ted Kremenek15322172011-11-10 08:43:12 +00003106 " c-index-test -write-pch <file> <compiler arguments>\n");
3107 fprintf(stderr,
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003108 " c-index-test -compilation-db [lookup <filename>] database\n");
3109 fprintf(stderr,
Ted Kremenek15322172011-11-10 08:43:12 +00003110 " c-index-test -read-diagnostics <file>\n\n");
Douglas Gregorcaf4bd32010-07-20 14:34:35 +00003111 fprintf(stderr,
Ted Kremenek7d405622010-01-12 23:34:26 +00003112 " <symbol filter> values:\n%s",
Ted Kremenek0d435192009-11-17 18:13:31 +00003113 " all - load all symbols, including those from PCH\n"
3114 " local - load all symbols except those in PCH\n"
3115 " category - only load ObjC categories (non-PCH)\n"
3116 " interface - only load ObjC interfaces (non-PCH)\n"
3117 " protocol - only load ObjC protocols (non-PCH)\n"
3118 " function - only load functions (non-PCH)\n"
Daniel Dunbar625e4ef2009-12-01 02:35:37 +00003119 " typedef - only load typdefs (non-PCH)\n"
3120 " scan-function - scan function bodies (non-PCH)\n\n");
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003121}
3122
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003123/***/
3124
3125int cindextest_main(int argc, const char **argv) {
Douglas Gregor0a812cf2010-02-18 23:07:20 +00003126 clang_enableStackTraces();
Ted Kremenek15322172011-11-10 08:43:12 +00003127 if (argc > 2 && strcmp(argv[1], "-read-diagnostics") == 0)
3128 return read_diagnostics(argv[2]);
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003129 if (argc > 2 && strstr(argv[1], "-code-completion-at=") == argv[1])
Douglas Gregor1982c182010-07-12 18:38:41 +00003130 return perform_code_completion(argc, argv, 0);
3131 if (argc > 2 && strstr(argv[1], "-code-completion-timing=") == argv[1])
3132 return perform_code_completion(argc, argv, 1);
Douglas Gregorf2c87bd2010-01-15 19:40:17 +00003133 if (argc > 2 && strstr(argv[1], "-cursor-at=") == argv[1])
3134 return inspect_cursor_at(argc, argv);
Argyrios Kyrtzidisaed123e2011-10-06 07:00:54 +00003135 if (argc > 2 && strstr(argv[1], "-file-refs-at=") == argv[1])
3136 return find_file_refs_at(argc, argv);
Argyrios Kyrtzidis4e7064f2011-10-17 19:48:19 +00003137 if (argc > 2 && strcmp(argv[1], "-index-file") == 0)
3138 return index_file(argc - 2, argv + 2);
Argyrios Kyrtzidis21ee5702011-11-15 06:20:16 +00003139 if (argc > 2 && strcmp(argv[1], "-index-tu") == 0)
3140 return index_tu(argc - 2, argv + 2);
Ted Kremenek7d405622010-01-12 23:34:26 +00003141 else if (argc >= 4 && strncmp(argv[1], "-test-load-tu", 13) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003142 CXCursorVisitor I = GetVisitor(argv[1] + 13);
Ted Kremenek7d405622010-01-12 23:34:26 +00003143 if (I)
Ted Kremenekce2ae882010-01-26 17:59:48 +00003144 return perform_test_load_tu(argv[2], argv[3], argc >= 5 ? argv[4] : 0, I,
3145 NULL);
Ted Kremenek7d405622010-01-12 23:34:26 +00003146 }
Douglas Gregorabc563f2010-07-19 21:46:24 +00003147 else if (argc >= 5 && strncmp(argv[1], "-test-load-source-reparse", 25) == 0){
3148 CXCursorVisitor I = GetVisitor(argv[1] + 25);
3149 if (I) {
3150 int trials = atoi(argv[2]);
3151 return perform_test_reparse_source(argc - 4, argv + 4, trials, argv[3], I,
3152 NULL);
3153 }
3154 }
Ted Kremenek7d405622010-01-12 23:34:26 +00003155 else if (argc >= 4 && strncmp(argv[1], "-test-load-source", 17) == 0) {
Douglas Gregore5b72ba2010-01-20 21:32:04 +00003156 CXCursorVisitor I = GetVisitor(argv[1] + 17);
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003157
3158 PostVisitTU postVisit = 0;
3159 if (strstr(argv[1], "-memory-usage"))
3160 postVisit = PrintMemoryUsage;
3161
Ted Kremenek7d405622010-01-12 23:34:26 +00003162 if (I)
Ted Kremenek59fc1e52011-04-18 22:47:10 +00003163 return perform_test_load_source(argc - 3, argv + 3, argv[2], I,
3164 postVisit);
Ted Kremenek7d405622010-01-12 23:34:26 +00003165 }
3166 else if (argc >= 4 && strcmp(argv[1], "-test-file-scan") == 0)
Ted Kremenek1d5fdf32009-11-18 02:02:52 +00003167 return perform_file_scan(argv[2], argv[3],
3168 argc >= 5 ? argv[4] : 0);
Douglas Gregorfc8ea232010-01-26 17:06:03 +00003169 else if (argc > 2 && strstr(argv[1], "-test-annotate-tokens=") == argv[1])
3170 return perform_token_annotation(argc, argv);
Ted Kremenek16b55a72010-01-26 19:31:51 +00003171 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-source") == 0)
3172 return perform_test_load_source(argc - 2, argv + 2, "all", NULL,
3173 PrintInclusionStack);
3174 else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0)
3175 return perform_test_load_tu(argv[2], "all", NULL, NULL,
3176 PrintInclusionStack);
Ted Kremenek3bed5272010-03-03 06:37:58 +00003177 else if (argc > 2 && strcmp(argv[1], "-test-print-linkage-source") == 0)
3178 return perform_test_load_source(argc - 2, argv + 2, "all", PrintLinkage,
3179 NULL);
Ted Kremenek8e0ac172010-05-14 21:29:26 +00003180 else if (argc > 2 && strcmp(argv[1], "-test-print-typekind") == 0)
3181 return perform_test_load_source(argc - 2, argv + 2, "all",
3182 PrintTypeKind, 0);
Ted Kremenekf7b714d2010-03-25 02:00:39 +00003183 else if (argc > 1 && strcmp(argv[1], "-print-usr") == 0) {
3184 if (argc > 2)
3185 return print_usrs(argv + 2, argv + argc);
3186 else {
3187 display_usrs();
3188 return 1;
3189 }
3190 }
3191 else if (argc > 2 && strcmp(argv[1], "-print-usr-file") == 0)
3192 return print_usrs_file(argv[2]);
Douglas Gregor7ae2faa2010-08-13 05:36:37 +00003193 else if (argc > 2 && strcmp(argv[1], "-write-pch") == 0)
3194 return write_pch_file(argv[2], argc - 3, argv + 3);
Arnaud A. de Grandmaisondb293182012-06-30 11:27:57 +00003195 else if (argc > 2 && strcmp(argv[1], "-compilation-db") == 0)
3196 return perform_test_compilation_db(argv[argc-1], argc - 3, argv + 2);
3197
Ted Kremenekf5d9c932009-11-17 18:09:14 +00003198 print_usage();
3199 return 1;
Steve Naroff50398192009-08-28 15:28:48 +00003200}
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003201
3202/***/
3203
3204/* We intentionally run in a separate thread to ensure we at least minimal
3205 * testing of a multithreaded environment (for example, having a reduced stack
3206 * size). */
3207
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003208typedef struct thread_info {
3209 int argc;
3210 const char **argv;
3211 int result;
3212} thread_info;
Benjamin Kramer84294912010-11-04 19:11:31 +00003213void thread_runner(void *client_data_v) {
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003214 thread_info *client_data = client_data_v;
3215 client_data->result = cindextest_main(client_data->argc, client_data->argv);
NAKAMURA Takumi3be55cd2012-04-07 06:59:28 +00003216#ifdef __CYGWIN__
3217 fflush(stdout); /* stdout is not flushed on Cygwin. */
3218#endif
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003219}
3220
3221int main(int argc, const char **argv) {
3222 thread_info client_data;
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003223
Douglas Gregor61605982010-10-27 16:00:01 +00003224 if (getenv("CINDEXTEST_NOTHREADS"))
3225 return cindextest_main(argc, argv);
3226
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003227 client_data.argc = argc;
3228 client_data.argv = argv;
Daniel Dunbara32a6e12010-11-04 01:26:31 +00003229 clang_executeOnThread(thread_runner, &client_data, 0);
Daniel Dunbar6edc8002010-09-30 20:39:47 +00003230 return client_data.result;
3231}