11ff7c32fSIlya Biryukov //===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===//
21ff7c32fSIlya Biryukov //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
61ff7c32fSIlya Biryukov //
71ff7c32fSIlya Biryukov //===----------------------------------------------------------------------===//
81ff7c32fSIlya Biryukov //
91ff7c32fSIlya Biryukov // Tests for user-friendly output formatting of comments, i.e.
101ff7c32fSIlya Biryukov // RawComment::getFormattedText().
111ff7c32fSIlya Biryukov //
121ff7c32fSIlya Biryukov //===----------------------------------------------------------------------===//
131ff7c32fSIlya Biryukov
141ff7c32fSIlya Biryukov #include "clang/AST/RawCommentList.h"
151ff7c32fSIlya Biryukov #include "clang/Basic/CommentOptions.h"
161ff7c32fSIlya Biryukov #include "clang/Basic/Diagnostic.h"
171ff7c32fSIlya Biryukov #include "clang/Basic/DiagnosticIDs.h"
181ff7c32fSIlya Biryukov #include "clang/Basic/FileManager.h"
191ff7c32fSIlya Biryukov #include "clang/Basic/FileSystemOptions.h"
201ff7c32fSIlya Biryukov #include "clang/Basic/SourceLocation.h"
211ff7c32fSIlya Biryukov #include "clang/Basic/SourceManager.h"
221ff7c32fSIlya Biryukov #include "llvm/Support/MemoryBuffer.h"
23fc51490bSJonas Devlieghere #include "llvm/Support/VirtualFileSystem.h"
241ff7c32fSIlya Biryukov #include <gtest/gtest.h>
251ff7c32fSIlya Biryukov
261ff7c32fSIlya Biryukov namespace clang {
271ff7c32fSIlya Biryukov
281ff7c32fSIlya Biryukov class CommentTextTest : public ::testing::Test {
291ff7c32fSIlya Biryukov protected:
formatComment(llvm::StringRef CommentText)301ff7c32fSIlya Biryukov std::string formatComment(llvm::StringRef CommentText) {
311ff7c32fSIlya Biryukov SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText);
321ff7c32fSIlya Biryukov SourceManager& SourceMgr = FileSourceMgr.get();
331ff7c32fSIlya Biryukov
341ff7c32fSIlya Biryukov auto CommentStartOffset = CommentText.find("/");
351ff7c32fSIlya Biryukov assert(CommentStartOffset != llvm::StringRef::npos);
361ff7c32fSIlya Biryukov FileID File = SourceMgr.getMainFileID();
371ff7c32fSIlya Biryukov
381ff7c32fSIlya Biryukov SourceRange CommentRange(
391ff7c32fSIlya Biryukov SourceMgr.getLocForStartOfFile(File).getLocWithOffset(
401ff7c32fSIlya Biryukov CommentStartOffset),
411ff7c32fSIlya Biryukov SourceMgr.getLocForEndOfFile(File));
421ff7c32fSIlya Biryukov CommentOptions EmptyOpts;
431ff7c32fSIlya Biryukov // FIXME: technically, merged that we set here is incorrect, but that
441ff7c32fSIlya Biryukov // shouldn't matter.
451ff7c32fSIlya Biryukov RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true);
461ff7c32fSIlya Biryukov DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions);
471ff7c32fSIlya Biryukov return Comment.getFormattedText(SourceMgr, Diags);
481ff7c32fSIlya Biryukov }
491ff7c32fSIlya Biryukov };
501ff7c32fSIlya Biryukov
TEST_F(CommentTextTest,FormattedText)511ff7c32fSIlya Biryukov TEST_F(CommentTextTest, FormattedText) {
521ff7c32fSIlya Biryukov // clang-format off
531ff7c32fSIlya Biryukov auto ExpectedOutput =
541ff7c32fSIlya Biryukov R"(This function does this and that.
551ff7c32fSIlya Biryukov For example,
561ff7c32fSIlya Biryukov Runnning it in that case will give you
571ff7c32fSIlya Biryukov this result.
581ff7c32fSIlya Biryukov That's about it.)";
591ff7c32fSIlya Biryukov // Two-slash comments.
601b7bbb72SClement Courbet auto Formatted = formatComment(
611ff7c32fSIlya Biryukov R"cpp(
621ff7c32fSIlya Biryukov // This function does this and that.
631ff7c32fSIlya Biryukov // For example,
641ff7c32fSIlya Biryukov // Runnning it in that case will give you
651ff7c32fSIlya Biryukov // this result.
661b7bbb72SClement Courbet // That's about it.)cpp");
671b7bbb72SClement Courbet EXPECT_EQ(ExpectedOutput, Formatted);
681ff7c32fSIlya Biryukov
691ff7c32fSIlya Biryukov // Three-slash comments.
701b7bbb72SClement Courbet Formatted = formatComment(
711ff7c32fSIlya Biryukov R"cpp(
721ff7c32fSIlya Biryukov /// This function does this and that.
731ff7c32fSIlya Biryukov /// For example,
741ff7c32fSIlya Biryukov /// Runnning it in that case will give you
751ff7c32fSIlya Biryukov /// this result.
761b7bbb72SClement Courbet /// That's about it.)cpp");
771b7bbb72SClement Courbet EXPECT_EQ(ExpectedOutput, Formatted);
781ff7c32fSIlya Biryukov
791ff7c32fSIlya Biryukov // Block comments.
801b7bbb72SClement Courbet Formatted = formatComment(
811ff7c32fSIlya Biryukov R"cpp(
821ff7c32fSIlya Biryukov /* This function does this and that.
831ff7c32fSIlya Biryukov * For example,
841ff7c32fSIlya Biryukov * Runnning it in that case will give you
851ff7c32fSIlya Biryukov * this result.
861b7bbb72SClement Courbet * That's about it.*/)cpp");
871b7bbb72SClement Courbet EXPECT_EQ(ExpectedOutput, Formatted);
881ff7c32fSIlya Biryukov
891ff7c32fSIlya Biryukov // Doxygen-style block comments.
901b7bbb72SClement Courbet Formatted = formatComment(
911ff7c32fSIlya Biryukov R"cpp(
921ff7c32fSIlya Biryukov /** This function does this and that.
931ff7c32fSIlya Biryukov * For example,
941ff7c32fSIlya Biryukov * Runnning it in that case will give you
951ff7c32fSIlya Biryukov * this result.
961b7bbb72SClement Courbet * That's about it.*/)cpp");
971b7bbb72SClement Courbet EXPECT_EQ(ExpectedOutput, Formatted);
981ff7c32fSIlya Biryukov
991ff7c32fSIlya Biryukov // Weird indentation.
1001b7bbb72SClement Courbet Formatted = formatComment(
1011ff7c32fSIlya Biryukov R"cpp(
1021ff7c32fSIlya Biryukov // This function does this and that.
1031ff7c32fSIlya Biryukov // For example,
1041ff7c32fSIlya Biryukov // Runnning it in that case will give you
1051ff7c32fSIlya Biryukov // this result.
1061b7bbb72SClement Courbet // That's about it.)cpp");
1071b7bbb72SClement Courbet EXPECT_EQ(ExpectedOutput, Formatted);
1081ff7c32fSIlya Biryukov // clang-format on
1091ff7c32fSIlya Biryukov }
1101ff7c32fSIlya Biryukov
TEST_F(CommentTextTest,KeepsDoxygenControlSeqs)1111ff7c32fSIlya Biryukov TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) {
1121ff7c32fSIlya Biryukov // clang-format off
1131ff7c32fSIlya Biryukov auto ExpectedOutput =
1141ff7c32fSIlya Biryukov R"(\brief This is the brief part of the comment.
1151ff7c32fSIlya Biryukov \param a something about a.
1161ff7c32fSIlya Biryukov @param b something about b.)";
1171ff7c32fSIlya Biryukov
1181b7bbb72SClement Courbet auto Formatted = formatComment(
1191ff7c32fSIlya Biryukov R"cpp(
1201ff7c32fSIlya Biryukov /// \brief This is the brief part of the comment.
1211ff7c32fSIlya Biryukov /// \param a something about a.
1221b7bbb72SClement Courbet /// @param b something about b.)cpp");
1231b7bbb72SClement Courbet EXPECT_EQ(ExpectedOutput, Formatted);
1241ff7c32fSIlya Biryukov // clang-format on
1251ff7c32fSIlya Biryukov }
1261ff7c32fSIlya Biryukov
TEST_F(CommentTextTest,EmptyFormattedText)127*aa0d7179SDmitry Polukhin TEST_F(CommentTextTest, EmptyFormattedText) {
128*aa0d7179SDmitry Polukhin // Test that empty formatted text doesn't cause crash.
129*aa0d7179SDmitry Polukhin const char *ExpectedOutput = "";
130*aa0d7179SDmitry Polukhin auto Formatted = formatComment("//!<");
131*aa0d7179SDmitry Polukhin EXPECT_EQ(ExpectedOutput, Formatted);
132*aa0d7179SDmitry Polukhin }
133*aa0d7179SDmitry Polukhin
1341ff7c32fSIlya Biryukov } // namespace clang
135