| /* c-index-test.c */ |
| |
| #include "clang/Config/config.h" |
| #include "clang-c/Index.h" |
| #include "clang-c/CXCompilationDatabase.h" |
| #include "clang-c/BuildSystem.h" |
| #include "clang-c/Documentation.h" |
| #include <ctype.h> |
| #include <stdlib.h> |
| #include <stdio.h> |
| #include <string.h> |
| #include <assert.h> |
| |
| #ifdef CLANG_HAVE_LIBXML |
| #include <libxml/parser.h> |
| #include <libxml/relaxng.h> |
| #include <libxml/xmlerror.h> |
| #endif |
| |
| #ifdef _WIN32 |
| # include <direct.h> |
| #else |
| # include <unistd.h> |
| #endif |
| |
| extern int indextest_core_main(int argc, const char **argv); |
| |
| /******************************************************************************/ |
| /* Utility functions. */ |
| /******************************************************************************/ |
| |
| #ifdef _MSC_VER |
| char *basename(const char* path) |
| { |
| char* base1 = (char*)strrchr(path, '/'); |
| char* base2 = (char*)strrchr(path, '\\'); |
| if (base1 && base2) |
| return((base1 > base2) ? base1 + 1 : base2 + 1); |
| else if (base1) |
| return(base1 + 1); |
| else if (base2) |
| return(base2 + 1); |
| |
| return((char*)path); |
| } |
| char *dirname(char* path) |
| { |
| char* base1 = (char*)strrchr(path, '/'); |
| char* base2 = (char*)strrchr(path, '\\'); |
| if (base1 && base2) |
| if (base1 > base2) |
| *base1 = 0; |
| else |
| *base2 = 0; |
| else if (base1) |
| *base1 = 0; |
| else if (base2) |
| *base2 = 0; |
| |
| return path; |
| } |
| #else |
| extern char *basename(const char *); |
| extern char *dirname(char *); |
| #endif |
| |
| /** \brief Return the default parsing options. */ |
| static unsigned getDefaultParsingOptions() { |
| unsigned options = CXTranslationUnit_DetailedPreprocessingRecord; |
| |
| if (getenv("CINDEXTEST_EDITING")) |
| options |= clang_defaultEditingTranslationUnitOptions(); |
| if (getenv("CINDEXTEST_COMPLETION_CACHING")) |
| options |= CXTranslationUnit_CacheCompletionResults; |
| if (getenv("CINDEXTEST_COMPLETION_NO_CACHING")) |
| options &= ~CXTranslationUnit_CacheCompletionResults; |
| if (getenv("CINDEXTEST_SKIP_FUNCTION_BODIES")) |
| options |= CXTranslationUnit_SkipFunctionBodies; |
| if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS")) |
| options |= CXTranslationUnit_IncludeBriefCommentsInCodeCompletion; |
| if (getenv("CINDEXTEST_CREATE_PREAMBLE_ON_FIRST_PARSE")) |
| options |= CXTranslationUnit_CreatePreambleOnFirstParse; |
| if (getenv("CINDEXTEST_KEEP_GOING")) |
| options |= CXTranslationUnit_KeepGoing; |
| |
| return options; |
| } |
| |
| /** \brief Returns 0 in case of success, non-zero in case of a failure. */ |
| static int checkForErrors(CXTranslationUnit TU); |
| |
| static void describeLibclangFailure(enum CXErrorCode Err) { |
| switch (Err) { |
| case CXError_Success: |
| fprintf(stderr, "Success\n"); |
| return; |
| |
| case CXError_Failure: |
| fprintf(stderr, "Failure (no details available)\n"); |
| return; |
| |
| case CXError_Crashed: |
| fprintf(stderr, "Failure: libclang crashed\n"); |
| return; |
| |
| case CXError_InvalidArguments: |
| fprintf(stderr, "Failure: invalid arguments passed to a libclang routine\n"); |
| return; |
| |
| case CXError_ASTReadError: |
| fprintf(stderr, "Failure: AST deserialization error occurred\n"); |
| return; |
| } |
| } |
| |
| static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, |
| unsigned end_line, unsigned end_column) { |
| fprintf(out, "[%d:%d - %d:%d]", begin_line, begin_column, |
| end_line, end_column); |
| } |
| |
| static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, |
| CXTranslationUnit *TU) { |
| enum CXErrorCode Err = clang_createTranslationUnit2(Idx, file, TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); |
| describeLibclangFailure(Err); |
| *TU = 0; |
| return 0; |
| } |
| return 1; |
| } |
| |
| void free_remapped_files(struct CXUnsavedFile *unsaved_files, |
| int num_unsaved_files) { |
| int i; |
| for (i = 0; i != num_unsaved_files; ++i) { |
| free((char *)unsaved_files[i].Filename); |
| free((char *)unsaved_files[i].Contents); |
| } |
| free(unsaved_files); |
| } |
| |
| static int parse_remapped_files_with_opt(const char *opt_name, |
| int argc, const char **argv, |
| int start_arg, |
| struct CXUnsavedFile **unsaved_files, |
| int *num_unsaved_files) { |
| int i; |
| int arg; |
| int prefix_len = strlen(opt_name); |
| int arg_indices[20]; |
| *unsaved_files = 0; |
| *num_unsaved_files = 0; |
| |
| /* Count the number of remapped files. */ |
| for (arg = start_arg; arg < argc; ++arg) { |
| if (strncmp(argv[arg], opt_name, prefix_len)) |
| continue; |
| |
| assert(*num_unsaved_files < (int)(sizeof(arg_indices)/sizeof(int))); |
| arg_indices[*num_unsaved_files] = arg; |
| ++*num_unsaved_files; |
| } |
| |
| if (*num_unsaved_files == 0) |
| return 0; |
| |
| *unsaved_files |
| = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * |
| *num_unsaved_files); |
| for (i = 0; i != *num_unsaved_files; ++i) { |
| struct CXUnsavedFile *unsaved = *unsaved_files + i; |
| const char *arg_string = argv[arg_indices[i]] + prefix_len; |
| int filename_len; |
| char *filename; |
| char *contents; |
| FILE *to_file; |
| const char *sep = strchr(arg_string, ','); |
| if (!sep) { |
| fprintf(stderr, |
| "error: %sfrom:to argument is missing comma\n", opt_name); |
| free_remapped_files(*unsaved_files, i); |
| *unsaved_files = 0; |
| *num_unsaved_files = 0; |
| return -1; |
| } |
| |
| /* Open the file that we're remapping to. */ |
| to_file = fopen(sep + 1, "rb"); |
| if (!to_file) { |
| fprintf(stderr, "error: cannot open file %s that we are remapping to\n", |
| sep + 1); |
| free_remapped_files(*unsaved_files, i); |
| *unsaved_files = 0; |
| *num_unsaved_files = 0; |
| return -1; |
| } |
| |
| /* Determine the length of the file we're remapping to. */ |
| fseek(to_file, 0, SEEK_END); |
| unsaved->Length = ftell(to_file); |
| fseek(to_file, 0, SEEK_SET); |
| |
| /* Read the contents of the file we're remapping to. */ |
| contents = (char *)malloc(unsaved->Length + 1); |
| if (fread(contents, 1, unsaved->Length, to_file) != unsaved->Length) { |
| fprintf(stderr, "error: unexpected %s reading 'to' file %s\n", |
| (feof(to_file) ? "EOF" : "error"), sep + 1); |
| fclose(to_file); |
| free_remapped_files(*unsaved_files, i); |
| free(contents); |
| *unsaved_files = 0; |
| *num_unsaved_files = 0; |
| return -1; |
| } |
| contents[unsaved->Length] = 0; |
| unsaved->Contents = contents; |
| |
| /* Close the file. */ |
| fclose(to_file); |
| |
| /* Copy the file name that we're remapping from. */ |
| filename_len = sep - arg_string; |
| filename = (char *)malloc(filename_len + 1); |
| memcpy(filename, arg_string, filename_len); |
| filename[filename_len] = 0; |
| unsaved->Filename = filename; |
| } |
| |
| return 0; |
| } |
| |
| static int parse_remapped_files(int argc, const char **argv, int start_arg, |
| struct CXUnsavedFile **unsaved_files, |
| int *num_unsaved_files) { |
| return parse_remapped_files_with_opt("-remap-file=", argc, argv, start_arg, |
| unsaved_files, num_unsaved_files); |
| } |
| |
| static int parse_remapped_files_with_try(int try_idx, |
| int argc, const char **argv, |
| int start_arg, |
| struct CXUnsavedFile **unsaved_files, |
| int *num_unsaved_files) { |
| struct CXUnsavedFile *unsaved_files_no_try_idx; |
| int num_unsaved_files_no_try_idx; |
| struct CXUnsavedFile *unsaved_files_try_idx; |
| int num_unsaved_files_try_idx; |
| int ret; |
| char opt_name[32]; |
| |
| ret = parse_remapped_files(argc, argv, start_arg, |
| &unsaved_files_no_try_idx, &num_unsaved_files_no_try_idx); |
| if (ret) |
| return ret; |
| |
| sprintf(opt_name, "-remap-file-%d=", try_idx); |
| ret = parse_remapped_files_with_opt(opt_name, argc, argv, start_arg, |
| &unsaved_files_try_idx, &num_unsaved_files_try_idx); |
| if (ret) |
| return ret; |
| |
| if (num_unsaved_files_no_try_idx == 0) { |
| *unsaved_files = unsaved_files_try_idx; |
| *num_unsaved_files = num_unsaved_files_try_idx; |
| return 0; |
| } |
| if (num_unsaved_files_try_idx == 0) { |
| *unsaved_files = unsaved_files_no_try_idx; |
| *num_unsaved_files = num_unsaved_files_no_try_idx; |
| return 0; |
| } |
| |
| *num_unsaved_files = num_unsaved_files_no_try_idx + num_unsaved_files_try_idx; |
| *unsaved_files |
| = (struct CXUnsavedFile *)realloc(unsaved_files_no_try_idx, |
| sizeof(struct CXUnsavedFile) * |
| *num_unsaved_files); |
| memcpy(*unsaved_files + num_unsaved_files_no_try_idx, |
| unsaved_files_try_idx, sizeof(struct CXUnsavedFile) * |
| num_unsaved_files_try_idx); |
| free(unsaved_files_try_idx); |
| return 0; |
| } |
| |
| static const char *parse_comments_schema(int argc, const char **argv) { |
| const char *CommentsSchemaArg = "-comments-xml-schema="; |
| const char *CommentSchemaFile = NULL; |
| |
| if (argc == 0) |
| return CommentSchemaFile; |
| |
| if (!strncmp(argv[0], CommentsSchemaArg, strlen(CommentsSchemaArg))) |
| CommentSchemaFile = argv[0] + strlen(CommentsSchemaArg); |
| |
| return CommentSchemaFile; |
| } |
| |
| /******************************************************************************/ |
| /* Pretty-printing. */ |
| /******************************************************************************/ |
| |
| static const char *FileCheckPrefix = "CHECK"; |
| |
| static void PrintCString(const char *CStr) { |
| if (CStr != NULL && CStr[0] != '\0') { |
| for ( ; *CStr; ++CStr) { |
| const char C = *CStr; |
| switch (C) { |
| case '\n': printf("\\n"); break; |
| case '\r': printf("\\r"); break; |
| case '\t': printf("\\t"); break; |
| case '\v': printf("\\v"); break; |
| case '\f': printf("\\f"); break; |
| default: putchar(C); break; |
| } |
| } |
| } |
| } |
| |
| static void PrintCStringWithPrefix(const char *Prefix, const char *CStr) { |
| printf(" %s=[", Prefix); |
| PrintCString(CStr); |
| printf("]"); |
| } |
| |
| static void PrintCXStringAndDispose(CXString Str) { |
| PrintCString(clang_getCString(Str)); |
| clang_disposeString(Str); |
| } |
| |
| static void PrintCXStringWithPrefix(const char *Prefix, CXString Str) { |
| PrintCStringWithPrefix(Prefix, clang_getCString(Str)); |
| } |
| |
| static void PrintCXStringWithPrefixAndDispose(const char *Prefix, |
| CXString Str) { |
| PrintCStringWithPrefix(Prefix, clang_getCString(Str)); |
| clang_disposeString(Str); |
| } |
| |
| static void PrintRange(CXSourceRange R, const char *str) { |
| CXFile begin_file, end_file; |
| unsigned begin_line, begin_column, end_line, end_column; |
| |
| clang_getSpellingLocation(clang_getRangeStart(R), |
| &begin_file, &begin_line, &begin_column, 0); |
| clang_getSpellingLocation(clang_getRangeEnd(R), |
| &end_file, &end_line, &end_column, 0); |
| if (!begin_file || !end_file) |
| return; |
| |
| if (str) |
| printf(" %s=", str); |
| PrintExtent(stdout, begin_line, begin_column, end_line, end_column); |
| } |
| |
| int want_display_name = 0; |
| |
| static void printVersion(const char *Prefix, CXVersion Version) { |
| if (Version.Major < 0) |
| return; |
| printf("%s%d", Prefix, Version.Major); |
| |
| if (Version.Minor < 0) |
| return; |
| printf(".%d", Version.Minor); |
| |
| if (Version.Subminor < 0) |
| return; |
| printf(".%d", Version.Subminor); |
| } |
| |
| struct CommentASTDumpingContext { |
| int IndentLevel; |
| }; |
| |
| static void DumpCXCommentInternal(struct CommentASTDumpingContext *Ctx, |
| CXComment Comment) { |
| unsigned i; |
| unsigned e; |
| enum CXCommentKind Kind = clang_Comment_getKind(Comment); |
| |
| Ctx->IndentLevel++; |
| for (i = 0, e = Ctx->IndentLevel; i != e; ++i) |
| printf(" "); |
| |
| printf("("); |
| switch (Kind) { |
| case CXComment_Null: |
| printf("CXComment_Null"); |
| break; |
| case CXComment_Text: |
| printf("CXComment_Text"); |
| PrintCXStringWithPrefixAndDispose("Text", |
| clang_TextComment_getText(Comment)); |
| if (clang_Comment_isWhitespace(Comment)) |
| printf(" IsWhitespace"); |
| if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| printf(" HasTrailingNewline"); |
| break; |
| case CXComment_InlineCommand: |
| printf("CXComment_InlineCommand"); |
| PrintCXStringWithPrefixAndDispose( |
| "CommandName", |
| clang_InlineCommandComment_getCommandName(Comment)); |
| switch (clang_InlineCommandComment_getRenderKind(Comment)) { |
| case CXCommentInlineCommandRenderKind_Normal: |
| printf(" RenderNormal"); |
| break; |
| case CXCommentInlineCommandRenderKind_Bold: |
| printf(" RenderBold"); |
| break; |
| case CXCommentInlineCommandRenderKind_Monospaced: |
| printf(" RenderMonospaced"); |
| break; |
| case CXCommentInlineCommandRenderKind_Emphasized: |
| printf(" RenderEmphasized"); |
| break; |
| } |
| for (i = 0, e = clang_InlineCommandComment_getNumArgs(Comment); |
| i != e; ++i) { |
| printf(" Arg[%u]=", i); |
| PrintCXStringAndDispose( |
| clang_InlineCommandComment_getArgText(Comment, i)); |
| } |
| if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| printf(" HasTrailingNewline"); |
| break; |
| case CXComment_HTMLStartTag: { |
| unsigned NumAttrs; |
| printf("CXComment_HTMLStartTag"); |
| PrintCXStringWithPrefixAndDispose( |
| "Name", |
| clang_HTMLTagComment_getTagName(Comment)); |
| NumAttrs = clang_HTMLStartTag_getNumAttrs(Comment); |
| if (NumAttrs != 0) { |
| printf(" Attrs:"); |
| for (i = 0; i != NumAttrs; ++i) { |
| printf(" "); |
| PrintCXStringAndDispose(clang_HTMLStartTag_getAttrName(Comment, i)); |
| printf("="); |
| PrintCXStringAndDispose(clang_HTMLStartTag_getAttrValue(Comment, i)); |
| } |
| } |
| if (clang_HTMLStartTagComment_isSelfClosing(Comment)) |
| printf(" SelfClosing"); |
| if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| printf(" HasTrailingNewline"); |
| break; |
| } |
| case CXComment_HTMLEndTag: |
| printf("CXComment_HTMLEndTag"); |
| PrintCXStringWithPrefixAndDispose( |
| "Name", |
| clang_HTMLTagComment_getTagName(Comment)); |
| if (clang_InlineContentComment_hasTrailingNewline(Comment)) |
| printf(" HasTrailingNewline"); |
| break; |
| case CXComment_Paragraph: |
| printf("CXComment_Paragraph"); |
| if (clang_Comment_isWhitespace(Comment)) |
| printf(" IsWhitespace"); |
| break; |
| case CXComment_BlockCommand: |
| printf("CXComment_BlockCommand"); |
| PrintCXStringWithPrefixAndDispose( |
| "CommandName", |
| clang_BlockCommandComment_getCommandName(Comment)); |
| for (i = 0, e = clang_BlockCommandComment_getNumArgs(Comment); |
| i != e; ++i) { |
| printf(" Arg[%u]=", i); |
| PrintCXStringAndDispose( |
| clang_BlockCommandComment_getArgText(Comment, i)); |
| } |
| break; |
| case CXComment_ParamCommand: |
| printf("CXComment_ParamCommand"); |
| switch (clang_ParamCommandComment_getDirection(Comment)) { |
| case CXCommentParamPassDirection_In: |
| printf(" in"); |
| break; |
| case CXCommentParamPassDirection_Out: |
| printf(" out"); |
| break; |
| case CXCommentParamPassDirection_InOut: |
| printf(" in,out"); |
| break; |
| } |
| if (clang_ParamCommandComment_isDirectionExplicit(Comment)) |
| printf(" explicitly"); |
| else |
| printf(" implicitly"); |
| PrintCXStringWithPrefixAndDispose( |
| "ParamName", |
| clang_ParamCommandComment_getParamName(Comment)); |
| if (clang_ParamCommandComment_isParamIndexValid(Comment)) |
| printf(" ParamIndex=%u", clang_ParamCommandComment_getParamIndex(Comment)); |
| else |
| printf(" ParamIndex=Invalid"); |
| break; |
| case CXComment_TParamCommand: |
| printf("CXComment_TParamCommand"); |
| PrintCXStringWithPrefixAndDispose( |
| "ParamName", |
| clang_TParamCommandComment_getParamName(Comment)); |
| if (clang_TParamCommandComment_isParamPositionValid(Comment)) { |
| printf(" ParamPosition={"); |
| for (i = 0, e = clang_TParamCommandComment_getDepth(Comment); |
| i != e; ++i) { |
| printf("%u", clang_TParamCommandComment_getIndex(Comment, i)); |
| if (i != e - 1) |
| printf(", "); |
| } |
| printf("}"); |
| } else |
| printf(" ParamPosition=Invalid"); |
| break; |
| case CXComment_VerbatimBlockCommand: |
| printf("CXComment_VerbatimBlockCommand"); |
| PrintCXStringWithPrefixAndDispose( |
| "CommandName", |
| clang_BlockCommandComment_getCommandName(Comment)); |
| break; |
| case CXComment_VerbatimBlockLine: |
| printf("CXComment_VerbatimBlockLine"); |
| PrintCXStringWithPrefixAndDispose( |
| "Text", |
| clang_VerbatimBlockLineComment_getText(Comment)); |
| break; |
| case CXComment_VerbatimLine: |
| printf("CXComment_VerbatimLine"); |
| PrintCXStringWithPrefixAndDispose( |
| "Text", |
| clang_VerbatimLineComment_getText(Comment)); |
| break; |
| case CXComment_FullComment: |
| printf("CXComment_FullComment"); |
| break; |
| } |
| if (Kind != CXComment_Null) { |
| const unsigned NumChildren = clang_Comment_getNumChildren(Comment); |
| unsigned i; |
| for (i = 0; i != NumChildren; ++i) { |
| printf("\n// %s: ", FileCheckPrefix); |
| DumpCXCommentInternal(Ctx, clang_Comment_getChild(Comment, i)); |
| } |
| } |
| printf(")"); |
| Ctx->IndentLevel--; |
| } |
| |
| static void DumpCXComment(CXComment Comment) { |
| struct CommentASTDumpingContext Ctx; |
| Ctx.IndentLevel = 1; |
| printf("\n// %s: CommentAST=[\n// %s:", FileCheckPrefix, FileCheckPrefix); |
| DumpCXCommentInternal(&Ctx, Comment); |
| printf("]"); |
| } |
| |
| static void ValidateCommentXML(const char *Str, const char *CommentSchemaFile) { |
| #ifdef CLANG_HAVE_LIBXML |
| xmlRelaxNGParserCtxtPtr RNGParser; |
| xmlRelaxNGPtr Schema; |
| xmlDocPtr Doc; |
| xmlRelaxNGValidCtxtPtr ValidationCtxt; |
| int status; |
| |
| if (!CommentSchemaFile) |
| return; |
| |
| RNGParser = xmlRelaxNGNewParserCtxt(CommentSchemaFile); |
| if (!RNGParser) { |
| printf(" libXMLError"); |
| return; |
| } |
| Schema = xmlRelaxNGParse(RNGParser); |
| |
| Doc = xmlParseDoc((const xmlChar *) Str); |
| |
| if (!Doc) { |
| xmlErrorPtr Error = xmlGetLastError(); |
| printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message); |
| return; |
| } |
| |
| ValidationCtxt = xmlRelaxNGNewValidCtxt(Schema); |
| status = xmlRelaxNGValidateDoc(ValidationCtxt, Doc); |
| if (!status) |
| printf(" CommentXMLValid"); |
| else if (status > 0) { |
| xmlErrorPtr Error = xmlGetLastError(); |
| printf(" CommentXMLInvalid [not vaild XML: %s]", Error->message); |
| } else |
| printf(" libXMLError"); |
| |
| xmlRelaxNGFreeValidCtxt(ValidationCtxt); |
| xmlFreeDoc(Doc); |
| xmlRelaxNGFree(Schema); |
| xmlRelaxNGFreeParserCtxt(RNGParser); |
| #endif |
| } |
| |
| static void PrintCursorComments(CXCursor Cursor, |
| const char *CommentSchemaFile) { |
| { |
| CXString RawComment; |
| const char *RawCommentCString; |
| CXString BriefComment; |
| const char *BriefCommentCString; |
| |
| RawComment = clang_Cursor_getRawCommentText(Cursor); |
| RawCommentCString = clang_getCString(RawComment); |
| if (RawCommentCString != NULL && RawCommentCString[0] != '\0') { |
| PrintCStringWithPrefix("RawComment", RawCommentCString); |
| PrintRange(clang_Cursor_getCommentRange(Cursor), "RawCommentRange"); |
| |
| BriefComment = clang_Cursor_getBriefCommentText(Cursor); |
| BriefCommentCString = clang_getCString(BriefComment); |
| if (BriefCommentCString != NULL && BriefCommentCString[0] != '\0') |
| PrintCStringWithPrefix("BriefComment", BriefCommentCString); |
| clang_disposeString(BriefComment); |
| } |
| clang_disposeString(RawComment); |
| } |
| |
| { |
| CXComment Comment = clang_Cursor_getParsedComment(Cursor); |
| if (clang_Comment_getKind(Comment) != CXComment_Null) { |
| PrintCXStringWithPrefixAndDispose("FullCommentAsHTML", |
| clang_FullComment_getAsHTML(Comment)); |
| { |
| CXString XML; |
| XML = clang_FullComment_getAsXML(Comment); |
| PrintCXStringWithPrefix("FullCommentAsXML", XML); |
| ValidateCommentXML(clang_getCString(XML), CommentSchemaFile); |
| clang_disposeString(XML); |
| } |
| |
| DumpCXComment(Comment); |
| } |
| } |
| } |
| |
| typedef struct { |
| unsigned line; |
| unsigned col; |
| } LineCol; |
| |
| static int lineCol_cmp(const void *p1, const void *p2) { |
| const LineCol *lhs = p1; |
| const LineCol *rhs = p2; |
| if (lhs->line != rhs->line) |
| return (int)lhs->line - (int)rhs->line; |
| return (int)lhs->col - (int)rhs->col; |
| } |
| |
| static void PrintCursor(CXCursor Cursor, const char *CommentSchemaFile) { |
| CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
| if (clang_isInvalid(Cursor.kind)) { |
| CXString ks = clang_getCursorKindSpelling(Cursor.kind); |
| printf("Invalid Cursor => %s", clang_getCString(ks)); |
| clang_disposeString(ks); |
| } |
| else { |
| CXString string, ks; |
| CXCursor Referenced; |
| unsigned line, column; |
| CXCursor SpecializationOf; |
| CXCursor *overridden; |
| unsigned num_overridden; |
| unsigned RefNameRangeNr; |
| CXSourceRange CursorExtent; |
| CXSourceRange RefNameRange; |
| int AlwaysUnavailable; |
| int AlwaysDeprecated; |
| CXString UnavailableMessage; |
| CXString DeprecatedMessage; |
| CXPlatformAvailability PlatformAvailability[2]; |
| int NumPlatformAvailability; |
| int I; |
| |
| ks = clang_getCursorKindSpelling(Cursor.kind); |
| string = want_display_name? clang_getCursorDisplayName(Cursor) |
| : clang_getCursorSpelling(Cursor); |
| printf("%s=%s", clang_getCString(ks), |
| clang_getCString(string)); |
| clang_disposeString(ks); |
| clang_disposeString(string); |
| |
| Referenced = clang_getCursorReferenced(Cursor); |
| if (!clang_equalCursors(Referenced, clang_getNullCursor())) { |
| if (clang_getCursorKind(Referenced) == CXCursor_OverloadedDeclRef) { |
| unsigned I, N = clang_getNumOverloadedDecls(Referenced); |
| printf("["); |
| for (I = 0; I != N; ++I) { |
| CXCursor Ovl = clang_getOverloadedDecl(Referenced, I); |
| CXSourceLocation Loc; |
| if (I) |
| printf(", "); |
| |
| Loc = clang_getCursorLocation(Ovl); |
| clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
| printf("%d:%d", line, column); |
| } |
| printf("]"); |
| } else { |
| CXSourceLocation Loc = clang_getCursorLocation(Referenced); |
| clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
| printf(":%d:%d", line, column); |
| } |
| |
| if (clang_getCursorKind(Referenced) == CXCursor_TypedefDecl) { |
| CXType T = clang_getCursorType(Referenced); |
| if (clang_Type_isTransparentTagTypedef(T)) { |
| CXType Underlying = clang_getTypedefDeclUnderlyingType(Referenced); |
| CXString S = clang_getTypeSpelling(Underlying); |
| printf(" (Transparent: %s)", clang_getCString(S)); |
| clang_disposeString(S); |
| } |
| } |
| } |
| |
| if (clang_isCursorDefinition(Cursor)) |
| printf(" (Definition)"); |
| |
| switch (clang_getCursorAvailability(Cursor)) { |
| case CXAvailability_Available: |
| break; |
| |
| case CXAvailability_Deprecated: |
| printf(" (deprecated)"); |
| break; |
| |
| case CXAvailability_NotAvailable: |
| printf(" (unavailable)"); |
| break; |
| |
| case CXAvailability_NotAccessible: |
| printf(" (inaccessible)"); |
| break; |
| } |
| |
| NumPlatformAvailability |
| = clang_getCursorPlatformAvailability(Cursor, |
| &AlwaysDeprecated, |
| &DeprecatedMessage, |
| &AlwaysUnavailable, |
| &UnavailableMessage, |
| PlatformAvailability, 2); |
| if (AlwaysUnavailable) { |
| printf(" (always unavailable: \"%s\")", |
| clang_getCString(UnavailableMessage)); |
| } else if (AlwaysDeprecated) { |
| printf(" (always deprecated: \"%s\")", |
| clang_getCString(DeprecatedMessage)); |
| } else { |
| for (I = 0; I != NumPlatformAvailability; ++I) { |
| if (I >= 2) |
| break; |
| |
| printf(" (%s", clang_getCString(PlatformAvailability[I].Platform)); |
| if (PlatformAvailability[I].Unavailable) |
| printf(", unavailable"); |
| else { |
| printVersion(", introduced=", PlatformAvailability[I].Introduced); |
| printVersion(", deprecated=", PlatformAvailability[I].Deprecated); |
| printVersion(", obsoleted=", PlatformAvailability[I].Obsoleted); |
| } |
| if (clang_getCString(PlatformAvailability[I].Message)[0]) |
| printf(", message=\"%s\"", |
| clang_getCString(PlatformAvailability[I].Message)); |
| printf(")"); |
| } |
| } |
| for (I = 0; I != NumPlatformAvailability; ++I) { |
| if (I >= 2) |
| break; |
| clang_disposeCXPlatformAvailability(PlatformAvailability + I); |
| } |
| |
| clang_disposeString(DeprecatedMessage); |
| clang_disposeString(UnavailableMessage); |
| |
| if (clang_CXXConstructor_isDefaultConstructor(Cursor)) |
| printf(" (default constructor)"); |
| |
| if (clang_CXXConstructor_isMoveConstructor(Cursor)) |
| printf(" (move constructor)"); |
| if (clang_CXXConstructor_isCopyConstructor(Cursor)) |
| printf(" (copy constructor)"); |
| if (clang_CXXConstructor_isConvertingConstructor(Cursor)) |
| printf(" (converting constructor)"); |
| if (clang_CXXField_isMutable(Cursor)) |
| printf(" (mutable)"); |
| if (clang_CXXMethod_isDefaulted(Cursor)) |
| printf(" (defaulted)"); |
| if (clang_CXXMethod_isStatic(Cursor)) |
| printf(" (static)"); |
| if (clang_CXXMethod_isVirtual(Cursor)) |
| printf(" (virtual)"); |
| if (clang_CXXMethod_isConst(Cursor)) |
| printf(" (const)"); |
| if (clang_CXXMethod_isPureVirtual(Cursor)) |
| printf(" (pure)"); |
| if (clang_EnumDecl_isScoped(Cursor)) |
| printf(" (scoped)"); |
| if (clang_Cursor_isVariadic(Cursor)) |
| printf(" (variadic)"); |
| if (clang_Cursor_isObjCOptional(Cursor)) |
| printf(" (@optional)"); |
| |
| switch (clang_getCursorExceptionSpecificationType(Cursor)) |
| { |
| case CXCursor_ExceptionSpecificationKind_None: |
| break; |
| |
| case CXCursor_ExceptionSpecificationKind_DynamicNone: |
| printf(" (noexcept dynamic none)"); |
| break; |
| |
| case CXCursor_ExceptionSpecificationKind_Dynamic: |
| printf(" (noexcept dynamic)"); |
| break; |
| |
| case CXCursor_ExceptionSpecificationKind_MSAny: |
| printf(" (noexcept dynamic any)"); |
| break; |
| |
| case CXCursor_ExceptionSpecificationKind_BasicNoexcept: |
| printf(" (noexcept)"); |
| break; |
| |
| case CXCursor_ExceptionSpecificationKind_ComputedNoexcept: |
| printf(" (computed-noexcept)"); |
| break; |
| |
| case CXCursor_ExceptionSpecificationKind_Unevaluated: |
| case CXCursor_ExceptionSpecificationKind_Uninstantiated: |
| case CXCursor_ExceptionSpecificationKind_Unparsed: |
| break; |
| } |
| |
| { |
| CXString language; |
| CXString definedIn; |
| unsigned generated; |
| if (clang_Cursor_isExternalSymbol(Cursor, &language, &definedIn, |
| &generated)) { |
| printf(" (external lang: %s, defined: %s, gen: %d)", |
| clang_getCString(language), clang_getCString(definedIn), generated); |
| clang_disposeString(language); |
| clang_disposeString(definedIn); |
| } |
| } |
| |
| if (Cursor.kind == CXCursor_IBOutletCollectionAttr) { |
| CXType T = |
| clang_getCanonicalType(clang_getIBOutletCollectionType(Cursor)); |
| CXString S = clang_getTypeKindSpelling(T.kind); |
| printf(" [IBOutletCollection=%s]", clang_getCString(S)); |
| clang_disposeString(S); |
| } |
| |
| if (Cursor.kind == CXCursor_CXXBaseSpecifier) { |
| enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); |
| unsigned isVirtual = clang_isVirtualBase(Cursor); |
| const char *accessStr = 0; |
| |
| switch (access) { |
| case CX_CXXInvalidAccessSpecifier: |
| accessStr = "invalid"; break; |
| case CX_CXXPublic: |
| accessStr = "public"; break; |
| case CX_CXXProtected: |
| accessStr = "protected"; break; |
| case CX_CXXPrivate: |
| accessStr = "private"; break; |
| } |
| |
| printf(" [access=%s isVirtual=%s]", accessStr, |
| isVirtual ? "true" : "false"); |
| } |
| |
| SpecializationOf = clang_getSpecializedCursorTemplate(Cursor); |
| if (!clang_equalCursors(SpecializationOf, clang_getNullCursor())) { |
| CXSourceLocation Loc = clang_getCursorLocation(SpecializationOf); |
| CXString Name = clang_getCursorSpelling(SpecializationOf); |
| clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
| printf(" [Specialization of %s:%d:%d]", |
| clang_getCString(Name), line, column); |
| clang_disposeString(Name); |
| |
| if (Cursor.kind == CXCursor_FunctionDecl) { |
| /* Collect the template parameter kinds from the base template. */ |
| int NumTemplateArgs = clang_Cursor_getNumTemplateArguments(Cursor); |
| int I; |
| if (NumTemplateArgs < 0) { |
| printf(" [no template arg info]"); |
| } |
| for (I = 0; I < NumTemplateArgs; I++) { |
| enum CXTemplateArgumentKind TAK = |
| clang_Cursor_getTemplateArgumentKind(Cursor, I); |
| switch(TAK) { |
| case CXTemplateArgumentKind_Type: |
| { |
| CXType T = clang_Cursor_getTemplateArgumentType(Cursor, I); |
| CXString S = clang_getTypeSpelling(T); |
| printf(" [Template arg %d: kind: %d, type: %s]", |
| I, TAK, clang_getCString(S)); |
| clang_disposeString(S); |
| } |
| break; |
| case CXTemplateArgumentKind_Integral: |
| printf(" [Template arg %d: kind: %d, intval: %lld]", |
| I, TAK, clang_Cursor_getTemplateArgumentValue(Cursor, I)); |
| break; |
| default: |
| printf(" [Template arg %d: kind: %d]\n", I, TAK); |
| } |
| } |
| } |
| } |
| |
| clang_getOverriddenCursors(Cursor, &overridden, &num_overridden); |
| if (num_overridden) { |
| unsigned I; |
| LineCol lineCols[50]; |
| assert(num_overridden <= 50); |
| printf(" [Overrides "); |
| for (I = 0; I != num_overridden; ++I) { |
| CXSourceLocation Loc = clang_getCursorLocation(overridden[I]); |
| clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
| lineCols[I].line = line; |
| lineCols[I].col = column; |
| } |
| /* Make the order of the override list deterministic. */ |
| qsort(lineCols, num_overridden, sizeof(LineCol), lineCol_cmp); |
| for (I = 0; I != num_overridden; ++I) { |
| if (I) |
| printf(", "); |
| printf("@%d:%d", lineCols[I].line, lineCols[I].col); |
| } |
| printf("]"); |
| clang_disposeOverriddenCursors(overridden); |
| } |
| |
| if (Cursor.kind == CXCursor_InclusionDirective) { |
| CXFile File = clang_getIncludedFile(Cursor); |
| CXString Included = clang_getFileName(File); |
| printf(" (%s)", clang_getCString(Included)); |
| clang_disposeString(Included); |
| |
| if (clang_isFileMultipleIncludeGuarded(TU, File)) |
| printf(" [multi-include guarded]"); |
| } |
| |
| CursorExtent = clang_getCursorExtent(Cursor); |
| RefNameRange = clang_getCursorReferenceNameRange(Cursor, |
| CXNameRange_WantQualifier |
| | CXNameRange_WantSinglePiece |
| | CXNameRange_WantTemplateArgs, |
| 0); |
| if (!clang_equalRanges(CursorExtent, RefNameRange)) |
| PrintRange(RefNameRange, "SingleRefName"); |
| |
| for (RefNameRangeNr = 0; 1; RefNameRangeNr++) { |
| RefNameRange = clang_getCursorReferenceNameRange(Cursor, |
| CXNameRange_WantQualifier |
| | CXNameRange_WantTemplateArgs, |
| RefNameRangeNr); |
| if (clang_equalRanges(clang_getNullRange(), RefNameRange)) |
| break; |
| if (!clang_equalRanges(CursorExtent, RefNameRange)) |
| PrintRange(RefNameRange, "RefName"); |
| } |
| |
| PrintCursorComments(Cursor, CommentSchemaFile); |
| |
| { |
| unsigned PropAttrs = clang_Cursor_getObjCPropertyAttributes(Cursor, 0); |
| if (PropAttrs != CXObjCPropertyAttr_noattr) { |
| printf(" ["); |
| #define PRINT_PROP_ATTR(A) \ |
| if (PropAttrs & CXObjCPropertyAttr_##A) printf(#A ",") |
| PRINT_PROP_ATTR(readonly); |
| PRINT_PROP_ATTR(getter); |
| PRINT_PROP_ATTR(assign); |
| PRINT_PROP_ATTR(readwrite); |
| PRINT_PROP_ATTR(retain); |
| PRINT_PROP_ATTR(copy); |
| PRINT_PROP_ATTR(nonatomic); |
| PRINT_PROP_ATTR(setter); |
| PRINT_PROP_ATTR(atomic); |
| PRINT_PROP_ATTR(weak); |
| PRINT_PROP_ATTR(strong); |
| PRINT_PROP_ATTR(unsafe_unretained); |
| PRINT_PROP_ATTR(class); |
| printf("]"); |
| } |
| } |
| |
| { |
| unsigned QT = clang_Cursor_getObjCDeclQualifiers(Cursor); |
| if (QT != CXObjCDeclQualifier_None) { |
| printf(" ["); |
| #define PRINT_OBJC_QUAL(A) \ |
| if (QT & CXObjCDeclQualifier_##A) printf(#A ",") |
| PRINT_OBJC_QUAL(In); |
| PRINT_OBJC_QUAL(Inout); |
| PRINT_OBJC_QUAL(Out); |
| PRINT_OBJC_QUAL(Bycopy); |
| PRINT_OBJC_QUAL(Byref); |
| PRINT_OBJC_QUAL(Oneway); |
| printf("]"); |
| } |
| } |
| } |
| } |
| |
| static const char* GetCursorSource(CXCursor Cursor) { |
| CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
| CXString source; |
| CXFile file; |
| clang_getExpansionLocation(Loc, &file, 0, 0, 0); |
| source = clang_getFileName(file); |
| if (!clang_getCString(source)) { |
| clang_disposeString(source); |
| return "<invalid loc>"; |
| } |
| else { |
| const char *b = basename(clang_getCString(source)); |
| clang_disposeString(source); |
| return b; |
| } |
| } |
| |
| /******************************************************************************/ |
| /* Callbacks. */ |
| /******************************************************************************/ |
| |
| typedef void (*PostVisitTU)(CXTranslationUnit); |
| |
| void PrintDiagnostic(CXDiagnostic Diagnostic) { |
| FILE *out = stderr; |
| CXFile file; |
| CXString Msg; |
| unsigned display_opts = CXDiagnostic_DisplaySourceLocation |
| | CXDiagnostic_DisplayColumn | CXDiagnostic_DisplaySourceRanges |
| | CXDiagnostic_DisplayOption; |
| unsigned i, num_fixits; |
| |
| if (clang_getDiagnosticSeverity(Diagnostic) == CXDiagnostic_Ignored) |
| return; |
| |
| Msg = clang_formatDiagnostic(Diagnostic, display_opts); |
| fprintf(stderr, "%s\n", clang_getCString(Msg)); |
| clang_disposeString(Msg); |
| |
| clang_getSpellingLocation(clang_getDiagnosticLocation(Diagnostic), |
| &file, 0, 0, 0); |
| if (!file) |
| return; |
| |
| num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); |
| fprintf(stderr, "Number FIX-ITs = %d\n", num_fixits); |
| for (i = 0; i != num_fixits; ++i) { |
| CXSourceRange range; |
| CXString insertion_text = clang_getDiagnosticFixIt(Diagnostic, i, &range); |
| CXSourceLocation start = clang_getRangeStart(range); |
| CXSourceLocation end = clang_getRangeEnd(range); |
| unsigned start_line, start_column, end_line, end_column; |
| CXFile start_file, end_file; |
| clang_getSpellingLocation(start, &start_file, &start_line, |
| &start_column, 0); |
| clang_getSpellingLocation(end, &end_file, &end_line, &end_column, 0); |
| if (clang_equalLocations(start, end)) { |
| /* Insertion. */ |
| if (start_file == file) |
| fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", |
| clang_getCString(insertion_text), start_line, start_column); |
| } else if (strcmp(clang_getCString(insertion_text), "") == 0) { |
| /* Removal. */ |
| if (start_file == file && end_file == file) { |
| fprintf(out, "FIX-IT: Remove "); |
| PrintExtent(out, start_line, start_column, end_line, end_column); |
| fprintf(out, "\n"); |
| } |
| } else { |
| /* Replacement. */ |
| if (start_file == end_file) { |
| fprintf(out, "FIX-IT: Replace "); |
| PrintExtent(out, start_line, start_column, end_line, end_column); |
| fprintf(out, " with \"%s\"\n", clang_getCString(insertion_text)); |
| } |
| } |
| clang_disposeString(insertion_text); |
| } |
| } |
| |
| void PrintDiagnosticSet(CXDiagnosticSet Set) { |
| int i = 0, n = clang_getNumDiagnosticsInSet(Set); |
| for ( ; i != n ; ++i) { |
| CXDiagnostic Diag = clang_getDiagnosticInSet(Set, i); |
| CXDiagnosticSet ChildDiags = clang_getChildDiagnostics(Diag); |
| PrintDiagnostic(Diag); |
| if (ChildDiags) |
| PrintDiagnosticSet(ChildDiags); |
| } |
| } |
| |
| void PrintDiagnostics(CXTranslationUnit TU) { |
| CXDiagnosticSet TUSet = clang_getDiagnosticSetFromTU(TU); |
| PrintDiagnosticSet(TUSet); |
| clang_disposeDiagnosticSet(TUSet); |
| } |
| |
| void PrintMemoryUsage(CXTranslationUnit TU) { |
| unsigned long total = 0; |
| unsigned i = 0; |
| CXTUResourceUsage usage = clang_getCXTUResourceUsage(TU); |
| fprintf(stderr, "Memory usage:\n"); |
| for (i = 0 ; i != usage.numEntries; ++i) { |
| const char *name = clang_getTUResourceUsageName(usage.entries[i].kind); |
| unsigned long amount = usage.entries[i].amount; |
| total += amount; |
| fprintf(stderr, " %s : %ld bytes (%f MBytes)\n", name, amount, |
| ((double) amount)/(1024*1024)); |
| } |
| fprintf(stderr, " TOTAL = %ld bytes (%f MBytes)\n", total, |
| ((double) total)/(1024*1024)); |
| clang_disposeCXTUResourceUsage(usage); |
| } |
| |
| /******************************************************************************/ |
| /* Logic for testing traversal. */ |
| /******************************************************************************/ |
| |
| static void PrintCursorExtent(CXCursor C) { |
| CXSourceRange extent = clang_getCursorExtent(C); |
| PrintRange(extent, "Extent"); |
| } |
| |
| /* Data used by the visitors. */ |
| typedef struct { |
| CXTranslationUnit TU; |
| enum CXCursorKind *Filter; |
| const char *CommentSchemaFile; |
| } VisitorData; |
| |
| |
| enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, |
| CXCursor Parent, |
| CXClientData ClientData) { |
| VisitorData *Data = (VisitorData *)ClientData; |
| if (!Data->Filter || (Cursor.kind == *(enum CXCursorKind *)Data->Filter)) { |
| CXSourceLocation Loc = clang_getCursorLocation(Cursor); |
| unsigned line, column; |
| clang_getSpellingLocation(Loc, 0, &line, &column, 0); |
| printf("// %s: %s:%d:%d: ", FileCheckPrefix, |
| GetCursorSource(Cursor), line, column); |
| PrintCursor(Cursor, Data->CommentSchemaFile); |
| PrintCursorExtent(Cursor); |
| if (clang_isDeclaration(Cursor.kind)) { |
| enum CX_CXXAccessSpecifier access = clang_getCXXAccessSpecifier(Cursor); |
| const char *accessStr = 0; |
| |
| switch (access) { |
| case CX_CXXInvalidAccessSpecifier: break; |
| case CX_CXXPublic: |
| accessStr = "public"; break; |
| case CX_CXXProtected: |
| accessStr = "protected"; break; |
| case CX_CXXPrivate: |
| accessStr = "private"; break; |
| } |
| |
| if (accessStr) |
| printf(" [access=%s]", accessStr); |
| } |
| printf("\n"); |
| return CXChildVisit_Recurse; |
| } |
| |
| return CXChildVisit_Continue; |
| } |
| |
| static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, |
| CXCursor Parent, |
| CXClientData ClientData) { |
| const char *startBuf, *endBuf; |
| unsigned startLine, startColumn, endLine, endColumn, curLine, curColumn; |
| CXCursor Ref; |
| VisitorData *Data = (VisitorData *)ClientData; |
| |
| if (Cursor.kind != CXCursor_FunctionDecl || |
| !clang_isCursorDefinition(Cursor)) |
| return CXChildVisit_Continue; |
| |
| clang_getDefinitionSpellingAndExtent(Cursor, &startBuf, &endBuf, |
| &startLine, &startColumn, |
| &endLine, &endColumn); |
| /* Probe the entire body, looking for both decls and refs. */ |
| curLine = startLine; |
| curColumn = startColumn; |
| |
| while (startBuf < endBuf) { |
| CXSourceLocation Loc; |
| CXFile file; |
| CXString source; |
| |
| if (*startBuf == '\n') { |
| startBuf++; |
| curLine++; |
| curColumn = 1; |
| } else if (*startBuf != '\t') |
| curColumn++; |
| |
| Loc = clang_getCursorLocation(Cursor); |
| clang_getSpellingLocation(Loc, &file, 0, 0, 0); |
| |
| source = clang_getFileName(file); |
| if (clang_getCString(source)) { |
| CXSourceLocation RefLoc |
| = clang_getLocation(Data->TU, file, curLine, curColumn); |
| Ref = clang_getCursor(Data->TU, RefLoc); |
| if (Ref.kind == CXCursor_NoDeclFound) { |
| /* Nothing found here; that's fine. */ |
| } else if (Ref.kind != CXCursor_FunctionDecl) { |
| printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), |
| curLine, curColumn); |
| PrintCursor(Ref, Data->CommentSchemaFile); |
| printf("\n"); |
| } |
| } |
| clang_disposeString(source); |
| startBuf++; |
| } |
| |
| return CXChildVisit_Continue; |
| } |
| |
| /******************************************************************************/ |
| /* USR testing. */ |
| /******************************************************************************/ |
| |
| enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, |
| CXClientData ClientData) { |
| VisitorData *Data = (VisitorData *)ClientData; |
| if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { |
| CXString USR = clang_getCursorUSR(C); |
| const char *cstr = clang_getCString(USR); |
| if (!cstr || cstr[0] == '\0') { |
| clang_disposeString(USR); |
| return CXChildVisit_Recurse; |
| } |
| printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), cstr); |
| |
| PrintCursorExtent(C); |
| printf("\n"); |
| clang_disposeString(USR); |
| |
| return CXChildVisit_Recurse; |
| } |
| |
| return CXChildVisit_Continue; |
| } |
| |
| /******************************************************************************/ |
| /* Inclusion stack testing. */ |
| /******************************************************************************/ |
| |
| void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, |
| unsigned includeStackLen, CXClientData data) { |
| |
| unsigned i; |
| CXString fname; |
| |
| fname = clang_getFileName(includedFile); |
| printf("file: %s\nincluded by:\n", clang_getCString(fname)); |
| clang_disposeString(fname); |
| |
| for (i = 0; i < includeStackLen; ++i) { |
| CXFile includingFile; |
| unsigned line, column; |
| clang_getSpellingLocation(includeStack[i], &includingFile, &line, |
| &column, 0); |
| fname = clang_getFileName(includingFile); |
| printf(" %s:%d:%d\n", clang_getCString(fname), line, column); |
| clang_disposeString(fname); |
| } |
| printf("\n"); |
| } |
| |
| void PrintInclusionStack(CXTranslationUnit TU) { |
| clang_getInclusions(TU, InclusionVisitor, NULL); |
| } |
| |
| /******************************************************************************/ |
| /* Linkage testing. */ |
| /******************************************************************************/ |
| |
| static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| const char *linkage = 0; |
| |
| if (clang_isInvalid(clang_getCursorKind(cursor))) |
| return CXChildVisit_Recurse; |
| |
| switch (clang_getCursorLinkage(cursor)) { |
| case CXLinkage_Invalid: break; |
| case CXLinkage_NoLinkage: linkage = "NoLinkage"; break; |
| case CXLinkage_Internal: linkage = "Internal"; break; |
| case CXLinkage_UniqueExternal: linkage = "UniqueExternal"; break; |
| case CXLinkage_External: linkage = "External"; break; |
| } |
| |
| if (linkage) { |
| PrintCursor(cursor, NULL); |
| printf("linkage=%s\n", linkage); |
| } |
| |
| return CXChildVisit_Recurse; |
| } |
| |
| /******************************************************************************/ |
| /* Visibility testing. */ |
| /******************************************************************************/ |
| |
| static enum CXChildVisitResult PrintVisibility(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| const char *visibility = 0; |
| |
| if (clang_isInvalid(clang_getCursorKind(cursor))) |
| return CXChildVisit_Recurse; |
| |
| switch (clang_getCursorVisibility(cursor)) { |
| case CXVisibility_Invalid: break; |
| case CXVisibility_Hidden: visibility = "Hidden"; break; |
| case CXVisibility_Protected: visibility = "Protected"; break; |
| case CXVisibility_Default: visibility = "Default"; break; |
| } |
| |
| if (visibility) { |
| PrintCursor(cursor, NULL); |
| printf("visibility=%s\n", visibility); |
| } |
| |
| return CXChildVisit_Recurse; |
| } |
| |
| /******************************************************************************/ |
| /* Typekind testing. */ |
| /******************************************************************************/ |
| |
| static void PrintTypeAndTypeKind(CXType T, const char *Format) { |
| CXString TypeSpelling, TypeKindSpelling; |
| |
| TypeSpelling = clang_getTypeSpelling(T); |
| TypeKindSpelling = clang_getTypeKindSpelling(T.kind); |
| printf(Format, |
| clang_getCString(TypeSpelling), |
| clang_getCString(TypeKindSpelling)); |
| clang_disposeString(TypeSpelling); |
| clang_disposeString(TypeKindSpelling); |
| } |
| |
| static enum CXVisitorResult FieldVisitor(CXCursor C, |
| CXClientData client_data) { |
| (*(int *) client_data)+=1; |
| return CXVisit_Continue; |
| } |
| |
| static void PrintTypeTemplateArgs(CXType T, const char *Format) { |
| int NumTArgs = clang_Type_getNumTemplateArguments(T); |
| if (NumTArgs != -1 && NumTArgs != 0) { |
| int i; |
| CXType TArg; |
| printf(Format, NumTArgs); |
| for (i = 0; i < NumTArgs; ++i) { |
| TArg = clang_Type_getTemplateArgumentAsType(T, i); |
| if (TArg.kind != CXType_Invalid) { |
| PrintTypeAndTypeKind(TArg, " [type=%s] [typekind=%s]"); |
| } |
| } |
| /* Ensure that the returned type is invalid when indexing off-by-one. */ |
| TArg = clang_Type_getTemplateArgumentAsType(T, i); |
| assert(TArg.kind == CXType_Invalid); |
| printf("]"); |
| } |
| } |
| |
| static enum CXChildVisitResult PrintType(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| if (!clang_isInvalid(clang_getCursorKind(cursor))) { |
| CXType T = clang_getCursorType(cursor); |
| enum CXRefQualifierKind RQ = clang_Type_getCXXRefQualifier(T); |
| PrintCursor(cursor, NULL); |
| PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]"); |
| if (clang_isConstQualifiedType(T)) |
| printf(" const"); |
| if (clang_isVolatileQualifiedType(T)) |
| printf(" volatile"); |
| if (clang_isRestrictQualifiedType(T)) |
| printf(" restrict"); |
| if (RQ == CXRefQualifier_LValue) |
| printf(" lvalue-ref-qualifier"); |
| if (RQ == CXRefQualifier_RValue) |
| printf(" rvalue-ref-qualifier"); |
| /* Print the template argument types if they exist. */ |
| PrintTypeTemplateArgs(T, " [templateargs/%d="); |
| /* Print the canonical type if it is different. */ |
| { |
| CXType CT = clang_getCanonicalType(T); |
| if (!clang_equalTypes(T, CT)) { |
| PrintTypeAndTypeKind(CT, " [canonicaltype=%s] [canonicaltypekind=%s]"); |
| PrintTypeTemplateArgs(CT, " [canonicaltemplateargs/%d="); |
| } |
| } |
| /* Print the return type if it exists. */ |
| { |
| CXType RT = clang_getCursorResultType(cursor); |
| if (RT.kind != CXType_Invalid) { |
| PrintTypeAndTypeKind(RT, " [resulttype=%s] [resulttypekind=%s]"); |
| } |
| } |
| /* Print the argument types if they exist. */ |
| { |
| int NumArgs = clang_Cursor_getNumArguments(cursor); |
| if (NumArgs != -1 && NumArgs != 0) { |
| int i; |
| printf(" [args="); |
| for (i = 0; i < NumArgs; ++i) { |
| CXType T = clang_getCursorType(clang_Cursor_getArgument(cursor, i)); |
| if (T.kind != CXType_Invalid) { |
| PrintTypeAndTypeKind(T, " [%s] [%s]"); |
| } |
| } |
| printf("]"); |
| } |
| } |
| /* Print if this is a non-POD type. */ |
| printf(" [isPOD=%d]", clang_isPODType(T)); |
| /* Print the pointee type. */ |
| { |
| CXType PT = clang_getPointeeType(T); |
| if (PT.kind != CXType_Invalid) { |
| PrintTypeAndTypeKind(PT, " [pointeetype=%s] [pointeekind=%s]"); |
| } |
| } |
| /* Print the number of fields if they exist. */ |
| { |
| int numFields = 0; |
| if (clang_Type_visitFields(T, FieldVisitor, &numFields)){ |
| if (numFields != 0) { |
| printf(" [nbFields=%d]", numFields); |
| } |
| /* Print if it is an anonymous record. */ |
| { |
| unsigned isAnon = clang_Cursor_isAnonymous(cursor); |
| if (isAnon != 0) { |
| printf(" [isAnon=%d]", isAnon); |
| } |
| } |
| } |
| } |
| |
| printf("\n"); |
| } |
| return CXChildVisit_Recurse; |
| } |
| |
| static enum CXChildVisitResult PrintTypeSize(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| CXType T; |
| enum CXCursorKind K = clang_getCursorKind(cursor); |
| if (clang_isInvalid(K)) |
| return CXChildVisit_Recurse; |
| T = clang_getCursorType(cursor); |
| PrintCursor(cursor, NULL); |
| PrintTypeAndTypeKind(T, " [type=%s] [typekind=%s]"); |
| /* Print the type sizeof if applicable. */ |
| { |
| long long Size = clang_Type_getSizeOf(T); |
| if (Size >= 0 || Size < -1 ) { |
| printf(" [sizeof=%lld]", Size); |
| } |
| } |
| /* Print the type alignof if applicable. */ |
| { |
| long long Align = clang_Type_getAlignOf(T); |
| if (Align >= 0 || Align < -1) { |
| printf(" [alignof=%lld]", Align); |
| } |
| } |
| /* Print the record field offset if applicable. */ |
| { |
| CXString FieldSpelling = clang_getCursorSpelling(cursor); |
| const char *FieldName = clang_getCString(FieldSpelling); |
| /* recurse to get the first parent record that is not anonymous. */ |
| unsigned RecordIsAnonymous = 0; |
| if (clang_getCursorKind(cursor) == CXCursor_FieldDecl) { |
| CXCursor Record; |
| CXCursor Parent = p; |
| do { |
| Record = Parent; |
| Parent = clang_getCursorSemanticParent(Record); |
| RecordIsAnonymous = clang_Cursor_isAnonymous(Record); |
| /* Recurse as long as the parent is a CXType_Record and the Record |
| is anonymous */ |
| } while ( clang_getCursorType(Parent).kind == CXType_Record && |
| RecordIsAnonymous > 0); |
| { |
| long long Offset = clang_Type_getOffsetOf(clang_getCursorType(Record), |
| FieldName); |
| long long Offset2 = clang_Cursor_getOffsetOfField(cursor); |
| if (Offset == Offset2){ |
| printf(" [offsetof=%lld]", Offset); |
| } else { |
| /* Offsets will be different in anonymous records. */ |
| printf(" [offsetof=%lld/%lld]", Offset, Offset2); |
| } |
| } |
| } |
| clang_disposeString(FieldSpelling); |
| } |
| /* Print if its a bitfield */ |
| { |
| int IsBitfield = clang_Cursor_isBitField(cursor); |
| if (IsBitfield) |
| printf(" [BitFieldSize=%d]", clang_getFieldDeclBitWidth(cursor)); |
| } |
| printf("\n"); |
| return CXChildVisit_Recurse; |
| } |
| |
| /******************************************************************************/ |
| /* Mangling testing. */ |
| /******************************************************************************/ |
| |
| static enum CXChildVisitResult PrintMangledName(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| CXString MangledName; |
| if (clang_isUnexposed(clang_getCursorKind(cursor))) |
| return CXChildVisit_Recurse; |
| PrintCursor(cursor, NULL); |
| MangledName = clang_Cursor_getMangling(cursor); |
| printf(" [mangled=%s]\n", clang_getCString(MangledName)); |
| clang_disposeString(MangledName); |
| return CXChildVisit_Continue; |
| } |
| |
| static enum CXChildVisitResult PrintManglings(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| unsigned I, E; |
| CXStringSet *Manglings = NULL; |
| if (clang_isUnexposed(clang_getCursorKind(cursor))) |
| return CXChildVisit_Recurse; |
| if (!clang_isDeclaration(clang_getCursorKind(cursor))) |
| return CXChildVisit_Recurse; |
| if (clang_getCursorKind(cursor) == CXCursor_ParmDecl) |
| return CXChildVisit_Continue; |
| PrintCursor(cursor, NULL); |
| Manglings = clang_Cursor_getCXXManglings(cursor); |
| if (Manglings) { |
| for (I = 0, E = Manglings->Count; I < E; ++I) |
| printf(" [mangled=%s]", clang_getCString(Manglings->Strings[I])); |
| clang_disposeStringSet(Manglings); |
| printf("\n"); |
| } |
| Manglings = clang_Cursor_getObjCManglings(cursor); |
| if (Manglings) { |
| for (I = 0, E = Manglings->Count; I < E; ++I) |
| printf(" [mangled=%s]", clang_getCString(Manglings->Strings[I])); |
| clang_disposeStringSet(Manglings); |
| printf("\n"); |
| } |
| return CXChildVisit_Recurse; |
| } |
| |
| /******************************************************************************/ |
| /* Bitwidth testing. */ |
| /******************************************************************************/ |
| |
| static enum CXChildVisitResult PrintBitWidth(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| int Bitwidth; |
| if (clang_getCursorKind(cursor) != CXCursor_FieldDecl) |
| return CXChildVisit_Recurse; |
| |
| Bitwidth = clang_getFieldDeclBitWidth(cursor); |
| if (Bitwidth >= 0) { |
| PrintCursor(cursor, NULL); |
| printf(" bitwidth=%d\n", Bitwidth); |
| } |
| |
| return CXChildVisit_Recurse; |
| } |
| |
| /******************************************************************************/ |
| /* Type declaration testing */ |
| /******************************************************************************/ |
| |
| static enum CXChildVisitResult PrintTypeDeclaration(CXCursor cursor, CXCursor p, |
| CXClientData d) { |
| CXCursor typeDeclaration = clang_getTypeDeclaration(clang_getCursorType(cursor)); |
| |
| if (clang_isDeclaration(typeDeclaration.kind)) { |
| PrintCursor(cursor, NULL); |
| PrintTypeAndTypeKind(clang_getCursorType(typeDeclaration), " [typedeclaration=%s] [typekind=%s]\n"); |
| } |
| |
| return CXChildVisit_Recurse; |
| } |
| |
| /******************************************************************************/ |
| /* Target information testing. */ |
| /******************************************************************************/ |
| |
| static int print_target_info(int argc, const char **argv) { |
| CXIndex Idx; |
| CXTranslationUnit TU; |
| CXTargetInfo TargetInfo; |
| CXString Triple; |
| const char *FileName; |
| enum CXErrorCode Err; |
| int PointerWidth; |
| |
| if (argc == 0) { |
| fprintf(stderr, "No filename specified\n"); |
| return 1; |
| } |
| |
| FileName = argv[1]; |
| |
| Idx = clang_createIndex(0, 1); |
| Err = clang_parseTranslationUnit2(Idx, FileName, argv, argc, NULL, 0, |
| getDefaultParsingOptions(), &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Couldn't parse translation unit!\n"); |
| describeLibclangFailure(Err); |
| clang_disposeIndex(Idx); |
| return 1; |
| } |
| |
| TargetInfo = clang_getTranslationUnitTargetInfo(TU); |
| |
| Triple = clang_TargetInfo_getTriple(TargetInfo); |
| printf("TargetTriple: %s\n", clang_getCString(Triple)); |
| clang_disposeString(Triple); |
| |
| PointerWidth = clang_TargetInfo_getPointerWidth(TargetInfo); |
| printf("PointerWidth: %d\n", PointerWidth); |
| |
| clang_TargetInfo_dispose(TargetInfo); |
| clang_disposeTranslationUnit(TU); |
| clang_disposeIndex(Idx); |
| return 0; |
| } |
| |
| /******************************************************************************/ |
| /* Loading ASTs/source. */ |
| /******************************************************************************/ |
| |
| static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, |
| const char *filter, const char *prefix, |
| CXCursorVisitor Visitor, |
| PostVisitTU PV, |
| const char *CommentSchemaFile) { |
| |
| if (prefix) |
| FileCheckPrefix = prefix; |
| |
| if (Visitor) { |
| enum CXCursorKind K = CXCursor_NotImplemented; |
| enum CXCursorKind *ck = &K; |
| VisitorData Data; |
| |
| /* Perform some simple filtering. */ |
| if (!strcmp(filter, "all") || !strcmp(filter, "local")) ck = NULL; |
| else if (!strcmp(filter, "all-display") || |
| !strcmp(filter, "local-display")) { |
| ck = NULL; |
| want_display_name = 1; |
| } |
| else if (!strcmp(filter, "none")) K = (enum CXCursorKind) ~0; |
| else if (!strcmp(filter, "category")) K = CXCursor_ObjCCategoryDecl; |
| else if (!strcmp(filter, "interface")) K = CXCursor_ObjCInterfaceDecl; |
| else if (!strcmp(filter, "protocol")) K = CXCursor_ObjCProtocolDecl; |
| else if (!strcmp(filter, "function")) K = CXCursor_FunctionDecl; |
| else if (!strcmp(filter, "typedef")) K = CXCursor_TypedefDecl; |
| else if (!strcmp(filter, "scan-function")) Visitor = FunctionScanVisitor; |
| else { |
| fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); |
| return 1; |
| } |
| |
| Data.TU = TU; |
| Data.Filter = ck; |
| Data.CommentSchemaFile = CommentSchemaFile; |
| clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); |
| } |
| |
| if (PV) |
| PV(TU); |
| |
| PrintDiagnostics(TU); |
| if (checkForErrors(TU) != 0) { |
| clang_disposeTranslationUnit(TU); |
| return -1; |
| } |
| |
| clang_disposeTranslationUnit(TU); |
| return 0; |
| } |
| |
| int perform_test_load_tu(const char *file, const char *filter, |
| const char *prefix, CXCursorVisitor Visitor, |
| PostVisitTU PV) { |
| CXIndex Idx; |
| CXTranslationUnit TU; |
| int result; |
| Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| !strcmp(filter, "local") ? 1 : 0, |
| /* displayDiagnostics=*/1); |
| |
| if (!CreateTranslationUnit(Idx, file, &TU)) { |
| clang_disposeIndex(Idx); |
| return 1; |
| } |
| |
| result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL); |
| clang_disposeIndex(Idx); |
| return result; |
| } |
| |
| int perform_test_load_source(int argc, const char **argv, |
| const char *filter, CXCursorVisitor Visitor, |
| PostVisitTU PV) { |
| CXIndex Idx; |
| CXTranslationUnit TU; |
| const char *CommentSchemaFile; |
| struct CXUnsavedFile *unsaved_files = 0; |
| int num_unsaved_files = 0; |
| enum CXErrorCode Err; |
| int result; |
| unsigned Repeats = 0; |
| unsigned I; |
| |
| Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| (!strcmp(filter, "local") || |
| !strcmp(filter, "local-display"))? 1 : 0, |
| /* displayDiagnostics=*/1); |
| |
| if ((CommentSchemaFile = parse_comments_schema(argc, argv))) { |
| argc--; |
| argv++; |
| } |
| |
| if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| clang_disposeIndex(Idx); |
| return -1; |
| } |
| |
| if (getenv("CINDEXTEST_EDITING")) |
| Repeats = 5; |
| |
| Err = clang_parseTranslationUnit2(Idx, 0, |
| argv + num_unsaved_files, |
| argc - num_unsaved_files, |
| unsaved_files, num_unsaved_files, |
| getDefaultParsingOptions(), &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to load translation unit!\n"); |
| describeLibclangFailure(Err); |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| clang_disposeIndex(Idx); |
| return 1; |
| } |
| |
| for (I = 0; I != Repeats; ++I) { |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| if (Repeats > 1) { |
| clang_suspendTranslationUnit(TU); |
| |
| Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| clang_defaultReparseOptions(TU)); |
| if (Err != CXError_Success) { |
| describeLibclangFailure(Err); |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| clang_disposeIndex(Idx); |
| return 1; |
| } |
| } |
| } |
| |
| result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, |
| CommentSchemaFile); |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| clang_disposeIndex(Idx); |
| return result; |
| } |
| |
| int perform_test_reparse_source(int argc, const char **argv, int trials, |
| const char *filter, CXCursorVisitor Visitor, |
| PostVisitTU PV) { |
| CXIndex Idx; |
| CXTranslationUnit TU; |
| struct CXUnsavedFile *unsaved_files = 0; |
| int num_unsaved_files = 0; |
| int compiler_arg_idx = 0; |
| enum CXErrorCode Err; |
| int result, i; |
| int trial; |
| int remap_after_trial = 0; |
| char *endptr = 0; |
| |
| Idx = clang_createIndex(/* excludeDeclsFromPCH */ |
| !strcmp(filter, "local") ? 1 : 0, |
| /* displayDiagnostics=*/1); |
| |
| if (parse_remapped_files(argc, argv, 0, &unsaved_files, &num_unsaved_files)) { |
| clang_disposeIndex(Idx); |
| return -1; |
| } |
| |
| for (i = 0; i < argc; ++i) { |
| if (strcmp(argv[i], "--") == 0) |
| break; |
| } |
| if (i < argc) |
| compiler_arg_idx = i+1; |
| if (num_unsaved_files > compiler_arg_idx) |
| compiler_arg_idx = num_unsaved_files; |
| |
| /* Load the initial translation unit -- we do this without honoring remapped |
| * files, so that we have a way to test results after changing the source. */ |
| Err = clang_parseTranslationUnit2(Idx, 0, |
| argv + compiler_arg_idx, |
| argc - compiler_arg_idx, |
| 0, 0, getDefaultParsingOptions(), &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to load translation unit!\n"); |
| describeLibclangFailure(Err); |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| clang_disposeIndex(Idx); |
| return 1; |
| } |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| if (getenv("CINDEXTEST_REMAP_AFTER_TRIAL")) { |
| remap_after_trial = |
| strtol(getenv("CINDEXTEST_REMAP_AFTER_TRIAL"), &endptr, 10); |
| } |
| |
| for (trial = 0; trial < trials; ++trial) { |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| if (parse_remapped_files_with_try(trial, argc, argv, 0, |
| &unsaved_files, &num_unsaved_files)) { |
| clang_disposeTranslationUnit(TU); |
| clang_disposeIndex(Idx); |
| return -1; |
| } |
| |
| Err = clang_reparseTranslationUnit( |
| TU, |
| trial >= remap_after_trial ? num_unsaved_files : 0, |
| trial >= remap_after_trial ? unsaved_files : 0, |
| clang_defaultReparseOptions(TU)); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to reparse translation unit!\n"); |
| describeLibclangFailure(Err); |
| clang_disposeTranslationUnit(TU); |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| clang_disposeIndex(Idx); |
| return -1; |
| } |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| } |
| |
| result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL); |
| |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| clang_disposeIndex(Idx); |
| return result; |
| } |
| |
| static int perform_single_file_parse(const char *filename) { |
| CXIndex Idx; |
| CXTranslationUnit TU; |
| enum CXErrorCode Err; |
| int result; |
| |
| Idx = clang_createIndex(/* excludeDeclsFromPCH */1, |
| /* displayDiagnostics=*/1); |
| |
| Err = clang_parseTranslationUnit2(Idx, filename, |
| /*command_line_args=*/NULL, |
| /*num_command_line_args=*/0, |
| /*unsaved_files=*/NULL, |
| /*num_unsaved_files=*/0, |
| CXTranslationUnit_SingleFileParse, &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to load translation unit!\n"); |
| describeLibclangFailure(Err); |
| clang_disposeIndex(Idx); |
| return 1; |
| } |
| |
| result = perform_test_load(Idx, TU, /*filter=*/"all", /*prefix=*/NULL, FilteredPrintingVisitor, /*PostVisit=*/NULL, |
| /*CommentSchemaFile=*/NULL); |
| clang_disposeIndex(Idx); |
| return result; |
| } |
| |
| /******************************************************************************/ |
| /* Logic for testing clang_getCursor(). */ |
| /******************************************************************************/ |
| |
| static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor, |
| unsigned start_line, unsigned start_col, |
| unsigned end_line, unsigned end_col, |
| const char *prefix) { |
| printf("// %s: ", FileCheckPrefix); |
| if (prefix) |
| printf("-%s", prefix); |
| PrintExtent(stdout, start_line, start_col, end_line, end_col); |
| printf(" "); |
| PrintCursor(cursor, NULL); |
| printf("\n"); |
| } |
| |
| static int perform_file_scan(const char *ast_file, const char *source_file, |
| const char *prefix) { |
| CXIndex Idx; |
| CXTranslationUnit TU; |
| FILE *fp; |
| CXCursor prevCursor = clang_getNullCursor(); |
| CXFile file; |
| unsigned line = 1, col = 1; |
| unsigned start_line = 1, start_col = 1; |
| |
| if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 1, |
| /* displayDiagnostics=*/1))) { |
| fprintf(stderr, "Could not create Index\n"); |
| return 1; |
| } |
| |
| if (!CreateTranslationUnit(Idx, ast_file, &TU)) |
| return 1; |
| |
| if ((fp = fopen(source_file, "r")) == NULL) { |
| fprintf(stderr, "Could not open '%s'\n", source_file); |
| clang_disposeTranslationUnit(TU); |
| return 1; |
| } |
| |
| file = clang_getFile(TU, source_file); |
| for (;;) { |
| CXCursor cursor; |
| int c = fgetc(fp); |
| |
| if (c == '\n') { |
| ++line; |
| col = 1; |
| } else |
| ++col; |
| |
| /* Check the cursor at this position, and dump the previous one if we have |
| * found something new. |
| */ |
| cursor = clang_getCursor(TU, clang_getLocation(TU, file, line, col)); |
| if ((c == EOF || !clang_equalCursors(cursor, prevCursor)) && |
| prevCursor.kind != CXCursor_InvalidFile) { |
| print_cursor_file_scan(TU, prevCursor, start_line, start_col, |
| line, col, prefix); |
| start_line = line; |
| start_col = col; |
| } |
| if (c == EOF) |
| break; |
| |
| prevCursor = cursor; |
| } |
| |
| fclose(fp); |
| clang_disposeTranslationUnit(TU); |
| clang_disposeIndex(Idx); |
| return 0; |
| } |
| |
| /******************************************************************************/ |
| /* Logic for testing clang code completion. */ |
| /******************************************************************************/ |
| |
| /* Parse file:line:column from the input string. Returns 0 on success, non-zero |
| on failure. If successful, the pointer *filename will contain newly-allocated |
| memory (that will be owned by the caller) to store the file name. */ |
| int parse_file_line_column(const char *input, char **filename, unsigned *line, |
| unsigned *column, unsigned *second_line, |
| unsigned *second_column) { |
| /* Find the second colon. */ |
| const char *last_colon = strrchr(input, ':'); |
| unsigned values[4], i; |
| unsigned num_values = (second_line && second_column)? 4 : 2; |
| |
| char *endptr = 0; |
| if (!last_colon || last_colon == input) { |
| if (num_values == 4) |
| fprintf(stderr, "could not parse filename:line:column:line:column in " |
| "'%s'\n", input); |
| else |
| fprintf(stderr, "could not parse filename:line:column in '%s'\n", input); |
| return 1; |
| } |
| |
| for (i = 0; i != num_values; ++i) { |
| const char *prev_colon; |
| |
| /* Parse the next line or column. */ |
| values[num_values - i - 1] = strtol(last_colon + 1, &endptr, 10); |
| if (*endptr != 0 && *endptr != ':') { |
| fprintf(stderr, "could not parse %s in '%s'\n", |
| (i % 2 ? "column" : "line"), input); |
| return 1; |
| } |
| |
| if (i + 1 == num_values) |
| break; |
| |
| /* Find the previous colon. */ |
| prev_colon = last_colon - 1; |
| while (prev_colon != input && *prev_colon != ':') |
| --prev_colon; |
| if (prev_colon == input) { |
| fprintf(stderr, "could not parse %s in '%s'\n", |
| (i % 2 == 0? "column" : "line"), input); |
| return 1; |
| } |
| |
| last_colon = prev_colon; |
| } |
| |
| *line = values[0]; |
| *column = values[1]; |
| |
| if (second_line && second_column) { |
| *second_line = values[2]; |
| *second_column = values[3]; |
| } |
| |
| /* Copy the file name. */ |
| *filename = (char*)malloc(last_colon - input + 1); |
| memcpy(*filename, input, last_colon - input); |
| (*filename)[last_colon - input] = 0; |
| return 0; |
| } |
| |
| const char * |
| clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { |
| switch (Kind) { |
| case CXCompletionChunk_Optional: return "Optional"; |
| case CXCompletionChunk_TypedText: return "TypedText"; |
| case CXCompletionChunk_Text: return "Text"; |
| case CXCompletionChunk_Placeholder: return "Placeholder"; |
| case CXCompletionChunk_Informative: return "Informative"; |
| case CXCompletionChunk_CurrentParameter: return "CurrentParameter"; |
| case CXCompletionChunk_LeftParen: return "LeftParen"; |
| case CXCompletionChunk_RightParen: return "RightParen"; |
| case CXCompletionChunk_LeftBracket: return "LeftBracket"; |
| case CXCompletionChunk_RightBracket: return "RightBracket"; |
| case CXCompletionChunk_LeftBrace: return "LeftBrace"; |
| case CXCompletionChunk_RightBrace: return "RightBrace"; |
| case CXCompletionChunk_LeftAngle: return "LeftAngle"; |
| case CXCompletionChunk_RightAngle: return "RightAngle"; |
| case CXCompletionChunk_Comma: return "Comma"; |
| case CXCompletionChunk_ResultType: return "ResultType"; |
| case CXCompletionChunk_Colon: return "Colon"; |
| case CXCompletionChunk_SemiColon: return "SemiColon"; |
| case CXCompletionChunk_Equal: return "Equal"; |
| case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; |
| case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; |
| } |
| |
| return "Unknown"; |
| } |
| |
| static int checkForErrors(CXTranslationUnit TU) { |
| unsigned Num, i; |
| CXDiagnostic Diag; |
| CXString DiagStr; |
| |
| if (!getenv("CINDEXTEST_FAILONERROR")) |
| return 0; |
| |
| Num = clang_getNumDiagnostics(TU); |
| for (i = 0; i != Num; ++i) { |
| Diag = clang_getDiagnostic(TU, i); |
| if (clang_getDiagnosticSeverity(Diag) >= CXDiagnostic_Error) { |
| DiagStr = clang_formatDiagnostic(Diag, |
| clang_defaultDiagnosticDisplayOptions()); |
| fprintf(stderr, "%s\n", clang_getCString(DiagStr)); |
| clang_disposeString(DiagStr); |
| clang_disposeDiagnostic(Diag); |
| return -1; |
| } |
| clang_disposeDiagnostic(Diag); |
| } |
| |
| return 0; |
| } |
| |
| static void print_completion_string(CXCompletionString completion_string, |
| FILE *file) { |
| int I, N; |
| |
| N = clang_getNumCompletionChunks(completion_string); |
| for (I = 0; I != N; ++I) { |
| CXString text; |
| const char *cstr; |
| enum CXCompletionChunkKind Kind |
| = clang_getCompletionChunkKind(completion_string, I); |
| |
| if (Kind == CXCompletionChunk_Optional) { |
| fprintf(file, "{Optional "); |
| print_completion_string( |
| clang_getCompletionChunkCompletionString(completion_string, I), |
| file); |
| fprintf(file, "}"); |
| continue; |
| } |
| |
| if (Kind == CXCompletionChunk_VerticalSpace) { |
| fprintf(file, "{VerticalSpace }"); |
| continue; |
| } |
| |
| text = clang_getCompletionChunkText(completion_string, I); |
| cstr = clang_getCString(text); |
| fprintf(file, "{%s %s}", |
| clang_getCompletionChunkKindSpelling(Kind), |
| cstr ? cstr : ""); |
| clang_disposeString(text); |
| } |
| |
| } |
| |
| static void print_completion_result(CXCompletionResult *completion_result, |
| FILE *file) { |
| CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); |
| unsigned annotationCount; |
| enum CXCursorKind ParentKind; |
| CXString ParentName; |
| CXString BriefComment; |
| CXString Annotation; |
| const char *BriefCommentCString; |
| |
| fprintf(file, "%s:", clang_getCString(ks)); |
| clang_disposeString(ks); |
| |
| print_completion_string(completion_result->CompletionString, file); |
| fprintf(file, " (%u)", |
| clang_getCompletionPriority(completion_result->CompletionString)); |
| switch (clang_getCompletionAvailability(completion_result->CompletionString)){ |
| case CXAvailability_Available: |
| break; |
| |
| case CXAvailability_Deprecated: |
| fprintf(file, " (deprecated)"); |
| break; |
| |
| case CXAvailability_NotAvailable: |
| fprintf(file, " (unavailable)"); |
| break; |
| |
| case CXAvailability_NotAccessible: |
| fprintf(file, " (inaccessible)"); |
| break; |
| } |
| |
| annotationCount = clang_getCompletionNumAnnotations( |
| completion_result->CompletionString); |
| if (annotationCount) { |
| unsigned i; |
| fprintf(file, " ("); |
| for (i = 0; i < annotationCount; ++i) { |
| if (i != 0) |
| fprintf(file, ", "); |
| Annotation = |
| clang_getCompletionAnnotation(completion_result->CompletionString, i); |
| fprintf(file, "\"%s\"", clang_getCString(Annotation)); |
| clang_disposeString(Annotation); |
| } |
| fprintf(file, ")"); |
| } |
| |
| if (!getenv("CINDEXTEST_NO_COMPLETION_PARENTS")) { |
| ParentName = clang_getCompletionParent(completion_result->CompletionString, |
| &ParentKind); |
| if (ParentKind != CXCursor_NotImplemented) { |
| CXString KindSpelling = clang_getCursorKindSpelling(ParentKind); |
| fprintf(file, " (parent: %s '%s')", |
| clang_getCString(KindSpelling), |
| clang_getCString(ParentName)); |
| clang_disposeString(KindSpelling); |
| } |
| clang_disposeString(ParentName); |
| } |
| |
| BriefComment = clang_getCompletionBriefComment( |
| completion_result->CompletionString); |
| BriefCommentCString = clang_getCString(BriefComment); |
| if (BriefCommentCString && *BriefCommentCString != '\0') { |
| fprintf(file, "(brief comment: %s)", BriefCommentCString); |
| } |
| clang_disposeString(BriefComment); |
| |
| fprintf(file, "\n"); |
| } |
| |
| void print_completion_contexts(unsigned long long contexts, FILE *file) { |
| fprintf(file, "Completion contexts:\n"); |
| if (contexts == CXCompletionContext_Unknown) { |
| fprintf(file, "Unknown\n"); |
| } |
| if (contexts & CXCompletionContext_AnyType) { |
| fprintf(file, "Any type\n"); |
| } |
| if (contexts & CXCompletionContext_AnyValue) { |
| fprintf(file, "Any value\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCObjectValue) { |
| fprintf(file, "Objective-C object value\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCSelectorValue) { |
| fprintf(file, "Objective-C selector value\n"); |
| } |
| if (contexts & CXCompletionContext_CXXClassTypeValue) { |
| fprintf(file, "C++ class type value\n"); |
| } |
| if (contexts & CXCompletionContext_DotMemberAccess) { |
| fprintf(file, "Dot member access\n"); |
| } |
| if (contexts & CXCompletionContext_ArrowMemberAccess) { |
| fprintf(file, "Arrow member access\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCPropertyAccess) { |
| fprintf(file, "Objective-C property access\n"); |
| } |
| if (contexts & CXCompletionContext_EnumTag) { |
| fprintf(file, "Enum tag\n"); |
| } |
| if (contexts & CXCompletionContext_UnionTag) { |
| fprintf(file, "Union tag\n"); |
| } |
| if (contexts & CXCompletionContext_StructTag) { |
| fprintf(file, "Struct tag\n"); |
| } |
| if (contexts & CXCompletionContext_ClassTag) { |
| fprintf(file, "Class name\n"); |
| } |
| if (contexts & CXCompletionContext_Namespace) { |
| fprintf(file, "Namespace or namespace alias\n"); |
| } |
| if (contexts & CXCompletionContext_NestedNameSpecifier) { |
| fprintf(file, "Nested name specifier\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCInterface) { |
| fprintf(file, "Objective-C interface\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCProtocol) { |
| fprintf(file, "Objective-C protocol\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCCategory) { |
| fprintf(file, "Objective-C category\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCInstanceMessage) { |
| fprintf(file, "Objective-C instance method\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCClassMessage) { |
| fprintf(file, "Objective-C class method\n"); |
| } |
| if (contexts & CXCompletionContext_ObjCSelectorName) { |
| fprintf(file, "Objective-C selector name\n"); |
| } |
| if (contexts & CXCompletionContext_MacroName) { |
| fprintf(file, "Macro name\n"); |
| } |
| if (contexts & CXCompletionContext_NaturalLanguage) { |
| fprintf(file, "Natural language\n"); |
| } |
| } |
| |
| int perform_code_completion(int argc, const char **argv, int timing_only) { |
| const char *input = argv[1]; |
| char *filename = 0; |
| unsigned line; |
| unsigned column; |
| CXIndex CIdx; |
| int errorCode; |
| struct CXUnsavedFile *unsaved_files = 0; |
| int num_unsaved_files = 0; |
| CXCodeCompleteResults *results = 0; |
| enum CXErrorCode Err; |
| CXTranslationUnit TU; |
| unsigned I, Repeats = 1; |
| unsigned completionOptions = clang_defaultCodeCompleteOptions(); |
| |
| if (getenv("CINDEXTEST_CODE_COMPLETE_PATTERNS")) |
| completionOptions |= CXCodeComplete_IncludeCodePatterns; |
| if (getenv("CINDEXTEST_COMPLETION_BRIEF_COMMENTS")) |
| completionOptions |= CXCodeComplete_IncludeBriefComments; |
| |
| if (timing_only) |
| input += strlen("-code-completion-timing="); |
| else |
| input += strlen("-code-completion-at="); |
| |
| if ((errorCode = parse_file_line_column(input, &filename, &line, &column, |
| 0, 0))) |
| return errorCode; |
| |
| if (parse_remapped_files(argc, argv, 2, &unsaved_files, &num_unsaved_files)) |
| return -1; |
| |
| CIdx = clang_createIndex(0, 0); |
| |
| if (getenv("CINDEXTEST_EDITING")) |
| Repeats = 5; |
| |
| Err = clang_parseTranslationUnit2(CIdx, 0, |
| argv + num_unsaved_files + 2, |
| argc - num_unsaved_files - 2, |
| 0, 0, getDefaultParsingOptions(), &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to load translation unit!\n"); |
| describeLibclangFailure(Err); |
| return 1; |
| } |
| |
| Err = clang_reparseTranslationUnit(TU, 0, 0, |
| clang_defaultReparseOptions(TU)); |
| |
| if (Err != CXError_Success) { |
| fprintf(stderr, "Unable to reparse translation unit!\n"); |
| describeLibclangFailure(Err); |
| clang_disposeTranslationUnit(TU); |
| return 1; |
| } |
| |
| for (I = 0; I != Repeats; ++I) { |
| results = clang_codeCompleteAt(TU, filename, line, column, |
| unsaved_files, num_unsaved_files, |
| completionOptions); |
| if (!results) { |
| fprintf(stderr, "Unable to perform code completion!\n"); |
| return 1; |
| } |
| if (I != Repeats-1) |
| clang_disposeCodeCompleteResults(results); |
| } |
| |
| if (results) { |
| unsigned i, n = results->NumResults, containerIsIncomplete = 0; |
| unsigned long long contexts; |
| enum CXCursorKind containerKind; |
| CXString objCSelector; |
| const char *selectorString; |
| if (!timing_only) { |
| /* Sort the code-completion results based on the typed text. */ |
| clang_sortCodeCompletionResults(results->Results, results->NumResults); |
| |
| for (i = 0; i != n; ++i) |
| print_completion_result(results->Results + i, stdout); |
| } |
| n = clang_codeCompleteGetNumDiagnostics(results); |
| for (i = 0; i != n; ++i) { |
| CXDiagnostic diag = clang_codeCompleteGetDiagnostic(results, i); |
| PrintDiagnostic(diag); |
| clang_disposeDiagnostic(diag); |
| } |
| |
| contexts = clang_codeCompleteGetContexts(results); |
| print_completion_contexts(contexts, stdout); |
| |
| containerKind = clang_codeCompleteGetContainerKind(results, |
| &containerIsIncomplete); |
| |
| if (containerKind != CXCursor_InvalidCode) { |
| /* We have found a container */ |
| CXString containerUSR, containerKindSpelling; |
| containerKindSpelling = clang_getCursorKindSpelling(containerKind); |
| printf("Container Kind: %s\n", clang_getCString(containerKindSpelling)); |
| clang_disposeString(containerKindSpelling); |
| |
| if (containerIsIncomplete) { |
| printf("Container is incomplete\n"); |
| } |
| else { |
| printf("Container is complete\n"); |
| } |
| |
| containerUSR = clang_codeCompleteGetContainerUSR(results); |
| printf("Container USR: %s\n", clang_getCString(containerUSR)); |
| clang_disposeString(containerUSR); |
| } |
| |
| objCSelector = clang_codeCompleteGetObjCSelector(results); |
| selectorString = clang_getCString(objCSelector); |
| if (selectorString && strlen(selectorString) > 0) { |
| printf("Objective-C selector: %s\n", selectorString); |
| } |
| clang_disposeString(objCSelector); |
| |
| clang_disposeCodeCompleteResults(results); |
| } |
| clang_disposeTranslationUnit(TU); |
| clang_disposeIndex(CIdx); |
| free(filename); |
| |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| |
| return 0; |
| } |
| |
| typedef struct { |
| char *filename; |
| unsigned line; |
| unsigned column; |
| } CursorSourceLocation; |
| |
| typedef void (*cursor_handler_t)(CXCursor cursor); |
| |
| static int inspect_cursor_at(int argc, const char **argv, |
| const char *locations_flag, |
| cursor_handler_t handler) { |
| CXIndex CIdx; |
| int errorCode; |
| struct CXUnsavedFile *unsaved_files = 0; |
| int num_unsaved_files = 0; |
| enum CXErrorCode Err; |
| CXTranslationUnit TU; |
| CXCursor Cursor; |
| CursorSourceLocation *Locations = 0; |
| unsigned NumLocations = 0, Loc; |
| unsigned Repeats = 1; |
| unsigned I; |
| |
| /* Count the number of locations. */ |
| while (strstr(argv[NumLocations+1], locations_flag) == argv[NumLocations+1]) |
| ++NumLocations; |
| |
| /* Parse the locations. */ |
| assert(NumLocations > 0 && "Unable to count locations?"); |
| Locations = (CursorSourceLocation *)malloc( |
| NumLocations * sizeof(CursorSourceLocation)); |
| for (Loc = 0; Loc < NumLocations; ++Loc) { |
| const char *input = argv[Loc + 1] + strlen(locations_flag); |
| if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| &Locations[Loc].line, |
| &Locations[Loc].column, 0, 0))) |
| return errorCode; |
| } |
| |
| if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
| &num_unsaved_files)) |
| return -1; |
| |
| if (getenv("CINDEXTEST_EDITING")) |
| Repeats = 5; |
| |
| /* Parse the translation unit. When we're testing clang_getCursor() after |
| reparsing, don't remap unsaved files until the second parse. */ |
| CIdx = clang_createIndex(1, 1); |
| Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| argv + num_unsaved_files + 1 + NumLocations, |
| argc - num_unsaved_files - 2 - NumLocations, |
| unsaved_files, |
| Repeats > 1? 0 : num_unsaved_files, |
| getDefaultParsingOptions(), &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "unable to parse input\n"); |
| describeLibclangFailure(Err); |
| return -1; |
| } |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| for (I = 0; I != Repeats; ++I) { |
| if (Repeats > 1) { |
| Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| clang_defaultReparseOptions(TU)); |
| if (Err != CXError_Success) { |
| describeLibclangFailure(Err); |
| clang_disposeTranslationUnit(TU); |
| return 1; |
| } |
| } |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| for (Loc = 0; Loc < NumLocations; ++Loc) { |
| CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| if (!file) |
| continue; |
| |
| Cursor = clang_getCursor(TU, |
| clang_getLocation(TU, file, Locations[Loc].line, |
| Locations[Loc].column)); |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| if (I + 1 == Repeats) { |
| handler(Cursor); |
| free(Locations[Loc].filename); |
| } |
| } |
| } |
| |
| PrintDiagnostics(TU); |
| clang_disposeTranslationUnit(TU); |
| clang_disposeIndex(CIdx); |
| free(Locations); |
| free_remapped_files(unsaved_files, num_unsaved_files); |
| return 0; |
| } |
| |
| static void inspect_print_cursor(CXCursor Cursor) { |
| CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); |
| CXCompletionString completionString = clang_getCursorCompletionString( |
| Cursor); |
| CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); |
| CXString Spelling; |
| const char *cspell; |
| unsigned line, column; |
| clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); |
| printf("%d:%d ", line, column); |
| PrintCursor(Cursor, NULL); |
| PrintCursorExtent(Cursor); |
| Spelling = clang_getCursorSpelling(Cursor); |
| cspell = clang_getCString(Spelling); |
| if (cspell && strlen(cspell) != 0) { |
| unsigned pieceIndex; |
| printf(" Spelling=%s (", cspell); |
| for (pieceIndex = 0; ; ++pieceIndex) { |
| CXSourceRange range = |
| clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); |
| if (clang_Range_isNull(range)) |
| break; |
| PrintRange(range, 0); |
| } |
| printf(")"); |
| } |
| clang_disposeString(Spelling); |
| if (clang_Cursor_getObjCSelectorIndex(Cursor) != -1) |
| printf(" Selector index=%d", |
| clang_Cursor_getObjCSelectorIndex(Cursor)); |
| if (clang_Cursor_isDynamicCall(Cursor)) |
| printf(" Dynamic-call"); |
| if (Cursor.kind == CXCursor_ObjCMessageExpr || |
| Cursor.kind == CXCursor_MemberRefExpr) { |
| CXType T = clang_Cursor_getReceiverType(Cursor); |
| if (T.kind != CXType_Invalid) { |
| CXString S = clang_getTypeKindSpelling(T.kind); |
| printf(" Receiver-type=%s", clang_getCString(S)); |
| clang_disposeString(S); |
| } |
| } |
| |
| { |
| CXModule mod = clang_Cursor_getModule(Cursor); |
| CXFile astFile; |
| CXString name, astFilename; |
| unsigned i, numHeaders; |
| if (mod) { |
| astFile = clang_Module_getASTFile(mod); |
| astFilename = clang_getFileName(astFile); |
| name = clang_Module_getFullName(mod); |
| numHeaders = clang_Module_getNumTopLevelHeaders(TU, mod); |
| printf(" ModuleName=%s (%s) system=%d Headers(%d):", |
| clang_getCString(name), clang_getCString(astFilename), |
| clang_Module_isSystem(mod), numHeaders); |
| clang_disposeString(name); |
| clang_disposeString(astFilename); |
| for (i = 0; i < numHeaders; ++i) { |
| CXFile file = clang_Module_getTopLevelHeader(TU, mod, i); |
| CXString filename = clang_getFileName(file); |
| printf("\n%s", clang_getCString(filename)); |
| clang_disposeString(filename); |
| } |
| } |
| } |
| |
| if (completionString != NULL) { |
| printf("\nCompletion string: "); |
| print_completion_string(completionString, stdout); |
| } |
| printf("\n"); |
| } |
| |
| static void display_evaluate_results(CXEvalResult result) { |
| switch (clang_EvalResult_getKind(result)) { |
| case CXEval_Int: |
| { |
| printf("Kind: Int, "); |
| if (clang_EvalResult_isUnsignedInt(result)) { |
| unsigned long long val = clang_EvalResult_getAsUnsigned(result); |
| printf("unsigned, Value: %llu", val); |
| } else { |
| long long val = clang_EvalResult_getAsLongLong(result); |
| printf("Value: %lld", val); |
| } |
| break; |
| } |
| case CXEval_Float: |
| { |
| double val = clang_EvalResult_getAsDouble(result); |
| printf("Kind: Float , Value: %f", val); |
| break; |
| } |
| case CXEval_ObjCStrLiteral: |
| { |
| const char* str = clang_EvalResult_getAsStr(result); |
| printf("Kind: ObjCString , Value: %s", str); |
| break; |
| } |
| case CXEval_StrLiteral: |
| { |
| const char* str = clang_EvalResult_getAsStr(result); |
| printf("Kind: CString , Value: %s", str); |
| break; |
| } |
| case CXEval_CFStr: |
| { |
| const char* str = clang_EvalResult_getAsStr(result); |
| printf("Kind: CFString , Value: %s", str); |
| break; |
| } |
| default: |
| printf("Unexposed"); |
| break; |
| } |
| } |
| |
| static void inspect_evaluate_cursor(CXCursor Cursor) { |
| CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); |
| CXString Spelling; |
| const char *cspell; |
| unsigned line, column; |
| CXEvalResult ER; |
| |
| clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); |
| printf("%d:%d ", line, column); |
| PrintCursor(Cursor, NULL); |
| PrintCursorExtent(Cursor); |
| Spelling = clang_getCursorSpelling(Cursor); |
| cspell = clang_getCString(Spelling); |
| if (cspell && strlen(cspell) != 0) { |
| unsigned pieceIndex; |
| printf(" Spelling=%s (", cspell); |
| for (pieceIndex = 0; ; ++pieceIndex) { |
| CXSourceRange range = |
| clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); |
| if (clang_Range_isNull(range)) |
| break; |
| PrintRange(range, 0); |
| } |
| printf(")"); |
| } |
| clang_disposeString(Spelling); |
| |
| ER = clang_Cursor_Evaluate(Cursor); |
| if (!ER) { |
| printf("Not Evaluatable"); |
| } else { |
| display_evaluate_results(ER); |
| clang_EvalResult_dispose(ER); |
| } |
| printf("\n"); |
| } |
| |
| static void inspect_macroinfo_cursor(CXCursor Cursor) { |
| CXSourceLocation CursorLoc = clang_getCursorLocation(Cursor); |
| CXString Spelling; |
| const char *cspell; |
| unsigned line, column; |
| clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); |
| printf("%d:%d ", line, column); |
| PrintCursor(Cursor, NULL); |
| PrintCursorExtent(Cursor); |
| Spelling = clang_getCursorSpelling(Cursor); |
| cspell = clang_getCString(Spelling); |
| if (cspell && strlen(cspell) != 0) { |
| unsigned pieceIndex; |
| printf(" Spelling=%s (", cspell); |
| for (pieceIndex = 0; ; ++pieceIndex) { |
| CXSourceRange range = |
| clang_Cursor_getSpellingNameRange(Cursor, pieceIndex, 0); |
| if (clang_Range_isNull(range)) |
| break; |
| PrintRange(range, 0); |
| } |
| printf(")"); |
| } |
| clang_disposeString(Spelling); |
| |
| if (clang_Cursor_isMacroBuiltin(Cursor)) { |
| printf("[builtin macro]"); |
| } else if (clang_Cursor_isMacroFunctionLike(Cursor)) { |
| printf("[function macro]"); |
| } |
| printf("\n"); |
| } |
| |
| static enum CXVisitorResult findFileRefsVisit(void *context, |
| CXCursor cursor, CXSourceRange range) { |
| if (clang_Range_isNull(range)) |
| return CXVisit_Continue; |
| |
| PrintCursor(cursor, NULL); |
| PrintRange(range, ""); |
| printf("\n"); |
| return CXVisit_Continue; |
| } |
| |
| static int find_file_refs_at(int argc, const char **argv) { |
| CXIndex CIdx; |
| int errorCode; |
| struct CXUnsavedFile *unsaved_files = 0; |
| int num_unsaved_files = 0; |
| enum CXErrorCode Err; |
| CXTranslationUnit TU; |
| CXCursor Cursor; |
| CursorSourceLocation *Locations = 0; |
| unsigned NumLocations = 0, Loc; |
| unsigned Repeats = 1; |
| unsigned I; |
| |
| /* Count the number of locations. */ |
| while (strstr(argv[NumLocations+1], "-file-refs-at=") == argv[NumLocations+1]) |
| ++NumLocations; |
| |
| /* Parse the locations. */ |
| assert(NumLocations > 0 && "Unable to count locations?"); |
| Locations = (CursorSourceLocation *)malloc( |
| NumLocations * sizeof(CursorSourceLocation)); |
| for (Loc = 0; Loc < NumLocations; ++Loc) { |
| const char *input = argv[Loc + 1] + strlen("-file-refs-at="); |
| if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, |
| &Locations[Loc].line, |
| &Locations[Loc].column, 0, 0))) |
| return errorCode; |
| } |
| |
| if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, |
| &num_unsaved_files)) |
| return -1; |
| |
| if (getenv("CINDEXTEST_EDITING")) |
| Repeats = 5; |
| |
| /* Parse the translation unit. When we're testing clang_getCursor() after |
| reparsing, don't remap unsaved files until the second parse. */ |
| CIdx = clang_createIndex(1, 1); |
| Err = clang_parseTranslationUnit2(CIdx, argv[argc - 1], |
| argv + num_unsaved_files + 1 + NumLocations, |
| argc - num_unsaved_files - 2 - NumLocations, |
| unsaved_files, |
| Repeats > 1? 0 : num_unsaved_files, |
| getDefaultParsingOptions(), &TU); |
| if (Err != CXError_Success) { |
| fprintf(stderr, "unable to parse input\n"); |
| describeLibclangFailure(Err); |
| clang_disposeTranslationUnit(TU); |
| return -1; |
| } |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| for (I = 0; I != Repeats; ++I) { |
| if (Repeats > 1) { |
| Err = clang_reparseTranslationUnit(TU, num_unsaved_files, unsaved_files, |
| clang_defaultReparseOptions(TU)); |
| if (Err != CXError_Success) { |
| describeLibclangFailure(Err); |
| clang_disposeTranslationUnit(TU); |
| return 1; |
| } |
| } |
| |
| if (checkForErrors(TU) != 0) |
| return -1; |
| |
| for (Loc = 0; Loc < NumLocations; ++Loc) { |
| CXFile file = clang_getFile(TU, Locations[Loc].filename); |
| if (!file) |
| continue; |
| |
| Cursor = clang_getCursor(TU, |
|