diff --git a/clang-tools-extra/clangd/CodeCompletionStrings.h b/clang-tools-extra/clangd/CodeCompletionStrings.h --- a/clang-tools-extra/clangd/CodeCompletionStrings.h +++ b/clang-tools-extra/clangd/CodeCompletionStrings.h @@ -19,6 +19,11 @@ namespace clang { class ASTContext; +namespace comments { +class CommandTraits; +class FullComment; +} // namespace comments + namespace clangd { /// Gets a minimally formatted documentation comment of \p Result, with comment @@ -61,6 +66,12 @@ /// is usually the return type of a function. std::string getReturnType(const CodeCompletionString &CCS); +/// Parse the \p Comment, storing the result in \p Allocator, assuming +/// that comment markers have already been stripped (e.g. via getDocComment()) +comments::FullComment *parseComment(llvm::StringRef Comment, + llvm::BumpPtrAllocator &Allocator, + comments::CommandTraits &Traits); + } // namespace clangd } // namespace clang diff --git a/clang-tools-extra/clangd/CodeCompletionStrings.cpp b/clang-tools-extra/clangd/CodeCompletionStrings.cpp --- a/clang-tools-extra/clangd/CodeCompletionStrings.cpp +++ b/clang-tools-extra/clangd/CodeCompletionStrings.cpp @@ -8,6 +8,9 @@ #include "CodeCompletionStrings.h" #include "clang/AST/ASTContext.h" +#include "clang/AST/CommentLexer.h" +#include "clang/AST/CommentParser.h" +#include "clang/AST/CommentSema.h" #include "clang/AST/RawCommentList.h" #include "clang/Basic/SourceManager.h" #include "clang/Sema/CodeCompleteConsumer.h" @@ -279,5 +282,26 @@ return ""; } +comments::FullComment *parseComment(llvm::StringRef Comment, + llvm::BumpPtrAllocator &Allocator, + comments::CommandTraits &Traits) { + // The comment lexer expects markers, so add them back + auto CommentWithMarkers = "/*" + Comment.str() + "*/"; + + SourceManagerForFile SourceMgrForFile("mock_file.cpp", CommentWithMarkers); + SourceManager &SourceMgr = SourceMgrForFile.get(); + + comments::Lexer L(Allocator, SourceMgr.getDiagnostics(), Traits, + SourceMgr.getLocForStartOfFile(SourceMgr.getMainFileID()), + CommentWithMarkers.data(), + CommentWithMarkers.data() + CommentWithMarkers.size()); + comments::Sema S(Allocator, SourceMgr, SourceMgr.getDiagnostics(), Traits, + nullptr); + comments::Parser P(L, S, Allocator, SourceMgr, SourceMgr.getDiagnostics(), + Traits); + + return P.parseFullComment(); +} + } // namespace clangd } // namespace clang diff --git a/clang/include/clang/Basic/SourceManager.h b/clang/include/clang/Basic/SourceManager.h --- a/clang/include/clang/Basic/SourceManager.h +++ b/clang/include/clang/Basic/SourceManager.h @@ -1944,7 +1944,7 @@ }; /// SourceManager and necessary dependencies (e.g. VFS, FileManager) for a -/// single in-memorty file. +/// single in-memory file. class SourceManagerForFile { public: /// Creates SourceManager and necessary dependencies (e.g. VFS, FileManager).