Skip to content

Commit 8430421

Browse files
AmrDeveloperbenlangmuir
authored andcommitted
[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 (cherry picked from commit 2464f4b)
1 parent 5842d95 commit 8430421

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1399,6 +1399,23 @@ inline Error createFileError(const Twine &F, size_t Line, std::error_code EC) {
13991399
return createFileError(F, Line, errorCodeToError(EC));
14001400
}
14011401

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

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

llvm/unittests/Support/ErrorTest.cpp

Lines changed: 11 additions & 0 deletions
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)