Skip to content

Avoid std::string -> (char *) roundtrip in createStringError() (NFC) #93242

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions llvm/include/llvm/Support/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -1236,10 +1236,10 @@ class StringError : public ErrorInfo<StringError> {
public:
static char ID;

// Prints EC + S and converts to EC
StringError(std::string &&S, std::error_code EC, bool PrintMsgOnly);
/// Prints EC + S and converts to EC.
StringError(std::error_code EC, const Twine &S = Twine());

// Prints S and converts to EC
/// Prints S and converts to EC.
StringError(const Twine &S, std::error_code EC);

void log(raw_ostream &OS) const override;
Expand All @@ -1258,15 +1258,18 @@ template <typename... Ts>
inline Error createStringError(std::error_code EC, char const *Fmt,
const Ts &... Vals) {
std::string Buffer;
raw_string_ostream Stream(Buffer);
Stream << format(Fmt, Vals...);
return make_error<StringError>(Stream.str(), EC);
raw_string_ostream(Buffer) << format(Fmt, Vals...);
return make_error<StringError>(Buffer, EC);
}

Error createStringError(std::error_code EC, char const *Msg);
Error createStringError(std::string &&Msg, std::error_code EC);

inline Error createStringError(std::error_code EC, const char *S) {
return createStringError(std::string(S), EC);
}

inline Error createStringError(std::error_code EC, const Twine &S) {
return createStringError(EC, S.str().c_str());
return createStringError(S.str(), EC);
}

/// Create a StringError with an inconvertible error code.
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Support/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,9 @@ StringError::StringError(std::error_code EC, const Twine &S)
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) {}

void StringError::log(raw_ostream &OS) const {
if (PrintMsgOnly) {
OS << Msg;
Expand All @@ -149,7 +152,7 @@ std::error_code StringError::convertToErrorCode() const {
return EC;
}

Error createStringError(std::error_code EC, char const *Msg) {
Error createStringError(std::string &&Msg, std::error_code EC) {
return make_error<StringError>(Msg, EC);
}

Expand Down
Loading