[llvm] [llvm][Support] Remove unnecessary allocations when creating StringEr… (PR #175863)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 13 15:04:37 PST 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Ebuka Ezike (da-viper)

<details>
<summary>Changes</summary>

…rors

The String Error class has three constructors .
StringError::StringError(const Twine &S, std::error_code EC) StringError::StringError(std::error_code EC, const Twine &S) StringError::StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)

When we use the `createStringError(std::error_code, char const *, ... )` it ends up using twine variant and ends up creating a new string twice

---
Full diff: https://github.com/llvm/llvm-project/pull/175863.diff


2 Files Affected:

- (modified) llvm/include/llvm/Support/Error.h (+1-1) 
- (modified) llvm/lib/Support/Error.cpp (+2-2) 


``````````diff
diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index ed83e6e3a40ed..c9fd16fdb7c2b 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1306,7 +1306,7 @@ inline Error createStringError(std::error_code EC, char const *Fmt,
                                const Ts &... Vals) {
   std::string Buffer;
   raw_string_ostream(Buffer) << format(Fmt, Vals...);
-  return make_error<StringError>(Buffer, EC);
+  return make_error<StringError>(std::move(Buffer), EC, true);
 }
 
 LLVM_ABI Error createStringError(std::string &&Msg, std::error_code EC);
diff --git a/llvm/lib/Support/Error.cpp b/llvm/lib/Support/Error.cpp
index d168b462a6eb2..ede51d8836ccc 100644
--- a/llvm/lib/Support/Error.cpp
+++ b/llvm/lib/Support/Error.cpp
@@ -144,7 +144,7 @@ StringError::StringError(const Twine &S, std::error_code EC)
     : Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
 
 StringError::StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)
-    : Msg(S), EC(EC), PrintMsgOnly(PrintMsgOnly) {}
+    : Msg(std::move(S)), EC(EC), PrintMsgOnly(PrintMsgOnly) {}
 
 void StringError::log(raw_ostream &OS) const {
   if (PrintMsgOnly) {
@@ -161,7 +161,7 @@ std::error_code StringError::convertToErrorCode() const {
 }
 
 Error createStringError(std::string &&Msg, std::error_code EC) {
-  return make_error<StringError>(Msg, EC);
+  return make_error<StringError>(std::move(Msg), EC, true);
 }
 
 void report_fatal_error(Error Err, bool GenCrashDiag) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/175863


More information about the llvm-commits mailing list