Index: clangd/ClangdUnit.cpp =================================================================== --- clangd/ClangdUnit.cpp +++ clangd/ClangdUnit.cpp @@ -25,6 +25,7 @@ #include "clang/Sema/Sema.h" #include "clang/Serialization/ASTWriter.h" #include "clang/Tooling/CompilationDatabase.h" +#include "clang/Basic/LangOptions.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/SmallVector.h" #include "llvm/Support/CrashRecoveryContext.h" @@ -358,6 +359,7 @@ } // createInvocationFromCommandLine sets DisableFree. CI->getFrontendOpts().DisableFree = false; + CI->getLangOpts()->CommentOpts.ParseAllComments = true; } std::unique_ptr ContentsBuffer = Index: clangd/CodeComplete.cpp =================================================================== --- clangd/CodeComplete.cpp +++ clangd/CodeComplete.cpp @@ -22,6 +22,7 @@ #include "SourceCode.h" #include "Trace.h" #include "index/Index.h" +#include "clang/Basic/LangOptions.h" #include "clang/Format/Format.h" #include "clang/Frontend/CompilerInstance.h" #include "clang/Frontend/FrontendActions.h" @@ -703,6 +704,7 @@ return false; } CI->getFrontendOpts().DisableFree = false; + CI->getLangOpts()->CommentOpts.ParseAllComments = true; std::unique_ptr ContentsBuffer = llvm::MemoryBuffer::getMemBufferCopy(Input.Contents, Input.FileName); Index: unittests/clangd/CodeCompleteTests.cpp =================================================================== --- unittests/clangd/CodeCompleteTests.cpp +++ unittests/clangd/CodeCompleteTests.cpp @@ -17,6 +17,7 @@ #include "SyncAPI.h" #include "TestFS.h" #include "index/MemIndex.h" +#include "llvm/ADT/StringRef.h" #include "gmock/gmock.h" #include "gtest/gtest.h" @@ -808,6 +809,74 @@ UnorderedElementsAre("")))); } +TEST(CompletionTest, Comments) { + auto ExtractComment = [](llvm::StringRef CommentText) -> std::string { + std::string CompletionSource = CommentText; + CompletionSource += "\n"; + CompletionSource += "void foobarbaz() { foobarbaz^ }"; + + auto Completions = completions(CompletionSource).items; + assert(Completions.size() == 1); + return Completions.front().documentation; + }; + + // FIXME: this code should be a unit-test instead for + // RawComment::getFormattedText instead. + + // clang-format off + auto ExpectedOutput = +R"(This function does this and that. +For example, + Runnning it in that case will give you + this result. +That's about it.)"; + // Two-slash comments. + EXPECT_EQ(ExpectedOutput, ExtractComment( +R"cpp( +// This function does this and that. +// For example, +// Runnning it in that case will give you +// this result. +// That's about it.)cpp")); + + // Three-slash comments. + EXPECT_EQ(ExpectedOutput, ExtractComment( +R"cpp( +/// This function does this and that. +/// For example, +/// Runnning it in that case will give you +/// this result. +/// That's about it.)cpp")); + + // Block comments. + EXPECT_EQ(ExpectedOutput, ExtractComment( +R"cpp( +/* This function does this and that. + * For example, + * Runnning it in that case will give you + * this result. + * That's about it.*/)cpp")); + + // Doxygen-style block comments. + EXPECT_EQ(ExpectedOutput, ExtractComment( +R"cpp( +/** This function does this and that. + * For example, + * Runnning it in that case will give you + * this result. + * That's about it.*/)cpp")); + + // Weird indentation. + EXPECT_EQ(ExpectedOutput, ExtractComment( +R"cpp( + // This function does this and that. + // For example, + // Runnning it in that case will give you + // this result. + // That's about it.)cpp")); + // clang-format on +} + } // namespace } // namespace clangd } // namespace clang