[PATCH] D113225: Support: Pass wrapped Error's error code through FileError

Duncan P. N. Exon Smith via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 4 16:14:35 PDT 2021


dexonsmith created this revision.
dexonsmith added reviewers: dblaikie, lhames.
Herald added a subscriber: hiraditya.
dexonsmith requested review of this revision.
Herald added a project: LLVM.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113225

Files:
  llvm/lib/Support/Error.cpp
  llvm/unittests/Support/ErrorTest.cpp


Index: llvm/unittests/Support/ErrorTest.cpp
===================================================================
--- llvm/unittests/Support/ErrorTest.cpp
+++ llvm/unittests/Support/ErrorTest.cpp
@@ -963,6 +963,33 @@
   });
 }
 
+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,
Index: llvm/lib/Support/Error.cpp
===================================================================
--- llvm/lib/Support/Error.cpp
+++ llvm/lib/Support/Error.cpp
@@ -80,8 +80,11 @@
 }
 
 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) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113225.384899.patch
Type: text/x-patch
Size: 2035 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211104/0c7a6a23/attachment.bin>


More information about the llvm-commits mailing list