diff options
author | 2020-08-03 14:31:31 +0000 | |
---|---|---|
committer | 2020-08-03 14:31:31 +0000 | |
commit | e5dd70708596ae51455a0ffa086a00c5b29f8583 (patch) | |
tree | 5d676f27b570bacf71e786c3b5cff3e6f6679b59 /gnu/llvm/clang/unittests/AST/CommentTextTest.cpp | |
parent | Import LLVM 10.0.0 release including clang, lld and lldb. (diff) | |
download | wireguard-openbsd-e5dd70708596ae51455a0ffa086a00c5b29f8583.tar.xz wireguard-openbsd-e5dd70708596ae51455a0ffa086a00c5b29f8583.zip |
Import LLVM 10.0.0 release including clang, lld and lldb.
ok hackroom
tested by plenty
Diffstat (limited to 'gnu/llvm/clang/unittests/AST/CommentTextTest.cpp')
-rw-r--r-- | gnu/llvm/clang/unittests/AST/CommentTextTest.cpp | 127 |
1 files changed, 127 insertions, 0 deletions
diff --git a/gnu/llvm/clang/unittests/AST/CommentTextTest.cpp b/gnu/llvm/clang/unittests/AST/CommentTextTest.cpp new file mode 100644 index 00000000000..3de6758e45b --- /dev/null +++ b/gnu/llvm/clang/unittests/AST/CommentTextTest.cpp @@ -0,0 +1,127 @@ +//===- unittest/AST/CommentTextTest.cpp - Comment text extraction test ----===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Tests for user-friendly output formatting of comments, i.e. +// RawComment::getFormattedText(). +// +//===----------------------------------------------------------------------===// + +#include "clang/AST/RawCommentList.h" +#include "clang/Basic/CommentOptions.h" +#include "clang/Basic/Diagnostic.h" +#include "clang/Basic/DiagnosticIDs.h" +#include "clang/Basic/FileManager.h" +#include "clang/Basic/FileSystemOptions.h" +#include "clang/Basic/SourceLocation.h" +#include "clang/Basic/SourceManager.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/VirtualFileSystem.h" +#include <gtest/gtest.h> + +namespace clang { + +class CommentTextTest : public ::testing::Test { +protected: + std::string formatComment(llvm::StringRef CommentText) { + SourceManagerForFile FileSourceMgr("comment-test.cpp", CommentText); + SourceManager& SourceMgr = FileSourceMgr.get(); + + auto CommentStartOffset = CommentText.find("/"); + assert(CommentStartOffset != llvm::StringRef::npos); + FileID File = SourceMgr.getMainFileID(); + + SourceRange CommentRange( + SourceMgr.getLocForStartOfFile(File).getLocWithOffset( + CommentStartOffset), + SourceMgr.getLocForEndOfFile(File)); + CommentOptions EmptyOpts; + // FIXME: technically, merged that we set here is incorrect, but that + // shouldn't matter. + RawComment Comment(SourceMgr, CommentRange, EmptyOpts, /*Merged=*/true); + DiagnosticsEngine Diags(new DiagnosticIDs, new DiagnosticOptions); + return Comment.getFormattedText(SourceMgr, Diags); + } +}; + +TEST_F(CommentTextTest, FormattedText) { + // clang-format off + auto ExpectedOutput = +R"(This function does this and that. +For example, + Runnning it in that case will give you + this result. +That's about it.)"; + // Two-slash comments. + auto Formatted = formatComment( +R"cpp( +// This function does this and that. +// For example, +// Runnning it in that case will give you +// this result. +// That's about it.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Three-slash comments. + Formatted = formatComment( +R"cpp( +/// This function does this and that. +/// For example, +/// Runnning it in that case will give you +/// this result. +/// That's about it.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Block comments. + Formatted = formatComment( +R"cpp( +/* This function does this and that. + * For example, + * Runnning it in that case will give you + * this result. + * That's about it.*/)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Doxygen-style block comments. + Formatted = formatComment( +R"cpp( +/** This function does this and that. + * For example, + * Runnning it in that case will give you + * this result. + * That's about it.*/)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + + // Weird indentation. + Formatted = formatComment( +R"cpp( + // This function does this and that. + // For example, + // Runnning it in that case will give you + // this result. + // That's about it.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + // clang-format on +} + +TEST_F(CommentTextTest, KeepsDoxygenControlSeqs) { + // clang-format off + auto ExpectedOutput = +R"(\brief This is the brief part of the comment. +\param a something about a. +@param b something about b.)"; + + auto Formatted = formatComment( +R"cpp( +/// \brief This is the brief part of the comment. +/// \param a something about a. +/// @param b something about b.)cpp"); + EXPECT_EQ(ExpectedOutput, Formatted); + // clang-format on +} + +} // namespace clang |