| 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 | |
| 14 | #include "clang/AST/RawCommentList.h" |
| 15 | #include "clang/Basic/CommentOptions.h" |
| 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | #include "clang/Basic/DiagnosticIDs.h" |
| 18 | #include "clang/Basic/FileManager.h" |
| 19 | #include "clang/Basic/FileSystemOptions.h" |
| 20 | #include "clang/Basic/SourceLocation.h" |
| 21 | #include "clang/Basic/SourceManager.h" |
| 22 | #include "llvm/Support/MemoryBuffer.h" |
| 23 | #include "llvm/Support/VirtualFileSystem.h" |
| 24 | #include <gtest/gtest.h> |
| 25 | |
| 26 | namespace clang { |
| 27 | |
| 28 | class : public ::testing::Test { |
| 29 | protected: |
| 30 | std::string (llvm::StringRef ) { |
| 31 | SourceManagerForFile FileSourceMgr("comment-test.cpp" , CommentText); |
| 32 | SourceManager& SourceMgr = FileSourceMgr.get(); |
| 33 | |
| 34 | auto = CommentText.find(Str: "/" ); |
| 35 | assert(CommentStartOffset != llvm::StringRef::npos); |
| 36 | FileID File = SourceMgr.getMainFileID(); |
| 37 | |
| 38 | SourceRange ( |
| 39 | SourceMgr.getLocForStartOfFile(FID: File).getLocWithOffset( |
| 40 | Offset: CommentStartOffset), |
| 41 | SourceMgr.getLocForEndOfFile(FID: File)); |
| 42 | CommentOptions EmptyOpts; |
| 43 | // FIXME: technically, merged that we set here is incorrect, but that |
| 44 | // shouldn't matter. |
| 45 | RawComment (SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true); |
| 46 | DiagnosticOptions DiagOpts; |
| 47 | DiagnosticsEngine Diags(new DiagnosticIDs, DiagOpts); |
| 48 | return Comment.getFormattedText(SourceMgr, Diags); |
| 49 | } |
| 50 | }; |
| 51 | |
| 52 | TEST_F(CommentTextTest, FormattedText) { |
| 53 | // clang-format off |
| 54 | auto ExpectedOutput = |
| 55 | R"(This function does this and that. |
| 56 | For example, |
| 57 | Runnning it in that case will give you |
| 58 | this result. |
| 59 | That's about it.)" ; |
| 60 | // Two-slash comments. |
| 61 | auto Formatted = formatComment( |
| 62 | CommentText: R"cpp( |
| 63 | // This function does this and that. |
| 64 | // For example, |
| 65 | // Runnning it in that case will give you |
| 66 | // this result. |
| 67 | // That's about it.)cpp" ); |
| 68 | EXPECT_EQ(ExpectedOutput, Formatted); |
| 69 | |
| 70 | // Three-slash comments. |
| 71 | Formatted = formatComment( |
| 72 | CommentText: R"cpp( |
| 73 | /// This function does this and that. |
| 74 | /// For example, |
| 75 | /// Runnning it in that case will give you |
| 76 | /// this result. |
| 77 | /// That's about it.)cpp" ); |
| 78 | EXPECT_EQ(ExpectedOutput, Formatted); |
| 79 | |
| 80 | // Block comments. |
| 81 | Formatted = formatComment( |
| 82 | CommentText: 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 | |
| 90 | // Doxygen-style block comments. |
| 91 | Formatted = formatComment( |
| 92 | CommentText: R"cpp( |
| 93 | /** This function does this and that. |
| 94 | * For example, |
| 95 | * Runnning it in that case will give you |
| 96 | * this result. |
| 97 | * That's about it.*/)cpp" ); |
| 98 | EXPECT_EQ(ExpectedOutput, Formatted); |
| 99 | |
| 100 | // Weird indentation. |
| 101 | Formatted = formatComment( |
| 102 | CommentText: R"cpp( |
| 103 | // This function does this and that. |
| 104 | // For example, |
| 105 | // Runnning it in that case will give you |
| 106 | // this result. |
| 107 | // That's about it.)cpp" ); |
| 108 | EXPECT_EQ(ExpectedOutput, Formatted); |
| 109 | // clang-format on |
| 110 | } |
| 111 | |
| 112 | TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) { |
| 113 | // clang-format off |
| 114 | auto ExpectedOutput = |
| 115 | R"(\brief This is the brief part of the comment. |
| 116 | \param a something about a. |
| 117 | @param b something about b.)" ; |
| 118 | |
| 119 | auto Formatted = formatComment( |
| 120 | CommentText: R"cpp( |
| 121 | /// \brief This is the brief part of the comment. |
| 122 | /// \param a something about a. |
| 123 | /// @param b something about b.)cpp" ); |
| 124 | EXPECT_EQ(ExpectedOutput, Formatted); |
| 125 | // clang-format on |
| 126 | } |
| 127 | |
| 128 | TEST_F(CommentTextTest, EmptyFormattedText) { |
| 129 | // Test that empty formatted text doesn't cause crash. |
| 130 | const char *ExpectedOutput = "" ; |
| 131 | auto Formatted = formatComment(CommentText: "//!<" ); |
| 132 | EXPECT_EQ(ExpectedOutput, Formatted); |
| 133 | } |
| 134 | |
| 135 | } // namespace clang |
| 136 | |