Skip to content

Commit f3c9687

Browse files
committed
llvm-lib: Pull error printing code out of two functions
Slightly changes the output in error code, but no behavior change in normal use. This is for preparation for using these two functions elsewhere.
1 parent 3caa2d3 commit f3c9687

File tree

1 file changed

+22
-22
lines changed

1 file changed

+22
-22
lines changed

llvm/lib/ToolDrivers/llvm-lib/LibDriver.cpp

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -139,35 +139,28 @@ static void doList(opt::InputArgList& Args) {
139139
fatalOpenError(std::move(Err), B->getBufferIdentifier());
140140
}
141141

142-
static COFF::MachineTypes getCOFFFileMachine(MemoryBufferRef MB) {
142+
static Expected<COFF::MachineTypes> getCOFFFileMachine(MemoryBufferRef MB) {
143143
std::error_code EC;
144144
auto Obj = object::COFFObjectFile::create(MB);
145-
if (!Obj) {
146-
llvm::errs() << MB.getBufferIdentifier()
147-
<< ": failed to open: " << Obj.takeError() << '\n';
148-
exit(1);
149-
}
145+
if (!Obj)
146+
return Obj.takeError();
150147

151148
uint16_t Machine = (*Obj)->getMachine();
152149
if (Machine != COFF::IMAGE_FILE_MACHINE_I386 &&
153150
Machine != COFF::IMAGE_FILE_MACHINE_AMD64 &&
154151
Machine != COFF::IMAGE_FILE_MACHINE_ARMNT &&
155152
Machine != COFF::IMAGE_FILE_MACHINE_ARM64) {
156-
llvm::errs() << MB.getBufferIdentifier() << ": unknown machine: " << Machine
157-
<< '\n';
158-
exit(1);
153+
return createStringError(inconvertibleErrorCode(),
154+
"unknown machine: " + std::to_string(Machine));
159155
}
160156

161157
return static_cast<COFF::MachineTypes>(Machine);
162158
}
163159

164-
static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) {
160+
static Expected<COFF::MachineTypes> getBitcodeFileMachine(MemoryBufferRef MB) {
165161
Expected<std::string> TripleStr = getBitcodeTargetTriple(MB);
166-
if (!TripleStr) {
167-
llvm::errs() << MB.getBufferIdentifier()
168-
<< ": failed to get target triple from bitcode\n";
169-
exit(1);
170-
}
162+
if (!TripleStr)
163+
return TripleStr.takeError();
171164

172165
switch (Triple(*TripleStr).getArch()) {
173166
case Triple::x86:
@@ -179,9 +172,8 @@ static COFF::MachineTypes getBitcodeFileMachine(MemoryBufferRef MB) {
179172
case Triple::aarch64:
180173
return COFF::IMAGE_FILE_MACHINE_ARM64;
181174
default:
182-
llvm::errs() << MB.getBufferIdentifier()
183-
<< ": unknown arch in target triple " << *TripleStr << '\n';
184-
exit(1);
175+
return createStringError(inconvertibleErrorCode(),
176+
"unknown arch in target triple: " + *TripleStr);
185177
}
186178
}
187179

@@ -201,7 +193,7 @@ static void appendFile(std::vector<NewArchiveMember> &Members,
201193

202194
// If a user attempts to add an archive to another archive, llvm-lib doesn't
203195
// handle the first archive file as a single file. Instead, it extracts all
204-
// members from the archive and add them to the second archive. This beahvior
196+
// members from the archive and add them to the second archive. This behavior
205197
// is for compatibility with Microsoft's lib command.
206198
if (Magic == file_magic::archive) {
207199
Error Err = Error::success();
@@ -233,9 +225,17 @@ static void appendFile(std::vector<NewArchiveMember> &Members,
233225
// in writeArchive() which needs to support many tools, can't assume the
234226
// input is COFF, and doesn't have a good way to report errors.
235227
if (Magic == file_magic::coff_object || Magic == file_magic::bitcode) {
236-
COFF::MachineTypes FileMachine = (Magic == file_magic::coff_object)
237-
? getCOFFFileMachine(MB)
238-
: getBitcodeFileMachine(MB);
228+
Expected<COFF::MachineTypes> MaybeFileMachine =
229+
(Magic == file_magic::coff_object) ? getCOFFFileMachine(MB)
230+
: getBitcodeFileMachine(MB);
231+
if (!MaybeFileMachine) {
232+
handleAllErrors(MaybeFileMachine.takeError(), [&](const ErrorInfoBase &EIB) {
233+
llvm::errs() << MB.getBufferIdentifier() << ": " << EIB.message()
234+
<< "\n";
235+
});
236+
exit(1);
237+
}
238+
COFF::MachineTypes FileMachine = *MaybeFileMachine;
239239

240240
// FIXME: Once lld-link rejects multiple resource .obj files:
241241
// Call convertResToCOFF() on .res files and add the resulting

0 commit comments

Comments
 (0)