Index: llvm/trunk/test/tools/llvm-objcopy/ELF/same-file-strip.test =================================================================== --- llvm/trunk/test/tools/llvm-objcopy/ELF/same-file-strip.test +++ llvm/trunk/test/tools/llvm-objcopy/ELF/same-file-strip.test @@ -0,0 +1,26 @@ +## Test llvm-strip using the same input file more than once. +## When using stdin ('-') more than once llvm-strip should give an error +## while a file more than once should be simply a warning. + +# 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: .alloc + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC ] Index: llvm/trunk/tools/llvm-objcopy/CopyConfig.h =================================================================== --- llvm/trunk/tools/llvm-objcopy/CopyConfig.h +++ llvm/trunk/tools/llvm-objcopy/CopyConfig.h @@ -188,8 +188,11 @@ // ParseStripOptions returns the config and sets the input arguments. If a // help flag is set then ParseStripOptions will print the help messege and -// exit. -Expected parseStripOptions(ArrayRef ArgsArr); +// exit. ErrorCallback is used to handle recoverable errors. An Error returned +// by the callback aborts the parsing and is then returned by this function. +Expected +parseStripOptions(ArrayRef ArgsArr, + std::function ErrorCallback); } // namespace objcopy } // namespace llvm Index: llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp =================================================================== --- llvm/trunk/tools/llvm-objcopy/CopyConfig.cpp +++ llvm/trunk/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" @@ -722,7 +723,9 @@ // ParseStripOptions returns the config and sets the input arguments. If a // help flag is set then ParseStripOptions will print the help messege and // exit. -Expected parseStripOptions(ArrayRef ArgsArr) { +Expected +parseStripOptions(ArrayRef ArgsArr, + std::function ErrorCallback) { StripOptTable T; unsigned MissingArgumentIndex, MissingArgumentCount; llvm::opt::InputArgList InputArgs = @@ -809,7 +812,18 @@ InputArgs.getLastArgValue(STRIP_output, Positional[0]); DC.CopyConfigs.push_back(std::move(Config)); } else { + StringMap 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"); + if (Error E = ErrorCallback(createStringError( + errc::invalid_argument, "'%s' was already specified", + Filename.str().c_str()))) + return std::move(E); + } Config.InputFilename = Filename; Config.OutputFilename = Filename; DC.CopyConfigs.push_back(Config); Index: llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp =================================================================== --- llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp +++ llvm/trunk/tools/llvm-objcopy/llvm-objcopy.cpp @@ -82,6 +82,12 @@ exit(1); } +ErrorSuccess reportWarning(Error E) { + assert(E); + WithColor::warning(errs(), ToolName) << toString(std::move(E)); + return Error::success(); +} + } // end namespace objcopy } // end namespace llvm @@ -263,7 +269,7 @@ ToolName = argv[0]; bool IsStrip = sys::path::stem(ToolName).contains("strip"); Expected DriverConfig = - IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc)) + IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc), reportWarning) : parseObjcopyOptions(makeArrayRef(argv + 1, argc)); if (!DriverConfig) { logAllUnhandledErrors(DriverConfig.takeError(),