CommentTextTest.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. //===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // Tests for user-friendly output formatting of comments, i.e.
  10. // RawComment::getFormattedText().
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "clang/AST/RawCommentList.h"
  14. #include "clang/Basic/CommentOptions.h"
  15. #include "clang/Basic/Diagnostic.h"
  16. #include "clang/Basic/DiagnosticIDs.h"
  17. #include "clang/Basic/FileManager.h"
  18. #include "clang/Basic/FileSystemOptions.h"
  19. #include "clang/Basic/SourceLocation.h"
  20. #include "clang/Basic/SourceManager.h"
  21. #include "llvm/Support/MemoryBuffer.h"
  22. #include "llvm/Support/VirtualFileSystem.h"
  23. #include <gtest/gtest.h>
  24. namespace clang {
  25. class CommentTextTest : public ::testing::Test {
  26. protected:
  27. std::string formatComment(llvm::StringRef CommentText) {
  28. SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);
  29. SourceManager& SourceMgr = FileSourceMgr.get();
  30. auto CommentStartOffset = CommentText.find("/");
  31. assert(CommentStartOffset != llvm::StringRef::npos);
  32. FileID File = SourceMgr.getMainFileID();
  33. SourceRange CommentRange(
  34. SourceMgr.getLocForStartOfFile(File).getLocWithOffset(
  35. CommentStartOffset),
  36. SourceMgr.getLocForEndOfFile(File));
  37. CommentOptions EmptyOpts;
  38. // FIXME: technically, merged that we set here is incorrect, but that
  39. // shouldn't matter.
  40. RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);
  41. DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions);
  42. return Comment.getFormattedText(SourceMgr, Diags);
  43. }
  44. };
  45. TEST_F(CommentTextTest, FormattedText) {
  46. // clang-format off
  47. auto ExpectedOutput =
  48. R"(This function does this and that.
  49. For example,
  50. Runnning it in that case will give you
  51. this result.
  52. That's about it.)";
  53. // Two-slash comments.
  54. auto Formatted = formatComment(
  55. R"cpp(
  56. // This function does this and that.
  57. // For example,
  58. // Runnning it in that case will give you
  59. // this result.
  60. // That's about it.)cpp");
  61. EXPECT_EQ(ExpectedOutput, Formatted);
  62. // Three-slash comments.
  63. Formatted = formatComment(
  64. R"cpp(
  65. /// This function does this and that.
  66. /// For example,
  67. /// Runnning it in that case will give you
  68. /// this result.
  69. /// That's about it.)cpp");
  70. EXPECT_EQ(ExpectedOutput, Formatted);
  71. // Block comments.
  72. Formatted = formatComment(
  73. R"cpp(
  74. /* This function does this and that.
  75. * For example,
  76. * Runnning it in that case will give you
  77. * this result.
  78. * That's about it.*/)cpp");
  79. EXPECT_EQ(ExpectedOutput, Formatted);
  80. // Doxygen-style block comments.
  81. Formatted = formatComment(
  82. R"cpp(
  83. /** This function does this and that.
  84. * For example,
  85. * Runnning it in that case will give you
  86. * this result.
  87. * That's about it.*/)cpp");
  88. EXPECT_EQ(ExpectedOutput, Formatted);
  89. // Weird indentation.
  90. Formatted = formatComment(
  91. R"cpp(
  92. // This function does this and that.
  93. // For example,
  94. // Runnning it in that case will give you
  95. // this result.
  96. // That's about it.)cpp");
  97. EXPECT_EQ(ExpectedOutput, Formatted);
  98. // clang-format on
  99. }
  100. TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {
  101. // clang-format off
  102. auto ExpectedOutput =
  103. R"(\brief This is the brief part of the comment.
  104. \param a something about a.
  105. @param b something about b.)";
  106. auto Formatted = formatComment(
  107. R"cpp(
  108. /// \brief This is the brief part of the comment.
  109. /// \param a something about a.
  110. /// @param b something about b.)cpp");
  111. EXPECT_EQ(ExpectedOutput, Formatted);
  112. // clang-format on
  113. }
  114. } // namespace clang