[PATCH] D63122: [llvm-strip] Error when using stdin twice

Alex Brachet via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 11 15:27:24 PDT 2019


abrachet updated this revision to Diff 204182.
abrachet added a comment.

Added period at the end of comment.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D63122/new/

https://reviews.llvm.org/D63122

Files:
  llvm/test/tools/llvm-objcopy/ELF/same-file-strip.test
  llvm/tools/llvm-objcopy/CopyConfig.cpp


Index: llvm/tools/llvm-objcopy/CopyConfig.cpp
===================================================================
--- llvm/tools/llvm-objcopy/CopyConfig.cpp
+++ llvm/tools/llvm-objcopy/CopyConfig.cpp
@@ -11,6 +11,7 @@
 #include "llvm/ADT/Optional.h"
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/StringSet.h"
 #include "llvm/Option/Arg.h"
 #include "llvm/Option/ArgList.h"
 #include "llvm/Support/CommandLine.h"
@@ -19,11 +20,19 @@
 #include "llvm/Support/JamCRC.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/StringSaver.h"
+#include "llvm/Support/WithColor.h"
 #include <memory>
 
 namespace llvm {
 namespace objcopy {
 
+extern StringRef ToolName;
+
+static void reportWarning(Twine Message) {
+  WithColor::warning(errs(), ToolName) << Message << "\n";
+  errs().flush();
+}
+
 namespace {
 enum ObjcopyID {
   OBJCOPY_INVALID = 0, // This is not an option ID.
@@ -735,7 +744,7 @@
     exit(0);
   }
 
-  SmallVector<const char *, 2> Positional;
+  SmallVector<StringRef, 2> Positional;
   for (auto Arg : InputArgs.filtered(STRIP_UNKNOWN))
     return createStringError(errc::invalid_argument, "unknown argument '%s'",
                              Arg->getAsString(InputArgs).c_str());
@@ -800,7 +809,15 @@
         InputArgs.getLastArgValue(STRIP_output, Positional[0]);
     DC.CopyConfigs.push_back(std::move(Config));
   } else {
-    for (const char *Filename : Positional) {
+    StringMap<int> InputFiles;
+    for (StringRef Filename : Positional) {
+      if (InputFiles[Filename]++ == 1) {
+        if (Filename == "-")
+          return createStringError(
+              errc::invalid_argument,
+              "cannot specify '-' as an input file more than once");
+        reportWarning("'" + Filename + "' was already specified");
+      }
       Config.InputFilename = Filename;
       Config.OutputFilename = Filename;
       DC.CopyConfigs.push_back(Config);
Index: llvm/test/tools/llvm-objcopy/ELF/same-file-strip.test
===================================================================
--- /dev/null
+++ llvm/test/tools/llvm-objcopy/ELF/same-file-strip.test
@@ -0,0 +1,24 @@
+## Test strip using the same input file more than once.
+
+# RUN: yaml2obj %s -o %t
+
+# RUN: not llvm-strip - - < %t 2>&1 | FileCheck -check-prefix=ERR %s
+# RUN: not llvm-strip - %t - < %t 2>&1 | FileCheck -check-prefix=ERR %s
+
+# ERR: error: cannot specify '-' as an input file more than once
+
+# RUN: llvm-strip %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t
+# RUN: llvm-strip %t %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t
+
+# WARN: warning: '[[FILE]]' was already specified
+
+!ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D63122.204182.patch
Type: text/x-patch
Size: 2932 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190611/68538418/attachment.bin>


More information about the llvm-commits mailing list