diff options
author | orivej <[email protected]> | 2022-02-10 16:44:49 +0300 |
---|---|---|
committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:49 +0300 |
commit | 718c552901d703c502ccbefdfc3c9028d608b947 (patch) | |
tree | 46534a98bbefcd7b1f3faa5b52c138ab27db75b7 /contrib/libs/llvm12/include/llvm/Testing/Support | |
parent | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (diff) |
Restoring authorship annotation for <[email protected]>. Commit 1 of 2.
Diffstat (limited to 'contrib/libs/llvm12/include/llvm/Testing/Support')
3 files changed, 442 insertions, 442 deletions
diff --git a/contrib/libs/llvm12/include/llvm/Testing/Support/Annotations.h b/contrib/libs/llvm12/include/llvm/Testing/Support/Annotations.h index 14384efbca6..46091ab1dd4 100644 --- a/contrib/libs/llvm12/include/llvm/Testing/Support/Annotations.h +++ b/contrib/libs/llvm12/include/llvm/Testing/Support/Annotations.h @@ -1,103 +1,103 @@ -#pragma once - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" -#endif - -//===--- Annotations.h - Annotated source code for tests ---------*- C++-*-===// -// -// 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 -// -//===----------------------------------------------------------------------===// -#ifndef LLVM_TESTING_SUPPORT_ANNOTATIONS_H -#define LLVM_TESTING_SUPPORT_ANNOTATIONS_H - -#include "llvm/ADT/SmallVector.h" -#include "llvm/ADT/StringMap.h" -#include "llvm/ADT/StringRef.h" -#include <tuple> -#include <vector> - -namespace llvm { - -/// Annotations lets you mark points and ranges inside source code, for tests: -/// -/// Annotations Example(R"cpp( -/// int complete() { x.pri^ } // ^ indicates a point -/// void err() { [["hello" == 42]]; } // [[this is a range]] -/// $definition^class Foo{}; // points can be named: "definition" -/// $fail[[static_assert(false, "")]] // ranges can be named too: "fail" -/// )cpp"); -/// -/// StringRef Code = Example.code(); // annotations stripped. -/// std::vector<size_t> PP = Example.points(); // all unnamed points -/// size_t P = Example.point(); // there must be exactly one -/// llvm::Range R = Example.range("fail"); // find named ranges -/// -/// Points/ranges are coordinated into `code()` which is stripped of -/// annotations. -/// -/// Ranges may be nested (and points can be inside ranges), but there's no way -/// to define general overlapping ranges. -/// -/// FIXME: the choice of the marking syntax makes it impossible to represent -/// some of the C++ and Objective C constructs (including common ones -/// like C++ attributes). We can fix this by: -/// 1. introducing an escaping mechanism for the special characters, -/// 2. making characters for marking points and ranges configurable, -/// 3. changing the syntax to something less commonly used, -/// 4. ... -class Annotations { -public: - /// Two offsets pointing to a continuous substring. End is not included, i.e. - /// represents a half-open range. - struct Range { - size_t Begin = 0; - size_t End = 0; - - friend bool operator==(const Range &L, const Range &R) { - return std::tie(L.Begin, L.End) == std::tie(R.Begin, R.End); - } - friend bool operator!=(const Range &L, const Range &R) { return !(L == R); } - }; - - /// Parses the annotations from Text. Crashes if it's malformed. - Annotations(llvm::StringRef Text); - - /// The input text with all annotations stripped. - /// All points and ranges are relative to this stripped text. - llvm::StringRef code() const { return Code; } - - /// Returns the position of the point marked by ^ (or $name^) in the text. - /// Crashes if there isn't exactly one. - size_t point(llvm::StringRef Name = "") const; - /// Returns the position of all points marked by ^ (or $name^) in the text. - /// Order matches the order within the text. - std::vector<size_t> points(llvm::StringRef Name = "") const; - - /// Returns the location of the range marked by [[ ]] (or $name[[ ]]). - /// Crashes if there isn't exactly one. - Range range(llvm::StringRef Name = "") const; - /// Returns the location of all ranges marked by [[ ]] (or $name[[ ]]). - /// They are ordered by start position within the text. - std::vector<Range> ranges(llvm::StringRef Name = "") const; - -private: - std::string Code; - llvm::StringMap<llvm::SmallVector<size_t, 1>> Points; - llvm::StringMap<llvm::SmallVector<Range, 1>> Ranges; -}; - -llvm::raw_ostream &operator<<(llvm::raw_ostream &O, - const llvm::Annotations::Range &R); - -} // namespace llvm - -#endif - -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===--- Annotations.h - Annotated source code for tests ---------*- C++-*-===// +// +// 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 +// +//===----------------------------------------------------------------------===// +#ifndef LLVM_TESTING_SUPPORT_ANNOTATIONS_H +#define LLVM_TESTING_SUPPORT_ANNOTATIONS_H + +#include "llvm/ADT/SmallVector.h" +#include "llvm/ADT/StringMap.h" +#include "llvm/ADT/StringRef.h" +#include <tuple> +#include <vector> + +namespace llvm { + +/// Annotations lets you mark points and ranges inside source code, for tests: +/// +/// Annotations Example(R"cpp( +/// int complete() { x.pri^ } // ^ indicates a point +/// void err() { [["hello" == 42]]; } // [[this is a range]] +/// $definition^class Foo{}; // points can be named: "definition" +/// $fail[[static_assert(false, "")]] // ranges can be named too: "fail" +/// )cpp"); +/// +/// StringRef Code = Example.code(); // annotations stripped. +/// std::vector<size_t> PP = Example.points(); // all unnamed points +/// size_t P = Example.point(); // there must be exactly one +/// llvm::Range R = Example.range("fail"); // find named ranges +/// +/// Points/ranges are coordinated into `code()` which is stripped of +/// annotations. +/// +/// Ranges may be nested (and points can be inside ranges), but there's no way +/// to define general overlapping ranges. +/// +/// FIXME: the choice of the marking syntax makes it impossible to represent +/// some of the C++ and Objective C constructs (including common ones +/// like C++ attributes). We can fix this by: +/// 1. introducing an escaping mechanism for the special characters, +/// 2. making characters for marking points and ranges configurable, +/// 3. changing the syntax to something less commonly used, +/// 4. ... +class Annotations { +public: + /// Two offsets pointing to a continuous substring. End is not included, i.e. + /// represents a half-open range. + struct Range { + size_t Begin = 0; + size_t End = 0; + + friend bool operator==(const Range &L, const Range &R) { + return std::tie(L.Begin, L.End) == std::tie(R.Begin, R.End); + } + friend bool operator!=(const Range &L, const Range &R) { return !(L == R); } + }; + + /// Parses the annotations from Text. Crashes if it's malformed. + Annotations(llvm::StringRef Text); + + /// The input text with all annotations stripped. + /// All points and ranges are relative to this stripped text. + llvm::StringRef code() const { return Code; } + + /// Returns the position of the point marked by ^ (or $name^) in the text. + /// Crashes if there isn't exactly one. + size_t point(llvm::StringRef Name = "") const; + /// Returns the position of all points marked by ^ (or $name^) in the text. + /// Order matches the order within the text. + std::vector<size_t> points(llvm::StringRef Name = "") const; + + /// Returns the location of the range marked by [[ ]] (or $name[[ ]]). + /// Crashes if there isn't exactly one. + Range range(llvm::StringRef Name = "") const; + /// Returns the location of all ranges marked by [[ ]] (or $name[[ ]]). + /// They are ordered by start position within the text. + std::vector<Range> ranges(llvm::StringRef Name = "") const; + +private: + std::string Code; + llvm::StringMap<llvm::SmallVector<size_t, 1>> Points; + llvm::StringMap<llvm::SmallVector<Range, 1>> Ranges; +}; + +llvm::raw_ostream &operator<<(llvm::raw_ostream &O, + const llvm::Annotations::Range &R); + +} // namespace llvm + +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif diff --git a/contrib/libs/llvm12/include/llvm/Testing/Support/Error.h b/contrib/libs/llvm12/include/llvm/Testing/Support/Error.h index 0b3f2fb7e64..6f651b3e7cd 100644 --- a/contrib/libs/llvm12/include/llvm/Testing/Support/Error.h +++ b/contrib/libs/llvm12/include/llvm/Testing/Support/Error.h @@ -1,217 +1,217 @@ -#pragma once - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" -#endif - -//===- llvm/Testing/Support/Error.h ---------------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_TESTING_SUPPORT_ERROR_H -#define LLVM_TESTING_SUPPORT_ERROR_H - -#include "llvm/ADT/Optional.h" -#include "llvm/Support/Error.h" -#include "llvm/Testing/Support/SupportHelpers.h" - -#include "gmock/gmock.h" -#include <ostream> - -namespace llvm { -namespace detail { -ErrorHolder TakeError(Error Err); - -template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) { - return {TakeError(Exp.takeError()), Exp}; -} - -template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) { - return TakeExpected(Exp); -} - -template <typename T> -class ValueMatchesMono - : public testing::MatcherInterface<const ExpectedHolder<T> &> { -public: - explicit ValueMatchesMono(const testing::Matcher<T> &Matcher) - : Matcher(Matcher) {} - - bool MatchAndExplain(const ExpectedHolder<T> &Holder, - testing::MatchResultListener *listener) const override { - if (!Holder.Success()) - return false; - - bool result = Matcher.MatchAndExplain(*Holder.Exp, listener); - - if (result) - return result; - *listener << "("; - Matcher.DescribeNegationTo(listener->stream()); - *listener << ")"; - return result; - } - - void DescribeTo(std::ostream *OS) const override { - *OS << "succeeded with value ("; - Matcher.DescribeTo(OS); - *OS << ")"; - } - - void DescribeNegationTo(std::ostream *OS) const override { - *OS << "did not succeed or value ("; - Matcher.DescribeNegationTo(OS); - *OS << ")"; - } - -private: - testing::Matcher<T> Matcher; -}; - -template<typename M> -class ValueMatchesPoly { -public: - explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {} - - template <typename T> - operator testing::Matcher<const ExpectedHolder<T> &>() const { - return MakeMatcher( - new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher))); - } - -private: - M Matcher; -}; - -template <typename InfoT> -class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> { -public: - explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT &>> Matcher) - : Matcher(std::move(Matcher)) {} - - bool MatchAndExplain(const ErrorHolder &Holder, - testing::MatchResultListener *listener) const override { - if (Holder.Success()) - return false; - - if (Holder.Infos.size() > 1) { - *listener << "multiple errors"; - return false; - } - - auto &Info = *Holder.Infos[0]; - if (!Info.isA<InfoT>()) { - *listener << "Error was not of given type"; - return false; - } - - if (!Matcher) - return true; - - return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener); - } - - void DescribeTo(std::ostream *OS) const override { - *OS << "failed with Error of given type"; - if (Matcher) { - *OS << " and the error "; - Matcher->DescribeTo(OS); - } - } - - void DescribeNegationTo(std::ostream *OS) const override { - *OS << "succeeded or did not fail with the error of given type"; - if (Matcher) { - *OS << " or the error "; - Matcher->DescribeNegationTo(OS); - } - } - -private: - Optional<testing::Matcher<InfoT &>> Matcher; -}; - -class ErrorMessageMatches - : public testing::MatcherInterface<const ErrorHolder &> { -public: - explicit ErrorMessageMatches( - testing::Matcher<std::vector<std::string>> Matcher) - : Matcher(std::move(Matcher)) {} - - bool MatchAndExplain(const ErrorHolder &Holder, - testing::MatchResultListener *listener) const override { - std::vector<std::string> Messages; - for (const std::shared_ptr<ErrorInfoBase> &Info: Holder.Infos) - Messages.push_back(Info->message()); - - return Matcher.MatchAndExplain(Messages, listener); - } - - void DescribeTo(std::ostream *OS) const override { - *OS << "failed with Error whose message "; - Matcher.DescribeTo(OS); - } - - void DescribeNegationTo(std::ostream *OS) const override { - *OS << "failed with an Error whose message "; - Matcher.DescribeNegationTo(OS); - } - -private: - testing::Matcher<std::vector<std::string>> Matcher; -}; -} // namespace detail - -#define EXPECT_THAT_ERROR(Err, Matcher) \ - EXPECT_THAT(llvm::detail::TakeError(Err), Matcher) -#define ASSERT_THAT_ERROR(Err, Matcher) \ - ASSERT_THAT(llvm::detail::TakeError(Err), Matcher) - -#define EXPECT_THAT_EXPECTED(Err, Matcher) \ - EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher) -#define ASSERT_THAT_EXPECTED(Err, Matcher) \ - ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher) - -MATCHER(Succeeded, "") { return arg.Success(); } -MATCHER(Failed, "") { return !arg.Success(); } - -template <typename InfoT> -testing::Matcher<const detail::ErrorHolder &> Failed() { - return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(None)); -} - -template <typename InfoT, typename M> -testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) { - return MakeMatcher(new detail::ErrorMatchesMono<InfoT>( - testing::SafeMatcherCast<InfoT &>(Matcher))); -} - -template <typename... M> -testing::Matcher<const detail::ErrorHolder &> FailedWithMessage(M... Matcher) { - static_assert(sizeof...(M) > 0, ""); - return MakeMatcher( - new detail::ErrorMessageMatches(testing::ElementsAre(Matcher...))); -} - -template <typename M> -testing::Matcher<const detail::ErrorHolder &> FailedWithMessageArray(M Matcher) { - return MakeMatcher(new detail::ErrorMessageMatches(Matcher)); -} - -template <typename M> -detail::ValueMatchesPoly<M> HasValue(M Matcher) { - return detail::ValueMatchesPoly<M>(Matcher); -} - -} // namespace llvm - -#endif - -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===- llvm/Testing/Support/Error.h ---------------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TESTING_SUPPORT_ERROR_H +#define LLVM_TESTING_SUPPORT_ERROR_H + +#include "llvm/ADT/Optional.h" +#include "llvm/Support/Error.h" +#include "llvm/Testing/Support/SupportHelpers.h" + +#include "gmock/gmock.h" +#include <ostream> + +namespace llvm { +namespace detail { +ErrorHolder TakeError(Error Err); + +template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &Exp) { + return {TakeError(Exp.takeError()), Exp}; +} + +template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) { + return TakeExpected(Exp); +} + +template <typename T> +class ValueMatchesMono + : public testing::MatcherInterface<const ExpectedHolder<T> &> { +public: + explicit ValueMatchesMono(const testing::Matcher<T> &Matcher) + : Matcher(Matcher) {} + + bool MatchAndExplain(const ExpectedHolder<T> &Holder, + testing::MatchResultListener *listener) const override { + if (!Holder.Success()) + return false; + + bool result = Matcher.MatchAndExplain(*Holder.Exp, listener); + + if (result) + return result; + *listener << "("; + Matcher.DescribeNegationTo(listener->stream()); + *listener << ")"; + return result; + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "succeeded with value ("; + Matcher.DescribeTo(OS); + *OS << ")"; + } + + void DescribeNegationTo(std::ostream *OS) const override { + *OS << "did not succeed or value ("; + Matcher.DescribeNegationTo(OS); + *OS << ")"; + } + +private: + testing::Matcher<T> Matcher; +}; + +template<typename M> +class ValueMatchesPoly { +public: + explicit ValueMatchesPoly(const M &Matcher) : Matcher(Matcher) {} + + template <typename T> + operator testing::Matcher<const ExpectedHolder<T> &>() const { + return MakeMatcher( + new ValueMatchesMono<T>(testing::SafeMatcherCast<T>(Matcher))); + } + +private: + M Matcher; +}; + +template <typename InfoT> +class ErrorMatchesMono : public testing::MatcherInterface<const ErrorHolder &> { +public: + explicit ErrorMatchesMono(Optional<testing::Matcher<InfoT &>> Matcher) + : Matcher(std::move(Matcher)) {} + + bool MatchAndExplain(const ErrorHolder &Holder, + testing::MatchResultListener *listener) const override { + if (Holder.Success()) + return false; + + if (Holder.Infos.size() > 1) { + *listener << "multiple errors"; + return false; + } + + auto &Info = *Holder.Infos[0]; + if (!Info.isA<InfoT>()) { + *listener << "Error was not of given type"; + return false; + } + + if (!Matcher) + return true; + + return Matcher->MatchAndExplain(static_cast<InfoT &>(Info), listener); + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "failed with Error of given type"; + if (Matcher) { + *OS << " and the error "; + Matcher->DescribeTo(OS); + } + } + + void DescribeNegationTo(std::ostream *OS) const override { + *OS << "succeeded or did not fail with the error of given type"; + if (Matcher) { + *OS << " or the error "; + Matcher->DescribeNegationTo(OS); + } + } + +private: + Optional<testing::Matcher<InfoT &>> Matcher; +}; + +class ErrorMessageMatches + : public testing::MatcherInterface<const ErrorHolder &> { +public: + explicit ErrorMessageMatches( + testing::Matcher<std::vector<std::string>> Matcher) + : Matcher(std::move(Matcher)) {} + + bool MatchAndExplain(const ErrorHolder &Holder, + testing::MatchResultListener *listener) const override { + std::vector<std::string> Messages; + for (const std::shared_ptr<ErrorInfoBase> &Info: Holder.Infos) + Messages.push_back(Info->message()); + + return Matcher.MatchAndExplain(Messages, listener); + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "failed with Error whose message "; + Matcher.DescribeTo(OS); + } + + void DescribeNegationTo(std::ostream *OS) const override { + *OS << "failed with an Error whose message "; + Matcher.DescribeNegationTo(OS); + } + +private: + testing::Matcher<std::vector<std::string>> Matcher; +}; +} // namespace detail + +#define EXPECT_THAT_ERROR(Err, Matcher) \ + EXPECT_THAT(llvm::detail::TakeError(Err), Matcher) +#define ASSERT_THAT_ERROR(Err, Matcher) \ + ASSERT_THAT(llvm::detail::TakeError(Err), Matcher) + +#define EXPECT_THAT_EXPECTED(Err, Matcher) \ + EXPECT_THAT(llvm::detail::TakeExpected(Err), Matcher) +#define ASSERT_THAT_EXPECTED(Err, Matcher) \ + ASSERT_THAT(llvm::detail::TakeExpected(Err), Matcher) + +MATCHER(Succeeded, "") { return arg.Success(); } +MATCHER(Failed, "") { return !arg.Success(); } + +template <typename InfoT> +testing::Matcher<const detail::ErrorHolder &> Failed() { + return MakeMatcher(new detail::ErrorMatchesMono<InfoT>(None)); +} + +template <typename InfoT, typename M> +testing::Matcher<const detail::ErrorHolder &> Failed(M Matcher) { + return MakeMatcher(new detail::ErrorMatchesMono<InfoT>( + testing::SafeMatcherCast<InfoT &>(Matcher))); +} + +template <typename... M> +testing::Matcher<const detail::ErrorHolder &> FailedWithMessage(M... Matcher) { + static_assert(sizeof...(M) > 0, ""); + return MakeMatcher( + new detail::ErrorMessageMatches(testing::ElementsAre(Matcher...))); +} + +template <typename M> +testing::Matcher<const detail::ErrorHolder &> FailedWithMessageArray(M Matcher) { + return MakeMatcher(new detail::ErrorMessageMatches(Matcher)); +} + +template <typename M> +detail::ValueMatchesPoly<M> HasValue(M Matcher) { + return detail::ValueMatchesPoly<M>(Matcher); +} + +} // namespace llvm + +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif diff --git a/contrib/libs/llvm12/include/llvm/Testing/Support/SupportHelpers.h b/contrib/libs/llvm12/include/llvm/Testing/Support/SupportHelpers.h index fd732dd15f2..d8c59432fe4 100644 --- a/contrib/libs/llvm12/include/llvm/Testing/Support/SupportHelpers.h +++ b/contrib/libs/llvm12/include/llvm/Testing/Support/SupportHelpers.h @@ -1,119 +1,119 @@ -#pragma once - -#ifdef __GNUC__ -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wunused-parameter" -#endif - -//===- Testing/Support/SupportHelpers.h -----------------------------------===// -// -// 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 -// -//===----------------------------------------------------------------------===// - -#ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H -#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H - -#include "llvm/ADT/Optional.h" -#include "llvm/ADT/SmallString.h" -#include "llvm/Support/Error.h" +#pragma once + +#ifdef __GNUC__ +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-parameter" +#endif + +//===- Testing/Support/SupportHelpers.h -----------------------------------===// +// +// 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 +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H +#define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H + +#include "llvm/ADT/Optional.h" +#include "llvm/ADT/SmallString.h" +#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Path.h" -#include "llvm/Support/raw_os_ostream.h" -#include "gmock/gmock-matchers.h" -#include "gtest/gtest-printers.h" - -#include <string> - -namespace llvm { -namespace detail { -struct ErrorHolder { - std::vector<std::shared_ptr<ErrorInfoBase>> Infos; - - bool Success() const { return Infos.empty(); } -}; - -template <typename T> struct ExpectedHolder : public ErrorHolder { - ExpectedHolder(ErrorHolder Err, Expected<T> &Exp) - : ErrorHolder(std::move(Err)), Exp(Exp) {} - - Expected<T> &Exp; -}; - -inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) { - raw_os_ostream OS(*Out); - OS << (Err.Success() ? "succeeded" : "failed"); - if (!Err.Success()) { - const char *Delim = " ("; - for (const auto &Info : Err.Infos) { - OS << Delim; - Delim = "; "; - Info->log(OS); - } - OS << ")"; - } -} - -template <typename T> -void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) { - if (Item.Success()) { - *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp); - } else { - PrintTo(static_cast<const ErrorHolder &>(Item), Out); - } -} - -template <class InnerMatcher> class ValueIsMatcher { -public: - explicit ValueIsMatcher(InnerMatcher ValueMatcher) - : ValueMatcher(ValueMatcher) {} - - template <class T> - operator ::testing::Matcher<const llvm::Optional<T> &>() const { - return ::testing::MakeMatcher( - new Impl<T>(::testing::SafeMatcherCast<T>(ValueMatcher))); - } - - template <class T> - class Impl : public ::testing::MatcherInterface<const llvm::Optional<T> &> { - public: - explicit Impl(const ::testing::Matcher<T> &ValueMatcher) - : ValueMatcher(ValueMatcher) {} - - bool MatchAndExplain(const llvm::Optional<T> &Input, - testing::MatchResultListener *L) const override { - return Input && ValueMatcher.MatchAndExplain(Input.getValue(), L); - } - - void DescribeTo(std::ostream *OS) const override { - *OS << "has a value that "; - ValueMatcher.DescribeTo(OS); - } - void DescribeNegationTo(std::ostream *OS) const override { - *OS << "does not have a value that "; - ValueMatcher.DescribeTo(OS); - } - - private: - testing::Matcher<T> ValueMatcher; - }; - -private: - InnerMatcher ValueMatcher; -}; -} // namespace detail - -/// Matches an llvm::Optional<T> with a value that conforms to an inner matcher. -/// To match llvm::None you could use Eq(llvm::None). -template <class InnerMatcher> -detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) { - return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher); -} -namespace unittest { - -SmallString<128> getInputFileDirectory(const char *Argv0); +#include "llvm/Support/raw_os_ostream.h" +#include "gmock/gmock-matchers.h" +#include "gtest/gtest-printers.h" + +#include <string> + +namespace llvm { +namespace detail { +struct ErrorHolder { + std::vector<std::shared_ptr<ErrorInfoBase>> Infos; + + bool Success() const { return Infos.empty(); } +}; + +template <typename T> struct ExpectedHolder : public ErrorHolder { + ExpectedHolder(ErrorHolder Err, Expected<T> &Exp) + : ErrorHolder(std::move(Err)), Exp(Exp) {} + + Expected<T> &Exp; +}; + +inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) { + raw_os_ostream OS(*Out); + OS << (Err.Success() ? "succeeded" : "failed"); + if (!Err.Success()) { + const char *Delim = " ("; + for (const auto &Info : Err.Infos) { + OS << Delim; + Delim = "; "; + Info->log(OS); + } + OS << ")"; + } +} + +template <typename T> +void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) { + if (Item.Success()) { + *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp); + } else { + PrintTo(static_cast<const ErrorHolder &>(Item), Out); + } +} + +template <class InnerMatcher> class ValueIsMatcher { +public: + explicit ValueIsMatcher(InnerMatcher ValueMatcher) + : ValueMatcher(ValueMatcher) {} + + template <class T> + operator ::testing::Matcher<const llvm::Optional<T> &>() const { + return ::testing::MakeMatcher( + new Impl<T>(::testing::SafeMatcherCast<T>(ValueMatcher))); + } + + template <class T> + class Impl : public ::testing::MatcherInterface<const llvm::Optional<T> &> { + public: + explicit Impl(const ::testing::Matcher<T> &ValueMatcher) + : ValueMatcher(ValueMatcher) {} + + bool MatchAndExplain(const llvm::Optional<T> &Input, + testing::MatchResultListener *L) const override { + return Input && ValueMatcher.MatchAndExplain(Input.getValue(), L); + } + + void DescribeTo(std::ostream *OS) const override { + *OS << "has a value that "; + ValueMatcher.DescribeTo(OS); + } + void DescribeNegationTo(std::ostream *OS) const override { + *OS << "does not have a value that "; + ValueMatcher.DescribeTo(OS); + } + + private: + testing::Matcher<T> ValueMatcher; + }; + +private: + InnerMatcher ValueMatcher; +}; +} // namespace detail + +/// Matches an llvm::Optional<T> with a value that conforms to an inner matcher. +/// To match llvm::None you could use Eq(llvm::None). +template <class InnerMatcher> +detail::ValueIsMatcher<InnerMatcher> ValueIs(const InnerMatcher &ValueMatcher) { + return detail::ValueIsMatcher<InnerMatcher>(ValueMatcher); +} +namespace unittest { + +SmallString<128> getInputFileDirectory(const char *Argv0); /// A RAII object that creates a temporary directory upon initialization and /// removes it upon destruction. @@ -249,11 +249,11 @@ public: StringRef path() const { return Path; } }; -} // namespace unittest -} // namespace llvm - -#endif - -#ifdef __GNUC__ -#pragma GCC diagnostic pop -#endif +} // namespace unittest +} // namespace llvm + +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif |