88 lines · c
1//===- OffloadError.h - Definition of error class -------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8//9//===----------------------------------------------------------------------===//10 11#ifndef OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_OFFLOAD_ERROR_H12#define OPENMP_LIBOMPTARGET_PLUGINS_NEXTGEN_COMMON_OFFLOAD_ERROR_H13 14#include "llvm/Support/Error.h"15#include "llvm/Support/ErrorHandling.h"16 17namespace error {18 19enum class ErrorCode {20#define OFFLOAD_ERRC(Name, _, Value) Name = Value,21#include "OffloadErrcodes.inc"22#undef OFFLOAD_ERRC23};24 25} // namespace error26 27namespace std {28template <> struct is_error_code_enum<error::ErrorCode> : std::true_type {};29} // namespace std30 31namespace error {32 33const std::error_category &OffloadErrCategory();34 35inline std::error_code make_error_code(ErrorCode E) {36 return std::error_code(static_cast<int>(E), OffloadErrCategory());37}38 39/// Base class for errors originating in DIA SDK, e.g. COM calls40class OffloadError : public llvm::ErrorInfo<OffloadError, llvm::StringError> {41public:42 using ErrorInfo<OffloadError, StringError>::ErrorInfo;43 44 OffloadError(const llvm::Twine &S) : ErrorInfo(S, ErrorCode::UNKNOWN) {}45 46 // The definition for this resides in the plugin static library47 static char ID;48};49 50/// Create an Offload error.51template <typename... ArgsTy>52static llvm::Error createOffloadError(error::ErrorCode Code, const char *ErrFmt,53 ArgsTy... Args) {54 std::string Buffer;55 llvm::raw_string_ostream(Buffer) << llvm::format(ErrFmt, Args...);56 return llvm::make_error<error::OffloadError>(Code, Buffer);57}58 59inline llvm::Error createOffloadError(error::ErrorCode Code, const char *S) {60 return llvm::make_error<error::OffloadError>(Code, S);61}62 63// The OffloadError will have a message of either:64// * "{Context}: {Message}" if the other error is a StringError65// * "{Context}" otherwise66inline llvm::Error createOffloadError(error::ErrorCode Code,67 llvm::Error &&OtherError,68 const char *Context) {69 std::string Buffer{Context};70 llvm::raw_string_ostream buffer(Buffer);71 72 handleAllErrors(73 std::move(OtherError),74 [&](llvm::StringError &Err) {75 buffer << ": ";76 buffer << Err.getMessage();77 },78 [&](llvm::ErrorInfoBase &Err) {79 // Non-string error message don't add anything to the offload error's80 // error message81 });82 83 return llvm::make_error<error::OffloadError>(Code, Buffer);84}85} // namespace error86 87#endif88