[lld] r224208 - Simplify InputGraph API.
Rui Ueyama
ruiu at google.com
Sat Dec 13 18:04:01 PST 2014
Author: ruiu
Date: Sat Dec 13 20:04:01 2014
New Revision: 224208
URL: http://llvm.org/viewvc/llvm-project?rev=224208&view=rev
Log:
Simplify InputGraph API.
These member functions returns either no_more_files error or a File object.
We could simply return a nullptr instead of a no_more_files.
This function will be removed soon as a part of InputGraph cleanup.
I had to do that step by step.
Modified:
lld/trunk/include/lld/Core/Error.h
lld/trunk/include/lld/Core/InputGraph.h
lld/trunk/include/lld/Core/Resolver.h
lld/trunk/lib/Core/Error.cpp
lld/trunk/lib/Core/InputGraph.cpp
lld/trunk/lib/Core/Resolver.cpp
lld/trunk/unittests/DriverTests/InputGraphTest.cpp
Modified: lld/trunk/include/lld/Core/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Error.h?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Error.h (original)
+++ lld/trunk/include/lld/Core/Error.h Sat Dec 13 20:04:01 2014
@@ -58,20 +58,6 @@ inline std::error_code make_error_code(L
return std::error_code(static_cast<int>(e), LinkerScriptReaderCategory());
}
-/// \brief Errors returned by InputGraph functionality
-const std::error_category &InputGraphErrorCategory();
-
-enum class InputGraphError {
- success = 0,
- failure = 1,
- no_more_elements,
- no_more_files
-};
-
-inline std::error_code make_error_code(InputGraphError e) {
- return std::error_code(static_cast<int>(e), InputGraphErrorCategory());
-}
-
/// \brief Errors returned by Reader.
const std::error_category &ReaderErrorCategory();
@@ -102,7 +88,6 @@ struct is_error_code_enum<lld::NativeRea
template <> struct is_error_code_enum<lld::YamlReaderError> : std::true_type {};
template <>
struct is_error_code_enum<lld::LinkerScriptReaderError> : std::true_type {};
-template <> struct is_error_code_enum<lld::InputGraphError> : std::true_type {};
template <> struct is_error_code_enum<lld::ReaderError> : std::true_type {};
}
Modified: lld/trunk/include/lld/Core/InputGraph.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/InputGraph.h?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/InputGraph.h (original)
+++ lld/trunk/include/lld/Core/InputGraph.h Sat Dec 13 20:04:01 2014
@@ -54,10 +54,8 @@ public:
/// getNextFile returns the next file that needs to be processed by
/// the resolver. When there are no more files to be processed, an
- /// appropriate InputGraphError is returned. Ordinals are assigned
- /// to files returned by getNextFile, which means ordinals would be
- /// assigned in the way files are resolved.
- virtual ErrorOr<File &> getNextFile();
+ /// nullptr is returned.
+ File *getNextFile();
/// Adds an observer of getNextFile(). Each time a new file is about to be
/// returned from getNextFile(), registered observers are called with the file
@@ -97,7 +95,7 @@ protected:
std::vector<std::function<void(File *)>> _observers;
private:
- ErrorOr<InputElement *> getNextInputElement();
+ InputElement *getNextInputElement();
};
/// \brief This describes each element in the InputGraph. The Kind
@@ -125,7 +123,7 @@ public:
/// \brief functions for the resolver to use
/// Get the next file to be processed by the resolver
- virtual ErrorOr<File &> getNextFile() = 0;
+ virtual File *getNextFile() = 0;
/// Get the elements that we want to expand with.
virtual bool getReplacements(InputGraph::InputElementVectorT &) {
@@ -153,9 +151,7 @@ public:
return std::error_code();
}
- ErrorOr<File &> getNextFile() override {
- llvm_unreachable("shouldn't be here.");
- }
+ File *getNextFile() override { llvm_unreachable("shouldn't be here."); }
private:
int _size;
@@ -206,10 +202,10 @@ public:
/// \brief Return the next File thats part of this node to the
/// resolver.
- ErrorOr<File &> getNextFile() override {
+ File *getNextFile() override {
if (_nextFileIndex == _files.size())
- return make_error_code(InputGraphError::no_more_files);
- return *_files[_nextFileIndex++];
+ return nullptr;
+ return _files[_nextFileIndex++].get();
}
protected:
Modified: lld/trunk/include/lld/Core/Resolver.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Resolver.h?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Resolver.h (original)
+++ lld/trunk/include/lld/Core/Resolver.h Sat Dec 13 20:04:01 2014
@@ -56,13 +56,13 @@ private:
typedef std::function<void(StringRef, bool)> UndefCallback;
bool undefinesAdded(int count);
- ErrorOr<File &> nextFile(bool &inGroup);
+ File *nextFile(bool &inGroup);
/// \brief Add section group/.gnu.linkonce if it does not exist previously.
void maybeAddSectionGroupOrGnuLinkOnce(const DefinedAtom &atom);
/// \brief The main function that iterates over the files to resolve
- bool resolveUndefines();
+ void resolveUndefines();
void updateReferences();
void deadStripOptimize();
bool checkUndefines();
Modified: lld/trunk/lib/Core/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/Error.cpp?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/lib/Core/Error.cpp (original)
+++ lld/trunk/lib/Core/Error.cpp Sat Dec 13 20:04:01 2014
@@ -97,33 +97,6 @@ const std::error_category &lld::LinkerSc
return o;
}
-class _InputGraphErrorCategory : public std::error_category {
-public:
- const char *name() const LLVM_NOEXCEPT override {
- return "lld.inputGraph.parse";
- }
-
- std::string message(int ev) const override {
- switch (static_cast<InputGraphError>(ev)) {
- case InputGraphError::success:
- return "Success";
- case InputGraphError::failure:
- return "failure";
- case InputGraphError::no_more_elements:
- return "no more elements";
- case InputGraphError::no_more_files:
- return "no more files";
- }
- llvm_unreachable("An enumerator of InputGraphError does not have a "
- "message defined.");
- }
-};
-
-const std::error_category &lld::InputGraphErrorCategory() {
- static _InputGraphErrorCategory i;
- return i;
-}
-
class _ReaderErrorCategory : public std::error_category {
public:
const char *name() const LLVM_NOEXCEPT override {
Modified: lld/trunk/lib/Core/InputGraph.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/InputGraph.cpp?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/lib/Core/InputGraph.cpp (original)
+++ lld/trunk/lib/Core/InputGraph.cpp Sat Dec 13 20:04:01 2014
@@ -15,24 +15,24 @@ using namespace lld;
InputGraph::~InputGraph() { }
-ErrorOr<File &> InputGraph::getNextFile() {
+File *InputGraph::getNextFile() {
// Try to get the next file of _currentInputElement. If the current input
// element points to an archive file, and there's a file left in the archive,
// it will succeed. If not, try to get the next file in the input graph.
for (;;) {
if (_currentInputElement) {
- ErrorOr<File &> next = _currentInputElement->getNextFile();
- if (next.getError() != InputGraphError::no_more_files) {
+ File *next = _currentInputElement->getNextFile();
+ if (next) {
for (const std::function<void(File *)> &observer : _observers)
- observer(&next.get());
- return std::move(next);
+ observer(next);
+ return next;
}
}
- ErrorOr<InputElement *> elt = getNextInputElement();
- if (elt.getError() == InputGraphError::no_more_elements || *elt == nullptr)
- return make_error_code(InputGraphError::no_more_files);
- _currentInputElement = *elt;
+ InputElement *elt = getNextInputElement();
+ if (!elt)
+ return nullptr;
+ _currentInputElement = elt;
}
}
@@ -56,9 +56,9 @@ bool InputGraph::dump(raw_ostream &diagn
}
/// \brief Helper functions for the resolver
-ErrorOr<InputElement *> InputGraph::getNextInputElement() {
+InputElement *InputGraph::getNextInputElement() {
if (_nextElementIndex >= _inputArgs.size())
- return make_error_code(InputGraphError::no_more_elements);
+ return nullptr;
InputElement *elem = _inputArgs[_nextElementIndex++].get();
if (isa<GroupEnd>(elem))
return getNextInputElement();
Modified: lld/trunk/lib/Core/Resolver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/Resolver.cpp?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/lib/Core/Resolver.cpp (original)
+++ lld/trunk/lib/Core/Resolver.cpp Sat Dec 13 20:04:01 2014
@@ -238,7 +238,7 @@ bool Resolver::undefinesAdded(int n) {
return false;
}
-ErrorOr<File &> Resolver::nextFile(bool &inGroup) {
+File *Resolver::nextFile(bool &inGroup) {
if (size_t groupSize = _context.getInputGraph().getGroupSize()) {
// We are at the end of the current group. If one or more new
// undefined atom has been added in the last groupSize files, we
@@ -251,32 +251,29 @@ ErrorOr<File &> Resolver::nextFile(bool
if (_fileIndex < _files.size()) {
// We are still in the current group.
inGroup = true;
- return *_files[_fileIndex++];
+ return _files[_fileIndex++];
}
// We are not in a group. Get a new file.
- ErrorOr<File &> file = _context.getInputGraph().getNextFile();
- if (std::error_code ec = file.getError()) {
- if (ec != InputGraphError::no_more_files)
- llvm::errs() << "Error occurred in getNextFile: " << ec.message() << "\n";
- return ec;
- }
+ File *file = _context.getInputGraph().getNextFile();
+ if (!file)
+ return nullptr;
_files.push_back(&*file);
++_fileIndex;
inGroup = false;
- return *file;
+ return file;
}
// Keep adding atoms until _context.getNextFile() returns an error. This
// function is where undefined atoms are resolved.
-bool Resolver::resolveUndefines() {
+void Resolver::resolveUndefines() {
ScopedTask task(getDefaultDomain(), "resolveUndefines");
for (;;) {
bool inGroup = false;
bool undefAdded = false;
- ErrorOr<File &> file = nextFile(inGroup);
- if (std::error_code ec = file.getError())
- return ec == InputGraphError::no_more_files;
+ File *file = nextFile(inGroup);
+ if (!file)
+ return;
switch (file->kind()) {
case File::kindObject:
if (inGroup)
@@ -446,8 +443,7 @@ void Resolver::removeCoalescedAwayAtoms(
}
bool Resolver::resolve() {
- if (!resolveUndefines())
- return false;
+ resolveUndefines();
updateReferences();
deadStripOptimize();
if (checkUndefines())
Modified: lld/trunk/unittests/DriverTests/InputGraphTest.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/DriverTests/InputGraphTest.cpp?rev=224208&r1=224207&r2=224208&view=diff
==============================================================================
--- lld/trunk/unittests/DriverTests/InputGraphTest.cpp (original)
+++ lld/trunk/unittests/DriverTests/InputGraphTest.cpp Sat Dec 13 20:04:01 2014
@@ -54,14 +54,14 @@ public:
}
StringRef getNext() {
- ErrorOr<File &> file = _graph->getNextFile();
- EXPECT_TRUE(!file.getError());
- return file.get().path();
+ File *file = _graph->getNextFile();
+ EXPECT_TRUE(file);
+ return file->path();
}
void expectEnd() {
- ErrorOr<File &> file = _graph->getNextFile();
- EXPECT_EQ(file.getError(), InputGraphError::no_more_files);
+ File *file = _graph->getNextFile();
+ EXPECT_TRUE(file == nullptr);
}
protected:
More information about the llvm-commits
mailing list