Move duplicate code in InputGraphs to the parent class.
authorRui Ueyama <ruiu@google.com>
Tue, 8 Oct 2013 05:52:01 +0000 (05:52 +0000)
committerRui Ueyama <ruiu@google.com>
Tue, 8 Oct 2013 05:52:01 +0000 (05:52 +0000)
llvm-svn: 192170

lld/include/lld/Driver/DarwinInputGraph.h
lld/include/lld/Driver/GnuLdInputGraph.h
lld/include/lld/Driver/InputGraph.h
lld/include/lld/Driver/WinLinkInputGraph.h
lld/lib/Driver/InputGraph.cpp

index 620e398..f8be824 100644 (file)
@@ -42,34 +42,9 @@ public:
 
   /// \brief Parse the input file to lld::File.
   llvm::error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) {
-    ErrorOr<StringRef> 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<llvm::MemoryBuffer> opmb;
-    llvm::error_code ec;
-
-    if ((ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb)))
+    if (error_code ec = readFile(ctx, diagnostics))
       return ec;
-
-    std::unique_ptr<MemoryBuffer> 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();
   }
 
index 54f83ea..74bd163 100644 (file)
@@ -72,33 +72,10 @@ public:
 
   /// \brief Parse the input file to lld::File.
   llvm::error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) {
-    ErrorOr<StringRef> 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<llvm::MemoryBuffer> 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<MemoryBuffer> 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> 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
index ae32b25..019c3e5 100644 (file)
@@ -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<llvm::MemoryBuffer> _buffer;
index 7587716..eec4ec2 100644 (file)
@@ -38,51 +38,28 @@ public:
   virtual llvm::ErrorOr<StringRef> getPath(const LinkingContext &ctx) const;
 
   /// \brief Parse the input file to lld::File.
-  llvm::error_code parse(const LinkingContext &ctx, raw_ostream &diagnostics) {
-    ErrorOr<StringRef> 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<llvm::MemoryBuffer> 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<MemoryBuffer> 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<File> 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
index ddbc36f..3a00caf 100644 (file)
@@ -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<StringRef> 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<llvm::MemoryBuffer> opmb;
+
+  if (error_code ec = llvm::MemoryBuffer::getFileOrSTDIN(*filePath, opmb))
+    return ec;
+
+  std::unique_ptr<MemoryBuffer> 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) {