summaryrefslogtreecommitdiff
path: root/unittests/AST/CommentTextTest.cpp
diff options
context:
space:
mode:
authorIlya Biryukov <ibiryukov@google.com>2018-05-16 12:30:09 +0000
committerIlya Biryukov <ibiryukov@google.com>2018-05-16 12:30:09 +0000
commitbfc7cfab82c332ec0070bf8cfb2c24359bef640d (patch)
tree00280f2b6232b877cd3ac843373a21f4080c2ef9 /unittests/AST/CommentTextTest.cpp
parent983ac4e991973db70b0e0ef336baa525f293ed99 (diff)
downloadclang-bfc7cfab82c332ec0070bf8cfb2c24359bef640d.tar.gz
[AST] Added a helper to extract a user-friendly text of a comment.
Summary: The helper is used in clangd for documentation shown in code completion and storing the docs in the symbols. See D45999. This patch reuses the code of the Doxygen comment lexer, disabling the bits that do command and html tag parsing. The new helper works on all comments, including non-doxygen comments. However, it does not understand or transform any doxygen directives, i.e. cannot extract brief text, etc. Reviewers: sammccall, hokein, ioeric Reviewed By: ioeric Subscribers: mgorny, cfe-commits Differential Revision: https://reviews.llvm.org/D46000 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@332458 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests/AST/CommentTextTest.cpp')
-rw-r--r--unittests/AST/CommentTextTest.cpp122
1 files changed, 122 insertions, 0 deletions
diff --git a/unittests/AST/CommentTextTest.cpp b/unittests/AST/CommentTextTest.cpp
new file mode 100644
index 0000000000..05003b599e
--- /dev/null
+++ b/unittests/AST/CommentTextTest.cpp
@@ -0,0 +1,122 @@
+//===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Tests for user-friendly output formatting of comments, i.e.
+// RawComment::getFormattedText().
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/AST/RawCommentList.h"
+#include "clang/Basic/CommentOptions.h"
+#include "clang/Basic/Diagnostic.h"
+#include "clang/Basic/DiagnosticIDs.h"
+#include "clang/Basic/FileManager.h"
+#include "clang/Basic/FileSystemOptions.h"
+#include "clang/Basic/SourceLocation.h"
+#include "clang/Basic/SourceManager.h"
+#include "clang/Basic/VirtualFileSystem.h"
+#include "llvm/Support/MemoryBuffer.h"
+#include <gtest/gtest.h>
+
+namespace clang {
+
+class CommentTextTest : public ::testing::Test {
+protected:
+ std::string formatComment(llvm::StringRef CommentText) {
+ SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);
+ SourceManager& SourceMgr = FileSourceMgr.get();
+
+ auto CommentStartOffset = CommentText.find("/");
+ assert(CommentStartOffset != llvm::StringRef::npos);
+ FileID File = SourceMgr.getMainFileID();
+
+ SourceRange CommentRange(
+ SourceMgr.getLocForStartOfFile(File).getLocWithOffset(
+ CommentStartOffset),
+ SourceMgr.getLocForEndOfFile(File));
+ CommentOptions EmptyOpts;
+ // FIXME: technically, merged that we set here is incorrect, but that
+ // shouldn't matter.
+ RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);
+ DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions);
+ return Comment.getFormattedText(SourceMgr, Diags);
+ }
+};
+
+TEST_F(CommentTextTest, FormattedText) {
+ // 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, formatComment(
+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, formatComment(
+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, formatComment(
+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, formatComment(
+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, formatComment(
+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
+}
+
+TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {
+ // clang-format off
+ auto ExpectedOutput =
+R"(\brief This is the brief part of the comment.
+\param a something about a.
+@param b something about b.)";
+
+ EXPECT_EQ(ExpectedOutput, formatComment(
+R"cpp(
+/// \brief This is the brief part of the comment.
+/// \param a something about a.
+/// @param b something about b.)cpp"));
+ // clang-format on
+}
+
+} // namespace clang