diff options
Diffstat (limited to 'tools')
-rw-r--r-- | tools/c-index-test/CMakeLists.txt | 7 | ||||
-rw-r--r-- | tools/c-index-test/Makefile | 3 | ||||
-rw-r--r-- | tools/c-index-test/c-index-test.c | 154 | ||||
-rw-r--r-- | tools/libclang/CXComment.cpp | 388 | ||||
-rw-r--r-- | tools/libclang/libclang.exports | 1 |
5 files changed, 528 insertions, 25 deletions
diff --git a/tools/c-index-test/CMakeLists.txt b/tools/c-index-test/CMakeLists.txt index afe49ed6a7..6379194c55 100644 --- a/tools/c-index-test/CMakeLists.txt +++ b/tools/c-index-test/CMakeLists.txt @@ -14,3 +14,10 @@ target_link_libraries(c-index-test set_target_properties(c-index-test PROPERTIES LINKER_LANGUAGE CXX) + +# If libxml2 is available, make it available for c-index-test. +if (LIBXML2_FOUND) + add_definitions(${LIBXML2_DEFINITIONS} "-DCLANG_HAVE_LIBXML") + include_directories(${LIBXML2_INCLUDE_DIR}) + target_link_libraries(c-index-test ${LIBXML2_LIBRARIES}) +endif() diff --git a/tools/c-index-test/Makefile b/tools/c-index-test/Makefile index 932dbb2d16..25478e1f96 100644 --- a/tools/c-index-test/Makefile +++ b/tools/c-index-test/Makefile @@ -28,3 +28,6 @@ USEDLIBS = clang.a clangFrontend.a clangDriver.a \ clangBasic.a include $(CLANG_LEVEL)/Makefile + +LIBS += "$(LIBXML2_LIBS)" +CPPFLAGS += "$(LIBXML2_INC)" diff --git a/tools/c-index-test/c-index-test.c b/tools/c-index-test/c-index-test.c index 4af2548895..f9b5cdf96b 100644 --- a/tools/c-index-test/c-index-test.c +++ b/tools/c-index-test/c-index-test.c @@ -2,12 +2,19 @@ #include "clang-c/Index.h" #include "clang-c/CXCompilationDatabase.h" +#include "llvm/Config/config.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 + /******************************************************************************/ /* Utility functions. */ /******************************************************************************/ @@ -179,6 +186,19 @@ int parse_remapped_files(int argc, const char **argv, int start_arg, 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. */ /******************************************************************************/ @@ -212,6 +232,10 @@ static void PrintCXStringAndDispose(CXString 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)); @@ -437,7 +461,60 @@ static void DumpCXComment(CXComment Comment) { printf("]"); } -static void PrintCursorComments(CXCursor Cursor) { +typedef struct { + const char *CommentSchemaFile; +#ifdef CLANG_HAVE_LIBXML + xmlRelaxNGParserCtxtPtr RNGParser; + xmlRelaxNGPtr Schema; +#endif +} CommentXMLValidationData; + +static void ValidateCommentXML(const char *Str, + CommentXMLValidationData *ValidationData) { +#ifdef CLANG_HAVE_LIBXML + xmlDocPtr Doc; + xmlRelaxNGValidCtxtPtr ValidationCtxt; + int status; + + if (!ValidationData || !ValidationData->CommentSchemaFile) + return; + + if (!ValidationData->RNGParser) { + ValidationData->RNGParser = + xmlRelaxNGNewParserCtxt(ValidationData->CommentSchemaFile); + ValidationData->Schema = xmlRelaxNGParse(ValidationData->RNGParser); + } + if (!ValidationData->RNGParser) { + printf(" libXMLError"); + return; + } + + Doc = xmlParseDoc((const xmlChar *) Str); + + if (!Doc) { + xmlErrorPtr Error = xmlGetLastError(); + printf(" CommentXMLInvalid [not well-formed XML: %s]", Error->message); + return; + } + + ValidationCtxt = xmlRelaxNGNewValidCtxt(ValidationData->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); +#endif +} + +static void PrintCursorComments(CXTranslationUnit TU, + CXCursor Cursor, + CommentXMLValidationData *ValidationData) { { CXString RawComment; const char *RawCommentCString; @@ -464,12 +541,21 @@ static void PrintCursorComments(CXCursor Cursor) { if (clang_Comment_getKind(Comment) != CXComment_Null) { PrintCXStringWithPrefixAndDispose("FullCommentAsHTML", clang_FullComment_getAsHTML(Comment)); + { + CXString XML; + XML = clang_FullComment_getAsXML(TU, Comment); + PrintCXStringWithPrefix("FullCommentAsXML", XML); + ValidateCommentXML(clang_getCString(XML), ValidationData); + clang_disposeString(XML); + } + DumpCXComment(Comment); } } } -static void PrintCursor(CXCursor Cursor) { +static void PrintCursor(CXCursor Cursor, + CommentXMLValidationData *ValidationData) { CXTranslationUnit TU = clang_Cursor_getTranslationUnit(Cursor); if (clang_isInvalid(Cursor.kind)) { CXString ks = clang_getCursorKindSpelling(Cursor.kind); @@ -674,7 +760,7 @@ static void PrintCursor(CXCursor Cursor) { PrintRange(RefNameRange, "RefName"); } - PrintCursorComments(Cursor); + PrintCursorComments(TU, Cursor, ValidationData); } } @@ -802,10 +888,11 @@ static void PrintCursorExtent(CXCursor C) { PrintRange(extent, "Extent"); } -/* Data used by all of the visitors. */ -typedef struct { +/* Data used by the visitors. */ +typedef struct { CXTranslationUnit TU; enum CXCursorKind *Filter; + CommentXMLValidationData ValidationData; } VisitorData; @@ -819,7 +906,7 @@ enum CXChildVisitResult FilteredPrintingVisitor(CXCursor Cursor, clang_getSpellingLocation(Loc, 0, &line, &column, 0); printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Cursor), line, column); - PrintCursor(Cursor); + PrintCursor(Cursor, &Data->ValidationData); PrintCursorExtent(Cursor); printf("\n"); return CXChildVisit_Recurse; @@ -872,7 +959,7 @@ static enum CXChildVisitResult FunctionScanVisitor(CXCursor Cursor, } else if (Ref.kind != CXCursor_FunctionDecl) { printf("// %s: %s:%d:%d: ", FileCheckPrefix, GetCursorSource(Ref), curLine, curColumn); - PrintCursor(Ref); + PrintCursor(Ref, &Data->ValidationData); printf("\n"); } } @@ -959,7 +1046,7 @@ static enum CXChildVisitResult PrintLinkage(CXCursor cursor, CXCursor p, } if (linkage) { - PrintCursor(cursor); + PrintCursor(cursor, NULL); printf("linkage=%s\n", linkage); } @@ -975,7 +1062,7 @@ static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, if (!clang_isInvalid(clang_getCursorKind(cursor))) { CXType T = clang_getCursorType(cursor); CXString S = clang_getTypeKindSpelling(T.kind); - PrintCursor(cursor); + PrintCursor(cursor, NULL); printf(" typekind=%s", clang_getCString(S)); if (clang_isConstQualifiedType(T)) printf(" const"); @@ -1035,7 +1122,8 @@ static enum CXChildVisitResult PrintTypeKind(CXCursor cursor, CXCursor p, static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, const char *filter, const char *prefix, CXCursorVisitor Visitor, - PostVisitTU PV) { + PostVisitTU PV, + const char *CommentSchemaFile) { if (prefix) FileCheckPrefix = prefix; @@ -1066,6 +1154,11 @@ static int perform_test_load(CXIndex Idx, CXTranslationUnit TU, Data.TU = TU; Data.Filter = ck; + Data.ValidationData.CommentSchemaFile = CommentSchemaFile; +#ifdef CLANG_HAVE_LIBXML + Data.ValidationData.RNGParser = NULL; + Data.ValidationData.Schema = NULL; +#endif clang_visitChildren(clang_getTranslationUnitCursor(TU), Visitor, &Data); } @@ -1097,7 +1190,7 @@ int perform_test_load_tu(const char *file, const char *filter, return 1; } - result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV); + result = perform_test_load(Idx, TU, filter, prefix, Visitor, PV, NULL); clang_disposeIndex(Idx); return result; } @@ -1107,6 +1200,7 @@ int perform_test_load_source(int argc, const char **argv, PostVisitTU PV) { CXIndex Idx; CXTranslationUnit TU; + const char *CommentSchemaFile; struct CXUnsavedFile *unsaved_files = 0; int num_unsaved_files = 0; int result; @@ -1116,6 +1210,11 @@ int perform_test_load_source(int argc, const char **argv, !strcmp(filter, "local-display"))? 1 : 0, /* displayDiagnosics=*/0); + 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; @@ -1133,7 +1232,8 @@ int perform_test_load_source(int argc, const char **argv, return 1; } - result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); + result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, + CommentSchemaFile); free_remapped_files(unsaved_files, num_unsaved_files); clang_disposeIndex(Idx); return result; @@ -1197,7 +1297,7 @@ int perform_test_reparse_source(int argc, const char **argv, int trials, return -1; } - result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV); + result = perform_test_load(Idx, TU, filter, NULL, Visitor, PV, NULL); free_remapped_files(unsaved_files, num_unsaved_files); clang_disposeIndex(Idx); @@ -1217,7 +1317,7 @@ static void print_cursor_file_scan(CXTranslationUnit TU, CXCursor cursor, printf("-%s", prefix); PrintExtent(stdout, start_line, start_col, end_line, end_col); printf(" "); - PrintCursor(cursor); + PrintCursor(cursor, NULL); printf("\n"); } @@ -1814,7 +1914,7 @@ static int inspect_cursor_at(int argc, const char **argv) { unsigned line, column; clang_getSpellingLocation(CursorLoc, 0, &line, &column, 0); printf("%d:%d ", line, column); - PrintCursor(Cursor); + PrintCursor(Cursor, NULL); PrintCursorExtent(Cursor); Spelling = clang_getCursorSpelling(Cursor); cspell = clang_getCString(Spelling); @@ -1859,7 +1959,7 @@ static enum CXVisitorResult findFileRefsVisit(void *context, if (clang_Range_isNull(range)) return CXVisit_Continue; - PrintCursor(cursor); + PrintCursor(cursor, NULL); PrintRange(range, ""); printf("\n"); return CXVisit_Continue; @@ -1943,7 +2043,7 @@ static int find_file_refs_at(int argc, const char **argv) { if (I + 1 == Repeats) { CXCursorAndRangeVisitor visitor = { 0, findFileRefsVisit }; - PrintCursor(Cursor); + PrintCursor(Cursor, NULL); printf("\n"); clang_findReferencesInFile(Cursor, file, visitor); free(Locations[Loc].filename); @@ -2141,7 +2241,7 @@ static void printEntityInfo(const char *cb, for (i = 0; i != info->numAttributes; ++i) { const CXIdxAttrInfo *Attr = info->attributes[i]; printf(" <attribute>: "); - PrintCursor(Attr->cursor); + PrintCursor(Attr->cursor, NULL); } } @@ -2149,7 +2249,7 @@ static void printBaseClassInfo(CXClientData client_data, const CXIdxBaseClassInfo *info) { printEntityInfo(" <base>", client_data, info->base); printf(" | cursor: "); - PrintCursor(info->cursor); + PrintCursor(info->cursor, NULL); printf(" | loc: "); printCXIndexLoc(info->loc, client_data); } @@ -2161,7 +2261,7 @@ static void printProtocolList(const CXIdxObjCProtocolRefListInfo *ProtoInfo, printEntityInfo(" <protocol>", client_data, ProtoInfo->protocols[i]->protocol); printf(" | cursor: "); - PrintCursor(ProtoInfo->protocols[i]->cursor); + PrintCursor(ProtoInfo->protocols[i]->cursor, NULL); printf(" | loc: "); printCXIndexLoc(ProtoInfo->protocols[i]->loc, client_data); printf("\n"); @@ -2251,7 +2351,7 @@ static void index_indexDeclaration(CXClientData client_data, printEntityInfo("[indexDeclaration]", client_data, info->entityInfo); printf(" | cursor: "); - PrintCursor(info->cursor); + PrintCursor(info->cursor, NULL); printf(" | loc: "); printCXIndexLoc(info->loc, client_data); printf(" | semantic-container: "); @@ -2266,7 +2366,7 @@ static void index_indexDeclaration(CXClientData client_data, for (i = 0; i != info->numAttributes; ++i) { const CXIdxAttrInfo *Attr = info->attributes[i]; printf(" <attribute>: "); - PrintCursor(Attr->cursor); + PrintCursor(Attr->cursor, NULL); printf("\n"); } @@ -2289,7 +2389,7 @@ static void index_indexDeclaration(CXClientData client_data, printEntityInfo(" <ObjCCategoryInfo>: class", client_data, CatInfo->objcClass); printf(" | cursor: "); - PrintCursor(CatInfo->classCursor); + PrintCursor(CatInfo->classCursor, NULL); printf(" | loc: "); printCXIndexLoc(CatInfo->classLoc, client_data); printf("\n"); @@ -2333,7 +2433,7 @@ static void index_indexEntityReference(CXClientData client_data, const CXIdxEntityRefInfo *info) { printEntityInfo("[indexEntityReference]", client_data, info->referencedEntity); printf(" | cursor: "); - PrintCursor(info->cursor); + PrintCursor(info->cursor, NULL); printf(" | loc: "); printCXIndexLoc(info->loc, client_data); printEntityInfo(" | <parent>:", client_data, info->parentEntity); @@ -2602,7 +2702,7 @@ int perform_token_annotation(int argc, const char **argv) { PrintExtent(stdout, start_line, start_column, end_line, end_column); if (!clang_isInvalid(cursors[i].kind)) { printf(" "); - PrintCursor(cursors[i]); + PrintCursor(cursors[i], NULL); } printf("\n"); } @@ -3255,6 +3355,10 @@ void thread_runner(void *client_data_v) { } int main(int argc, const char **argv) { +#ifdef CLANG_HAVE_LIBXML + LIBXML_TEST_VERSION +#endif + thread_info client_data; if (getenv("CINDEXTEST_NOTHREADS")) diff --git a/tools/libclang/CXComment.cpp b/tools/libclang/CXComment.cpp index fe6fddb795..acb4353418 100644 --- a/tools/libclang/CXComment.cpp +++ b/tools/libclang/CXComment.cpp @@ -14,9 +14,14 @@ #include "clang-c/Index.h" #include "CXString.h" #include "CXComment.h" +#include "CXCursor.h" +#include "CXTranslationUnit.h" #include "clang/AST/CommentVisitor.h" +#include "clang/AST/Decl.h" +#include "clang/Frontend/ASTUnit.h" +#include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/raw_ostream.h" @@ -826,3 +831,386 @@ CXString clang_FullComment_getAsHTML(CXComment CXC) { } // end extern "C" +namespace { +class CommentASTToXMLConverter : + public ConstCommentVisitor<CommentASTToXMLConverter> { +public: + /// \param Str accumulator for XML. + CommentASTToXMLConverter(const SourceManager &SM, + SmallVectorImpl<char> &Str) : + SM(SM), Result(Str) { } + + // Inline content. + void visitTextComment(const TextComment *C); + void visitInlineCommandComment(const InlineCommandComment *C); + void visitHTMLStartTagComment(const HTMLStartTagComment *C); + void visitHTMLEndTagComment(const HTMLEndTagComment *C); + + // Block content. + void visitParagraphComment(const ParagraphComment *C); + void visitBlockCommandComment(const BlockCommandComment *C); + void visitParamCommandComment(const ParamCommandComment *C); + void visitTParamCommandComment(const TParamCommandComment *C); + void visitVerbatimBlockComment(const VerbatimBlockComment *C); + void visitVerbatimBlockLineComment(const VerbatimBlockLineComment *C); + void visitVerbatimLineComment(const VerbatimLineComment *C); + + void visitFullComment(const FullComment *C); + + // Helpers. + void appendToResultWithXMLEscaping(StringRef S); + +private: + const SourceManager &SM; + /// Output stream for XML. + llvm::raw_svector_ostream Result; +}; +} // end unnamed namespace + +void CommentASTToXMLConverter::visitTextComment(const TextComment *C) { + appendToResultWithXMLEscaping(C->getText()); +} + +void CommentASTToXMLConverter::visitInlineCommandComment(const InlineCommandComment *C) { + // Nothing to render if no arguments supplied. + if (C->getNumArgs() == 0) + return; + + // Nothing to render if argument is empty. + StringRef Arg0 = C->getArgText(0); + if (Arg0.empty()) + return; + + switch (C->getRenderKind()) { + case InlineCommandComment::RenderNormal: + for (unsigned i = 0, e = C->getNumArgs(); i != e; ++i) { + appendToResultWithXMLEscaping(C->getArgText(i)); + Result << " "; + } + return; + case InlineCommandComment::RenderBold: + assert(C->getNumArgs() == 1); + Result << "<bold>"; + appendToResultWithXMLEscaping(Arg0); + Result << "</bold>"; + return; + case InlineCommandComment::RenderMonospaced: + assert(C->getNumArgs() == 1); + Result << "<monospaced>"; + appendToResultWithXMLEscaping(Arg0); + Result << "</monospaced>"; + return; + case InlineCommandComment::RenderEmphasized: + assert(C->getNumArgs() == 1); + Result << "<emphasized>"; + appendToResultWithXMLEscaping(Arg0); + Result << "</emphasized>"; + return; + } +} + +void CommentASTToXMLConverter::visitHTMLStartTagComment(const HTMLStartTagComment *C) { + Result << "<rawHTML><![CDATA["; + PrintHTMLStartTagComment(C, Result); + Result << "]]></rawHTML>"; +} + +void CommentASTToXMLConverter::visitHTMLEndTagComment(const HTMLEndTagComment *C) { + Result << "<rawHTML></" << C->getTagName() << "></rawHTML>"; +} + +void CommentASTToXMLConverter::visitParagraphComment(const ParagraphComment *C) { + if (C->isWhitespace()) + return; + + Result << "<Para>"; + for (Comment::child_iterator I = C->child_begin(), E = C->child_end(); + I != E; ++I) { + visit(*I); + } + Result << "</Para>"; +} + +void CommentASTToXMLConverter::visitBlockCommandComment(const BlockCommandComment *C) { + visit(C->getParagraph()); +} + +void CommentASTToXMLConverter::visitParamCommandComment(const ParamCommandComment *C) { + Result << "<Parameter><Name>"; + appendToResultWithXMLEscaping(C->getParamName()); + Result << "</Name>"; + + if (C->isParamIndexValid()) + Result << "<Index>" << C->getParamIndex() << "</Index>"; + + Result << "<Direction isExplicit=\"" << C->isDirectionExplicit() << "\">"; + switch (C->getDirection()) { + case ParamCommandComment::In: + Result << "in"; + break; + case ParamCommandComment::Out: + Result << "out"; + break; + case ParamCommandComment::InOut: + Result << "in,out"; + break; + } + Result << "</Direction><Discussion>"; + visit(C->getParagraph()); + Result << "</Discussion></Parameter>"; +} + +void CommentASTToXMLConverter::visitTParamCommandComment( + const TParamCommandComment *C) { + Result << "<Parameter><Name>"; + appendToResultWithXMLEscaping(C->getParamName()); + Result << "</Name>"; + + if (C->isPositionValid() && C->getDepth() == 1) { + Result << "<Index>" << C->getIndex(0) << "</Index>"; + } + + Result << "<Discussion>"; + visit(C->getParagraph()); + Result << "</Discussion></Parameter>"; +} + +void CommentASTToXMLConverter::visitVerbatimBlockComment( + const VerbatimBlockComment *C) { + unsigned NumLines = C->getNumLines(); + if (NumLines == 0) + return; + + Result << llvm::StringSwitch<const char *>(C->getCommandName()) + .Case("code", "<Verbatim kind=\"code\">") + .Default("<Verbatim kind=\"verbatim\">"); + for (unsigned i = 0; i != NumLines; ++i) { + appendToResultWithXMLEscaping(C->getText(i)); + if (i + 1 != NumLines) + Result << '\n'; + } + Result << "</Verbatim>"; +} + +void CommentASTToXMLConverter::visitVerbatimBlockLineComment( + const VerbatimBlockLineComment *C) { + llvm_unreachable("should not see this AST node"); +} + +void CommentASTToXMLConverter::visitVerbatimLineComment( + const VerbatimLineComment *C) { + Result << "<Verbatim kind=\"verbatim\">"; + appendToResultWithXMLEscaping(C->getText()); + Result << "</Verbatim>"; +} + +void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { + FullCommentParts Parts(C); + + const DeclInfo *DI = C->getDeclInfo(); + StringRef RootEndTag; + if (DI) { + switch (DI->getKind()) { + case DeclInfo::OtherKind: + RootEndTag = "</Other>"; + Result << "<Other"; + break; + case DeclInfo::FunctionKind: + RootEndTag = "</Function>"; + Result << "<Function"; + switch (DI->TemplateKind) { + case DeclInfo::NotTemplate: + break; + case DeclInfo::Template: + Result << " templateKind=\"template\""; + break; + case DeclInfo::TemplateSpecialization: + Result << " templateKind=\"specialization\""; + break; + case DeclInfo::TemplatePartialSpecialization: + llvm_unreachable("partial specializations of functions " + "are not allowed in C++"); + } + if (DI->IsInstanceMethod) + Result << " isInstanceMethod=\"1\""; + if (DI->IsClassMethod) + Result << " isClassMethod=\"1\""; + break; + case DeclInfo::ClassKind: + RootEndTag = "</Class>"; + Result << "<Class"; + switch (DI->TemplateKind) { + case DeclInfo::NotTemplate: + break; + case DeclInfo::Template: + Result << " templateKind=\"template\""; + break; + case DeclInfo::TemplateSpecialization: + Result << " templateKind=\"specialization\""; + break; + case DeclInfo::TemplatePartialSpecialization: + Result << " templateKind=\"partialSpecialization\""; + break; + } + break; + case DeclInfo::VariableKind: + RootEndTag = "</Variable>"; + Result << "<Variable"; + break; + case DeclInfo::NamespaceKind: + RootEndTag = "</Namespace>"; + Result << "<Namespace"; + break; + case DeclInfo::TypedefKind: + RootEndTag = "</Typedef>"; + Result << "<Typedef"; + break; + } + + { + // Print line and column number. + SourceLocation Loc = DI->ThisDecl->getLocation(); + std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); + FileID FID = LocInfo.first; + unsigned FileOffset = LocInfo.second; + + if (!FID.isInvalid()) { + if (const FileEntry *FE = SM.getFileEntryForID(FID)) { + Result << " file=\""; + appendToResultWithXMLEscaping(FE->getName()); + Result << "\""; + } + Result << " line=\"" << SM.getLineNumber(FID, FileOffset) + << "\" column=\"" << SM.getColumnNumber(FID, FileOffset) + << "\""; + } + } + + // Finish the root tag. + Result << ">"; + + bool FoundName = false; + if (const NamedDecl *ND = dyn_cast<NamedDecl>(DI->ThisDecl)) { + if (DeclarationName DeclName = ND->getDeclName()) { + Result << "<Name>"; + std::string Name = DeclName.getAsString(); + appendToResultWithXMLEscaping(Name); + FoundName = true; + Result << "</Name>"; + } + } + if (!FoundName) + Result << "<Name><anonymous></Name>"; + + { + // Print USR. + SmallString<128> USR; + cxcursor::getDeclCursorUSR(DI->ThisDecl, USR); + if (!USR.empty()) { + Result << "<USR>"; + appendToResultWithXMLEscaping(USR); + Result << "</USR>"; + } + } + } else { + // No DeclInfo -- just emit some root tag and name tag. + RootEndTag = "</Other>"; + Result << "<Other><Name>unknown</Name>"; + } + + bool FirstParagraphIsBrief = false; + if (Parts.Brief) { + Result << "<Abstract>"; + visit(Parts.Brief); + Result << "</Abstract>"; + } else if (Parts.FirstParagraph) { + Result << "<Abstract>"; + visit(Parts.FirstParagraph); + Result << "</Abstract>"; + FirstParagraphIsBrief = true; + } + + if (Parts.TParams.size() != 0) { + Result << "<TemplateParameters>"; + for (unsigned i = 0, e = Parts.TParams.size(); i != e; ++i) + visit(Parts.TParams[i]); + Result << "</TemplateParameters>"; + } + + if (Parts.Params.size() != 0) { + Result << "<Parameters>"; + for (unsigned i = 0, e = Parts.Params.size(); i != e; ++i) + visit(Parts.Params[i]); + Result << "</Parameters>"; + } + + if (Parts.Returns) { + Result << "<ResultDiscussion>"; + visit(Parts.Returns); + Result << "</ResultDiscussion>"; + } + + { + bool StartTagEmitted = false; + for (unsigned i = 0, e = Parts.MiscBlocks.size(); i != e; ++i) { + const Comment *C = Parts.MiscBlocks[i]; + if (FirstParagraphIsBrief && C == Parts.FirstParagraph) + continue; + if (!StartTagEmitted) { + Result << "<Discussion>"; + StartTagEmitted = true; + } + visit(C); + } + if (StartTagEmitted) + Result << "</Discussion>"; + } + + Result << RootEndTag; + + Result.flush(); +} + +void CommentASTToXMLConverter::appendToResultWithXMLEscaping(StringRef S) { + for (StringRef::iterator I = S.begin(), E = S.end(); I != E; ++I) { + const char C = *I; + switch (C) { + case '&': + Result << "&"; + break; + case '<': + Result << "<"; + break; + case '>': + Result << ">"; + break; + case '"': + Result << """; + break; + case '\'': + Result << "'"; + break; + default: + Result << C; + break; + } + } +} + +extern "C" { + +CXString clang_FullComment_getAsXML(CXTranslationUnit TU, CXComment CXC) { + const FullComment *FC = getASTNodeAs<FullComment>(CXC); + if (!FC) + return createCXString((const char *) 0); + + SourceManager &SM = static_cast<ASTUnit *>(TU->TUData)->getSourceManager(); + + SmallString<1024> XML; + CommentASTToXMLConverter Converter(SM, XML); + Converter.visit(FC); + return createCXString(XML.str(), /* DupString = */ true); +} + +} // end extern "C" + diff --git a/tools/libclang/libclang.exports b/tools/libclang/libclang.exports index d796b154ad..610bd91c6a 100644 --- a/tools/libclang/libclang.exports +++ b/tools/libclang/libclang.exports @@ -50,6 +50,7 @@ clang_VerbatimBlockLineComment_getText clang_VerbatimLineComment_getText clang_HTMLTagComment_getAsString clang_FullComment_getAsHTML +clang_FullComment_getAsXML clang_annotateTokens clang_codeCompleteAt clang_codeCompleteGetContainerKind |