Skip to content

[stable/20211026] Support: Pass wrapped Error's error code through FileError #4100

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
Mar 23, 2022
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
7 changes: 5 additions & 2 deletions llvm/lib/Support/Error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ std::error_code inconvertibleErrorCode() {
}

std::error_code FileError::convertToErrorCode() const {
return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
*ErrorErrorCat);
std::error_code NestedEC = Err->convertToErrorCode();
if (NestedEC == inconvertibleErrorCode())
return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
*ErrorErrorCat);
return NestedEC;
}

Error errorCodeToError(std::error_code EC) {
Expand Down
27 changes: 27 additions & 0 deletions llvm/unittests/Support/ErrorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -963,6 +963,33 @@ TEST(Error, FileErrorTest) {
});
}

TEST(Error, FileErrorErrorCode) {
for (std::error_code EC : {
make_error_code(std::errc::not_supported),
make_error_code(std::errc::invalid_argument),
make_error_code(std::errc::no_such_file_or_directory),
}) {
EXPECT_EQ(EC, errorToErrorCode(
createFileError("file.bin", EC)));
EXPECT_EQ(EC, errorToErrorCode(
createFileError("file.bin", /*Line=*/5, EC)));
EXPECT_EQ(EC, errorToErrorCode(
createFileError("file.bin", errorCodeToError(EC))));
EXPECT_EQ(EC, errorToErrorCode(
createFileError("file.bin", /*Line=*/5, errorCodeToError(EC))));
}

// inconvertibleErrorCode() should be wrapped to avoid a fatal error.
EXPECT_EQ(
"A file error occurred.",
errorToErrorCode(createFileError("file.bin", inconvertibleErrorCode()))
.message());
EXPECT_EQ(
"A file error occurred.",
errorToErrorCode(createFileError("file.bin", /*Line=*/5, inconvertibleErrorCode()))
.message());
}

enum class test_error_code {
unspecified = 1,
error_1,
Expand Down