Skip to content

[LLVM][Support] Add new CreateFileError functions #125906

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

Conversation

AmrDeveloper
Copy link
Member

Add new CreateFileError functions to create a StringError with the specified error code and prepend the file path to it

Needed for: #125345

@llvmbot
Copy link
Member

llvmbot commented Feb 5, 2025

@llvm/pr-subscribers-llvm-support

Author: Amr Hesham (AmrDeveloper)

Changes

Add new CreateFileError functions to create a StringError with the specified error code and prepend the file path to it

Needed for: #125345


Full diff: https://github.com/llvm/llvm-project/pull/125906.diff

2 Files Affected:

  • (modified) llvm/include/llvm/Support/Error.h (+16)
  • (modified) llvm/unittests/Support/ErrorTest.cpp (+8)
diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index 90120156ec2ead..48e17aabc1afda 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1404,6 +1404,22 @@ inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) {
   return createFileError(F, Line, errorCodeToError(EC));
 }
 
+/// Create a StringError with the specified error code and prepend the file path to it.
+inline Error createFileError(const Twine &F, std::error_code EC,
+                             const Twine &S) {
+  Error E = createStringError(EC, S);
+  return createFileError(F, std::move(E));
+}
+
+
+/// Create a StringError with the specified error code and prepend the file path to it.
+template <typename... Ts>
+inline Error createFileError(const Twine &F, std::error_code EC,
+                             char const *Fmt, const Ts &...Vals) {
+  Error E = createStringError(EC, Fmt, Vals...);
+  return createFileError(F, std::move(E));
+}
+
 Error createFileError(const Twine &F, ErrorSuccess) = delete;
 
 /// Helper for check-and-exit error handling.
diff --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp
index 98d19e8d2a15a3..b84fd621f76d43 100644
--- a/llvm/unittests/Support/ErrorTest.cpp
+++ b/llvm/unittests/Support/ErrorTest.cpp
@@ -976,6 +976,14 @@ TEST(Error, FileErrorTest) {
   handleAllErrors(std::move(FE6), [](std::unique_ptr<FileError> F) {
     EXPECT_EQ(F->messageWithoutFileInfo(), "CustomError {6}");
   });
+
+  Error FE7 = createFileError("file.bin", make_error_code(std::errc::invalid_argument), "invalid argument");
+  EXPECT_EQ(toString(std::move(FE7)), "'file.bin': invalid argument");
+
+  StringRef Argument = "arg";
+  Error FE8 = createFileError("file.bin", make_error_code(std::errc::invalid_argument),
+                              "invalid argument '%s'", Argument.str().c_str());
+  EXPECT_EQ(toString(std::move(FE8)), "'file.bin': invalid argument 'arg'");
 }
 
 TEST(Error, FileErrorErrorCode) {

Copy link

github-actions bot commented Feb 5, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

Copy link
Collaborator

@jh7370 jh7370 left a comment

Choose a reason for hiding this comment

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

LGTM. Do you need me to merge for you?

@AmrDeveloper
Copy link
Member Author

AmrDeveloper commented Feb 6, 2025

LGTM. Do you need me to merge for you?

Thank you, I can merge

@AmrDeveloper AmrDeveloper merged commit 2464f4b into llvm:main Feb 6, 2025
8 checks passed
swift-ci pushed a commit to swiftlang/llvm-project that referenced this pull request Feb 10, 2025
Add new CreateFileError functions to create a StringError with the
specified error code and prepend the file path to it

Needed for: llvm#125345

(cherry picked from commit 2464f4b)
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
Add new CreateFileError functions to create a StringError with the
specified error code and prepend the file path to it

Needed for: llvm#125345
benlangmuir pushed a commit to benlangmuir/llvm-project that referenced this pull request May 1, 2025
Add new CreateFileError functions to create a StringError with the
specified error code and prepend the file path to it

Needed for: llvm#125345

(cherry picked from commit 2464f4b)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants