Skip to content

[Error] Add non-consuming toString #95375

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 3 commits into from
Jun 14, 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
28 changes: 28 additions & 0 deletions llvm/include/llvm/Support/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,9 @@ class [[nodiscard]] Error {
// handleErrors needs to be able to set the Checked flag.
template <typename... HandlerTs>
friend Error handleErrors(Error E, HandlerTs &&... Handlers);
// visitErrors needs direct access to the payload.
template <typename HandlerT>
friend void visitErrors(const Error &E, HandlerT H);

// Expected<T> needs to be able to steal the payload when constructed from an
// error.
Expand Down Expand Up @@ -369,6 +372,10 @@ class ErrorList final : public ErrorInfo<ErrorList> {
// ErrorList.
template <typename... HandlerTs>
friend Error handleErrors(Error E, HandlerTs &&... Handlers);
// visitErrors needs to be able to iterate the payload list of an
// ErrorList.
template <typename HandlerT>
friend void visitErrors(const Error &E, HandlerT H);

// joinErrors is implemented in terms of join.
friend Error joinErrors(Error, Error);
Expand Down Expand Up @@ -977,6 +984,23 @@ inline void handleAllErrors(Error E) {
cantFail(std::move(E));
}

/// Visit all the ErrorInfo(s) contained in E by passing them to the respective
/// handler, without consuming the error.
template <typename HandlerT> void visitErrors(const Error &E, HandlerT H) {
const ErrorInfoBase *Payload = E.getPtr();
if (!Payload)
return;

if (Payload->isA<ErrorList>()) {
const ErrorList &List = static_cast<const ErrorList &>(*Payload);
for (const auto &P : List.Payloads)
H(*P);
return;
}

return H(*Payload);
}

/// Handle any errors (if present) in an Expected<T>, then try a recovery path.
///
/// If the incoming value is a success value it is returned unmodified. If it
Expand Down Expand Up @@ -1031,6 +1055,10 @@ void logAllUnhandledErrors(Error E, raw_ostream &OS, Twine ErrorBanner = {});
/// is used to separate error messages.
std::string toString(Error E);

/// Like toString(), but does not consume the error. This can be used to print
/// a warning while retaining the original error object.
std::string toStringWithoutConsuming(const Error &E);

/// Consume a Error without doing anything. This method should be used
/// only where an error can be considered a reasonable and expected return
/// value.
Expand Down
8 changes: 8 additions & 0 deletions llvm/lib/Support/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,14 @@ std::string toString(Error E) {
return join(Errors.begin(), Errors.end(), "\n");
}

std::string toStringWithoutConsuming(const Error &E) {
SmallVector<std::string, 2> Errors;
visitErrors(E, [&Errors](const ErrorInfoBase &EI) {
Errors.push_back(EI.message());
});
return join(Errors.begin(), Errors.end(), "\n");
}

std::error_code ErrorList::convertToErrorCode() const {
return std::error_code(static_cast<int>(ErrorErrorCode::MultipleErrors),
getErrorErrorCat());
Expand Down
4 changes: 2 additions & 2 deletions llvm/tools/dsymutil/DwarfLinkerForBinary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
if (!ObjectEntry) {
auto Err = ObjectEntry.takeError();
reportWarning(Twine(Obj.getObjectFilename()) + ": " +
toString(std::move(Err)),
toStringWithoutConsuming(Err),
Obj.getObjectFilename());
return errorToErrorCode(std::move(Err));
}
Expand All @@ -156,7 +156,7 @@ DwarfLinkerForBinary::loadObject(const DebugMapObject &Obj,
if (!Object) {
auto Err = Object.takeError();
reportWarning(Twine(Obj.getObjectFilename()) + ": " +
toString(std::move(Err)),
toStringWithoutConsuming(Err),
Obj.getObjectFilename());
return errorToErrorCode(std::move(Err));
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/tools/llvm-dwp/llvm-dwp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ getDWOFilenames(StringRef ExecFilename) {
std::string DWOCompDir =
dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), "");
if (!DWOCompDir.empty()) {
SmallString<16> DWOPath(std::move(DWOName));
SmallString<16> DWOPath(DWOName);
sys::fs::make_absolute(DWOCompDir, DWOPath);
if (!sys::fs::exists(DWOPath) && sys::fs::exists(DWOName))
DWOPaths.push_back(std::move(DWOName));
Expand Down
4 changes: 2 additions & 2 deletions llvm/tools/llvm-pdbutil/DumpOutputStyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1494,13 +1494,13 @@ Error DumpOutputStyle::dumpModuleSymsForPdb() {
if (auto EC = Visitor.visitSymbolStreamFiltered(ModS.getSymbolArray(),
Filter)) {
P.formatLine("Error while processing symbol records. {0}",
toString(std::move(EC)));
toStringWithoutConsuming(EC));
return EC;
}
} else if (auto EC = Visitor.visitSymbolStream(ModS.getSymbolArray(),
SS.Offset)) {
P.formatLine("Error while processing symbol records. {0}",
toString(std::move(EC)));
toStringWithoutConsuming(EC));
return EC;
}
return Error::success();
Expand Down
10 changes: 10 additions & 0 deletions llvm/unittests/Support/ErrorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -740,15 +740,25 @@ TEST(Error, ErrorCodeConversions) {
TEST(Error, ErrorMessage) {
EXPECT_EQ(toString(Error::success()), "");
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd want to keep this, just to have one example that doesn't go through toStringWithoutConsuming or visitErrors.


Error E0 = Error::success();
EXPECT_EQ(toStringWithoutConsuming(E0), "");
EXPECT_EQ(toString(std::move(E0)), "");

Error E1 = make_error<CustomError>(0);
EXPECT_EQ(toStringWithoutConsuming(E1), "CustomError {0}");
EXPECT_EQ(toString(std::move(E1)), "CustomError {0}");

Error E2 = make_error<CustomError>(0);
visitErrors(E2, [](const ErrorInfoBase &EI) {
EXPECT_EQ(EI.message(), "CustomError {0}");
});
handleAllErrors(std::move(E2), [](const CustomError &CE) {
EXPECT_EQ(CE.message(), "CustomError {0}");
});

Error E3 = joinErrors(make_error<CustomError>(0), make_error<CustomError>(1));
EXPECT_EQ(toStringWithoutConsuming(E3), "CustomError {0}\n"
"CustomError {1}");
EXPECT_EQ(toString(std::move(E3)), "CustomError {0}\n"
"CustomError {1}");
}
Expand Down
Loading