1 //===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===//
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
7 //===----------------------------------------------------------------------===//
9 // Tests for user-friendly output formatting of comments, i.e.
10 // RawComment::getFormattedText().
12 //===----------------------------------------------------------------------===//
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>
28 class CommentTextTest
: public ::testing::Test
{
30 std::string
formatComment(llvm::StringRef CommentText
) {
31 SourceManagerForFile
FileSourceMgr("comment-test.cpp", CommentText
);
32 SourceManager
& SourceMgr
= FileSourceMgr
.get();
34 auto CommentStartOffset
= CommentText
.find("/");
35 assert(CommentStartOffset
!= llvm::StringRef::npos
);
36 FileID File
= SourceMgr
.getMainFileID();
38 SourceRange
CommentRange(
39 SourceMgr
.getLocForStartOfFile(File
).getLocWithOffset(
41 SourceMgr
.getLocForEndOfFile(File
));
42 CommentOptions EmptyOpts
;
43 // FIXME: technically, merged that we set here is incorrect, but that
45 RawComment
Comment(SourceMgr
, CommentRange
, EmptyOpts
, /*Merged=*/true);
46 DiagnosticsEngine
Diags(new DiagnosticIDs
, new DiagnosticOptions
);
47 return Comment
.getFormattedText(SourceMgr
, Diags
);
51 TEST_F(CommentTextTest
, FormattedText
) {
54 R
"(This function does this and that.
56 Runnning it in that case will give you
59 // Two-slash comments.
60 auto Formatted
= formatComment(
62 // This function does this and that.
64 // Runnning it in that case will give you
66 // That's about it.)cpp");
67 EXPECT_EQ(ExpectedOutput
, Formatted
);
69 // Three-slash comments.
70 Formatted
= formatComment(
72 /// This function does this and that.
74 /// Runnning it in that case will give you
76 /// That's about it.)cpp");
77 EXPECT_EQ(ExpectedOutput
, Formatted
);
80 Formatted
= formatComment(
82 /* This function does this and that.
84 * Runnning it in that case will give you
86 * That's about it.*/)cpp");
87 EXPECT_EQ(ExpectedOutput
, Formatted
);
89 // Doxygen-style block comments.
90 Formatted
= formatComment(
92 /** This function does this and that.
94 * Runnning it in that case will give you
96 * That's about it.*/)cpp");
97 EXPECT_EQ(ExpectedOutput
, Formatted
);
100 Formatted
= formatComment(
102 // This function does this and that.
104 // Runnning it in that case will give you
106 // That's about it.)cpp");
107 EXPECT_EQ(ExpectedOutput
, Formatted
);
111 TEST_F(CommentTextTest
, KeepsDoxygenControlSeqs
) {
113 auto ExpectedOutput
=
114 R
"(\brief This is the brief part of the comment.
115 \param a something about a.
116 @param b something about b.)";
118 auto Formatted
= formatComment(
120 /// \brief This is the brief part of the comment.
121 /// \param a something about a.
122 /// @param b something about b.)cpp");
123 EXPECT_EQ(ExpectedOutput
, Formatted
);
127 TEST_F(CommentTextTest
, EmptyFormattedText
) {
128 // Test that empty formatted text doesn't cause crash.
129 const char *ExpectedOutput
= "";
130 auto Formatted
= formatComment("//!<");
131 EXPECT_EQ(ExpectedOutput
, Formatted
);