From: Pete Cooper Date: Wed, 30 Mar 2016 20:56:54 +0000 (+0000) Subject: Convert file handle* methods to llvm::Error instead of std::error_code. NFC. X-Git-Tag: llvmorg-3.9.0-rc1~10472 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=3c40b5b75010682f7ff520ce9426bbdb3212d390;p=platform%2Fupstream%2Fllvm.git Convert file handle* methods to llvm::Error instead of std::error_code. NFC. This updates most of the file handling methods in the linking context and resolver to use the new API. llvm-svn: 264924 --- diff --git a/lld/include/lld/Core/LinkingContext.h b/lld/include/lld/Core/LinkingContext.h index 7227b17..cfbc239 100644 --- a/lld/include/lld/Core/LinkingContext.h +++ b/lld/include/lld/Core/LinkingContext.h @@ -208,7 +208,7 @@ public: /// errors for any differences between the context state and a loaded file. /// For example, we can error if we try to load a file which is a different /// arch from that being linked. - virtual std::error_code handleLoadedFile(File &file) = 0; + virtual llvm::Error handleLoadedFile(File &file) = 0; /// @} protected: diff --git a/lld/include/lld/Core/Resolver.h b/lld/include/lld/Core/Resolver.h index 33892e9..fb62a77 100644 --- a/lld/include/lld/Core/Resolver.h +++ b/lld/include/lld/Core/Resolver.h @@ -42,13 +42,13 @@ public: // Handle files, this adds atoms from the current file thats // being processed by the resolver - ErrorOr handleFile(File &); + llvm::Expected handleFile(File &); // Handle an archive library file. - ErrorOr handleArchiveFile(File &); + llvm::Expected handleArchiveFile(File &); // Handle a shared library file. - std::error_code handleSharedLibrary(File &); + llvm::Error handleSharedLibrary(File &); /// @brief do work of merging and resolving and return list bool resolve(); @@ -56,7 +56,7 @@ public: std::unique_ptr resultFile() { return std::move(_result); } private: - typedef std::function(StringRef)> UndefCallback; + typedef std::function(StringRef)> UndefCallback; bool undefinesAdded(int begin, int end); File *getFile(int &index); @@ -67,7 +67,7 @@ private: void deadStripOptimize(); bool checkUndefines(); void removeCoalescedAwayAtoms(); - ErrorOr forEachUndefines(File &file, UndefCallback callback); + llvm::Expected forEachUndefines(File &file, UndefCallback callback); void markLive(const Atom *atom); diff --git a/lld/include/lld/ReaderWriter/MachOLinkingContext.h b/lld/include/lld/ReaderWriter/MachOLinkingContext.h index 4d1233f..acb6613 100644 --- a/lld/include/lld/ReaderWriter/MachOLinkingContext.h +++ b/lld/include/lld/ReaderWriter/MachOLinkingContext.h @@ -407,7 +407,7 @@ public: void finalizeInputFiles() override; - std::error_code handleLoadedFile(File &file) override; + llvm::Error handleLoadedFile(File &file) override; bool customAtomOrderer(const DefinedAtom *left, const DefinedAtom *right, bool &leftBeforeRight) const; diff --git a/lld/lib/Core/Resolver.cpp b/lld/lib/Core/Resolver.cpp index 7d2aada..7619e4c 100644 --- a/lld/lib/Core/Resolver.cpp +++ b/lld/lib/Core/Resolver.cpp @@ -19,6 +19,7 @@ #include "lld/Core/UndefinedAtom.h" #include "llvm/ADT/iterator_range.h" #include "llvm/Support/Debug.h" +#include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/Format.h" #include "llvm/Support/raw_ostream.h" @@ -29,9 +30,9 @@ namespace lld { -ErrorOr Resolver::handleFile(File &file) { +llvm::Expected Resolver::handleFile(File &file) { if (auto ec = _ctx.handleLoadedFile(file)) - return ec; + return std::move(ec); bool undefAdded = false; for (auto &atom : file.defined().owning_ptrs()) doDefinedAtom(std::move(atom)); @@ -46,7 +47,8 @@ ErrorOr Resolver::handleFile(File &file) { return undefAdded; } -ErrorOr Resolver::forEachUndefines(File &file, UndefCallback callback) { +llvm::Expected Resolver::forEachUndefines(File &file, + UndefCallback callback) { size_t i = _undefineIndex[&file]; bool undefAdded = false; do { @@ -61,8 +63,8 @@ ErrorOr Resolver::forEachUndefines(File &file, UndefCallback callback) { continue; } auto undefAddedOrError = callback(undefName); - if (undefAddedOrError.getError()) - return undefAddedOrError; + if (auto ec = undefAddedOrError.takeError()) + return std::move(ec); undefAdded |= undefAddedOrError.get(); } } while (i < _undefines.size()); @@ -70,9 +72,10 @@ ErrorOr Resolver::forEachUndefines(File &file, UndefCallback callback) { return undefAdded; } -ErrorOr Resolver::handleArchiveFile(File &file) { +llvm::Expected Resolver::handleArchiveFile(File &file) { ArchiveLibraryFile *archiveFile = cast(&file); - return forEachUndefines(file, [&](StringRef undefName) -> ErrorOr { + return forEachUndefines(file, + [&](StringRef undefName) -> llvm::Expected { if (File *member = archiveFile->find(undefName)) { member->setOrdinal(_ctx.getNextOrdinalAndIncrement()); return handleFile(*member); @@ -81,23 +84,23 @@ ErrorOr Resolver::handleArchiveFile(File &file) { }); } -std::error_code Resolver::handleSharedLibrary(File &file) { +llvm::Error Resolver::handleSharedLibrary(File &file) { // Add all the atoms from the shared library SharedLibraryFile *sharedLibrary = cast(&file); auto undefAddedOrError = handleFile(*sharedLibrary); - if (undefAddedOrError.getError()) - return undefAddedOrError.getError(); + if (auto ec = undefAddedOrError.takeError()) + return std::move(ec); undefAddedOrError = - forEachUndefines(file, [&](StringRef undefName) -> ErrorOr { + forEachUndefines(file, [&](StringRef undefName) -> llvm::Expected { auto atom = sharedLibrary->exports(undefName); if (atom.get()) doSharedLibraryAtom(std::move(atom)); return false; }); - if (undefAddedOrError.getError()) - return undefAddedOrError.getError(); - return std::error_code(); + if (auto ec = undefAddedOrError.takeError()) + return std::move(ec); + return llvm::Error(); } bool Resolver::doUndefinedAtom(OwningAtomPtr atom) { @@ -247,9 +250,11 @@ bool Resolver::resolveUndefines() { assert(!file->hasOrdinal()); file->setOrdinal(_ctx.getNextOrdinalAndIncrement()); auto undefAddedOrError = handleFile(*file); - if (undefAddedOrError.getError()) { - llvm::errs() << "Error in " + file->path() - << ": " << undefAddedOrError.getError().message() << "\n"; + if (auto EC = undefAddedOrError.takeError()) { + // FIXME: This should be passed to logAllUnhandledErrors but it needs + // to be passed a Twine instead of a string. + llvm::errs() << "Error in " + file->path() << ": "; + logAllUnhandledErrors(std::move(EC), llvm::errs(), std::string()); return false; } undefAdded = undefAddedOrError.get(); @@ -259,9 +264,11 @@ bool Resolver::resolveUndefines() { if (!file->hasOrdinal()) file->setOrdinal(_ctx.getNextOrdinalAndIncrement()); auto undefAddedOrError = handleArchiveFile(*file); - if (undefAddedOrError.getError()) { - llvm::errs() << "Error in " + file->path() - << ": " << undefAddedOrError.getError().message() << "\n"; + if (auto EC = undefAddedOrError.takeError()) { + // FIXME: This should be passed to logAllUnhandledErrors but it needs + // to be passed a Twine instead of a string. + llvm::errs() << "Error in " + file->path() << ": "; + logAllUnhandledErrors(std::move(EC), llvm::errs(), std::string()); return false; } undefAdded = undefAddedOrError.get(); @@ -271,8 +278,10 @@ bool Resolver::resolveUndefines() { if (!file->hasOrdinal()) file->setOrdinal(_ctx.getNextOrdinalAndIncrement()); if (auto EC = handleSharedLibrary(*file)) { - llvm::errs() << "Error in " + file->path() - << ": " << EC.message() << "\n"; + // FIXME: This should be passed to logAllUnhandledErrors but it needs + // to be passed a Twine instead of a string. + llvm::errs() << "Error in " + file->path() << ": "; + logAllUnhandledErrors(std::move(EC), llvm::errs(), std::string()); return false; } break; diff --git a/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp b/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp index 4431f34..67929c9 100644 --- a/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp +++ b/lld/lib/ReaderWriter/MachO/MachOLinkingContext.cpp @@ -1040,10 +1040,10 @@ void MachOLinkingContext::finalizeInputFiles() { elements.push_back(llvm::make_unique(numLibs)); } -std::error_code MachOLinkingContext::handleLoadedFile(File &file) { +llvm::Error MachOLinkingContext::handleLoadedFile(File &file) { auto *machoFile = dyn_cast(&file); if (!machoFile) - return std::error_code(); + return llvm::Error(); // Check that the arch of the context matches that of the file. // Also set the arch of the context if it didn't have one. @@ -1051,7 +1051,7 @@ std::error_code MachOLinkingContext::handleLoadedFile(File &file) { _arch = machoFile->arch(); } else if (machoFile->arch() != arch_unknown && machoFile->arch() != _arch) { // Archs are different. - return make_dynamic_error_code(file.path() + + return llvm::make_error(file.path() + Twine(" cannot be linked due to incompatible architecture")); } @@ -1061,7 +1061,7 @@ std::error_code MachOLinkingContext::handleLoadedFile(File &file) { _os = machoFile->OS(); } else if (machoFile->OS() != OS::unknown && machoFile->OS() != _os) { // OSes are different. - return make_dynamic_error_code(file.path() + + return llvm::make_error(file.path() + Twine(" cannot be linked due to incompatible operating systems")); } @@ -1078,7 +1078,7 @@ std::error_code MachOLinkingContext::handleLoadedFile(File &file) { // The file is built with simulator objc, so make sure that the context // is also building with simulator support. if (_os != OS::iOS_simulator) - return make_dynamic_error_code(file.path() + + return llvm::make_error(file.path() + Twine(" cannot be linked. It contains ObjC built for the simulator" " while we are linking a non-simulator target")); assert((_objcConstraint == objc_unknown || @@ -1090,7 +1090,7 @@ std::error_code MachOLinkingContext::handleLoadedFile(File &file) { // The file is built without simulator objc, so make sure that the // context is also building without simulator support. if (_os == OS::iOS_simulator) - return make_dynamic_error_code(file.path() + + return llvm::make_error(file.path() + Twine(" cannot be linked. It contains ObjC built for a non-simulator" " target while we are linking a simulator target")); assert((_objcConstraint == objc_unknown || @@ -1107,10 +1107,10 @@ std::error_code MachOLinkingContext::handleLoadedFile(File &file) { } else if (machoFile->swiftVersion() && machoFile->swiftVersion() != _swiftVersion) { // Swift versions are different. - return make_dynamic_error_code("different swift versions"); + return llvm::make_error("different swift versions"); } - return std::error_code(); + return llvm::Error(); } } // end namespace lld