[llvm] ee34ca3 - [llvm-cvtres] Reduce the set of dependencies of llvm-cvtres. NFC.

Martin Storsjö via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 21 01:50:44 PDT 2021


Author: Martin Storsjö
Date: 2021-04-21T11:50:10+03:00
New Revision: ee34ca34c6675531c0d86709ebf99beef2ee7db1

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

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

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 a stripped llvm-cvtres from 4.6 MB to 463 KB.

Differential Revision: https://reviews.llvm.org/D100833

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/llvm/tools/llvm-cvtres/CMakeLists.txt b/llvm/tools/llvm-cvtres/CMakeLists.txt
index e912030e205ee..0898318ddfcc6 100644
--- a/llvm/tools/llvm-cvtres/CMakeLists.txt
+++ b/llvm/tools/llvm-cvtres/CMakeLists.txt
@@ -1,4 +1,5 @@
 set(LLVM_LINK_COMPONENTS
+  BinaryFormat
   Object
   Option
   Support

diff  --git a/llvm/tools/llvm-cvtres/llvm-cvtres.cpp b/llvm/tools/llvm-cvtres/llvm-cvtres.cpp
index 11cfb466e119e..a77d5f1126a07 100644
--- a/llvm/tools/llvm-cvtres/llvm-cvtres.cpp
+++ b/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 @@ static void reportError(StringRef Input, std::error_code EC) {
   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 @@ template <typename T> T error(Expected<T> EC) {
   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 @@ int main(int Argc, const char **Argv) {
   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 @@ int main(int Argc, const char **Argv) {
   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;


        


More information about the llvm-commits mailing list