Debian Codegraph @

clang/unittests/AST/CommentTextTest.cpp

raw
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 CommentTextTest : public ::testing::Test {
29 protected:
30 std::string formatComment(llvm::StringRef CommentText) {
31 SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);
32 SourceManager& SourceMgr = FileSourceMgr.get();
33
34 auto CommentStartOffset = CommentText.find("/");
35 assert(CommentStartOffset != llvm::StringRef::npos);
36 FileID File = SourceMgr.getMainFileID();
37
38 SourceRange CommentRange(
39 SourceMgr.getLocForStartOfFile(File).getLocWithOffset(
40 CommentStartOffset),
41 SourceMgr.getLocForEndOfFile(File));
42 CommentOptions EmptyOpts;
43 // FIXME: technically, merged that we set here is incorrect, but that
44 // shouldn't matter.
45 RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);
46 DiagnosticOptions DiagOpts;
47 DiagnosticsEngine Diags(DiagnosticIDs::create(), 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 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 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 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 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 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 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("//!<");
132 EXPECT_EQ(ExpectedOutput, Formatted);
133 }
134
135 } // namespace clang