[PATCH] D100833: [llvm-cvtres] Reduce the set of dependencies of llvm-cvtres. NFC.

Martin Storsjö via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 20 02:36:29 PDT 2021


mstorsjo created this revision.
mstorsjo added reviewers: thakis, amccarth.
Herald added a subscriber: mgorny.
mstorsjo requested review of this revision.
Herald added a project: LLVM.

Don't use createBinary() but call the WindowsResource class directly.
The createBinary() function references all supported object file
types and ends up pulling way more from all the underlying libraries
than what is necessary.

This shrinks llvm-cvtres from 4.6 MB to 463 KB.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D100833

Files:
  llvm/tools/llvm-cvtres/CMakeLists.txt
  llvm/tools/llvm-cvtres/llvm-cvtres.cpp


Index: llvm/tools/llvm-cvtres/llvm-cvtres.cpp
===================================================================
--- llvm/tools/llvm-cvtres/llvm-cvtres.cpp
+++ llvm/tools/llvm-cvtres/llvm-cvtres.cpp
@@ -11,6 +11,7 @@
 //
 //===----------------------------------------------------------------------===//
 
+#include "llvm/BinaryFormat/Magic.h"
 #include "llvm/Object/Binary.h"
 #include "llvm/Object/WindowsMachineFlag.h"
 #include "llvm/Object/WindowsResource.h"
@@ -75,6 +76,14 @@
   reportError(Twine(Input) + ": " + EC.message() + ".\n");
 }
 
+static void error(StringRef Input, Error EC) {
+  if (!EC)
+    return;
+  handleAllErrors(std::move(EC), [&](const ErrorInfoBase &EI) {
+    reportError(Twine(Input) + ": " + EI.message() + ".\n");
+  });
+}
+
 static void error(Error EC) {
   if (!EC)
     return;
@@ -95,6 +104,16 @@
   return std::move(EC.get());
 }
 
+template <typename T> T error(StringRef Input, Expected<T> EC) {
+  if (!EC)
+    error(Input, EC.takeError());
+  return std::move(EC.get());
+}
+
+template <typename T> T error(StringRef Input, ErrorOr<T> &&EC) {
+  return error(Input, errorOrToExpected(std::move(EC)));
+}
+
 int main(int Argc, const char **Argv) {
   InitLLVM X(Argc, Argv);
 
@@ -155,15 +174,17 @@
   WindowsResourceParser Parser;
 
   for (const auto &File : InputFiles) {
-    Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(File);
-    if (!BinaryOrErr)
-      reportError(File, errorToErrorCode(BinaryOrErr.takeError()));
-
-    Binary &Binary = *BinaryOrErr.get().getBinary();
-
-    WindowsResource *RF = dyn_cast<WindowsResource>(&Binary);
-    if (!RF)
+    std::unique_ptr<MemoryBuffer> Buffer = error(
+        File, MemoryBuffer::getFileOrSTDIN(File, /*IsText=*/false,
+                                           /*RequiresNullTerminator=*/false));
+    file_magic Type = identify_magic(Buffer->getMemBufferRef().getBuffer());
+    if (Type != file_magic::windows_resource)
       reportError(File + ": unrecognized file format.\n");
+    std::unique_ptr<WindowsResource> Binary = error(
+        File,
+        WindowsResource::createWindowsResource(Buffer->getMemBufferRef()));
+
+    WindowsResource *RF = Binary.get();
 
     if (Verbose) {
       int EntryNumber = 0;
@@ -199,12 +220,14 @@
   error(FileBuffer->commit());
 
   if (Verbose) {
-    Expected<OwningBinary<Binary>> BinaryOrErr = createBinary(OutputFile);
-    if (!BinaryOrErr)
-      reportError(OutputFile, errorToErrorCode(BinaryOrErr.takeError()));
-    Binary &Binary = *BinaryOrErr.get().getBinary();
+    std::unique_ptr<MemoryBuffer> Buffer =
+        error(OutputFile,
+              MemoryBuffer::getFileOrSTDIN(OutputFile, /*IsText=*/false,
+                                           /*RequiresNullTerminator=*/false));
+
     ScopedPrinter W(errs());
-    W.printBinaryBlock("Output File Raw Data", Binary.getData());
+    W.printBinaryBlock("Output File Raw Data",
+                       Buffer->getMemBufferRef().getBuffer());
   }
 
   return 0;
Index: llvm/tools/llvm-cvtres/CMakeLists.txt
===================================================================
--- llvm/tools/llvm-cvtres/CMakeLists.txt
+++ llvm/tools/llvm-cvtres/CMakeLists.txt
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  BinaryFormat
   Object
   Option
   Support


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D100833.338781.patch
Type: text/x-patch
Size: 3283 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210420/56c4242e/attachment.bin>


More information about the llvm-commits mailing list