diff options
author | dim <dim@FreeBSD.org> | 2012-12-02 13:20:44 +0000 |
---|---|---|
committer | dim <dim@FreeBSD.org> | 2012-12-02 13:20:44 +0000 |
commit | 056abd2059c65a3e908193aeae16fad98017437c (patch) | |
tree | 2732d02d7d51218d6eed98ac7fcfc5b8794896b5 /tools/libclang/CXComment.cpp | |
parent | cc73504950eb7b5dff2dded9bedd67bc36d64641 (diff) | |
download | FreeBSD-src-056abd2059c65a3e908193aeae16fad98017437c.zip FreeBSD-src-056abd2059c65a3e908193aeae16fad98017437c.tar.gz |
Vendor import of clang release_32 branch r168974 (effectively, 3.2 RC2):
http://llvm.org/svn/llvm-project/cfe/branches/release_32@168974
Diffstat (limited to 'tools/libclang/CXComment.cpp')
-rw-r--r-- | tools/libclang/CXComment.cpp | 197 |
1 files changed, 152 insertions, 45 deletions
diff --git a/tools/libclang/CXComment.cpp b/tools/libclang/CXComment.cpp index c5c9ca8..fa149a0 100644 --- a/tools/libclang/CXComment.cpp +++ b/tools/libclang/CXComment.cpp @@ -15,12 +15,11 @@ #include "CXString.h" #include "CXComment.h" #include "CXCursor.h" -#include "CXTranslationUnit.h" +#include "clang/AST/PrettyPrinter.h" #include "clang/AST/CommentVisitor.h" #include "clang/AST/CommentCommandTraits.h" #include "clang/AST/Decl.h" -#include "clang/Frontend/ASTUnit.h" #include "llvm/ADT/StringSwitch.h" #include "llvm/Support/ErrorHandling.h" @@ -94,9 +93,9 @@ unsigned clang_Comment_getNumChildren(CXComment CXC) { CXComment clang_Comment_getChild(CXComment CXC, unsigned ChildIdx) { const Comment *C = getASTNode(CXC); if (!C || ChildIdx >= C->child_count()) - return createCXComment(NULL); + return createCXComment(NULL, NULL); - return createCXComment(*(C->child_begin() + ChildIdx)); + return createCXComment(*(C->child_begin() + ChildIdx), CXC.TranslationUnit); } unsigned clang_Comment_isWhitespace(CXComment CXC) { @@ -134,7 +133,8 @@ CXString clang_InlineCommandComment_getCommandName(CXComment CXC) { if (!ICC) return createCXString((const char *) 0); - return createCXString(ICC->getCommandName(), /*DupString=*/ false); + const CommandTraits &Traits = getCommandTraits(CXC); + return createCXString(ICC->getCommandName(Traits), /*DupString=*/ false); } enum CXCommentInlineCommandRenderKind @@ -221,7 +221,8 @@ CXString clang_BlockCommandComment_getCommandName(CXComment CXC) { if (!BCC) return createCXString((const char *) 0); - return createCXString(BCC->getCommandName(), /*DupString=*/ false); + const CommandTraits &Traits = getCommandTraits(CXC); + return createCXString(BCC->getCommandName(Traits), /*DupString=*/ false); } unsigned clang_BlockCommandComment_getNumArgs(CXComment CXC) { @@ -244,9 +245,9 @@ CXString clang_BlockCommandComment_getArgText(CXComment CXC, CXComment clang_BlockCommandComment_getParagraph(CXComment CXC) { const BlockCommandComment *BCC = getASTNodeAs<BlockCommandComment>(CXC); if (!BCC) - return createCXComment(NULL); + return createCXComment(NULL, NULL); - return createCXComment(BCC->getParagraph()); + return createCXComment(BCC->getParagraph(), CXC.TranslationUnit); } CXString clang_ParamCommandComment_getParamName(CXComment CXC) { @@ -254,7 +255,7 @@ CXString clang_ParamCommandComment_getParamName(CXComment CXC) { if (!PCC || !PCC->hasParamName()) return createCXString((const char *) 0); - return createCXString(PCC->getParamName(), /*DupString=*/ false); + return createCXString(PCC->getParamNameAsWritten(), /*DupString=*/ false); } unsigned clang_ParamCommandComment_isParamIndexValid(CXComment CXC) { @@ -305,7 +306,7 @@ CXString clang_TParamCommandComment_getParamName(CXComment CXC) { if (!TPCC || !TPCC->hasParamName()) return createCXString((const char *) 0); - return createCXString(TPCC->getParamName(), /*DupString=*/ false); + return createCXString(TPCC->getParamNameAsWritten(), /*DupString=*/ false); } unsigned clang_TParamCommandComment_isParamPositionValid(CXComment CXC) { @@ -405,7 +406,8 @@ public: /// Separate parts of a FullComment. struct FullCommentParts { /// Take a full comment apart and initialize members accordingly. - FullCommentParts(const FullComment *C); + FullCommentParts(const FullComment *C, + const CommandTraits &Traits); const BlockContentComment *Brief; const ParagraphComment *FirstParagraph; @@ -415,9 +417,9 @@ struct FullCommentParts { SmallVector<const BlockContentComment *, 8> MiscBlocks; }; -FullCommentParts::FullCommentParts(const FullComment *C) : +FullCommentParts::FullCommentParts(const FullComment *C, + const CommandTraits &Traits) : Brief(NULL), FirstParagraph(NULL), Returns(NULL) { - const CommandTraits Traits; for (Comment::child_iterator I = C->child_begin(), E = C->child_end(); I != E; ++I) { const Comment *Child = *I; @@ -440,12 +442,12 @@ FullCommentParts::FullCommentParts(const FullComment *C) : case Comment::BlockCommandCommentKind: { const BlockCommandComment *BCC = cast<BlockCommandComment>(Child); - StringRef CommandName = BCC->getCommandName(); - if (!Brief && Traits.isBriefCommand(CommandName)) { + const CommandInfo *Info = Traits.getCommandInfo(BCC->getCommandID()); + if (!Brief && Info->IsBriefCommand) { Brief = BCC; break; } - if (!Returns && Traits.isReturnsCommand(CommandName)) { + if (!Returns && Info->IsReturnsCommand) { Returns = BCC; break; } @@ -483,7 +485,8 @@ FullCommentParts::FullCommentParts(const FullComment *C) : case Comment::VerbatimLineCommentKind: { const VerbatimLineComment *VLC = cast<VerbatimLineComment>(Child); - if (!Traits.isDeclarationCommand(VLC->getCommandName())) + const CommandInfo *Info = Traits.getCommandInfo(VLC->getCommandID()); + if (!Info->IsDeclarationCommand) MiscBlocks.push_back(VLC); break; } @@ -533,7 +536,11 @@ class CommentASTToHTMLConverter : public ConstCommentVisitor<CommentASTToHTMLConverter> { public: /// \param Str accumulator for HTML. - CommentASTToHTMLConverter(SmallVectorImpl<char> &Str) : Result(Str) { } + CommentASTToHTMLConverter(const FullComment *FC, + SmallVectorImpl<char> &Str, + const CommandTraits &Traits) : + FC(FC), Result(Str), Traits(Traits) + { } // Inline content. void visitTextComment(const TextComment *C); @@ -561,10 +568,11 @@ public: void appendToResultWithHTMLEscaping(StringRef S); private: - const CommandTraits Traits; - + const FullComment *FC; /// Output stream for HTML. llvm::raw_svector_ostream Result; + + const CommandTraits &Traits; }; } // end unnamed namespace @@ -637,14 +645,14 @@ void CommentASTToHTMLConverter::visitParagraphComment( void CommentASTToHTMLConverter::visitBlockCommandComment( const BlockCommandComment *C) { - StringRef CommandName = C->getCommandName(); - if (Traits.isBriefCommand(CommandName)) { + const CommandInfo *Info = Traits.getCommandInfo(C->getCommandID()); + if (Info->IsBriefCommand) { Result << "<p class=\"para-brief\">"; visitNonStandaloneParagraphComment(C->getParagraph()); Result << "</p>"; return; } - if (Traits.isReturnsCommand(CommandName)) { + if (Info->IsReturnsCommand) { Result << "<p class=\"para-returns\">" "<span class=\"word-returns\">Returns</span> "; visitNonStandaloneParagraphComment(C->getParagraph()); @@ -661,10 +669,11 @@ void CommentASTToHTMLConverter::visitParamCommandComment( Result << "<dt class=\"param-name-index-" << C->getParamIndex() << "\">"; - } else + appendToResultWithHTMLEscaping(C->getParamName(FC)); + } else { Result << "<dt class=\"param-name-index-invalid\">"; - - appendToResultWithHTMLEscaping(C->getParamName()); + appendToResultWithHTMLEscaping(C->getParamNameAsWritten()); + } Result << "</dt>"; if (C->isParamIndexValid()) { @@ -687,10 +696,12 @@ void CommentASTToHTMLConverter::visitTParamCommandComment( << "\">"; else Result << "<dt class=\"tparam-name-index-other\">"; - } else + appendToResultWithHTMLEscaping(C->getParamName(FC)); + } else { Result << "<dt class=\"tparam-name-index-invalid\">"; - - appendToResultWithHTMLEscaping(C->getParamName()); + appendToResultWithHTMLEscaping(C->getParamNameAsWritten()); + } + Result << "</dt>"; if (C->isPositionValid()) { @@ -735,7 +746,7 @@ void CommentASTToHTMLConverter::visitVerbatimLineComment( } void CommentASTToHTMLConverter::visitFullComment(const FullComment *C) { - FullCommentParts Parts(C); + FullCommentParts Parts(C, Traits); bool FirstParagraphIsBrief = false; if (Parts.Brief) @@ -822,7 +833,7 @@ CXString clang_HTMLTagComment_getAsString(CXComment CXC) { return createCXString((const char *) 0); SmallString<128> HTML; - CommentASTToHTMLConverter Converter(HTML); + CommentASTToHTMLConverter Converter(0, HTML, getCommandTraits(CXC)); Converter.visit(HTC); return createCXString(HTML.str(), /* DupString = */ true); } @@ -833,7 +844,7 @@ CXString clang_FullComment_getAsHTML(CXComment CXC) { return createCXString((const char *) 0); SmallString<1024> HTML; - CommentASTToHTMLConverter Converter(HTML); + CommentASTToHTMLConverter Converter(FC, HTML, getCommandTraits(CXC)); Converter.visit(FC); return createCXString(HTML.str(), /* DupString = */ true); } @@ -845,9 +856,11 @@ class CommentASTToXMLConverter : public ConstCommentVisitor<CommentASTToXMLConverter> { public: /// \param Str accumulator for XML. - CommentASTToXMLConverter(const SourceManager &SM, - SmallVectorImpl<char> &Str) : - SM(SM), Result(Str) { } + CommentASTToXMLConverter(const FullComment *FC, + SmallVectorImpl<char> &Str, + const CommandTraits &Traits, + const SourceManager &SM) : + FC(FC), Result(Str), Traits(Traits), SM(SM) { } // Inline content. void visitTextComment(const TextComment *C); @@ -870,11 +883,27 @@ public: void appendToResultWithXMLEscaping(StringRef S); private: - const SourceManager &SM; + const FullComment *FC; /// Output stream for XML. llvm::raw_svector_ostream Result; + + const CommandTraits &Traits; + const SourceManager &SM; }; + +void getSourceTextOfDeclaration(const DeclInfo *ThisDecl, + SmallVectorImpl<char> &Str) { + ASTContext &Context = ThisDecl->CurrentDecl->getASTContext(); + const LangOptions &LangOpts = Context.getLangOpts(); + llvm::raw_svector_ostream OS(Str); + PrintingPolicy PPolicy(LangOpts); + PPolicy.SuppressAttributes = true; + PPolicy.TerseOutput = true; + ThisDecl->CurrentDecl->print(OS, PPolicy, + /*Indentation*/0, /*PrintInstantiation*/true); +} + } // end unnamed namespace void CommentASTToXMLConverter::visitTextComment(const TextComment *C) { @@ -947,7 +976,8 @@ void CommentASTToXMLConverter::visitBlockCommandComment(const BlockCommandCommen void CommentASTToXMLConverter::visitParamCommandComment(const ParamCommandComment *C) { Result << "<Parameter><Name>"; - appendToResultWithXMLEscaping(C->getParamName()); + appendToResultWithXMLEscaping(C->isParamIndexValid() ? C->getParamName(FC) + : C->getParamNameAsWritten()); Result << "</Name>"; if (C->isParamIndexValid()) @@ -973,7 +1003,8 @@ void CommentASTToXMLConverter::visitParamCommandComment(const ParamCommandCommen void CommentASTToXMLConverter::visitTParamCommandComment( const TParamCommandComment *C) { Result << "<Parameter><Name>"; - appendToResultWithXMLEscaping(C->getParamName()); + appendToResultWithXMLEscaping(C->isPositionValid() ? C->getParamName(FC) + : C->getParamNameAsWritten()); Result << "</Name>"; if (C->isPositionValid() && C->getDepth() == 1) { @@ -991,7 +1022,7 @@ void CommentASTToXMLConverter::visitVerbatimBlockComment( if (NumLines == 0) return; - Result << llvm::StringSwitch<const char *>(C->getCommandName()) + Result << llvm::StringSwitch<const char *>(C->getCommandName(Traits)) .Case("code", "<Verbatim xml:space=\"preserve\" kind=\"code\">") .Default("<Verbatim xml:space=\"preserve\" kind=\"verbatim\">"); for (unsigned i = 0; i != NumLines; ++i) { @@ -1015,7 +1046,7 @@ void CommentASTToXMLConverter::visitVerbatimLineComment( } void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { - FullCommentParts Parts(C); + FullCommentParts Parts(C, Traits); const DeclInfo *DI = C->getDeclInfo(); StringRef RootEndTag; @@ -1083,7 +1114,7 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { { // Print line and column number. - SourceLocation Loc = DI->ThisDecl->getLocation(); + SourceLocation Loc = DI->CurrentDecl->getLocation(); std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); FileID FID = LocInfo.first; unsigned FileOffset = LocInfo.second; @@ -1104,7 +1135,7 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { Result << ">"; bool FoundName = false; - if (const NamedDecl *ND = dyn_cast<NamedDecl>(DI->ThisDecl)) { + if (const NamedDecl *ND = dyn_cast<NamedDecl>(DI->CommentDecl)) { if (DeclarationName DeclName = ND->getDeclName()) { Result << "<Name>"; std::string Name = DeclName.getAsString(); @@ -1119,7 +1150,7 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { { // Print USR. SmallString<128> USR; - cxcursor::getDeclCursorUSR(DI->ThisDecl, USR); + cxcursor::getDeclCursorUSR(DI->CommentDecl, USR); if (!USR.empty()) { Result << "<USR>"; appendToResultWithXMLEscaping(USR); @@ -1132,6 +1163,15 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { Result << "<Other><Name>unknown</Name>"; } + { + // Pretty-print the declaration. + Result << "<Declaration>"; + SmallString<128> Declaration; + getSourceTextOfDeclaration(DI, Declaration); + appendToResultWithXMLEscaping(Declaration); + Result << "</Declaration>"; + } + bool FirstParagraphIsBrief = false; if (Parts.Brief) { Result << "<Abstract>"; @@ -1163,6 +1203,72 @@ void CommentASTToXMLConverter::visitFullComment(const FullComment *C) { visit(Parts.Returns); Result << "</ResultDiscussion>"; } + + if (DI->CommentDecl->hasAttrs()) { + const AttrVec &Attrs = DI->CommentDecl->getAttrs(); + for (unsigned i = 0, e = Attrs.size(); i != e; i++) { + const AvailabilityAttr *AA = dyn_cast<AvailabilityAttr>(Attrs[i]); + if (!AA) { + if (const DeprecatedAttr *DA = dyn_cast<DeprecatedAttr>(Attrs[i])) { + if (DA->getMessage().empty()) + Result << "<Deprecated/>"; + else { + Result << "<Deprecated>"; + appendToResultWithXMLEscaping(DA->getMessage()); + Result << "</Deprecated>"; + } + } + else if (const UnavailableAttr *UA = dyn_cast<UnavailableAttr>(Attrs[i])) { + if (UA->getMessage().empty()) + Result << "<Unavailable/>"; + else { + Result << "<Unavailable>"; + appendToResultWithXMLEscaping(UA->getMessage()); + Result << "</Unavailable>"; + } + } + continue; + } + + // 'availability' attribute. + Result << "<Availability"; + StringRef Distribution; + if (AA->getPlatform()) { + Distribution = AvailabilityAttr::getPrettyPlatformName( + AA->getPlatform()->getName()); + if (Distribution.empty()) + Distribution = AA->getPlatform()->getName(); + } + Result << " distribution=\"" << Distribution << "\">"; + VersionTuple IntroducedInVersion = AA->getIntroduced(); + if (!IntroducedInVersion.empty()) { + Result << "<IntroducedInVersion>" + << IntroducedInVersion.getAsString() + << "</IntroducedInVersion>"; + } + VersionTuple DeprecatedInVersion = AA->getDeprecated(); + if (!DeprecatedInVersion.empty()) { + Result << "<DeprecatedInVersion>" + << DeprecatedInVersion.getAsString() + << "</DeprecatedInVersion>"; + } + VersionTuple RemovedAfterVersion = AA->getObsoleted(); + if (!RemovedAfterVersion.empty()) { + Result << "<RemovedAfterVersion>" + << RemovedAfterVersion.getAsString() + << "</RemovedAfterVersion>"; + } + StringRef DeprecationSummary = AA->getMessage(); + if (!DeprecationSummary.empty()) { + Result << "<DeprecationSummary>"; + appendToResultWithXMLEscaping(DeprecationSummary); + Result << "</DeprecationSummary>"; + } + if (AA->getUnavailable()) + Result << "<Unavailable/>"; + Result << "</Availability>"; + } + } { bool StartTagEmitted = false; @@ -1213,15 +1319,16 @@ void CommentASTToXMLConverter::appendToResultWithXMLEscaping(StringRef S) { extern "C" { -CXString clang_FullComment_getAsXML(CXTranslationUnit TU, CXComment CXC) { +CXString clang_FullComment_getAsXML(CXComment CXC) { const FullComment *FC = getASTNodeAs<FullComment>(CXC); if (!FC) return createCXString((const char *) 0); + CXTranslationUnit TU = CXC.TranslationUnit; SourceManager &SM = static_cast<ASTUnit *>(TU->TUData)->getSourceManager(); SmallString<1024> XML; - CommentASTToXMLConverter Converter(SM, XML); + CommentASTToXMLConverter Converter(FC, XML, getCommandTraits(CXC), SM); Converter.visit(FC); return createCXString(XML.str(), /* DupString = */ true); } |