From b7bf7a539eec38b902d41a3762a5d7fe3ab8a1cc Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Wed, 5 Feb 2025 19:39:29 +0100 Subject: [PATCH 1/2] [LLVM][Support] Add new CreateFileError functions --- llvm/include/llvm/Support/Error.h | 16 ++++++++++++++++ llvm/unittests/Support/ErrorTest.cpp | 8 ++++++++ 2 files changed, 24 insertions(+) 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 +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 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) { From 4d22018c798b96110da1a2d70da2ddd6322476d0 Mon Sep 17 00:00:00 2001 From: AmrDeveloper Date: Wed, 5 Feb 2025 19:59:36 +0100 Subject: [PATCH 2/2] Fix code format --- llvm/include/llvm/Support/Error.h | 7 ++++--- llvm/unittests/Support/ErrorTest.cpp | 9 ++++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h index 48e17aabc1afda..c1b809a09bb80e 100644 --- a/llvm/include/llvm/Support/Error.h +++ b/llvm/include/llvm/Support/Error.h @@ -1404,15 +1404,16 @@ 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. +/// 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. +/// Create a StringError with the specified error code and prepend the file path +/// to it. template inline Error createFileError(const Twine &F, std::error_code EC, char const *Fmt, const Ts &...Vals) { diff --git a/llvm/unittests/Support/ErrorTest.cpp b/llvm/unittests/Support/ErrorTest.cpp index b84fd621f76d43..00c562ecc059d3 100644 --- a/llvm/unittests/Support/ErrorTest.cpp +++ b/llvm/unittests/Support/ErrorTest.cpp @@ -977,12 +977,15 @@ TEST(Error, FileErrorTest) { EXPECT_EQ(F->messageWithoutFileInfo(), "CustomError {6}"); }); - Error FE7 = createFileError("file.bin", make_error_code(std::errc::invalid_argument), "invalid argument"); + 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()); + 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'"); }