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

…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
This commit is contained in:
Ebuka Ezike
2026-01-14 09:25:28 +00:00
committed by GitHub
parent f4e74b6e4e
commit 75c597aa50
2 changed files with 3 additions and 3 deletions

View File

@@ -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);

View File

@@ -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) {