Files
concrete/compiler/include/concretelang/Support/Error.h
youben11 e73291abdc chore: rename compiler to concrete-compiler
zamalang => concretelang
zamacompiler => concretecompiler
2021-12-29 15:13:34 +01:00

57 lines
1.5 KiB
C++

// Part of the Concrete Compiler Project, under the BSD3 License with Zama Exceptions.
// See https://github.com/zama-ai/homomorphizer/blob/master/LICENSE.txt for license information.
#ifndef CONCRETELANG_SUPPORT_STRING_ERROR_H
#define CONCRETELANG_SUPPORT_STRING_ERROR_H
#include <llvm/Support/Error.h>
namespace mlir {
namespace concretelang {
// Internal error class that allows for composing `llvm::Error`s
// similar to `llvm::createStringError()`, but using stream-like
// composition with `operator<<`.
//
// Example:
//
// llvm::Error foo(int i, size_t s, ...) {
// ...
// if(...) {
// return StreamStringError()
// << "Some error message with an integer: "
// << i << " and a size_t: " << s;
// }
// ...
// }
class StreamStringError {
public:
StreamStringError(const llvm::StringRef &s) : buffer(s.str()), os(buffer){};
StreamStringError() : buffer(""), os(buffer){};
template <typename T> StreamStringError &operator<<(const T &v) {
this->os << v;
return *this;
}
operator llvm::Error() {
return llvm::make_error<llvm::StringError>(os.str(),
llvm::inconvertibleErrorCode());
}
template <typename T> operator llvm::Expected<T>() {
return this->operator llvm::Error();
}
protected:
std::string buffer;
llvm::raw_string_ostream os;
};
StreamStringError &operator<<(StreamStringError &se, llvm::Error &err);
} // namespace concretelang
} // namespace mlir
#endif