Skip to content

Commit c15c8fe

Browse files
dexonsmithbnbarham
authored andcommitted
Support: Pass wrapped Error's error code through FileError
Change FileError to pass through the error code from the Error it wraps. This allows APIs that return ECError to transition to FileError without changing returned std::error_code. This was extracted from https://reviews.llvm.org/D109345. Differential Revision: https://reviews.llvm.org/D113225
1 parent 96968bd commit c15c8fe

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

llvm/lib/Support/Error.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,11 @@ std::error_code inconvertibleErrorCode() {
8080
}
8181

8282
std::error_code FileError::convertToErrorCode() const {
83-
return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
84-
*ErrorErrorCat);
83+
std::error_code NestedEC = Err->convertToErrorCode();
84+
if (NestedEC == inconvertibleErrorCode())
85+
return std::error_code(static_cast<int>(ErrorErrorCode::FileError),
86+
*ErrorErrorCat);
87+
return NestedEC;
8588
}
8689

8790
Error errorCodeToError(std::error_code EC) {

llvm/unittests/Support/ErrorTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -963,6 +963,33 @@ TEST(Error, FileErrorTest) {
963963
});
964964
}
965965

966+
TEST(Error, FileErrorErrorCode) {
967+
for (std::error_code EC : {
968+
make_error_code(std::errc::not_supported),
969+
make_error_code(std::errc::invalid_argument),
970+
make_error_code(std::errc::no_such_file_or_directory),
971+
}) {
972+
EXPECT_EQ(EC, errorToErrorCode(
973+
createFileError("file.bin", EC)));
974+
EXPECT_EQ(EC, errorToErrorCode(
975+
createFileError("file.bin", /*Line=*/5, EC)));
976+
EXPECT_EQ(EC, errorToErrorCode(
977+
createFileError("file.bin", errorCodeToError(EC))));
978+
EXPECT_EQ(EC, errorToErrorCode(
979+
createFileError("file.bin", /*Line=*/5, errorCodeToError(EC))));
980+
}
981+
982+
// inconvertibleErrorCode() should be wrapped to avoid a fatal error.
983+
EXPECT_EQ(
984+
"A file error occurred.",
985+
errorToErrorCode(createFileError("file.bin", inconvertibleErrorCode()))
986+
.message());
987+
EXPECT_EQ(
988+
"A file error occurred.",
989+
errorToErrorCode(createFileError("file.bin", /*Line=*/5, inconvertibleErrorCode()))
990+
.message());
991+
}
992+
966993
enum class test_error_code {
967994
unspecified = 1,
968995
error_1,

0 commit comments

Comments
 (0)