From 29004673f709174d5016c36fe5bd07003dad5667 Mon Sep 17 00:00:00 2001 From: Ted Kremenek Date: Wed, 17 Feb 2010 00:41:32 +0000 Subject: [PATCH] Change clang_getCursorKindSpelling() to return a CXString instead of a 'const char *'. llvm-svn: 96425 --- clang/include/clang-c/Index.h | 2 +- clang/tools/CIndex/CIndex.cpp | 124 ++++++++----- clang/tools/c-index-test/c-index-test.c | 234 ++++++++++++------------ 3 files changed, 202 insertions(+), 158 deletions(-) diff --git a/clang/include/clang-c/Index.h b/clang/include/clang-c/Index.h index 5e41624729ca..1d654c884886 100644 --- a/clang/include/clang-c/Index.h +++ b/clang/include/clang-c/Index.h @@ -1221,7 +1221,7 @@ CINDEX_LINKAGE void clang_disposeTokens(CXTranslationUnit TU, */ /* for debug/testing */ -CINDEX_LINKAGE const char *clang_getCursorKindSpelling(enum CXCursorKind Kind); +CINDEX_LINKAGE CXString clang_getCursorKindSpelling(enum CXCursorKind Kind); CINDEX_LINKAGE void clang_getDefinitionSpellingAndExtent(CXCursor, const char **startBuf, const char **endBuf, diff --git a/clang/tools/CIndex/CIndex.cpp b/clang/tools/CIndex/CIndex.cpp index 44efba7568a6..eb30ba69255b 100644 --- a/clang/tools/CIndex/CIndex.cpp +++ b/clang/tools/CIndex/CIndex.cpp @@ -1369,80 +1369,114 @@ CXString clang_getCursorSpelling(CXCursor C) { if (clang_isDeclaration(C.kind)) return getDeclSpelling(getCursorDecl(C)); - + return createCXString(""); } -const char *clang_getCursorKindSpelling(enum CXCursorKind Kind) { +CXString clang_getCursorKindSpelling(enum CXCursorKind Kind) { switch (Kind) { - case CXCursor_FunctionDecl: return "FunctionDecl"; - case CXCursor_TypedefDecl: return "TypedefDecl"; - case CXCursor_EnumDecl: return "EnumDecl"; - case CXCursor_EnumConstantDecl: return "EnumConstantDecl"; - case CXCursor_StructDecl: return "StructDecl"; - case CXCursor_UnionDecl: return "UnionDecl"; - case CXCursor_ClassDecl: return "ClassDecl"; - case CXCursor_FieldDecl: return "FieldDecl"; - case CXCursor_VarDecl: return "VarDecl"; - case CXCursor_ParmDecl: return "ParmDecl"; - case CXCursor_ObjCInterfaceDecl: return "ObjCInterfaceDecl"; - case CXCursor_ObjCCategoryDecl: return "ObjCCategoryDecl"; - case CXCursor_ObjCProtocolDecl: return "ObjCProtocolDecl"; - case CXCursor_ObjCPropertyDecl: return "ObjCPropertyDecl"; - case CXCursor_ObjCIvarDecl: return "ObjCIvarDecl"; - case CXCursor_ObjCInstanceMethodDecl: return "ObjCInstanceMethodDecl"; - case CXCursor_ObjCClassMethodDecl: return "ObjCClassMethodDecl"; - case CXCursor_ObjCImplementationDecl: return "ObjCImplementationDecl"; - case CXCursor_ObjCCategoryImplDecl: return "ObjCCategoryImplDecl"; - case CXCursor_UnexposedDecl: return "UnexposedDecl"; - case CXCursor_ObjCSuperClassRef: return "ObjCSuperClassRef"; - case CXCursor_ObjCProtocolRef: return "ObjCProtocolRef"; - case CXCursor_ObjCClassRef: return "ObjCClassRef"; - case CXCursor_TypeRef: return "TypeRef"; - case CXCursor_UnexposedExpr: return "UnexposedExpr"; - case CXCursor_DeclRefExpr: return "DeclRefExpr"; - case CXCursor_MemberRefExpr: return "MemberRefExpr"; - case CXCursor_CallExpr: return "CallExpr"; - case CXCursor_ObjCMessageExpr: return "ObjCMessageExpr"; - case CXCursor_UnexposedStmt: return "UnexposedStmt"; - case CXCursor_InvalidFile: return "InvalidFile"; - case CXCursor_NoDeclFound: return "NoDeclFound"; - case CXCursor_NotImplemented: return "NotImplemented"; - case CXCursor_TranslationUnit: return "TranslationUnit"; + case CXCursor_FunctionDecl: + return createCXString("FunctionDecl"); + case CXCursor_TypedefDecl: + return createCXString("TypedefDecl"); + case CXCursor_EnumDecl: + return createCXString("EnumDecl"); + case CXCursor_EnumConstantDecl: + return createCXString("EnumConstantDecl"); + case CXCursor_StructDecl: + return createCXString("StructDecl"); + case CXCursor_UnionDecl: + return createCXString("UnionDecl"); + case CXCursor_ClassDecl: + return createCXString("ClassDecl"); + case CXCursor_FieldDecl: + return createCXString("FieldDecl"); + case CXCursor_VarDecl: + return createCXString("VarDecl"); + case CXCursor_ParmDecl: + return createCXString("ParmDecl"); + case CXCursor_ObjCInterfaceDecl: + return createCXString("ObjCInterfaceDecl"); + case CXCursor_ObjCCategoryDecl: + return createCXString("ObjCCategoryDecl"); + case CXCursor_ObjCProtocolDecl: + return createCXString("ObjCProtocolDecl"); + case CXCursor_ObjCPropertyDecl: + return createCXString("ObjCPropertyDecl"); + case CXCursor_ObjCIvarDecl: + return createCXString("ObjCIvarDecl"); + case CXCursor_ObjCInstanceMethodDecl: + return createCXString("ObjCInstanceMethodDecl"); + case CXCursor_ObjCClassMethodDecl: + return createCXString("ObjCClassMethodDecl"); + case CXCursor_ObjCImplementationDecl: + return createCXString("ObjCImplementationDecl"); + case CXCursor_ObjCCategoryImplDecl: + return createCXString("ObjCCategoryImplDecl"); + case CXCursor_UnexposedDecl: + return createCXString("UnexposedDecl"); + case CXCursor_ObjCSuperClassRef: + return createCXString("ObjCSuperClassRef"); + case CXCursor_ObjCProtocolRef: + return createCXString("ObjCProtocolRef"); + case CXCursor_ObjCClassRef: + return createCXString("ObjCClassRef"); + case CXCursor_TypeRef: + return createCXString("TypeRef"); + case CXCursor_UnexposedExpr: + return createCXString("UnexposedExpr"); + case CXCursor_DeclRefExpr: + return createCXString("DeclRefExpr"); + case CXCursor_MemberRefExpr: + return createCXString("MemberRefExpr"); + case CXCursor_CallExpr: + return createCXString("CallExpr"); + case CXCursor_ObjCMessageExpr: + return createCXString("ObjCMessageExpr"); + case CXCursor_UnexposedStmt: + return createCXString("UnexposedStmt"); + case CXCursor_InvalidFile: + return createCXString("InvalidFile"); + case CXCursor_NoDeclFound: + return createCXString("NoDeclFound"); + case CXCursor_NotImplemented: + return createCXString("NotImplemented"); + case CXCursor_TranslationUnit: + return createCXString("TranslationUnit"); } - + llvm_unreachable("Unhandled CXCursorKind"); - return NULL; + return createCXString(NULL); } -enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, - CXCursor parent, +enum CXChildVisitResult GetCursorVisitor(CXCursor cursor, + CXCursor parent, CXClientData client_data) { CXCursor *BestCursor = static_cast(client_data); *BestCursor = cursor; return CXChildVisit_Recurse; } - + CXCursor clang_getCursor(CXTranslationUnit TU, CXSourceLocation Loc) { if (!TU) return clang_getNullCursor(); - + ASTUnit *CXXUnit = static_cast(TU); SourceLocation SLoc = cxloc::translateSourceLocation(Loc); CXCursor Result = MakeCXCursorInvalid(CXCursor_NoDeclFound); if (SLoc.isValid()) { SourceRange RegionOfInterest(SLoc, SLoc.getFileLocWithOffset(1)); - + // FIXME: Would be great to have a "hint" cursor, then walk from that // hint cursor upward until we find a cursor whose source range encloses // the region of interest, rather than starting from the translation unit. CXCursor Parent = clang_getTranslationUnitCursor(CXXUnit); - CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result, + CursorVisitor CursorVis(CXXUnit, GetCursorVisitor, &Result, Decl::MaxPCHLevel, RegionOfInterest); CursorVis.VisitChildren(Parent); } - return Result; + return Result; } CXCursor clang_getNullCursor(void) { diff --git a/clang/tools/c-index-test/c-index-test.c b/clang/tools/c-index-test/c-index-test.c index 334c7891f7d3..360f75a66e0d 100644 --- a/clang/tools/c-index-test/c-index-test.c +++ b/clang/tools/c-index-test/c-index-test.c @@ -28,7 +28,7 @@ char *basename(const char* path) extern char *basename(const char *); #endif -static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, +static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, CXClientData ClientData); @@ -40,12 +40,12 @@ static void PrintExtent(FILE *out, unsigned begin_line, unsigned begin_column, static unsigned CreateTranslationUnit(CXIndex Idx, const char *file, CXTranslationUnit *TU) { - + *TU = clang_createTranslationUnit(Idx, file, PrintDiagnosticCallback, stderr); if (!TU) { fprintf(stderr, "Unable to load translation unit from '%s'!\n", file); return 0; - } + } return 1; } @@ -66,20 +66,20 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, int prefix_len = strlen("-remap-file="); *unsaved_files = 0; *num_unsaved_files = 0; - + /* Count the number of remapped files. */ for (arg = start_arg; arg < argc; ++arg) { if (strncmp(argv[arg], "-remap-file=", prefix_len)) break; - + ++*num_unsaved_files; } - + if (*num_unsaved_files == 0) return 0; - + *unsaved_files - = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * + = (struct CXUnsavedFile *)malloc(sizeof(struct CXUnsavedFile) * *num_unsaved_files); for (arg = start_arg, i = 0; i != *num_unsaved_files; ++i, ++arg) { struct CXUnsavedFile *unsaved = *unsaved_files + i; @@ -90,14 +90,14 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, FILE *to_file; const char *semi = strchr(arg_string, ';'); if (!semi) { - fprintf(stderr, + fprintf(stderr, "error: -remap-file=from;to argument is missing semicolon\n"); 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(semi + 1, "r"); if (!to_file) { @@ -108,12 +108,12 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, *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) { @@ -127,10 +127,10 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, } contents[unsaved->Length] = 0; unsaved->Contents = contents; - + /* Close the file. */ fclose(to_file); - + /* Copy the file name that we're remapping from. */ filename_len = semi - arg_string; filename = (char *)malloc(filename_len + 1); @@ -138,7 +138,7 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, filename[filename_len] = 0; unsaved->Filename = filename; } - + return 0; } @@ -147,17 +147,23 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, /******************************************************************************/ static void PrintCursor(CXCursor Cursor) { - if (clang_isInvalid(Cursor.kind)) - printf("Invalid Cursor => %s", clang_getCursorKindSpelling(Cursor.kind)); + if (clang_isInvalid(Cursor.kind)) { + CXString ks = clang_getCursorKindSpelling(Cursor.kind); + printf("Invalid Cursor => %s", clang_getCString(ks)); + clang_disposeString(ks); + } else { - CXString string; + CXString string, ks; CXCursor Referenced; unsigned line, column; + + ks = clang_getCursorKindSpelling(Cursor.kind); string = clang_getCursorSpelling(Cursor); - printf("%s=%s", clang_getCursorKindSpelling(Cursor.kind), - clang_getCString(string)); + 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())) { CXSourceLocation Loc = clang_getCursorLocation(Referenced); @@ -170,18 +176,18 @@ static void PrintCursor(CXCursor Cursor) { } } -static const char* GetCursorSource(CXCursor Cursor) { +static const char* GetCursorSource(CXCursor Cursor) { CXSourceLocation Loc = clang_getCursorLocation(Cursor); CXString source; CXFile file; clang_getInstantiationLocation(Loc, &file, 0, 0, 0); source = clang_getFileName(file); - if (!source.Spelling) { + if (!clang_getCString(source)) { clang_disposeString(source); return ""; } else { - const char *b = basename(source.Spelling); + const char *b = basename(clang_getCString(source)); clang_disposeString(source); return b; } @@ -193,18 +199,18 @@ static const char* GetCursorSource(CXCursor Cursor) { typedef void (*PostVisitTU)(CXTranslationUnit); -static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, +static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, CXClientData ClientData) { FILE *out = (FILE *)ClientData; CXFile file; unsigned line, column; CXString text; enum CXDiagnosticSeverity severity = clang_getDiagnosticSeverity(Diagnostic); - + /* Ignore diagnostics that should be ignored. */ if (severity == CXDiagnostic_Ignored) return; - + /* Print file:line:column. */ clang_getInstantiationLocation(clang_getDiagnosticLocation(Diagnostic), &file, &line, &column, 0); @@ -212,22 +218,22 @@ static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, unsigned i, n; unsigned printed_any_ranges = 0; CXString fname; - + fname = clang_getFileName(file); - fprintf(out, "%s:%d:%d:", fname.Spelling, line, column); + fprintf(out, "%s:%d:%d:", clang_getCString(fname), line, column); clang_disposeString(fname); - + n = clang_getDiagnosticNumRanges(Diagnostic); for (i = 0; i != n; ++i) { CXFile start_file, end_file; CXSourceRange range = clang_getDiagnosticRange(Diagnostic, i); - + unsigned start_line, start_column, end_line, end_column; clang_getInstantiationLocation(clang_getRangeStart(range), &start_file, &start_line, &start_column,0); clang_getInstantiationLocation(clang_getRangeEnd(range), &end_file, &end_line, &end_column, 0); - + if (start_file != end_file || start_file != file) continue; @@ -236,10 +242,10 @@ static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, } if (printed_any_ranges) fprintf(out, ":"); - + fprintf(out, " "); } - + /* Print warning/error/etc. */ switch (severity) { case CXDiagnostic_Ignored: assert(0 && "impossible"); break; @@ -248,14 +254,14 @@ static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, case CXDiagnostic_Error: fprintf(out, "error: "); break; case CXDiagnostic_Fatal: fprintf(out, "fatal error: "); break; } - + text = clang_getDiagnosticSpelling(Diagnostic); if (clang_getCString(text)) fprintf(out, "%s\n", clang_getCString(text)); else fprintf(out, "\n"); clang_disposeString(text); - + if (file) { unsigned i, num_fixits = clang_getDiagnosticNumFixIts(Diagnostic); for (i = 0; i != num_fixits; ++i) { @@ -265,7 +271,7 @@ static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, CXFile insertion_file; unsigned insertion_line, insertion_column; text = clang_getDiagnosticFixItInsertion(Diagnostic, i, &insertion_loc); - clang_getInstantiationLocation(insertion_loc, &insertion_file, + clang_getInstantiationLocation(insertion_loc, &insertion_file, &insertion_line, &insertion_column, 0); if (insertion_file == file) fprintf(out, "FIX-IT: Insert \"%s\" at %d:%d\n", @@ -291,7 +297,7 @@ static void PrintDiagnosticCallback(CXDiagnostic Diagnostic, } break; } - + case CXFixIt_Replacement: { CXFile start_file, end_file; unsigned start_line, start_column, end_line, end_column; @@ -325,7 +331,7 @@ static void PrintCursorExtent(CXCursor C) { CXSourceRange extent = clang_getCursorExtent(C); CXFile begin_file, end_file; unsigned begin_line, begin_column, end_line, end_column; - + clang_getInstantiationLocation(clang_getRangeStart(extent), &begin_file, &begin_line, &begin_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(extent), @@ -344,7 +350,7 @@ typedef struct { } VisitorData; -enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, +enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, CXCursor Parent, CXClientData ClientData) { VisitorData *Data = (VisitorData *)ClientData; @@ -356,14 +362,14 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, GetCursorSource(Cursor), line, column); PrintCursor(Cursor); PrintCursorExtent(Cursor); - printf("\n"); + printf("\n"); return CXChildVisit_Recurse; } - + return CXChildVisit_Continue; } -static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, +static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, CXCursor Parent, CXClientData ClientData) { const char *startBuf, *endBuf; @@ -386,19 +392,19 @@ static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, CXSourceLocation Loc; CXFile file; CXString source; - + if (*startBuf == '\n') { startBuf++; curLine++; curColumn = 1; } else if (*startBuf != '\t') curColumn++; - + Loc = clang_getCursorLocation(Cursor); clang_getInstantiationLocation(Loc, &file, 0, 0, 0); - + source = clang_getFileName(file); - if (source.Spelling) { + if (clang_getCString(source)) { CXSourceLocation RefLoc = clang_getLocation(Data->TU, file, curLine, curColumn); Ref = clang_getCursor(Data->TU, RefLoc); @@ -414,7 +420,7 @@ static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, clang_disposeString(source); startBuf++; } - + return CXChildVisit_Continue; } @@ -427,18 +433,19 @@ enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, VisitorData *Data = (VisitorData *)ClientData; if (!Data->Filter || (C.kind == *(enum CXCursorKind *)Data->Filter)) { CXString USR = clang_getCursorUSR(C); - if (!USR.Spelling) { + if (!clang_getCString(USR)) { clang_disposeString(USR); return CXChildVisit_Continue; } - printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), USR.Spelling); + printf("// %s: %s %s", FileCheckPrefix, GetCursorSource(C), + clang_getCString(USR)); PrintCursorExtent(C); printf("\n"); clang_disposeString(USR); - + return CXChildVisit_Recurse; - } - + } + return CXChildVisit_Continue; } @@ -448,28 +455,28 @@ enum CXChildVisitResult USRVisitor(CXCursor C, CXCursor parent, void InclusionVisitor(CXFile includedFile, CXSourceLocation *includeStack, unsigned includeStackLen, CXClientData data) { - + unsigned i; CXString fname; fname = clang_getFileName(includedFile); - printf("file: %s\nincluded by:\n", fname.Spelling); + printf("file: %s\nincluded by:\n", clang_getCString(fname)); clang_disposeString(fname); - + for (i = 0; i < includeStackLen; ++i) { CXFile includingFile; unsigned line, column; clang_getInstantiationLocation(includeStack[i], &includingFile, &line, &column, 0); fname = clang_getFileName(includingFile); - printf(" %s:%d:%d\n", fname.Spelling, line, column); + printf(" %s:%d:%d\n", clang_getCString(fname), line, column); clang_disposeString(fname); } printf("\n"); } void PrintInclusionStack(CXTranslationUnit TU) { - clang_getInclusions(TU, InclusionVisitor, NULL); + clang_getInclusions(TU, InclusionVisitor, NULL); } /******************************************************************************/ @@ -480,15 +487,15 @@ static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, const char *filter, const char *prefix, CXCursorVisitor Visitor, PostVisitTU PV) { - + if (prefix) - FileCheckPrefix = 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, "none")) K = (enum CXCursorKind) ~0; @@ -502,12 +509,12 @@ static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, fprintf(stderr, "Unknown filter for -test-load-tu: %s\n", filter); return 1; } - + Data.TU = TU; Data.Filter = ck; clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); } - + if (PV) PV(TU); @@ -521,9 +528,9 @@ int perform_test_load_tu(const char *file, const char *filter, CXIndex Idx; CXTranslationUnit TU; int result; - Idx = clang_createIndex(/* excludeDeclsFromPCH */ + Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(filter, "local") ? 1 : 0); - + if (!CreateTranslationUnit(Idx, file, &TU)) { clang_disposeIndex(Idx); return 1; @@ -544,7 +551,7 @@ int perform_test_load_source(int argc, const char **argv, struct CXUnsavedFile *unsaved_files = 0; int num_unsaved_files = 0; int result; - + Idx = clang_createIndex(/* excludeDeclsFromPCH */ !strcmp(filter, "local") ? 1 : 0); @@ -556,8 +563,8 @@ int perform_test_load_source(int argc, const char **argv, return -1; } - TU = clang_createTranslationUnitFromSourceFile(Idx, 0, - argc - num_unsaved_files, + TU = clang_createTranslationUnitFromSourceFile(Idx, 0, + argc - num_unsaved_files, argv + num_unsaved_files, num_unsaved_files, unsaved_files, @@ -601,20 +608,20 @@ static int perform_file_scan(const char *ast_file, const char *source_file, CXFile file; unsigned line = 1, col = 1; unsigned start_line = 1, start_col = 1; - + if (!(Idx = clang_createIndex(/* excludeDeclsFromPCH */ 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); return 1; } - + file = clang_getFile(TU, source_file); for (;;) { CXCursor cursor; @@ -642,7 +649,7 @@ static int perform_file_scan(const char *ast_file, const char *source_file, prevCursor = cursor; } - + fclose(fp); return 0; } @@ -654,7 +661,7 @@ static int perform_file_scan(const char *ast_file, const char *source_file, /* 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, +int parse_file_line_column(const char *input, char **filename, unsigned *line, unsigned *column, unsigned *second_line, unsigned *second_column) { /* Find the second colon. */ @@ -678,11 +685,11 @@ int parse_file_line_column(const char *input, char **filename, unsigned *line, /* 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", + fprintf(stderr, "could not parse %s in '%s'\n", (i % 2 ? "column" : "line"), input); return 1; } - + if (i + 1 == num_values) break; @@ -691,9 +698,9 @@ int parse_file_line_column(const char *input, char **filename, unsigned *line, while (prev_colon != input && *prev_colon != ':') --prev_colon; if (prev_colon == input) { - fprintf(stderr, "could not parse %s in '%s'\n", + fprintf(stderr, "could not parse %s in '%s'\n", (i % 2 == 0? "column" : "line"), input); - return 1; + return 1; } last_colon = prev_colon; @@ -701,7 +708,7 @@ int parse_file_line_column(const char *input, char **filename, unsigned *line, *line = values[0]; *column = values[1]; - + if (second_line && second_column) { *second_line = values[2]; *second_column = values[3]; @@ -739,30 +746,30 @@ clang_getCompletionChunkKindSpelling(enum CXCompletionChunkKind Kind) { case CXCompletionChunk_HorizontalSpace: return "HorizontalSpace"; case CXCompletionChunk_VerticalSpace: return "VerticalSpace"; } - + return "Unknown"; } void print_completion_string(CXCompletionString completion_string, FILE *file) { int I, N; - + N = clang_getNumCompletionChunks(completion_string); for (I = 0; I != N; ++I) { const char *text = 0; enum CXCompletionChunkKind Kind = clang_getCompletionChunkKind(completion_string, I); - + if (Kind == CXCompletionChunk_Optional) { fprintf(file, "{Optional "); print_completion_string( - clang_getCompletionChunkCompletionString(completion_string, I), + clang_getCompletionChunkCompletionString(completion_string, I), file); fprintf(file, "}"); continue; } - + text = clang_getCompletionChunkText(completion_string, I); - fprintf(file, "{%s %s}", + fprintf(file, "{%s %s}", clang_getCompletionChunkKindSpelling(Kind), text? text : ""); } @@ -771,8 +778,11 @@ void print_completion_string(CXCompletionString completion_string, FILE *file) { void print_completion_result(CXCompletionResult *completion_result, CXClientData client_data) { FILE *file = (FILE *)client_data; - fprintf(file, "%s:", - clang_getCursorKindSpelling(completion_result->CursorKind)); + CXString ks = clang_getCursorKindSpelling(completion_result->CursorKind); + + fprintf(file, "%s:", clang_getCString(ks)); + clang_disposeString(ks); + print_completion_string(completion_result->CompletionString, file); fprintf(file, "\n"); } @@ -789,7 +799,7 @@ int perform_code_completion(int argc, const char **argv) { CXCodeCompleteResults *results = 0; input += strlen("-code-completion-at="); - if ((errorCode = parse_file_line_column(input, &filename, &line, &column, + if ((errorCode = parse_file_line_column(input, &filename, &line, &column, 0, 0))) return errorCode; @@ -797,9 +807,9 @@ int perform_code_completion(int argc, const char **argv) { return -1; CIdx = clang_createIndex(0); - results = clang_codeComplete(CIdx, - argv[argc - 1], argc - num_unsaved_files - 3, - argv + num_unsaved_files + 2, + results = clang_codeComplete(CIdx, + argv[argc - 1], argc - num_unsaved_files - 3, + argv + num_unsaved_files + 2, num_unsaved_files, unsaved_files, filename, line, column, PrintDiagnosticCallback, stderr); @@ -813,7 +823,7 @@ int perform_code_completion(int argc, const char **argv) { clang_disposeIndex(CIdx); free(filename); - + free_remapped_files(unsaved_files, num_unsaved_files); return 0; @@ -835,26 +845,26 @@ int inspect_cursor_at(int argc, const char **argv) { CursorSourceLocation *Locations = 0; unsigned NumLocations = 0, Loc; - /* Count the number of locations. */ + /* Count the number of locations. */ while (strstr(argv[NumLocations+1], "-cursor-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("-cursor-at="); - if ((errorCode = parse_file_line_column(input, &Locations[Loc].filename, - &Locations[Loc].line, + 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, + + if (parse_remapped_files(argc, argv, NumLocations + 1, &unsaved_files, &num_unsaved_files)) return -1; - + CIdx = clang_createIndex(0); TU = clang_createTranslationUnitFromSourceFile(CIdx, argv[argc - 1], argc - num_unsaved_files - 2 - NumLocations, @@ -867,20 +877,20 @@ int inspect_cursor_at(int argc, const char **argv) { fprintf(stderr, "unable to parse input\n"); 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)); + + Cursor = clang_getCursor(TU, + clang_getLocation(TU, file, Locations[Loc].line, + Locations[Loc].column)); PrintCursor(Cursor); printf("\n"); free(Locations[Loc].filename); } - + clang_disposeTranslationUnit(TU); clang_disposeIndex(CIdx); free(Locations); @@ -928,7 +938,7 @@ int perform_token_annotation(int argc, const char **argv) { free(filename); free_remapped_files(unsaved_files, num_unsaved_files); return -1; - } + } errorCode = 0; file = clang_getFile(TU, filename); @@ -940,18 +950,18 @@ int perform_token_annotation(int argc, const char **argv) { startLoc = clang_getLocation(TU, file, line, column); if (clang_equalLocations(clang_getNullLocation(), startLoc)) { - fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, + fprintf(stderr, "invalid source location %s:%d:%d\n", filename, line, column); errorCode = -1; - goto teardown; + goto teardown; } endLoc = clang_getLocation(TU, file, second_line, second_column); if (clang_equalLocations(clang_getNullLocation(), endLoc)) { - fprintf(stderr, "invalid source location %s:%d:%d\n", filename, + fprintf(stderr, "invalid source location %s:%d:%d\n", filename, second_line, second_column); errorCode = -1; - goto teardown; + goto teardown; } range = clang_getRange(startLoc, endLoc); @@ -971,7 +981,7 @@ int perform_token_annotation(int argc, const char **argv) { case CXToken_Literal: kind = "Literal"; break; case CXToken_Comment: kind = "Comment"; break; } - clang_getInstantiationLocation(clang_getRangeStart(extent), + clang_getInstantiationLocation(clang_getRangeStart(extent), 0, &start_line, &start_column, 0); clang_getInstantiationLocation(clang_getRangeEnd(extent), 0, &end_line, &end_column, 0); @@ -1059,7 +1069,7 @@ int main(int argc, const char **argv) { else if (argc > 2 && strcmp(argv[1], "-test-inclusion-stack-tu") == 0) return perform_test_load_tu(argv[2], "all", NULL, NULL, PrintInclusionStack); - + print_usage(); return 1; }