[lld] r275504 - COFF: Remove `void error()` functions and use fatal instead.

Rui Ueyama via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 14 17:40:47 PDT 2016


Author: ruiu
Date: Thu Jul 14 19:40:46 2016
New Revision: 275504

URL: http://llvm.org/viewvc/llvm-project?rev=275504&view=rev
Log:
COFF: Remove `void error()` functions and use fatal instead.

This change makes the control flow more explicit.

Modified:
    lld/trunk/COFF/Driver.cpp
    lld/trunk/COFF/DriverUtils.cpp
    lld/trunk/COFF/Error.cpp
    lld/trunk/COFF/Error.h
    lld/trunk/COFF/InputFiles.cpp
    lld/trunk/COFF/Librarian.cpp
    lld/trunk/COFF/PDB.cpp
    lld/trunk/COFF/Writer.cpp

Modified: lld/trunk/COFF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Driver.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/Driver.cpp (original)
+++ lld/trunk/COFF/Driver.cpp Thu Jul 14 19:40:46 2016
@@ -62,7 +62,8 @@ static std::string getOutputPath(StringR
 // Newly created memory buffers are owned by this driver.
 MemoryBufferRef LinkerDriver::openFile(StringRef Path) {
   auto MBOrErr = MemoryBuffer::getFile(Path);
-  check(MBOrErr, "Could not open " + Path);
+  if (auto EC = MBOrErr.getError())
+    fatal(EC, "Could not open " + Path);
   std::unique_ptr<MemoryBuffer> &MB = *MBOrErr;
   MemoryBufferRef MBRef = MB->getMemBufferRef();
   OwningMBs.push_back(std::move(MB)); // take ownership
@@ -683,7 +684,8 @@ void LinkerDriver::link(llvm::ArrayRef<c
   if (auto *Arg = Args.getLastArg(OPT_lldmap)) {
     std::error_code EC;
     llvm::raw_fd_ostream Out(Arg->getValue(), EC, OpenFlags::F_Text);
-    check(EC, "Could not create the symbol map");
+    if (EC)
+      fatal(EC, "Could not create the symbol map");
     Symtab.printMap(Out);
   }
   // Call exit to avoid calling destructors.

Modified: lld/trunk/COFF/DriverUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/DriverUtils.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/DriverUtils.cpp (original)
+++ lld/trunk/COFF/DriverUtils.cpp Thu Jul 14 19:40:46 2016
@@ -50,7 +50,8 @@ public:
 
   void run() {
     ErrorOr<std::string> ExeOrErr = llvm::sys::findProgramByName(Prog);
-    check(ExeOrErr, "unable to find " + Prog + " in PATH: ");
+    if (auto EC = ExeOrErr.getError())
+      fatal(EC, "unable to find " + Prog + " in PATH: ");
     const char *Exe = Saver.save(*ExeOrErr);
     Args.insert(Args.begin(), Exe);
     Args.push_back(nullptr);
@@ -282,12 +283,14 @@ static void quoteAndPrint(raw_ostream &O
 static std::string createDefaultXml() {
   // Create a temporary file.
   SmallString<128> Path;
-  std::error_code EC = sys::fs::createTemporaryFile("tmp", "manifest", Path);
-  check(EC, "cannot create a temporary file");
+  if (auto EC = sys::fs::createTemporaryFile("tmp", "manifest", Path))
+    fatal(EC, "cannot create a temporary file");
 
   // Open the temporary file for writing.
+  std::error_code EC;
   llvm::raw_fd_ostream OS(Path, EC, sys::fs::F_Text);
-  check(EC, "failed to open " + Path);
+  if (EC)
+    fatal(EC, "failed to open " + Path);
 
   // Emit the XML. Note that we do *not* verify that the XML attributes are
   // syntactically correct. This is intentional for link.exe compatibility.
@@ -318,7 +321,8 @@ static std::string createDefaultXml() {
 
 static std::string readFile(StringRef Path) {
   ErrorOr<std::unique_ptr<MemoryBuffer>> BufOrErr = MemoryBuffer::getFile(Path);
-  check(BufOrErr, "Could not open " + Path);
+  if (auto EC = BufOrErr.getError())
+    fatal(EC, "Could not open " + Path);
   std::unique_ptr<MemoryBuffer> Buf(std::move(*BufOrErr));
   return Buf->getBuffer();
 }
@@ -332,8 +336,8 @@ static std::string createManifestXml() {
   // If manifest files are supplied by the user using /MANIFESTINPUT
   // option, we need to merge them with the default manifest.
   SmallString<128> Path2;
-  std::error_code EC = sys::fs::createTemporaryFile("tmp", "manifest", Path2);
-  check(EC, "cannot create a temporary file");
+  if (auto EC = sys::fs::createTemporaryFile("tmp", "manifest", Path2))
+    fatal(EC, "cannot create a temporary file");
   FileRemover Remover1(Path1);
   FileRemover Remover2(Path2);
 
@@ -354,13 +358,15 @@ static std::string createManifestXml() {
 std::unique_ptr<MemoryBuffer> createManifestRes() {
   // Create a temporary file for the resource script file.
   SmallString<128> RCPath;
-  std::error_code EC = sys::fs::createTemporaryFile("tmp", "rc", RCPath);
-  check(EC, "cannot create a temporary file");
+  if (auto EC = sys::fs::createTemporaryFile("tmp", "rc", RCPath))
+    fatal(EC, "cannot create a temporary file");
   FileRemover RCRemover(RCPath);
 
   // Open the temporary file for writing.
+  std::error_code EC;
   llvm::raw_fd_ostream Out(RCPath, EC, sys::fs::F_Text);
-  check(EC, "failed to open " + RCPath);
+  if (EC)
+    fatal(EC, "failed to open " + RCPath);
 
   // Write resource script to the RC file.
   Out << "#define LANG_ENGLISH 9\n"
@@ -375,8 +381,8 @@ std::unique_ptr<MemoryBuffer> createMani
 
   // Create output resource file.
   SmallString<128> ResPath;
-  EC = sys::fs::createTemporaryFile("tmp", "res", ResPath);
-  check(EC, "cannot create a temporary file");
+  if (auto EC = sys::fs::createTemporaryFile("tmp", "res", ResPath))
+    fatal(EC, "cannot create a temporary file");
 
   Executor E("rc.exe");
   E.add("/fo");
@@ -385,7 +391,8 @@ std::unique_ptr<MemoryBuffer> createMani
   E.add(RCPath.str());
   E.run();
   ErrorOr<std::unique_ptr<MemoryBuffer>> Ret = MemoryBuffer::getFile(ResPath);
-  check(Ret, "Could not open " + ResPath);
+  if (auto EC = Ret.getError())
+    fatal(EC, "Could not open " + ResPath);
   return std::move(*Ret);
 }
 
@@ -395,7 +402,8 @@ void createSideBySideManifest() {
     Path = Config->OutputFile + ".manifest";
   std::error_code EC;
   llvm::raw_fd_ostream Out(Path, EC, llvm::sys::fs::F_Text);
-  check(EC, "failed to create manifest");
+  if (EC)
+    fatal(EC, "failed to create manifest");
   Out << createManifestXml();
 }
 
@@ -565,7 +573,8 @@ convertResToCOFF(const std::vector<Memor
     E.add(MB.getBufferIdentifier());
   E.run();
   ErrorOr<std::unique_ptr<MemoryBuffer>> Ret = MemoryBuffer::getFile(Path);
-  check(Ret, "Could not open " + Path);
+  if (auto EC = Ret.getError())
+    fatal(EC, "Could not open " + Path);
   return std::move(*Ret);
 }
 

Modified: lld/trunk/COFF/Error.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/Error.cpp (original)
+++ lld/trunk/COFF/Error.cpp Thu Jul 14 19:40:46 2016
@@ -10,6 +10,7 @@
 #include "Error.h"
 
 #include "llvm/ADT/Twine.h"
+#include "llvm/Support/Error.h"
 #include "llvm/Support/raw_ostream.h"
 
 namespace lld {
@@ -20,18 +21,12 @@ void fatal(const Twine &Msg) {
   exit(1);
 }
 
-void check(std::error_code EC, const Twine &Prefix) {
-  if (!EC)
-    return;
-  fatal(Prefix + ": " + EC.message());
+void fatal(std::error_code EC, const Twine &Msg) {
+  fatal(Msg + ": " + EC.message());
 }
 
-void check(llvm::Error E, const Twine &Prefix) {
-  if (!E)
-    return;
-  handleAllErrors(std::move(E), [&](const llvm::ErrorInfoBase &EIB) {
-    fatal(Prefix + ": " + EIB.message());
-  });
+void fatal(llvm::Error &Err, const Twine &Msg) {
+  fatal(errorToErrorCode(std::move(Err)), Msg);
 }
 
 } // namespace coff

Modified: lld/trunk/COFF/Error.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Error.h?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/Error.h (original)
+++ lld/trunk/COFF/Error.h Thu Jul 14 19:40:46 2016
@@ -17,18 +17,18 @@ namespace lld {
 namespace coff {
 
 LLVM_ATTRIBUTE_NORETURN void fatal(const Twine &Msg);
-void check(std::error_code EC, const Twine &Prefix);
-void check(llvm::Error E, const Twine &Prefix);
+LLVM_ATTRIBUTE_NORETURN void fatal(std::error_code EC, const Twine &Prefix);
+LLVM_ATTRIBUTE_NORETURN void fatal(llvm::Error &Err, const Twine &Prefix);
 
 template <typename T> void check(const ErrorOr<T> &V, const Twine &Prefix) {
-  check(V.getError(), Prefix);
+  if (auto EC = V.getError())
+    fatal(EC, Prefix);
 }
 
 template <class T> T check(Expected<T> E, const Twine &Prefix) {
   if (E)
     return std::move(*E);
   fatal(E.takeError(), Prefix);
-  return T();
 }
 
 } // namespace coff

Modified: lld/trunk/COFF/InputFiles.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/InputFiles.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/InputFiles.cpp (original)
+++ lld/trunk/COFF/InputFiles.cpp Thu Jul 14 19:40:46 2016
@@ -64,8 +64,8 @@ std::string InputFile::getShortName() {
 void ArchiveFile::parse() {
   // Parse a MemoryBufferRef as an archive file.
   auto ArchiveOrErr = Archive::create(MB);
-  check(errorToErrorCode(ArchiveOrErr.takeError()),
-        "Failed to parse static library");
+  if (auto Err = ArchiveOrErr.takeError())
+    fatal(Err, "Failed to parse static library");
   File = std::move(*ArchiveOrErr);
 
   // Allocate a buffer for Lazy objects.
@@ -82,31 +82,33 @@ void ArchiveFile::parse() {
   Error Err;
   for (auto &Child : File->children(Err))
     Seen[Child.getChildOffset()].clear();
-  check(std::move(Err), "Failed to parse static library");
+  if (Err)
+    fatal(Err, "Failed to parse static library");
 }
 
 // Returns a buffer pointing to a member file containing a given symbol.
 // This function is thread-safe.
 MemoryBufferRef ArchiveFile::getMember(const Archive::Symbol *Sym) {
   auto COrErr = Sym->getMember();
-  check(COrErr, "Could not get the member for symbol " + Sym->getName());
+  if (auto EC = COrErr.getError())
+    fatal(EC, "Could not get the member for symbol " + Sym->getName());
   const Archive::Child &C = *COrErr;
 
   // Return an empty buffer if we have already returned the same buffer.
   if (Seen[C.getChildOffset()].test_and_set())
     return MemoryBufferRef();
   ErrorOr<MemoryBufferRef> Ret = C.getMemoryBufferRef();
-  check(Ret, "Could not get the buffer for the member defining symbol " +
-                 Sym->getName());
+  if (auto EC = Ret.getError())
+    fatal(EC, "Could not get the buffer for the member defining symbol " +
+                  Sym->getName());
   return *Ret;
 }
 
 void ObjectFile::parse() {
   // Parse a memory buffer as a COFF file.
   auto BinOrErr = createBinary(MB);
-  if (!BinOrErr)
-    check(errorToErrorCode(BinOrErr.takeError()),
-                           "Failed to parse object file");
+  if (auto Err = BinOrErr.takeError())
+    fatal(Err, "Failed to parse object file");
   std::unique_ptr<Binary> Bin = std::move(*BinOrErr);
 
   if (auto *Obj = dyn_cast<COFFObjectFile>(Bin.get())) {
@@ -129,10 +131,10 @@ void ObjectFile::initializeChunks() {
   for (uint32_t I = 1; I < NumSections + 1; ++I) {
     const coff_section *Sec;
     StringRef Name;
-    std::error_code EC = COFFObj->getSection(I, Sec);
-    check(EC, "getSection failed: #" + Twine(I));
-    EC = COFFObj->getSectionName(Sec, Name);
-    check(EC, "getSectionName failed: #" + Twine(I));
+    if (auto EC = COFFObj->getSection(I, Sec))
+      fatal(EC, "getSection failed: #" + Twine(I));
+    if (auto EC = COFFObj->getSectionName(Sec, Name))
+      fatal(EC, "getSectionName failed: #" + Twine(I));
     if (Name == ".sxdata") {
       SXData = Sec;
       continue;
@@ -167,7 +169,8 @@ void ObjectFile::initializeSymbols() {
   for (uint32_t I = 0; I < NumSymbols; ++I) {
     // Get a COFFSymbolRef object.
     auto SymOrErr = COFFObj->getSymbol(I);
-    check(SymOrErr, "broken object file: " + getName());
+    if (auto EC = SymOrErr.getError())
+      fatal(EC, "broken object file: " + getName());
 
     COFFSymbolRef Sym = *SymOrErr;
 
@@ -334,7 +337,8 @@ void BitcodeFile::parse() {
   Context.enableDebugTypeODRUniquing();
   ErrorOr<std::unique_ptr<LTOModule>> ModOrErr = LTOModule::createFromBuffer(
       Context, MB.getBufferStart(), MB.getBufferSize(), llvm::TargetOptions());
-  check(ModOrErr, "Could not create lto module");
+  if (auto EC = ModOrErr.getError())
+    fatal(EC, "Could not create lto module");
   M = std::move(*ModOrErr);
 
   llvm::StringSaver Saver(Alloc);

Modified: lld/trunk/COFF/Librarian.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Librarian.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/Librarian.cpp (original)
+++ lld/trunk/COFF/Librarian.cpp Thu Jul 14 19:40:46 2016
@@ -484,5 +484,6 @@ void lld::coff::writeImportLibrary() {
   std::pair<StringRef, std::error_code> Result =
       writeArchive(Path, Members, /*WriteSymtab*/ true, object::Archive::K_GNU,
                    /*Deterministic*/ true, /*Thin*/ false);
-  check(Result.second, Twine("Failed to write ") + Path);
+  if (auto EC = Result.second)
+    fatal(EC, Twine("Failed to write ") + Path);
 }

Modified: lld/trunk/COFF/PDB.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/PDB.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/PDB.cpp (original)
+++ lld/trunk/COFF/PDB.cpp Thu Jul 14 19:40:46 2016
@@ -38,7 +38,8 @@ void lld::coff::createPDB(StringRef Path
   size_t FileSize = PageSize * 3;
   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
       FileOutputBuffer::create(Path, FileSize);
-  check(BufferOrErr, "failed to open " + Path);
+  if (auto EC = BufferOrErr.getError())
+    fatal(EC, "failed to open " + Path);
   std::unique_ptr<FileOutputBuffer> Buffer = std::move(*BufferOrErr);
 
   // Write the file header.

Modified: lld/trunk/COFF/Writer.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/COFF/Writer.cpp?rev=275504&r1=275503&r2=275504&view=diff
==============================================================================
--- lld/trunk/COFF/Writer.cpp (original)
+++ lld/trunk/COFF/Writer.cpp Thu Jul 14 19:40:46 2016
@@ -239,7 +239,8 @@ void Writer::run() {
   fixSafeSEHSymbols();
   writeSections();
   sortExceptionTable();
-  check(Buffer->commit(), "Failed to write the output file");
+  if (auto EC = Buffer->commit())
+    fatal(EC, "Failed to write the output file");
 }
 
 static StringRef getOutputSection(StringRef Name) {
@@ -653,7 +654,8 @@ template <typename PEHeaderTy> void Writ
 void Writer::openFile(StringRef Path) {
   ErrorOr<std::unique_ptr<FileOutputBuffer>> BufferOrErr =
       FileOutputBuffer::create(Path, FileSize, FileOutputBuffer::F_executable);
-  check(BufferOrErr, "failed to open " + Path);
+  if (auto EC = BufferOrErr.getError())
+    fatal(EC, "failed to open " + Path);
   Buffer = std::move(*BufferOrErr);
 }
 




More information about the llvm-commits mailing list