[clang] e0de264 - [LinkerWrapper][NFC] Move error handling to a common function

Joseph Huber via cfe-commits cfe-commits at lists.llvm.org
Fri Jul 8 08:18:53 PDT 2022


Author: Joseph Huber
Date: 2022-07-08T11:18:38-04:00
New Revision: e0de264f6355c397e91927827a9cbb940c903607

URL: https://github.com/llvm/llvm-project/commit/e0de264f6355c397e91927827a9cbb940c903607
DIFF: https://github.com/llvm/llvm-project/commit/e0de264f6355c397e91927827a9cbb940c903607.diff

LOG: [LinkerWrapper][NFC] Move error handling to a common function

Summary:
This patch merges all the error handling functions to a single function
call so we don't define the same lambda many times.

Added: 
    

Modified: 
    clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp

Removed: 
    


################################################################################
diff  --git a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
index 4e4f85cbcd02..75e1a1b8a72c 100644
--- a/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
+++ b/clang/tools/clang-linker-wrapper/ClangLinkerWrapper.cpp
@@ -186,6 +186,13 @@ void printCommands(ArrayRef<StringRef> CmdArgs) {
     llvm::errs() << *IC << (std::next(IC) != IE ? " " : "\n");
 }
 
+[[noreturn]] void reportError(Error E) {
+  outs().flush();
+  logAllUnhandledErrors(std::move(E),
+                        WithColor::error(errs(), LinkerExecutable));
+  exit(EXIT_FAILURE);
+}
+
 /// Create an extra user-specified \p OffloadFile.
 /// TODO: We should find a way to wrap these as libraries instead.
 Expected<OffloadFile> getInputBitcodeLibrary(StringRef Input) {
@@ -764,23 +771,18 @@ std::unique_ptr<lto::LTO> createLTO(
   Conf.PTO.SLPVectorization = Conf.OptLevel > 1;
 
   if (SaveTemps) {
-    auto HandleError = [=](Error Err) {
-      logAllUnhandledErrors(std::move(Err),
-                            WithColor::error(errs(), LinkerExecutable));
-      exit(1);
-    };
     Conf.PostInternalizeModuleHook = [&, Arch](size_t, const Module &M) {
       auto TempFileOrErr =
           createOutputFile(sys::path::filename(ExecutableName) + "-" +
                                Triple.getTriple() + "-" + Arch,
                            "bc");
       if (!TempFileOrErr)
-        HandleError(TempFileOrErr.takeError());
+        reportError(TempFileOrErr.takeError());
 
       std::error_code EC;
       raw_fd_ostream LinkedBitcode(*TempFileOrErr, EC, sys::fs::OF_None);
       if (EC)
-        HandleError(errorCodeToError(EC));
+        reportError(errorCodeToError(EC));
       WriteBitcodeToFile(M, LinkedBitcode);
       return true;
     };
@@ -863,12 +865,6 @@ Error linkBitcodeFiles(SmallVectorImpl<OffloadFile> &InputFiles,
   // Remove all the bitcode files that we moved from the original input.
   llvm::erase_if(InputFiles, [](OffloadFile &F) { return !F.getBinary(); });
 
-  auto HandleError = [&](Error Err) {
-    logAllUnhandledErrors(std::move(Err),
-                          WithColor::error(errs(), LinkerExecutable));
-    exit(1);
-  };
-
   // LTO Module hook to output bitcode without running the backend.
   SmallVector<StringRef, 4> BitcodeOutput;
   auto OutputBitcode = [&](size_t Task, const Module &M) {
@@ -876,12 +872,12 @@ Error linkBitcodeFiles(SmallVectorImpl<OffloadFile> &InputFiles,
                                               "-jit-" + Triple.getTriple(),
                                           "bc");
     if (!TempFileOrErr)
-      HandleError(TempFileOrErr.takeError());
+      reportError(TempFileOrErr.takeError());
 
     std::error_code EC;
     raw_fd_ostream LinkedBitcode(*TempFileOrErr, EC, sys::fs::OF_None);
     if (EC)
-      HandleError(errorCodeToError(EC));
+      reportError(errorCodeToError(EC));
     WriteBitcodeToFile(M, LinkedBitcode);
     BitcodeOutput.push_back(*TempFileOrErr);
     return false;
@@ -964,10 +960,10 @@ Error linkBitcodeFiles(SmallVectorImpl<OffloadFile> &InputFiles,
                                               "-device-" + Triple.getTriple(),
                                           Extension);
     if (!TempFileOrErr)
-      HandleError(TempFileOrErr.takeError());
+      reportError(TempFileOrErr.takeError());
     TempFile = *TempFileOrErr;
     if (std::error_code EC = sys::fs::openFileForWrite(TempFile, FD))
-      HandleError(errorCodeToError(EC));
+      reportError(errorCodeToError(EC));
     return std::make_unique<CachedFileStream>(
         std::make_unique<llvm::raw_fd_ostream>(FD, true));
   };
@@ -1323,10 +1319,6 @@ int main(int Argc, char **Argv) {
 
   LinkerExecutable = Argv[0];
   sys::PrintStackTraceOnErrorSignal(Argv[0]);
-  auto reportError = [Argv](Error E) {
-    logAllUnhandledErrors(std::move(E), WithColor::error(errs(), Argv[0]));
-    return EXIT_FAILURE;
-  };
 
   const OptTable &Tbl = getOptTable();
   BumpPtrAllocator Alloc;
@@ -1380,13 +1372,13 @@ int main(int Argc, char **Argv) {
     ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
         MemoryBuffer::getFileOrSTDIN(Filename);
     if (std::error_code EC = BufferOrErr.getError())
-      return reportError(createFileError(Filename, EC));
+      reportError(createFileError(Filename, EC));
 
     bool IsLazy =
         identify_magic((*BufferOrErr)->getBuffer()) == file_magic::archive;
     if (Error Err = extractFromBuffer(std::move(*BufferOrErr),
                                       IsLazy ? LazyInputFiles : InputFiles))
-      return reportError(std::move(Err));
+      reportError(std::move(Err));
   }
 
   // Try to extract input from input libraries.
@@ -1395,18 +1387,18 @@ int main(int Argc, char **Argv) {
       ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
           MemoryBuffer::getFileOrSTDIN(*Library);
       if (std::error_code EC = BufferOrErr.getError())
-        return reportError(createFileError(*Library, EC));
+        reportError(createFileError(*Library, EC));
 
       if (Error Err =
               extractFromBuffer(std::move(*BufferOrErr), LazyInputFiles))
-        return reportError(std::move(Err));
+        reportError(std::move(Err));
     }
   }
 
   for (StringRef Library : Args.getAllArgValues(OPT_bitcode_library_EQ)) {
     auto FileOrErr = getInputBitcodeLibrary(Library);
     if (!FileOrErr)
-      return reportError(FileOrErr.takeError());
+      reportError(FileOrErr.takeError());
   }
 
   DenseSet<OffloadFile::TargetID> IsTargetUsed;
@@ -1423,7 +1415,7 @@ int main(int Argc, char **Argv) {
   // Link and wrap the device images extracted from the linker input.
   auto FilesOrErr = linkAndWrapDeviceFiles(InputFiles, Args);
   if (!FilesOrErr)
-    return reportError(FilesOrErr.takeError());
+    reportError(FilesOrErr.takeError());
 
   // Render the linker arguments and add the newly created image. We add it
   // after the output file to ensure it is linked with the correct libraries.
@@ -1440,7 +1432,7 @@ int main(int Argc, char **Argv) {
   // Run the host linking job with the rendered arguments.
   StringRef LinkerPath = Args.getLastArgValue(OPT_linker_path_EQ);
   if (Error Err = runLinker(LinkerPath, LinkerArgs))
-    return reportError(std::move(Err));
+    reportError(std::move(Err));
 
   // Remove the temporary files created.
   if (!SaveTemps)


        


More information about the cfe-commits mailing list