1 //===- Testing/Support/SupportHelpers.h -----------------------------------===// 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 #ifndef LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H 11 #define LLVM_TESTING_SUPPORT_SUPPORTHELPERS_H 12 13 #include "llvm/ADT/StringRef.h" 14 #include "llvm/Support/Error.h" 15 #include "gtest/gtest-printers.h" 16 17 namespace llvm { 18 namespace detail { 19 struct ErrorHolder { 20 std::vector<std::shared_ptr<ErrorInfoBase>> Infos; 21 SuccessErrorHolder22 bool Success() const { return Infos.empty(); } 23 }; 24 25 template <typename T> struct ExpectedHolder : public ErrorHolder { ExpectedHolderExpectedHolder26 ExpectedHolder(ErrorHolder Err, Expected<T> &Exp) 27 : ErrorHolder(std::move(Err)), Exp(Exp) {} 28 29 Expected<T> &Exp; 30 }; 31 PrintTo(const ErrorHolder & Err,std::ostream * Out)32inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) { 33 raw_os_ostream OS(*Out); 34 OS << (Err.Success() ? "succeeded" : "failed"); 35 if (!Err.Success()) { 36 const char *Delim = " ("; 37 for (const auto &Info : Err.Infos) { 38 OS << Delim; 39 Delim = "; "; 40 Info->log(OS); 41 } 42 OS << ")"; 43 } 44 } 45 46 template <typename T> PrintTo(const ExpectedHolder<T> & Item,std::ostream * Out)47void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) { 48 if (Item.Success()) { 49 *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp); 50 } else { 51 PrintTo(static_cast<const ErrorHolder &>(Item), Out); 52 } 53 } 54 } // namespace detail 55 } // namespace llvm 56 57 #endif 58