From 9a13ac9a408a6e198444c5ccbba0fae3b6e9bdb0 Mon Sep 17 00:00:00 2001 From: Rui Ueyama Date: Tue, 8 Oct 2013 05:52:01 +0000 Subject: [PATCH] Move duplicate code in InputGraphs to the parent class. llvm-svn: 192170 --- lld/include/lld/Driver/DarwinInputGraph.h | 27 +-------------------- lld/include/lld/Driver/GnuLdInputGraph.h | 36 +++++---------------------- lld/include/lld/Driver/InputGraph.h | 3 +++ lld/include/lld/Driver/WinLinkInputGraph.h | 39 ++++++------------------------ lld/lib/Driver/InputGraph.cpp | 29 ++++++++++++++++++++++ 5 files changed, 47 insertions(+), 87 deletions(-) diff --git a/lld/include/lld/Driver/DarwinInputGraph.h b/lld/include/lld/Driver/DarwinInputGraph.h index 620e398..f8be824 100644 --- a/lld/include/lld/Driver/DarwinInputGraph.h +++ b/lld/include/lld/Driver/DarwinInputGraph.h @@ -42,34 +42,9 @@ public: /// \brief Parse the input file to lld::File. llvm::error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) { - ErrorOr filePath = getPath(ctx); - if (!filePath && - error_code(filePath) == llvm::errc::no_such_file_or_directory) - return make_error_code(llvm::errc::no_such_file_or_directory); - - // Create a memory buffer - OwningPtr opmb; - llvm::error_code ec; - - if ((ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb))) + if (error_code ec = readFile(ctx, diagnostics)) return ec; - - std::unique_ptr mb(opmb.take()); - _buffer = std::move(mb); - - if (ctx.logInputFiles()) - diagnostics << _buffer->getBufferIdentifier() << "\n"; - - // YAML file is identified by a .objtxt extension - // FIXME : Identify YAML files by using a magic - if (filePath->endswith(".objtxt")) { - ec = _ctx.getYAMLReader().parseFile(_buffer, _files); - if (!ec) - return ec; - } - (void) (_isWholeArchive); - return llvm::error_code::success(); } diff --git a/lld/include/lld/Driver/GnuLdInputGraph.h b/lld/include/lld/Driver/GnuLdInputGraph.h index 54f83ea..74bd163 100644 --- a/lld/include/lld/Driver/GnuLdInputGraph.h +++ b/lld/include/lld/Driver/GnuLdInputGraph.h @@ -72,33 +72,10 @@ public: /// \brief Parse the input file to lld::File. llvm::error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) { - ErrorOr filePath = getPath(ctx); - if (!filePath && - error_code(filePath) == llvm::errc::no_such_file_or_directory) - return make_error_code(llvm::errc::no_such_file_or_directory); - - // Create a memory buffer - OwningPtr opmb; - llvm::error_code ec; - - if ((ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb))) + // Read the file to _buffer. + if (error_code ec = readFile(ctx, diagnostics)) return ec; - std::unique_ptr mb(opmb.take()); - _buffer = std::move(mb); - - // If tracing is enabled, print the files being processed. - if (ctx.logInputFiles()) - diagnostics << _buffer->getBufferIdentifier() << "\n"; - - // YAML file is identified by a .objtxt extension - // FIXME : Identify YAML files by using a magic - if (filePath->endswith(".objtxt")) { - ec = _elfLinkingContext.getYAMLReader().parseFile(_buffer, _files); - if (!ec) - return ec; - } - // Identify File type llvm::sys::fs::file_magic FileType = llvm::sys::fs::identify_magic(_buffer->getBuffer()); @@ -107,12 +84,12 @@ public: case llvm::sys::fs::file_magic::elf_relocatable: case llvm::sys::fs::file_magic::elf_shared_object: // Call the default reader to read object files and shared objects - ec = _elfLinkingContext.getDefaultReader().parseFile(_buffer, _files); - break; + return _elfLinkingContext.getDefaultReader().parseFile(_buffer, _files); case llvm::sys::fs::file_magic::archive: { // Process archive files. If Whole Archive option is set, // parse all members of the archive. + error_code ec; std::unique_ptr fileArchive( new FileArchive(ctx, std::move(_buffer), ec, _isWholeArchive)); if (_isWholeArchive) { @@ -121,15 +98,14 @@ public: } else { _files.push_back(std::move(fileArchive)); } - break; + return ec; } default: // Process Linker script _elfLinkingContext.getLinkerScriptReader().parseFile(_buffer, _files); - break; + return error_code::success(); } - return ec; } /// \brief This is used by Group Nodes, when there is a need to reset the diff --git a/lld/include/lld/Driver/InputGraph.h b/lld/include/lld/Driver/InputGraph.h index ae32b25..019c3e5 100644 --- a/lld/include/lld/Driver/InputGraph.h +++ b/lld/include/lld/Driver/InputGraph.h @@ -302,6 +302,9 @@ public: virtual void assignFileOrdinals(uint64_t &startOrdinal); protected: + /// \brief Read the file into _buffer. + error_code readFile(const LinkingContext &ctx, raw_ostream &diagnostics); + StringRef _path; InputGraph::FileVectorT _files; std::unique_ptr _buffer; diff --git a/lld/include/lld/Driver/WinLinkInputGraph.h b/lld/include/lld/Driver/WinLinkInputGraph.h index 7587716..eec4ec2 100644 --- a/lld/include/lld/Driver/WinLinkInputGraph.h +++ b/lld/include/lld/Driver/WinLinkInputGraph.h @@ -38,51 +38,28 @@ public: virtual llvm::ErrorOr getPath(const LinkingContext &ctx) const; /// \brief Parse the input file to lld::File. - llvm::error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) { - ErrorOr filePath = getPath(ctx); - if (!filePath && - error_code(filePath) == llvm::errc::no_such_file_or_directory) - return make_error_code(llvm::errc::no_such_file_or_directory); - - // Create a memory buffer - OwningPtr opmb; - llvm::error_code ec; - - if ((ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb))) + error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) { + // Read the file to _buffer. + if (error_code ec = readFile(ctx, diagnostics)) return ec; - std::unique_ptr mb(opmb.take()); - _buffer = std::move(mb); - - if (ctx.logInputFiles()) - diagnostics << _buffer->getBufferIdentifier() << "\n"; - - // YAML file is identified by a .objtxt extension - // FIXME : Identify YAML files by using a magic - if (filePath->endswith(".objtxt")) { - ec = _ctx.getYAMLReader().parseFile(_buffer, _files); - if (!ec) - return ec; - } - llvm::sys::fs::file_magic FileType = llvm::sys::fs::identify_magic(_buffer->getBuffer()); - std::unique_ptr f; switch (FileType) { - case llvm::sys::fs::file_magic::archive: + case llvm::sys::fs::file_magic::archive: { // Archive File + error_code ec; f.reset(new FileArchive(ctx, std::move(_buffer), ec, false)); _files.push_back(std::move(f)); - break; + return ec; + } case llvm::sys::fs::file_magic::coff_object: default: - ec = _ctx.getDefaultReader().parseFile(_buffer, _files); - break; + return _ctx.getDefaultReader().parseFile(_buffer, _files); } - return ec; } /// \brief validates the Input Element diff --git a/lld/lib/Driver/InputGraph.cpp b/lld/lib/Driver/InputGraph.cpp index ddbc36f..3a00caf 100644 --- a/lld/lib/Driver/InputGraph.cpp +++ b/lld/lib/Driver/InputGraph.cpp @@ -105,6 +105,35 @@ void FileNode::assignFileOrdinals(uint64_t &startOrdinal) { file->setOrdinalAndIncrement(startOrdinal); } +/// \brief Read the file into _buffer. +error_code +FileNode::readFile(const LinkingContext &ctx, raw_ostream &diagnostics) { + ErrorOr filePath = getPath(ctx); + if (!filePath && + error_code(filePath) == llvm::errc::no_such_file_or_directory) + return make_error_code(llvm::errc::no_such_file_or_directory); + + // Create a memory buffer + OwningPtr opmb; + + if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb)) + return ec; + + std::unique_ptr mb(opmb.take()); + _buffer = std::move(mb); + + if (ctx.logInputFiles()) + diagnostics << _buffer->getBufferIdentifier() << "\n"; + + // YAML file is identified by a .objtxt extension + // FIXME : Identify YAML files by using a magic + if (filePath->endswith(".objtxt")) + if (error_code ec = ctx.getYAMLReader().parseFile(_buffer, _files)) + return ec; + return error_code::success(); +} + + /// \brief Assign File ordinals for files contained /// in the InputElement void ControlNode::assignFileOrdinals(uint64_t &startOrdinal) { -- 2.7.4