Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions llvm/include/llvm/Support/Error.h
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,23 @@ 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.
Expand Down
11 changes: 11 additions & 0 deletions llvm/unittests/Support/ErrorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -976,6 +976,17 @@ 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) {
Expand Down