Skip to content

Commit b7bf7a5

Browse files
committed
[LLVM][Support] Add new CreateFileError functions
1 parent 1c7625b commit b7bf7a5

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

llvm/include/llvm/Support/Error.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1404,6 +1404,22 @@ 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 to it.
1408+
inline Error createFileError(const Twine &F, std::error_code EC,
1409+
const Twine &S) {
1410+
Error E = createStringError(EC, S);
1411+
return createFileError(F, std::move(E));
1412+
}
1413+
1414+
1415+
/// Create a StringError with the specified error code and prepend the file path to it.
1416+
template <typename... Ts>
1417+
inline Error createFileError(const Twine &F, std::error_code EC,
1418+
char const *Fmt, const Ts &...Vals) {
1419+
Error E = createStringError(EC, Fmt, Vals...);
1420+
return createFileError(F, std::move(E));
1421+
}
1422+
14071423
Error createFileError(const Twine &F, ErrorSuccess) = delete;
14081424

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

llvm/unittests/Support/ErrorTest.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -976,6 +976,14 @@ 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 = createFileError("file.bin", make_error_code(std::errc::invalid_argument), "invalid argument");
981+
EXPECT_EQ(toString(std::move(FE7)), "'file.bin': invalid argument");
982+
983+
StringRef Argument = "arg";
984+
Error FE8 = createFileError("file.bin", make_error_code(std::errc::invalid_argument),
985+
"invalid argument '%s'", Argument.str().c_str());
986+
EXPECT_EQ(toString(std::move(FE8)), "'file.bin': invalid argument 'arg'");
979987
}
980988

981989
TEST(Error, FileErrorErrorCode) {

0 commit comments

Comments
 (0)