From: Eugene Leviant Date: Mon, 11 Feb 2019 09:49:37 +0000 (+0000) Subject: Small refactoring of FileError. NFC. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aed0779715d36927a2c3e68ad56ae1200612b8e5;p=llvm Small refactoring of FileError. NFC. Differential revision: https://reviews.llvm.org/D57945 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@353679 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Support/Error.h b/include/llvm/Support/Error.h index b2517983c8b..a9f131f1ef6 100644 --- a/include/llvm/Support/Error.h +++ b/include/llvm/Support/Error.h @@ -1176,7 +1176,7 @@ Error createStringError(std::error_code EC, char const *Msg); /// show more detailed information to the user. class FileError final : public ErrorInfo { - friend Error createFileError(std::string, Error); + friend Error createFileError(const Twine &, Error); public: void log(raw_ostream &OS) const override { @@ -1193,15 +1193,15 @@ public: static char ID; private: - FileError(std::string F, std::unique_ptr E) { + FileError(const Twine &F, std::unique_ptr E) { assert(E && "Cannot create FileError from Error success value."); assert(!F.empty() && "The file name provided to FileError must not be empty."); - FileName = F; + FileName = F.str(); Err = std::move(E); } - static Error build(std::string F, Error E) { + static Error build(const Twine &F, Error E) { return Error(std::unique_ptr(new FileError(F, E.takePayload()))); } @@ -1211,11 +1211,17 @@ private: /// Concatenate a source file path and/or name with an Error. The resulting /// Error is unchecked. -inline Error createFileError(std::string F, Error E) { +inline Error createFileError(const Twine &F, Error E) { return FileError::build(F, std::move(E)); } -Error createFileError(std::string F, ErrorSuccess) = delete; +/// Concatenate a source file path and/or name with a std::error_code +/// to form an Error object. +inline Error createFileError(const Twine &F, std::error_code EC) { + return createFileError(F, errorCodeToError(EC)); +} + +Error createFileError(const Twine &F, ErrorSuccess) = delete; /// Helper for check-and-exit error handling. /// diff --git a/tools/llvm-objcopy/CopyConfig.cpp b/tools/llvm-objcopy/CopyConfig.cpp index a2a58f20fe9..f643cb449a6 100644 --- a/tools/llvm-objcopy/CopyConfig.cpp +++ b/tools/llvm-objcopy/CopyConfig.cpp @@ -263,7 +263,7 @@ static Error addSymbolsToRenameFromFile(StringMap &SymbolsToRename, SmallVector Lines; auto BufOrErr = MemoryBuffer::getFile(Filename); if (!BufOrErr) - return createError(Filename, BufOrErr.getError()); + return createFileError(Filename, BufOrErr.getError()); BufOrErr.get()->getBuffer().split(Lines, '\n'); size_t NumLines = Lines.size(); diff --git a/tools/llvm-objcopy/llvm-objcopy.cpp b/tools/llvm-objcopy/llvm-objcopy.cpp index 72774ec6f3c..fdf1c305b61 100644 --- a/tools/llvm-objcopy/llvm-objcopy.cpp +++ b/tools/llvm-objcopy/llvm-objcopy.cpp @@ -52,11 +52,6 @@ namespace objcopy { // The name this program was invoked as. StringRef ToolName; -Error createError(StringRef File, std::error_code EC) { - assert(EC); - return createFileError(File, make_error(EC)); -} - LLVM_ATTRIBUTE_NORETURN void error(Twine Message) { WithColor::error(errs(), ToolName) << Message << ".\n"; errs().flush(); @@ -74,7 +69,8 @@ LLVM_ATTRIBUTE_NORETURN void error(Error E) { } LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, std::error_code EC) { - error(createError(File, EC)); + assert(EC); + error(createFileError(File, EC)); } LLVM_ATTRIBUTE_NORETURN void reportError(StringRef File, Error E) { diff --git a/tools/llvm-objcopy/llvm-objcopy.h b/tools/llvm-objcopy/llvm-objcopy.h index 4783e7804b5..18a789ca1f8 100644 --- a/tools/llvm-objcopy/llvm-objcopy.h +++ b/tools/llvm-objcopy/llvm-objcopy.h @@ -18,8 +18,6 @@ namespace llvm { namespace objcopy { -Error createError(StringRef File, std::error_code EC); - LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message); LLVM_ATTRIBUTE_NORETURN extern void error(Error E); LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E);