// Part of the Concrete Compiler Project, under the BSD3 License with Zama // Exceptions. See // https://github.com/zama-ai/concrete-compiler-internal/blob/master/LICENSE.txt // for license information. #ifndef CONCRETELANG_SUPPORT_STRING_ERROR_H #define CONCRETELANG_SUPPORT_STRING_ERROR_H #include 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 StreamStringError &operator<<(const T &v) { this->os << v; return *this; } operator llvm::Error() { return llvm::make_error(os.str(), llvm::inconvertibleErrorCode()); } template operator llvm::Expected() { return this->operator llvm::Error(); } protected: std::string buffer; llvm::raw_string_ostream os; }; inline StreamStringError &operator<<(StreamStringError &se, llvm::Error &err) { se << llvm::toString(std::move(err)); return se; } } // namespace concretelang } // namespace mlir #endif