Skip to content

Commit 2464f4b

Browse files
authored
[LLVM][Support] Add new CreateFileError functions (llvm#125906)
Add new CreateFileError functions to create a StringError with the specified error code and prepend the file path to it Needed for: llvm#125345
1 parent d0f472c commit 2464f4b

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

llvm/include/llvm/Support/Error.h

+17
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,23 @@ inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) {
14041404
return createFileError(F, Line, errorCodeToError(EC));
14051405
}
14061406

1407+
/// Create a StringError with the specified error code and prepend the file path
1408+
/// to it.
1409+
inline Error createFileError(const Twine &F, std::error_code EC,
1410+
const Twine &S) {
1411+
Error E = createStringError(EC, S);
1412+
return createFileError(F, std::move(E));
1413+
}
1414+
1415+
/// Create a StringError with the specified error code and prepend the file path
1416+
/// to it.
1417+
template <typename... Ts>
1418+
inline Error createFileError(const Twine &F, std::error_code EC,
1419+
char const *Fmt, const Ts &...Vals) {
1420+
Error E = createStringError(EC, Fmt, Vals...);
1421+
return createFileError(F, std::move(E));
1422+
}
1423+
14071424
Error createFileError(const Twine &F, ErrorSuccess) = delete;
14081425

14091426
/// Helper for check-and-exit error handling.

llvm/unittests/Support/ErrorTest.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,17 @@ TEST(Error, FileErrorTest) {
976976
handleAllErrors(std::move(FE6), [](std::unique_ptr<FileError> F) {
977977
EXPECT_EQ(F->messageWithoutFileInfo(), "CustomError {6}");
978978
});
979+
980+
Error FE7 =
981+
createFileError("file.bin", make_error_code(std::errc::invalid_argument),
982+
"invalid argument");
983+
EXPECT_EQ(toString(std::move(FE7)), "'file.bin': invalid argument");
984+
985+
StringRef Argument = "arg";
986+
Error FE8 =
987+
createFileError("file.bin", make_error_code(std::errc::invalid_argument),
988+
"invalid argument '%s'", Argument.str().c_str());
989+
EXPECT_EQ(toString(std::move(FE8)), "'file.bin': invalid argument 'arg'");
979990
}
980991

981992
TEST(Error, FileErrorErrorCode) {

0 commit comments

Comments
 (0)