Skip to content

Commit f303520

Browse files
committed
Avoid std::string -> (char *) roundtrip in createStringError() (NFC) (llvm#93242)
The current API first creates a temporary std::string, then passes it as a C string, only to then convert it into a std::string for storage, thus unnecessarily computing the length of the string and copying it. (cherry picked from commit 9223ccb)
1 parent 860935d commit f303520

File tree

2 files changed

+15
-9
lines changed

2 files changed

+15
-9
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,10 +1216,10 @@ class StringError : public ErrorInfo<StringError> {
12161216
public:
12171217
static char ID;
12181218

1219-
// Prints EC + S and converts to EC
1219+
StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly);
1220+
/// Prints EC + S and converts to EC.
12201221
StringError(std::error_code EC, const Twine &S = Twine());
1221-
1222-
// Prints S and converts to EC
1222+
/// Prints S and converts to EC.
12231223
StringError(const Twine &S, std::error_code EC);
12241224

12251225
void log(raw_ostream &OS) const override;
@@ -1238,15 +1238,18 @@ template <typename... Ts>
12381238
inline Error createStringError(std::error_code EC, char const *Fmt,
12391239
const Ts &... Vals) {
12401240
std::string Buffer;
1241-
raw_string_ostream Stream(Buffer);
1242-
Stream << format(Fmt, Vals...);
1243-
return make_error<StringError>(Stream.str(), EC);
1241+
raw_string_ostream(Buffer) << format(Fmt, Vals...);
1242+
return make_error<StringError>(Buffer, EC);
12441243
}
12451244

1246-
Error createStringError(std::error_code EC, char const *Msg);
1245+
Error createStringError(std::string &&Msg, std::error_code EC);
1246+
1247+
inline Error createStringError(std::error_code EC, const char *S) {
1248+
return createStringError(std::string(S), EC);
1249+
}
12471250

12481251
inline Error createStringError(std::error_code EC, const Twine &S) {
1249-
return createStringError(EC, S.str().c_str());
1252+
return createStringError(S.str(), EC);
12501253
}
12511254

12521255
/// Create a StringError with an inconvertible error code.

llvm/lib/Support/Error.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,9 @@ StringError::StringError(std::error_code EC, const Twine &S)
135135
StringError::StringError(const Twine &S, std::error_code EC)
136136
: Msg(S.str()), EC(EC), PrintMsgOnly(true) {}
137137

138+
StringError::StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly)
139+
: Msg(S), EC(EC), PrintMsgOnly(PrintMsgOnly) {}
140+
138141
void StringError::log(raw_ostream &OS) const {
139142
if (PrintMsgOnly) {
140143
OS << Msg;
@@ -149,7 +152,7 @@ std::error_code StringError::convertToErrorCode() const {
149152
return EC;
150153
}
151154

152-
Error createStringError(std::error_code EC, char const *Msg) {
155+
Error createStringError(std::string &&Msg, std::error_code EC) {
153156
return make_error<StringError>(Msg, EC);
154157
}
155158

0 commit comments

Comments
 (0)