Skip to content
This repository has been archived by the owner on Apr 23, 2020. It is now read-only.

Commit

Permalink
Convert the archive writer to use Error.
Browse files Browse the repository at this point in the history
This found one place in lld that was not checking the error.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313937 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
espindola committed Sep 21, 2017
1 parent b9223fa commit 203c90b
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 37 deletions.
9 changes: 5 additions & 4 deletions include/llvm/Object/ArchiveWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "llvm/ADT/StringRef.h"
#include "llvm/Object/Archive.h"
#include "llvm/Support/Error.h"
#include "llvm/Support/FileSystem.h"

namespace llvm {
Expand All @@ -37,10 +38,10 @@ struct NewArchiveMember {
bool Deterministic);
};

std::error_code
writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
bool WriteSymtab, object::Archive::Kind Kind, bool Deterministic,
bool Thin, std::unique_ptr<MemoryBuffer> OldArchiveBuf = nullptr);
Error writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
bool WriteSymtab, object::Archive::Kind Kind,
bool Deterministic, bool Thin,
std::unique_ptr<MemoryBuffer> OldArchiveBuf = nullptr);
}

#endif
8 changes: 3 additions & 5 deletions include/llvm/Object/COFFImportFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ struct COFFShortExport {
}
};

std::error_code writeImportLibrary(StringRef ImportName,
StringRef Path,
ArrayRef<COFFShortExport> Exports,
COFF::MachineTypes Machine,
bool MakeWeakAliases);
Error writeImportLibrary(StringRef ImportName, StringRef Path,
ArrayRef<COFFShortExport> Exports,
COFF::MachineTypes Machine, bool MakeWeakAliases);

} // namespace object
} // namespace llvm
Expand Down
26 changes: 13 additions & 13 deletions lib/Object/ArchiveWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ static sys::TimePoint<std::chrono::seconds> now(bool Deterministic) {
}

// Returns the offset of the first reference to a member offset.
static ErrorOr<unsigned>
static Expected<unsigned>
writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind,
ArrayRef<NewArchiveMember> Members,
std::vector<unsigned> &MemberOffsetRefs, bool Deterministic) {
Expand Down Expand Up @@ -320,8 +320,8 @@ writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind,
continue;

unsigned NameOffset = NameOS.tell();
if (auto EC = S.printName(NameOS))
return EC;
if (std::error_code EC = S.printName(NameOS))
return errorCodeToError(EC);
NameOS << '\0';
MemberOffsetRefs.push_back(MemberNum);
if (isBSDLike(Kind))
Expand Down Expand Up @@ -373,17 +373,17 @@ writeSymbolTable(raw_fd_ostream &Out, object::Archive::Kind Kind,
return BodyStartOffset + 4;
}

std::error_code
llvm::writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
bool WriteSymtab, object::Archive::Kind Kind,
bool Deterministic, bool Thin,
std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
Error llvm::writeArchive(StringRef ArcName,
ArrayRef<NewArchiveMember> NewMembers,
bool WriteSymtab, object::Archive::Kind Kind,
bool Deterministic, bool Thin,
std::unique_ptr<MemoryBuffer> OldArchiveBuf) {
assert((!Thin || !isBSDLike(Kind)) && "Only the gnu format has a thin mode");
SmallString<128> TmpArchive;
int TmpArchiveFD;
if (auto EC = sys::fs::createUniqueFile(ArcName + ".temp-archive-%%%%%%%.a",
TmpArchiveFD, TmpArchive))
return EC;
return errorCodeToError(EC);

tool_output_file Output(TmpArchive, TmpArchiveFD);
raw_fd_ostream &Out = Output.os();
Expand All @@ -396,10 +396,10 @@ llvm::writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,

unsigned MemberReferenceOffset = 0;
if (WriteSymtab) {
ErrorOr<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable(
Expected<unsigned> MemberReferenceOffsetOrErr = writeSymbolTable(
Out, Kind, NewMembers, MemberOffsetRefs, Deterministic);
if (auto EC = MemberReferenceOffsetOrErr.getError())
return EC;
if (auto E = MemberReferenceOffsetOrErr.takeError())
return E;
MemberReferenceOffset = MemberReferenceOffsetOrErr.get();
}

Expand Down Expand Up @@ -461,5 +461,5 @@ llvm::writeArchive(StringRef ArcName, ArrayRef<NewArchiveMember> NewMembers,
OldArchiveBuf.reset();

sys::fs::rename(TmpArchive, ArcName);
return std::error_code();
return Error::success();
}
11 changes: 5 additions & 6 deletions lib/Object/COFFImportFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -558,9 +558,9 @@ NewArchiveMember ObjectFactory::createWeakExternal(StringRef Sym,
return {MemoryBufferRef(StringRef(Buf, Buffer.size()), ImportName)};
}

std::error_code writeImportLibrary(StringRef ImportName, StringRef Path,
ArrayRef<COFFShortExport> Exports,
MachineTypes Machine, bool MakeWeakAliases) {
Error writeImportLibrary(StringRef ImportName, StringRef Path,
ArrayRef<COFFShortExport> Exports,
MachineTypes Machine, bool MakeWeakAliases) {

std::vector<NewArchiveMember> Members;
ObjectFactory OF(llvm::sys::path::filename(ImportName), Machine);
Expand Down Expand Up @@ -596,9 +596,8 @@ std::error_code writeImportLibrary(StringRef ImportName, StringRef Path,
? SymbolName
: replace(SymbolName, E.Name, E.ExtName);

if (!Name) {
return errorToErrorCode(Name.takeError());
}
if (!Name)
return Name.takeError();

Members.push_back(
OF.createShortImport(*Name, E.Ordinal, ImportType, NameType));
Expand Down
14 changes: 7 additions & 7 deletions lib/ToolDrivers/llvm-lib/LibDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,13 @@ int llvm::libDriverMain(ArrayRef<const char *> ArgsArr) {

// Create an archive file.
std::string OutputPath = getOutputPath(&Args, Members[0]);
std::error_code EC =
writeArchive(OutputPath, Members,
/*WriteSymtab=*/true, object::Archive::K_GNU,
/*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin));

if (EC) {
llvm::errs() << OutputPath << ": " << EC.message() << "\n";
if (Error E =
writeArchive(OutputPath, Members,
/*WriteSymtab=*/true, object::Archive::K_GNU,
/*Deterministic*/ true, Args.hasArg(OPT_llvmlibthin))) {
handleAllErrors(std::move(E), [&](const ErrorInfoBase &EI) {
llvm::errs() << OutputPath << ": " << EI.message() << "\n";
});
return 1;
}

Expand Down
4 changes: 2 additions & 2 deletions tools/llvm-ar/llvm-ar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -681,10 +681,10 @@ performWriteOperation(ArchiveOperation Operation,
break;
}

std::error_code EC =
Error E =
writeArchive(ArchiveName, NewMembersP ? *NewMembersP : NewMembers, Symtab,
Kind, Deterministic, Thin, std::move(OldArchiveBuf));
failIfError(EC, ArchiveName);
failIfError(std::move(E), ArchiveName);
}

static void createSymbolTable(object::Archive *OldArchive) {
Expand Down

0 comments on commit 203c90b

Please sign in to comment.