1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 11:42:57 +01:00
llvm-mirror/include/llvm/Testing/Support/Error.h
Pavel Labath 6694e4a6b1 [Testing/Support] Remove the const_cast in TakeExpected
Summary:
The const_cast in the "const" version of TakeExpected was quite
dangerous, as the function does indeed modify the apparently const
argument.

I assume the reason the const overload was added was to make the
function bind to xvalues(temporaries). That can be also achieved with
rvalue references, so I use that instead.

Using the ASSERT macros on const Expected objects will now become
illegal, but I believe that is correct, as it is not actually possible
to inspect the error stored in an Expected object without modifying it.

Reviewers: zturner, chandlerc

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D34405

llvm-svn: 306001
2017-06-22 13:11:50 +00:00

70 lines
2.1 KiB
C++

//===- llvm/Testing/Support/Error.h ---------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#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) {
llvm::detail::ExpectedHolder<T> Result;
auto &EH = static_cast<llvm::detail::ErrorHolder &>(Result);
EH = TakeError(Exp.takeError());
if (Result.Success)
Result.Value = &(*Exp);
return Result;
}
template <typename T> ExpectedHolder<T> TakeExpected(Expected<T> &&Exp) {
return TakeExpected(Exp);
}
} // 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; }
MATCHER_P(HasValue, value,
"succeeded with value " + testing::PrintToString(value)) {
if (!arg.Success) {
*result_listener << "operation failed";
return false;
}
assert(arg.Value.hasValue());
if (**arg.Value != value) {
*result_listener << "but \"" + testing::PrintToString(**arg.Value) +
"\" != " + testing::PrintToString(value);
return false;
}
return true;
}
} // namespace llvm
#endif