Skip to content

Commit ad29d29

Browse files
committed
[llvm-objcopy][NFC] More error cleanup
Summary: This removes calls to `error()`/`reportError()` in the main driver (llvm-objcopy.cpp) as well as the associated argv-parsing (CopyConfig.cpp). `logAllUnhandledErrors()` is now the main way to print errors. There are still a few uses from within the per-arch drivers, so we can't delete them yet... but almost! Reviewers: jhenderson, alexshap, espindola Reviewed By: jhenderson Subscribers: emaste, arichardson, jakehehrlich, jdoerfert, llvm-commits Tags: #llvm Differential Revision: https://reviews.llvm.org/D58316 llvm-svn: 354600
1 parent 1ed3a04 commit ad29d29

12 files changed

+188
-100
lines changed

llvm/test/tools/llvm-objcopy/ELF/bad-output-format.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ FileHeader:
1010
Type: ET_EXEC
1111
Machine: EM_386
1212

13-
# BAD-OUTPUT-FORMAT: Invalid output format: 'xyz'.
13+
# BAD-OUTPUT-FORMAT: Invalid output format: 'xyz'

llvm/test/tools/llvm-objcopy/ELF/binary-input-error.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
# RUN: not llvm-objcopy -I binary -B xyz %t.txt %t.o 2>&1 \
77
# RUN: | FileCheck %s --check-prefix=BAD-BINARY-ARCH
88

9-
# MISSING-BINARY-ARCH: Specified binary input without specifiying an architecture.
10-
# BAD-BINARY-ARCH: Invalid architecture: 'xyz'.
9+
# MISSING-BINARY-ARCH: Specified binary input without specifiying an architecture
10+
# BAD-BINARY-ARCH: Invalid architecture: 'xyz'

llvm/test/tools/llvm-objcopy/ELF/compress-and-decompress-debug-sections-error.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o
44
# RUN: not llvm-objcopy --compress-debug-sections=zlib --decompress-debug-sections %t.o 2>&1 | FileCheck %s
55

6-
# CHECK: Cannot specify --compress-debug-sections at the same time as --decompress-debug-sections at the same time.
6+
# CHECK: Cannot specify --compress-debug-sections at the same time as --decompress-debug-sections at the same time
77

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# RUN: yaml2obj %p/Inputs/compress-debug-sections.yaml -o %t.o
22
# RUN: not llvm-objcopy --compress-debug-sections=zlib-fake %t.o 2>&1 | FileCheck %s
33

4-
# CHECK: Invalid or unsupported --compress-debug-sections format: zlib-fake.
4+
# CHECK: Invalid or unsupported --compress-debug-sections format: zlib-fake
55

llvm/test/tools/llvm-objcopy/ELF/input-output-target.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
# RUN: %t.txt %t.4.txt 2>&1 \
2020
# RUN: | FileCheck %s --check-prefix=BAD-FLAG
2121

22-
# BAD-FLAG: --target cannot be used with --input-target or --output-target.
22+
# BAD-FLAG: --target cannot be used with --input-target or --output-target

llvm/test/tools/llvm-objcopy/ELF/rename-section-flag.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,4 @@ Sections:
5757
# WRITE-NEXT: SHF_WRITE (0x1)
5858
# CHECK-NEXT: ]
5959

60-
# BAD-FLAG: Unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings.
60+
# BAD-FLAG: Unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings

llvm/test/tools/llvm-objcopy/ELF/set-section-flags-and-rename.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ FileHeader:
1010
Type: ET_REL
1111
Machine: EM_X86_64
1212

13-
# SET-FOO: --set-section-flags=.foo conflicts with --rename-section=.foo=.bar.
14-
# SET-BAR: --set-section-flags=.bar conflicts with --rename-section=.foo=.bar.
13+
# SET-FOO: --set-section-flags=.foo conflicts with --rename-section=.foo=.bar
14+
# SET-BAR: --set-section-flags=.bar conflicts with --rename-section=.foo=.bar

llvm/test/tools/llvm-objcopy/ELF/set-section-flags.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,4 @@ Sections:
6565
# BAD-FORMAT: Bad format for --set-section-flags: missing '='
6666
# MULTIPLE-SETS: --set-section-flags set multiple times for section .foo
6767

68-
# BAD-FLAG: Unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings.
68+
# BAD-FLAG: Unrecognized section flag 'xyzzy'. Flags supported for GNU compatibility: alloc, load, noload, readonly, debug, code, data, rom, share, contents, merge, strings

llvm/test/tools/llvm-objcopy/ELF/strip-multiple-files.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,4 +72,4 @@ Symbols:
7272
# CHECK-NEXT: }
7373
# CHECK-NEXT: ]
7474

75-
# BAD-O-FLAG: Multiple input files cannot be used in combination with -o.
75+
# BAD-O-FLAG: Multiple input files cannot be used in combination with -o

llvm/tools/llvm-objcopy/CopyConfig.cpp

Lines changed: 137 additions & 66 deletions
Large diffs are not rendered by default.

llvm/tools/llvm-objcopy/CopyConfig.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "llvm/ADT/StringMap.h"
1616
#include "llvm/ADT/StringRef.h"
1717
#include "llvm/Support/Allocator.h"
18+
#include "llvm/Support/Error.h"
1819
#include "llvm/Support/Regex.h"
1920
// Necessary for llvm::DebugCompressionType::None
2021
#include "llvm/Target/TargetOptions.h"
@@ -131,12 +132,12 @@ struct DriverConfig {
131132
// ParseObjcopyOptions returns the config and sets the input arguments. If a
132133
// help flag is set then ParseObjcopyOptions will print the help messege and
133134
// exit.
134-
DriverConfig parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
135+
Expected<DriverConfig> parseObjcopyOptions(ArrayRef<const char *> ArgsArr);
135136

136137
// ParseStripOptions returns the config and sets the input arguments. If a
137138
// help flag is set then ParseStripOptions will print the help messege and
138139
// exit.
139-
DriverConfig parseStripOptions(ArrayRef<const char *> ArgsArr);
140+
Expected<DriverConfig> parseStripOptions(ArrayRef<const char *> ArgsArr);
140141

141142
} // namespace objcopy
142143
} // namespace llvm

llvm/tools/llvm-objcopy/llvm-objcopy.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -183,70 +183,86 @@ static Error executeObjcopyOnArchive(const CopyConfig &Config,
183183
Config.DeterministicArchives, Ar.isThin());
184184
}
185185

186-
static void restoreDateOnFile(StringRef Filename,
187-
const sys::fs::file_status &Stat) {
186+
static Error restoreDateOnFile(StringRef Filename,
187+
const sys::fs::file_status &Stat) {
188188
int FD;
189189

190190
if (auto EC =
191191
sys::fs::openFileForWrite(Filename, FD, sys::fs::CD_OpenExisting))
192-
reportError(Filename, EC);
192+
return createFileError(Filename, EC);
193193

194194
if (auto EC = sys::fs::setLastAccessAndModificationTime(
195195
FD, Stat.getLastAccessedTime(), Stat.getLastModificationTime()))
196-
reportError(Filename, EC);
196+
return createFileError(Filename, EC);
197197

198198
if (auto EC = sys::Process::SafelyCloseFileDescriptor(FD))
199-
reportError(Filename, EC);
199+
return createFileError(Filename, EC);
200+
201+
return Error::success();
200202
}
201203

202204
/// The function executeObjcopy does the higher level dispatch based on the type
203205
/// of input (raw binary, archive or single object file) and takes care of the
204206
/// format-agnostic modifications, i.e. preserving dates.
205-
static void executeObjcopy(const CopyConfig &Config) {
207+
static Error executeObjcopy(const CopyConfig &Config) {
206208
sys::fs::file_status Stat;
207209
if (Config.PreserveDates)
208210
if (auto EC = sys::fs::status(Config.InputFilename, Stat))
209-
reportError(Config.InputFilename, EC);
211+
return createFileError(Config.InputFilename, EC);
210212

211213
if (Config.InputFormat == "binary") {
212214
auto BufOrErr = MemoryBuffer::getFile(Config.InputFilename);
213215
if (!BufOrErr)
214-
reportError(Config.InputFilename, BufOrErr.getError());
216+
return createFileError(Config.InputFilename, BufOrErr.getError());
215217
FileBuffer FB(Config.OutputFilename);
216218
if (Error E = executeObjcopyOnRawBinary(Config, *BufOrErr->get(), FB))
217-
error(std::move(E));
219+
return E;
218220
} else {
219221
Expected<OwningBinary<llvm::object::Binary>> BinaryOrErr =
220222
createBinary(Config.InputFilename);
221223
if (!BinaryOrErr)
222-
reportError(Config.InputFilename, BinaryOrErr.takeError());
224+
return createFileError(Config.InputFilename, BinaryOrErr.takeError());
223225

224226
if (Archive *Ar = dyn_cast<Archive>(BinaryOrErr.get().getBinary())) {
225227
if (Error E = executeObjcopyOnArchive(Config, *Ar))
226-
error(std::move(E));
228+
return E;
227229
} else {
228230
FileBuffer FB(Config.OutputFilename);
229231
if (Error E = executeObjcopyOnBinary(Config,
230232
*BinaryOrErr.get().getBinary(), FB))
231-
error(std::move(E));
233+
return E;
232234
}
233235
}
234236

235237
if (Config.PreserveDates) {
236-
restoreDateOnFile(Config.OutputFilename, Stat);
238+
if (Error E = restoreDateOnFile(Config.OutputFilename, Stat))
239+
return E;
237240
if (!Config.SplitDWO.empty())
238-
restoreDateOnFile(Config.SplitDWO, Stat);
241+
if (Error E = restoreDateOnFile(Config.SplitDWO, Stat))
242+
return E;
239243
}
244+
245+
return Error::success();
240246
}
241247

242248
int main(int argc, char **argv) {
243249
InitLLVM X(argc, argv);
244250
ToolName = argv[0];
245-
DriverConfig DriverConfig;
246-
if (sys::path::stem(ToolName).contains("strip"))
247-
DriverConfig = parseStripOptions(makeArrayRef(argv + 1, argc));
248-
else
249-
DriverConfig = parseObjcopyOptions(makeArrayRef(argv + 1, argc));
250-
for (const CopyConfig &CopyConfig : DriverConfig.CopyConfigs)
251-
executeObjcopy(CopyConfig);
251+
bool IsStrip = sys::path::stem(ToolName).contains("strip");
252+
Expected<DriverConfig> DriverConfig =
253+
IsStrip ? parseStripOptions(makeArrayRef(argv + 1, argc))
254+
: parseObjcopyOptions(makeArrayRef(argv + 1, argc));
255+
if (!DriverConfig) {
256+
logAllUnhandledErrors(DriverConfig.takeError(),
257+
WithColor::error(errs(), ToolName));
258+
return 1;
259+
}
260+
for (const CopyConfig &CopyConfig : DriverConfig->CopyConfigs) {
261+
if (Error E = executeObjcopy(CopyConfig)) {
262+
logAllUnhandledErrors(std::move(E), WithColor::error(errs(), ToolName));
263+
return 1;
264+
}
265+
}
266+
267+
return 0;
252268
}

0 commit comments

Comments
 (0)