[lld] r264961 - Convert lld file writing to llvm::Error. NFC.

Pete Cooper via llvm-commits llvm-commits at lists.llvm.org
Wed Mar 30 16:10:39 PDT 2016


Author: pete
Date: Wed Mar 30 18:10:39 2016
New Revision: 264961

URL: http://llvm.org/viewvc/llvm-project?rev=264961&view=rev
Log:
Convert lld file writing to llvm::Error.  NFC.

This converts the writeFile method, as well as some of the ones it calls
in the normalized binary file writer and yaml writer.

Modified:
    lld/trunk/include/lld/Core/LinkingContext.h
    lld/trunk/include/lld/Core/Writer.h
    lld/trunk/lib/Core/LinkingContext.cpp
    lld/trunk/lib/Driver/DarwinLdDriver.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
    lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
    lld/trunk/lib/ReaderWriter/MachO/WriterMachO.cpp
    lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
    lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp
    lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp

Modified: lld/trunk/include/lld/Core/LinkingContext.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/LinkingContext.h?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/LinkingContext.h (original)
+++ lld/trunk/include/lld/Core/LinkingContext.h Wed Mar 30 18:10:39 2016
@@ -194,7 +194,7 @@ public:
   /// Calls through to the writeFile() method on the specified Writer.
   ///
   /// \param linkedFile This is the merged/linked graph of all input file Atoms.
-  virtual std::error_code writeFile(const File &linkedFile) const;
+  virtual llvm::Error writeFile(const File &linkedFile) const;
 
   /// Return the next ordinal and Increment it.
   virtual uint64_t getNextOrdinalAndIncrement() const { return _nextOrdinal++; }

Modified: lld/trunk/include/lld/Core/Writer.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/include/lld/Core/Writer.h?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/include/lld/Core/Writer.h (original)
+++ lld/trunk/include/lld/Core/Writer.h Wed Mar 30 18:10:39 2016
@@ -11,6 +11,7 @@
 #define LLD_CORE_WRITER_H
 
 #include "lld/Core/LLVM.h"
+#include "llvm/Support/Error.h"
 #include <memory>
 #include <vector>
 
@@ -27,7 +28,7 @@ public:
   virtual ~Writer();
 
   /// \brief Write a file from the supplied File object
-  virtual std::error_code writeFile(const File &linkedFile, StringRef path) = 0;
+  virtual llvm::Error writeFile(const File &linkedFile, StringRef path) = 0;
 
   /// \brief This method is called by Core Linking to give the Writer a chance
   /// to add file format specific "files" to set of files to be linked. This is

Modified: lld/trunk/lib/Core/LinkingContext.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Core/LinkingContext.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/Core/LinkingContext.cpp (original)
+++ lld/trunk/lib/Core/LinkingContext.cpp Wed Mar 30 18:10:39 2016
@@ -23,7 +23,7 @@ bool LinkingContext::validate(raw_ostrea
   return validateImpl(diagnostics);
 }
 
-std::error_code LinkingContext::writeFile(const File &linkedFile) const {
+llvm::Error LinkingContext::writeFile(const File &linkedFile) const {
   return this->writer().writeFile(linkedFile, _outputPath);
 }
 

Modified: lld/trunk/lib/Driver/DarwinLdDriver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/Driver/DarwinLdDriver.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/Driver/DarwinLdDriver.cpp (original)
+++ lld/trunk/lib/Driver/DarwinLdDriver.cpp Wed Mar 30 18:10:39 2016
@@ -1199,9 +1199,11 @@ bool link(llvm::ArrayRef<const char *> a
 
   // Give linked atoms to Writer to generate output file.
   ScopedTask writeTask(getDefaultDomain(), "Write");
-  if (std::error_code ec = ctx.writeFile(*merged)) {
-    diagnostics << "Failed to write file '" << ctx.outputPath()
-                << "': " << ec.message() << "\n";
+  if (auto ec = ctx.writeFile(*merged)) {
+    // FIXME: This should be passed to logAllUnhandledErrors but it needs
+    // to be passed a Twine instead of a string.
+    diagnostics << "Failed to write file '" << ctx.outputPath() << "': ";
+    logAllUnhandledErrors(std::move(ec), diagnostics, std::string());
     return false;
   }
 

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFile.h Wed Mar 30 18:10:39 2016
@@ -294,7 +294,7 @@ readBinary(std::unique_ptr<MemoryBuffer>
            const MachOLinkingContext::Arch arch);
 
 /// Takes in-memory normalized view and writes a mach-o object file.
-std::error_code writeBinary(const NormalizedFile &file, StringRef path);
+llvm::Error writeBinary(const NormalizedFile &file, StringRef path);
 
 size_t headerAndLoadCommandsSize(const NormalizedFile &file);
 
@@ -322,7 +322,7 @@ normalizedToAtoms(const NormalizedFile &
                   bool copyRefs);
 
 /// Takes atoms and generates a normalized macho-o view.
-ErrorOr<std::unique_ptr<NormalizedFile>>
+llvm::Expected<std::unique_ptr<NormalizedFile>>
 normalizedFromAtoms(const lld::File &atomFile, const MachOLinkingContext &ctxt);
 
 

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileBinaryWriter.cpp Wed Mar 30 18:10:39 2016
@@ -133,7 +133,7 @@ public:
   /// Writes the normalized file as a binary mach-o file to the specified
   /// path.  This does not have a stream interface because the generated
   /// file may need the 'x' bit set.
-  std::error_code writeBinary(StringRef path);
+  llvm::Error writeBinary(StringRef path);
 
 private:
   uint32_t    loadCommandsSize(uint32_t &count);
@@ -1459,10 +1459,10 @@ void MachOFileLayout::writeLinkEditConte
   }
 }
 
-std::error_code MachOFileLayout::writeBinary(StringRef path) {
+llvm::Error MachOFileLayout::writeBinary(StringRef path) {
   // Check for pending error from constructor.
   if (_ec)
-    return _ec;
+    return llvm::errorCodeToError(_ec);
   // Create FileOutputBuffer with calculated size.
   unsigned flags = 0;
   if (_file.fileType != llvm::MachO::MH_OBJECT)
@@ -1470,23 +1470,23 @@ std::error_code MachOFileLayout::writeBi
   ErrorOr<std::unique_ptr<llvm::FileOutputBuffer>> fobOrErr =
       llvm::FileOutputBuffer::create(path, size(), flags);
   if (std::error_code ec = fobOrErr.getError())
-    return ec;
+    return llvm::errorCodeToError(ec);
   std::unique_ptr<llvm::FileOutputBuffer> &fob = *fobOrErr;
   // Write content.
   _buffer = fob->getBufferStart();
   writeMachHeader();
   std::error_code ec = writeLoadCommands();
   if (ec)
-    return ec;
+    return llvm::errorCodeToError(ec);
   writeSectionContent();
   writeLinkEditContent();
   fob->commit();
 
-  return std::error_code();
+  return llvm::Error();
 }
 
 /// Takes in-memory normalized view and writes a mach-o object file.
-std::error_code writeBinary(const NormalizedFile &file, StringRef path) {
+llvm::Error writeBinary(const NormalizedFile &file, StringRef path) {
   MachOFileLayout layout(file);
   return layout.writeBinary(path);
 }

Modified: lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/MachONormalizedFileFromAtoms.cpp Wed Mar 30 18:10:39 2016
@@ -120,7 +120,7 @@ public:
   void      copySectionInfo(NormalizedFile &file);
   void      updateSectionInfo(NormalizedFile &file);
   void      buildAtomToAddressMap();
-  std::error_code addSymbols(const lld::File &atomFile, NormalizedFile &file);
+  llvm::Error addSymbols(const lld::File &atomFile, NormalizedFile &file);
   void      addIndirectSymbols(const lld::File &atomFile, NormalizedFile &file);
   void      addRebaseAndBindingInfo(const lld::File &, NormalizedFile &file);
   void      addExportInfo(const lld::File &, NormalizedFile &file);
@@ -164,9 +164,9 @@ private:
                              uint8_t &segmentIndex, uint64_t &segmentStartAddr);
   const Atom  *targetOfLazyPointer(const DefinedAtom *lpAtom);
   const Atom  *targetOfStub(const DefinedAtom *stubAtom);
-  std::error_code getSymbolTableRegion(const DefinedAtom* atom,
-                                       bool &inGlobalsRegion,
-                                       SymbolScope &symbolScope);
+  llvm::Error getSymbolTableRegion(const DefinedAtom* atom,
+                                   bool &inGlobalsRegion,
+                                   SymbolScope &symbolScope);
   void         appendSection(SectionInfo *si, NormalizedFile &file);
   uint32_t     sectionIndexForAtom(const Atom *atom);
 
@@ -820,55 +820,55 @@ bool Util::AtomSorter::operator()(const
   return (left.atom->name().compare(right.atom->name()) < 0);
 }
 
-std::error_code Util::getSymbolTableRegion(const DefinedAtom* atom,
-                                           bool &inGlobalsRegion,
-                                           SymbolScope &scope) {
+llvm::Error Util::getSymbolTableRegion(const DefinedAtom* atom,
+                                       bool &inGlobalsRegion,
+                                       SymbolScope &scope) {
   bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT);
   switch (atom->scope()) {
   case Atom::scopeTranslationUnit:
     scope = 0;
     inGlobalsRegion = false;
-    return std::error_code();
+    return llvm::Error();
   case Atom::scopeLinkageUnit:
     if ((_ctx.exportMode() == MachOLinkingContext::ExportMode::whiteList) &&
         _ctx.exportSymbolNamed(atom->name())) {
-      return make_dynamic_error_code(Twine("cannot export hidden symbol ")
-                                    + atom->name());
+      return llvm::make_error<GenericError>(
+                          Twine("cannot export hidden symbol ") + atom->name());
     }
     if (rMode) {
       if (_ctx.keepPrivateExterns()) {
         // -keep_private_externs means keep in globals region as N_PEXT.
         scope = N_PEXT | N_EXT;
         inGlobalsRegion = true;
-        return std::error_code();
+        return llvm::Error();
       }
     }
     // scopeLinkageUnit symbols are no longer global once linked.
     scope = N_PEXT;
     inGlobalsRegion = false;
-    return std::error_code();
+    return llvm::Error();
   case Atom::scopeGlobal:
     if (_ctx.exportRestrictMode()) {
       if (_ctx.exportSymbolNamed(atom->name())) {
         scope = N_EXT;
         inGlobalsRegion = true;
-        return std::error_code();
+        return llvm::Error();
       } else {
         scope = N_PEXT;
         inGlobalsRegion = false;
-        return std::error_code();
+        return llvm::Error();
       }
     } else {
       scope = N_EXT;
       inGlobalsRegion = true;
-      return std::error_code();
+      return llvm::Error();
     }
     break;
   }
 }
 
-std::error_code Util::addSymbols(const lld::File &atomFile,
-                                 NormalizedFile &file) {
+llvm::Error Util::addSymbols(const lld::File &atomFile,
+                             NormalizedFile &file) {
   bool rMode = (_ctx.outputMachOType() == llvm::MachO::MH_OBJECT);
   // Mach-O symbol table has three regions: locals, globals, undefs.
 
@@ -964,7 +964,7 @@ std::error_code Util::addSymbols(const l
     file.undefinedSymbols.push_back(sym);
   }
 
-  return std::error_code();
+  return llvm::Error();
 }
 
 const Atom *Util::targetOfLazyPointer(const DefinedAtom *lpAtom) {
@@ -1350,7 +1350,7 @@ namespace mach_o {
 namespace normalized {
 
 /// Convert a set of Atoms into a normalized mach-o file.
-ErrorOr<std::unique_ptr<NormalizedFile>>
+llvm::Expected<std::unique_ptr<NormalizedFile>>
 normalizedFromAtoms(const lld::File &atomFile,
                                            const MachOLinkingContext &context) {
   // The util object buffers info until the normalized file can be made.
@@ -1406,7 +1406,7 @@ normalizedFromAtoms(const lld::File &ato
   util.updateSectionInfo(normFile);
   util.copySectionContent(normFile);
   if (auto ec = util.addSymbols(atomFile, normFile)) {
-    return ec;
+    return std::move(ec);
   }
   util.addIndirectSymbols(atomFile, normFile);
   util.addRebaseAndBindingInfo(atomFile, normFile);

Modified: lld/trunk/lib/ReaderWriter/MachO/WriterMachO.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/MachO/WriterMachO.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/MachO/WriterMachO.cpp (original)
+++ lld/trunk/lib/ReaderWriter/MachO/WriterMachO.cpp Wed Mar 30 18:10:39 2016
@@ -28,17 +28,18 @@ class MachOWriter : public Writer {
 public:
   MachOWriter(const MachOLinkingContext &ctxt) : _ctx(ctxt) {}
 
-  std::error_code writeFile(const lld::File &file, StringRef path) override {
+  llvm::Error writeFile(const lld::File &file, StringRef path) override {
     // Construct empty normalized file from atoms.
-    ErrorOr<std::unique_ptr<NormalizedFile>> nFile =
+    llvm::Expected<std::unique_ptr<NormalizedFile>> nFile =
         normalized::normalizedFromAtoms(file, _ctx);
-    if (std::error_code ec = nFile.getError())
-      return ec;
+    if (auto ec = nFile.takeError())
+      return std::move(ec);
 
     // For testing, write out yaml form of normalized file.
     if (_ctx.printAtoms()) {
       std::unique_ptr<Writer> yamlWriter = createWriterYAML(_ctx);
-      yamlWriter->writeFile(file, "-");
+      if (auto ec = yamlWriter->writeFile(file, "-"))
+        return std::move(ec);
     }
 
     // Write normalized file as mach-o binary.

Modified: lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp (original)
+++ lld/trunk/lib/ReaderWriter/YAML/ReaderWriterYAML.cpp Wed Mar 30 18:10:39 2016
@@ -1280,12 +1280,12 @@ class Writer : public lld::Writer {
 public:
   Writer(const LinkingContext &context) : _ctx(context) {}
 
-  std::error_code writeFile(const lld::File &file, StringRef outPath) override {
+  llvm::Error writeFile(const lld::File &file, StringRef outPath) override {
     // Create stream to path.
     std::error_code ec;
     llvm::raw_fd_ostream out(outPath, ec, llvm::sys::fs::F_Text);
     if (ec)
-      return ec;
+      return llvm::errorCodeToError(ec);
 
     // Create yaml Output writer, using yaml options for context.
     YamlContext yamlContext;
@@ -1297,7 +1297,7 @@ public:
     const lld::File *fileRef = &file;
     yout << fileRef;
 
-    return std::error_code();
+    return llvm::Error();
   }
 
 private:

Modified: lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp (original)
+++ lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryReaderTests.cpp Wed Mar 30 18:10:39 2016
@@ -735,5 +735,6 @@ TEST(BinaryReaderTest, hello_obj_ppc) {
   EXPECT_EQ(printfLabel.type, N_UNDF);
   EXPECT_EQ(printfLabel.scope, SymbolScope(N_EXT));
 
-  writeBinary(*f, "/tmp/foo.o");
+  auto ec = writeBinary(*f, "/tmp/foo.o");
+  EXPECT_FALSE(ec);
 }

Modified: lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp?rev=264961&r1=264960&r2=264961&view=diff
==============================================================================
--- lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp (original)
+++ lld/trunk/unittests/MachOTests/MachONormalizedFileBinaryWriterTests.cpp Wed Mar 30 18:10:39 2016
@@ -148,8 +148,8 @@ TEST(BinaryWriterTest, obj_relocs_x86_64
     std::error_code ec =
         llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl);
     EXPECT_FALSE(ec);
-    ec = writeBinary(f, tmpFl);
-    EXPECT_FALSE(ec);
+    llvm::Error ec2 = writeBinary(f, tmpFl);
+    EXPECT_FALSE(ec2);
   }
 
   std::unique_ptr<MemoryBuffer> bufferOwner;
@@ -260,8 +260,8 @@ TEST(BinaryWriterTest, obj_relocs_x86) {
     std::error_code ec =
         llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl);
     EXPECT_FALSE(ec);
-    ec = writeBinary(f, tmpFl);
-    EXPECT_FALSE(ec);
+    llvm::Error ec2 = writeBinary(f, tmpFl);
+    EXPECT_FALSE(ec2);
   }
   std::unique_ptr<MemoryBuffer> bufferOwner;
   std::unique_ptr<NormalizedFile> f2;
@@ -378,8 +378,8 @@ TEST(BinaryWriterTest, obj_relocs_armv7)
     std::error_code ec =
         llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl);
     EXPECT_FALSE(ec);
-    ec = writeBinary(f, tmpFl);
-    EXPECT_FALSE(ec);
+    llvm::Error ec2 = writeBinary(f, tmpFl);
+    EXPECT_FALSE(ec2);
   }
   std::unique_ptr<MemoryBuffer> bufferOwner;
   std::unique_ptr<NormalizedFile> f2;
@@ -534,8 +534,8 @@ TEST(BinaryWriterTest, obj_relocs_ppc) {
     std::error_code ec =
         llvm::sys::fs::createTemporaryFile(Twine("xx"), "o", tmpFl);
     EXPECT_FALSE(ec);
-    ec = writeBinary(f, tmpFl);
-    EXPECT_FALSE(ec);
+    llvm::Error ec2 = writeBinary(f, tmpFl);
+    EXPECT_FALSE(ec2);
   }
   std::unique_ptr<MemoryBuffer> bufferOwner;
   std::unique_ptr<NormalizedFile> f2;




More information about the llvm-commits mailing list