diff options
author | Pavel Labath <pavel@labath.sk> | 2019-01-18 12:52:03 +0000 |
---|---|---|
committer | Pavel Labath <pavel@labath.sk> | 2019-01-18 12:52:03 +0000 |
commit | 47e9a21d34849b52bbde557f3aacca2941f34092 (patch) | |
tree | acf5460e9a8b9c0a2f8cce87790d7880efeb3bd2 /llvm/utils/unittest/googletest/include | |
parent | 5e36433189ba3297af33a9088ee17abde10c7871 (diff) | |
download | bcm5719-llvm-47e9a21d34849b52bbde557f3aacca2941f34092.tar.gz bcm5719-llvm-47e9a21d34849b52bbde557f3aacca2941f34092.zip |
[ADT] Add streaming operators for llvm::Optional
Summary:
The operators simply print the underlying value or "None".
The trickier part of this patch is making sure the streaming operators
work even in unit tests (which was my primary motivation, though I can
also see them being useful elsewhere). Since the stream operator was a
template, implicit conversions did not kick in, and our gtest glue code
was explicitly introducing an implicit conversion to make sure other
implicit conversions do not kick in :P. I resolve that by specializing
llvm_gtest::StreamSwitch for llvm:Optional<T>.
Reviewers: sammccall, dblaikie
Reviewed By: sammccall
Subscribers: mgorny, dexonsmith, kristina, llvm-commits
Differential Revision: https://reviews.llvm.org/D56795
llvm-svn: 351548
Diffstat (limited to 'llvm/utils/unittest/googletest/include')
-rw-r--r-- | llvm/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/llvm/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h b/llvm/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h index fd993db1602..f4430f8de47 100644 --- a/llvm/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h +++ b/llvm/utils/unittest/googletest/include/gtest/internal/custom/raw-ostream.h @@ -42,8 +42,9 @@ auto printable(const T &V) -> decltype(StreamSwitch<T>::printable(V)) { // If raw_ostream support is enabled, we specialize for types with operator<< // that takes a raw_ostream. #if !GTEST_NO_LLVM_RAW_OSTREAM -#include "llvm/Support/raw_ostream.h" +#include "llvm/ADT/Optional.h" #include "llvm/Support/raw_os_ostream.h" +#include "llvm/Support/raw_ostream.h" #include <ostream> namespace llvm_gtest { @@ -68,6 +69,18 @@ struct StreamSwitch<T, decltype((void)(std::declval<llvm::raw_ostream &>() << ConvertibleTo<const T &>()))> { static const RawStreamProxy<T> printable(const T &V) { return {V}; } }; + +// llvm::Optional has a template operator<<, which means it will not accept any +// implicit conversions, so we need to special-case it here. +template <typename T> +struct StreamSwitch<llvm::Optional<T>, + decltype((void)(std::declval<llvm::raw_ostream &>() + << std::declval<llvm::Optional<T>>()))> { + static const RawStreamProxy<llvm::Optional<T>> + printable(const llvm::Optional<T> &V) { + return {V}; + } +}; } // namespace llvm_gtest #endif // !GTEST_NO_LLVM_RAW_OSTREAM |