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/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<bool> Resolver::handleFile(File &file) {
+llvm::Expected<bool> 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 @@
return undefAdded;
}
-ErrorOr<bool> Resolver::forEachUndefines(File &file, UndefCallback callback) {
+llvm::Expected<bool> Resolver::forEachUndefines(File &file,
+ UndefCallback callback) {
size_t i = _undefineIndex[&file];
bool undefAdded = false;
do {
@@ -61,8 +63,8 @@
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 @@
return undefAdded;
}
-ErrorOr<bool> Resolver::handleArchiveFile(File &file) {
+llvm::Expected<bool> Resolver::handleArchiveFile(File &file) {
ArchiveLibraryFile *archiveFile = cast<ArchiveLibraryFile>(&file);
- return forEachUndefines(file, [&](StringRef undefName) -> ErrorOr<bool> {
+ return forEachUndefines(file,
+ [&](StringRef undefName) -> llvm::Expected<bool> {
if (File *member = archiveFile->find(undefName)) {
member->setOrdinal(_ctx.getNextOrdinalAndIncrement());
return handleFile(*member);
@@ -81,23 +84,23 @@
});
}
-std::error_code Resolver::handleSharedLibrary(File &file) {
+llvm::Error Resolver::handleSharedLibrary(File &file) {
// Add all the atoms from the shared library
SharedLibraryFile *sharedLibrary = cast<SharedLibraryFile>(&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<bool> {
+ forEachUndefines(file, [&](StringRef undefName) -> llvm::Expected<bool> {
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<UndefinedAtom> atom) {
@@ -247,9 +250,11 @@
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 @@
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 @@
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 @@
elements.push_back(llvm::make_unique<GroupEnd>(numLibs));
}
-std::error_code MachOLinkingContext::handleLoadedFile(File &file) {
+llvm::Error MachOLinkingContext::handleLoadedFile(File &file) {
auto *machoFile = dyn_cast<MachOFile>(&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 @@
_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<GenericError>(file.path() +
Twine(" cannot be linked due to incompatible architecture"));
}
@@ -1061,7 +1061,7 @@
_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<GenericError>(file.path() +
Twine(" cannot be linked due to incompatible operating systems"));
}
@@ -1078,7 +1078,7 @@
// 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<GenericError>(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 @@
// 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<GenericError>(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 @@
} else if (machoFile->swiftVersion() &&
machoFile->swiftVersion() != _swiftVersion) {
// Swift versions are different.
- return make_dynamic_error_code("different swift versions");
+ return llvm::make_error<GenericError>("different swift versions");
}
- return std::error_code();
+ return llvm::Error();
}
} // end namespace lld