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/SmallString.h" 14 #include "llvm/Support/Error.h" 15 #include "llvm/Support/raw_os_ostream.h" 16 #include "gtest/gtest-printers.h" 17 18 #include <string> 19 20 namespace llvm { 21 namespace detail { 22 struct ErrorHolder { 23 std::vector<std::shared_ptr<ErrorInfoBase>> Infos; 24 SuccessErrorHolder25 bool Success() const { return Infos.empty(); } 26 }; 27 28 template <typename T> struct ExpectedHolder : public ErrorHolder { ExpectedHolderExpectedHolder29 ExpectedHolder(ErrorHolder Err, Expected<T> &Exp) 30 : ErrorHolder(std::move(Err)), Exp(Exp) {} 31 32 Expected<T> &Exp; 33 }; 34 PrintTo(const ErrorHolder & Err,std::ostream * Out)35inline void PrintTo(const ErrorHolder &Err, std::ostream *Out) { 36 raw_os_ostream OS(*Out); 37 OS << (Err.Success() ? "succeeded" : "failed"); 38 if (!Err.Success()) { 39 const char *Delim = " ("; 40 for (const auto &Info : Err.Infos) { 41 OS << Delim; 42 Delim = "; "; 43 Info->log(OS); 44 } 45 OS << ")"; 46 } 47 } 48 49 template <typename T> PrintTo(const ExpectedHolder<T> & Item,std::ostream * Out)50void PrintTo(const ExpectedHolder<T> &Item, std::ostream *Out) { 51 if (Item.Success()) { 52 *Out << "succeeded with value " << ::testing::PrintToString(*Item.Exp); 53 } else { 54 PrintTo(static_cast<const ErrorHolder &>(Item), Out); 55 } 56 } 57 } // namespace detail 58 59 namespace unittest { 60 SmallString<128> getInputFileDirectory(const char *Argv0); 61 } 62 } // namespace llvm 63 64 #endif 65