[llvm] [Support] Add WrappedError class (PR #191706)

via llvm-commits llvm-commits at lists.llvm.org
Sun Apr 12 06:03:08 PDT 2026


https://github.com/aokblast created https://github.com/llvm/llvm-project/pull/191706

The error consumer filters duplicate errors based on a portion of the error message. Introduce a new Error kind that carries a prefix string to support this use case.

>From 29c603cef42dfb4382b363767fc5ecfa9fab0ca2 Mon Sep 17 00:00:00 2001
From: ShengYi Hung <aokblast at FreeBSD.org>
Date: Sun, 12 Apr 2026 20:13:20 +0800
Subject: [PATCH] [Support] Add WrappedError class

The error consumer filters duplicate errors based on a portion of the
error message. Introduce a new Error kind that carries a prefix string
to support this use case.
---
 llvm/include/llvm/Object/Error.h  |  4 ++++
 llvm/include/llvm/Support/Error.h | 18 ++++++++++++++++++
 llvm/lib/Support/Error.cpp        |  8 ++++++++
 3 files changed, 30 insertions(+)

diff --git a/llvm/include/llvm/Object/Error.h b/llvm/include/llvm/Object/Error.h
index 9c522f56b02b5..b7560a3f8630e 100644
--- a/llvm/include/llvm/Object/Error.h
+++ b/llvm/include/llvm/Object/Error.h
@@ -87,6 +87,10 @@ inline Error createError(const Twine &Err) {
   return make_error<StringError>(Err, object_error::parse_failed);
 }
 
+inline Error createError(const Twine &Msg, const Twine &Prefix) {
+  return make_error<WrappedError>(Msg, Prefix);
+}
+
 } // end namespace object.
 
 } // end namespace llvm.
diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index c9fd16fdb7c2b..f00e99feac43e 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1497,6 +1497,24 @@ inline Error unwrap(LLVMErrorRef ErrRef) {
       reinterpret_cast<ErrorInfoBase *>(ErrRef)));
 }
 
+class LLVM_ABI WrappedError : public ErrorInfo<WrappedError, ECError> {
+public:
+  static char ID;
+  WrappedError(const Twine &Msg, const Twine &Prefix = "")
+      : Msg(Msg.str()), Prefix(Prefix.str()) {}
+  void log(raw_ostream &OS) const override;
+  const std::string &getMessage() const { return Msg; }
+  const std::string &getPrefix() const { return Prefix; }
+
+private:
+  std::string Msg;
+  std::string Prefix;
+};
+
+inline Error createWrappedError(const Twine &Msg, const Twine &Prefix) {
+  return make_error<WrappedError>(Msg, Prefix);
+}
+
 } // end namespace llvm
 
 #endif // LLVM_SUPPORT_ERROR_H
diff --git a/llvm/lib/Support/Error.cpp b/llvm/lib/Support/Error.cpp
index c6743155ced85..668a9c5b66fa7 100644
--- a/llvm/lib/Support/Error.cpp
+++ b/llvm/lib/Support/Error.cpp
@@ -205,3 +205,11 @@ LLVMErrorTypeId LLVMGetStringErrorTypeId() {
 LLVMErrorRef LLVMCreateStringError(const char *ErrMsg) {
   return wrap(make_error<StringError>(ErrMsg, inconvertibleErrorCode()));
 }
+
+char WrappedError::ID = 0;
+
+void WrappedError::log(llvm::raw_ostream &OS) const {
+  if (Prefix.size())
+    OS << Prefix << ": ";
+  OS << Msg;
+}



More information about the llvm-commits mailing list