Skip to content
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

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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.

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.

2 participants