Move nextFile() from LinkingContext to InputGraph.
LinkingContext and InputGraph are unnecessarily entangled. Most linker
input file data, e.g. the vector containing input files, the next index
of the input file, etc. are managed by InputGraph, but only the current
input file is for no obvious reason managed by LinkingContext.
This patch is to move code from LinkingContext to InputGraph to fix it.
It's now clear who's reponsible for managing input file state, which is
InputGraph, and LinkingContext is now free from that responsibility.
It improves the readability as we now have fewer dependencies between
classes. No functionality change.
Differential Revision: http://llvm-reviews.chandlerc.com/D3259
llvm-svn: 205394
diff --git a/lld/lib/Core/InputGraph.cpp b/lld/lib/Core/InputGraph.cpp
index ad9779e..7d85021 100644
--- a/lld/lib/Core/InputGraph.cpp
+++ b/lld/lib/Core/InputGraph.cpp
@@ -20,6 +20,37 @@
return a->getOrdinal() < b->getOrdinal();
}
+ErrorOr<File &> InputGraph::nextFile() {
+ // When nextFile() is called for the first time, _currentInputElement is not
+ // initialized. Initialize it with the first element of the input graph.
+ if (_currentInputElement == nullptr) {
+ ErrorOr<InputElement *> elem = getNextInputElement();
+ if (elem.getError() == InputGraphError::no_more_elements)
+ return make_error_code(InputGraphError::no_more_files);
+ _currentInputElement = *elem;
+ }
+
+ // Otherwise, 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 (;;) {
+ ErrorOr<File &> nextFile = _currentInputElement->getNextFile();
+ if (nextFile.getError() != InputGraphError::no_more_files)
+ return std::move(nextFile);
+
+ ErrorOr<InputElement *> elem = getNextInputElement();
+ if (elem.getError() == InputGraphError::no_more_elements ||
+ *elem == nullptr)
+ return make_error_code(InputGraphError::no_more_files);
+ _currentInputElement = *elem;
+ }
+}
+
+void InputGraph::setResolverState(uint32_t state) {
+ _currentInputElement->setResolveState(state);
+}
+
bool InputGraph::addInputElement(std::unique_ptr<InputElement> ie) {
_inputArgs.push_back(std::move(ie));
return true;
diff --git a/lld/lib/Core/LinkingContext.cpp b/lld/lib/Core/LinkingContext.cpp
index d92ef27..7be4704 100644
--- a/lld/lib/Core/LinkingContext.cpp
+++ b/lld/lib/Core/LinkingContext.cpp
@@ -25,8 +25,7 @@
_warnIfCoalesableAtomsHaveDifferentLoadName(false),
_printRemainingUndefines(true), _allowRemainingUndefines(false),
_logInputFiles(false), _allowShlibUndefines(false),
- _outputFileType(OutputFileType::Default), _currentInputElement(nullptr),
- _nextOrdinal(0) {}
+ _outputFileType(OutputFileType::Default), _nextOrdinal(0) {}
LinkingContext::~LinkingContext() {}
@@ -83,37 +82,6 @@
result.push_back(std::move(internalFile));
}
-void LinkingContext::setResolverState(uint32_t state) {
- _currentInputElement->setResolveState(state);
-}
-
-ErrorOr<File &> LinkingContext::nextFile() {
- // When nextFile() is called for the first time, _currentInputElement is not
- // initialized. Initialize it with the first element of the input graph.
- if (_currentInputElement == nullptr) {
- ErrorOr<InputElement *> elem = inputGraph().getNextInputElement();
- if (elem.getError() == InputGraphError::no_more_elements)
- return make_error_code(InputGraphError::no_more_files);
- _currentInputElement = *elem;
- }
-
- // Otherwise, 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 (;;) {
- ErrorOr<File &> nextFile = _currentInputElement->getNextFile();
- if (nextFile.getError() != InputGraphError::no_more_files)
- return std::move(nextFile);
-
- ErrorOr<InputElement *> elem = inputGraph().getNextInputElement();
- if (elem.getError() == InputGraphError::no_more_elements ||
- *elem == nullptr)
- return make_error_code(InputGraphError::no_more_files);
- _currentInputElement = *elem;
- }
-}
-
void LinkingContext::addPasses(PassManager &pm) {}
} // end namespace lld
diff --git a/lld/lib/Core/Resolver.cpp b/lld/lib/Core/Resolver.cpp
index f8bcfbc..1c16084a 100644
--- a/lld/lib/Core/Resolver.cpp
+++ b/lld/lib/Core/Resolver.cpp
@@ -105,7 +105,7 @@
doAbsoluteAtom(*absAtom);
resolverState |= StateNewAbsoluteAtoms;
}
- _context.setResolverState(resolverState);
+ _context.inputGraph().setResolverState(resolverState);
}
void Resolver::forEachUndefines(UndefCallback callback,
@@ -305,8 +305,8 @@
ScopedTask task(getDefaultDomain(), "resolveUndefines");
for (;;) {
- ErrorOr<File &> file = _context.nextFile();
- _context.setResolverState(Resolver::StateNoChange);
+ ErrorOr<File &> file = _context.inputGraph().nextFile();
+ _context.inputGraph().setResolverState(Resolver::StateNoChange);
error_code ec = file.getError();
if (ec == InputGraphError::no_more_files)
return true;