llvm.org GIT mirror llvm / 1a8c687
[llvm-strip] Error when using stdin twice Summary: Implements bug [[ https://bugs.llvm.org/show_bug.cgi?id=42204 | 42204 ]]. llvm-strip now warns when the same input file is used more than once, and errors when stdin is used more than once. Reviewers: jhenderson, rupprecht, espindola, alexshap Reviewed By: jhenderson, rupprecht Subscribers: emaste, arichardson, jakehehrlich, MaskRay, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D63122 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@363638 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Brachet a month ago
4 changed file(s) with 53 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
0 ## Test llvm-strip using the same input file more than once.
1 ## When using stdin ('-') more than once llvm-strip should give an error
2 ## while a file more than once should be simply a warning.
3
4 # RUN: yaml2obj %s -o %t
5
6 # RUN: not llvm-strip - - < %t 2>&1 | FileCheck -check-prefix=ERR %s
7 # RUN: not llvm-strip - %t - < %t 2>&1 | FileCheck -check-prefix=ERR %s
8
9 # ERR: error: cannot specify '-' as an input file more than once
10
11 # RUN: llvm-strip %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t
12 # RUN: llvm-strip %t %t %t 2>&1 | FileCheck -check-prefix=WARN %s -DFILE=%t
13
14 # WARN: warning: '[[FILE]]' was already specified
15
16 --- !ELF
17 FileHeader:
18 Class: ELFCLASS64
19 Data: ELFDATA2LSB
20 Type: ET_REL
21 Machine: EM_X86_64
22 Sections:
23 - Name: .alloc
24 Type: SHT_PROGBITS
25 Flags: [ SHF_ALLOC ]
1010 #include "llvm/ADT/Optional.h"
1111 #include "llvm/ADT/SmallVector.h"
1212 #include "llvm/ADT/StringRef.h"
13 #include "llvm/ADT/StringSet.h"
1314 #include "llvm/Option/Arg.h"
1415 #include "llvm/Option/ArgList.h"
1516 #include "llvm/Support/CommandLine.h"
721722 // ParseStripOptions returns the config and sets the input arguments. If a
722723 // help flag is set then ParseStripOptions will print the help messege and
723724 // exit.
724 Expected parseStripOptions(ArrayRef ArgsArr) {
725 Expected
726 parseStripOptions(ArrayRef ArgsArr,
727 std::function ErrorCallback) {
725728 StripOptTable T;
726729 unsigned MissingArgumentIndex, MissingArgumentCount;
727730 llvm::opt::InputArgList InputArgs =
808811 InputArgs.getLastArgValue(STRIP_output, Positional[0]);
809812 DC.CopyConfigs.push_back(std::move(Config));
810813 } else {
814 StringMap InputFiles;
811815 for (StringRef Filename : Positional) {
816 if (InputFiles[Filename]++ == 1) {
817 if (Filename == "-")
818 return createStringError(
819 errc::invalid_argument,
820 "cannot specify '-' as an input file more than once");
821 if (Error E = ErrorCallback(createStringError(
822 errc::invalid_argument, "'%s' was already specified",
823 Filename.str().c_str())))
824 return std::move(E);
825 }
812826 Config.InputFilename = Filename;
813827 Config.OutputFilename = Filename;
814828 DC.CopyConfigs.push_back(Config);
187187
188188 // ParseStripOptions returns the config and sets the input arguments. If a
189189 // help flag is set then ParseStripOptions will print the help messege and
190 // exit.
191 Expected parseStripOptions(ArrayRef ArgsArr);
190 // exit. ErrorCallback is used to handle recoverable errors. An Error returned
191 // by the callback aborts the parsing and is then returned by this function.
192 Expected
193 parseStripOptions(ArrayRef ArgsArr,
194 std::function ErrorCallback);
192195
193196 } // namespace objcopy
194197 } // namespace llvm
8181 exit(1);
8282 }
8383
84 ErrorSuccess reportWarning(Error E) {
85 assert(E);
86 WithColor::warning(errs(), ToolName) << toString(std::move(E));
87 return Error::success();
88 }
89
8490 } // end namespace objcopy
8591 } // end namespace llvm
8692
262268 ToolName = argv[0];
263269 bool IsStrip = sys::path::stem(ToolName).contains("strip");
264270 Expected DriverConfig =
265 IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc))
271 IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc), reportWarning)
266272 : parseObjcopyOptions(makeArrayRef(argv + 1, argc));
267273 if (!DriverConfig) {
268274 logAllUnhandledErrors(DriverConfig.takeError(),