1 //===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Tests for user-friendly output formatting of comments, i.e. 11 // RawComment::getFormattedText(). 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "clang/AST/RawCommentList.h" 16 #include "clang/Basic/CommentOptions.h" 17 #include "clang/Basic/Diagnostic.h" 18 #include "clang/Basic/DiagnosticIDs.h" 19 #include "clang/Basic/FileManager.h" 20 #include "clang/Basic/FileSystemOptions.h" 21 #include "clang/Basic/SourceLocation.h" 22 #include "clang/Basic/SourceManager.h" 23 #include "llvm/Support/MemoryBuffer.h" 24 #include "llvm/Support/VirtualFileSystem.h" 25 #include <gtest/gtest.h> 26 27 namespace clang { 28 29 class CommentTextTest : public ::testing::Test { 30 protected: 31 std::string formatComment(llvm::StringRef CommentText) { 32 SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText); 33 SourceManager& SourceMgr = FileSourceMgr.get(); 34 35 auto CommentStartOffset = CommentText.find("/"); 36 assert(CommentStartOffset != llvm::StringRef::npos); 37 FileID File = SourceMgr.getMainFileID(); 38 39 SourceRange CommentRange( 40 SourceMgr.getLocForStartOfFile(File).getLocWithOffset( 41 CommentStartOffset), 42 SourceMgr.getLocForEndOfFile(File)); 43 CommentOptions EmptyOpts; 44 // FIXME: technically, merged that we set here is incorrect, but that 45 // shouldn't matter. 46 RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true); 47 DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions); 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 } // namespace clang 129