Skip to content

Commit 67cfef2

Browse files
gmittertGwen Mittertreiner
authored and
Gwen Mittertreiner
committed
SectionRef::getContents() now returns Expected
Updated uses of object::SectionRef::getContents() since it now returns an Expected<StringRef> instead of modifying the one it's passed. See also: git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@360892 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent dff6b6e commit 67cfef2

File tree

5 files changed

+43
-23
lines changed

5 files changed

+43
-23
lines changed

lib/IRGen/IRGen.cpp

+6-4
Original file line numberDiff line numberDiff line change
@@ -385,11 +385,13 @@ static bool needsRecompile(StringRef OutputFilename, ArrayRef<uint8_t> HashData,
385385
StringRef SectionName;
386386
Section.getName(SectionName);
387387
if (SectionName == HashSectionName) {
388-
StringRef SectionData;
389-
Section.getContents(SectionData);
388+
llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
389+
if (!SectionData) {
390+
return true;
391+
}
390392
ArrayRef<uint8_t> PrevHashData(
391-
reinterpret_cast<const uint8_t *>(SectionData.data()),
392-
SectionData.size());
393+
reinterpret_cast<const uint8_t *>(SectionData->data()),
394+
SectionData->size());
393395
LLVM_DEBUG(if (PrevHashData.size() == sizeof(MD5::MD5Result)) {
394396
if (DiagMutex) DiagMutex->lock();
395397
SmallString<32> HashStr;

tools/driver/autolink_extract_main.cpp

+20-10
Original file line numberDiff line numberDiff line change
@@ -107,26 +107,38 @@ class AutolinkExtractInvocation {
107107

108108
/// Look inside the object file 'ObjectFile' and append any linker flags found in
109109
/// its ".swift1_autolink_entries" section to 'LinkerFlags'.
110-
static void
110+
/// Return 'true' if there was an error, and 'false' otherwise.
111+
static bool
111112
extractLinkerFlagsFromObjectFile(const llvm::object::ObjectFile *ObjectFile,
112-
std::vector<std::string> &LinkerFlags) {
113+
std::vector<std::string> &LinkerFlags,
114+
CompilerInstance &Instance) {
113115
// Search for the section we hold autolink entries in
114116
for (auto &Section : ObjectFile->sections()) {
115117
llvm::StringRef SectionName;
116118
Section.getName(SectionName);
117119
if (SectionName == ".swift1_autolink_entries") {
118-
llvm::StringRef SectionData;
119-
Section.getContents(SectionData);
120+
llvm::Expected<llvm::StringRef> SectionData = Section.getContents();
121+
if (!SectionData) {
122+
std::string message;
123+
{
124+
llvm::raw_string_ostream os(message);
125+
logAllUnhandledErrors(SectionData.takeError(), os, "");
126+
}
127+
Instance.getDiags().diagnose(SourceLoc(), diag::error_open_input_file,
128+
ObjectFile->getFileName() , message);
129+
return true;
130+
}
120131

121132
// entries are null-terminated, so extract them and push them into
122133
// the set.
123134
llvm::SmallVector<llvm::StringRef, 4> SplitFlags;
124-
SectionData.split(SplitFlags, llvm::StringRef("\0", 1), -1,
125-
/*KeepEmpty=*/false);
135+
SectionData->split(SplitFlags, llvm::StringRef("\0", 1), -1,
136+
/*KeepEmpty=*/false);
126137
for (const auto &Flag : SplitFlags)
127138
LinkerFlags.push_back(Flag);
128139
}
129140
}
141+
return false;
130142
}
131143

132144
/// Look inside the binary 'Bin' and append any linker flags found in its
@@ -138,12 +150,10 @@ static bool extractLinkerFlags(const llvm::object::Binary *Bin,
138150
StringRef BinaryFileName,
139151
std::vector<std::string> &LinkerFlags) {
140152
if (auto *ObjectFile = llvm::dyn_cast<llvm::object::ELFObjectFileBase>(Bin)) {
141-
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags);
142-
return false;
153+
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
143154
} else if (auto *ObjectFile =
144155
llvm::dyn_cast<llvm::object::COFFObjectFile>(Bin)) {
145-
extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags);
146-
return false;
156+
return extractLinkerFlagsFromObjectFile(ObjectFile, LinkerFlags, Instance);
147157
} else if (auto *Archive = llvm::dyn_cast<llvm::object::Archive>(Bin)) {
148158
llvm::Error Error = llvm::Error::success();
149159
for (const auto &Child : Archive->children(Error)) {

tools/lldb-moduleimport-test/lldb-moduleimport-test.cpp

+8-3
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,15 @@ collectASTModules(llvm::cl::list<std::string> &InputNames,
169169
(ELF && Name == swift::ELFASTSectionName) ||
170170
(COFF && Name == swift::COFFASTSectionName)) {
171171
uint64_t Size = Section.getSize();
172-
StringRef ContentsReference;
173-
Section.getContents(ContentsReference);
172+
173+
llvm::Expected<llvm::StringRef> ContentsReference = Section.getContents();
174+
if (!ContentsReference) {
175+
llvm::outs() << "error: " << name << " "
176+
<< errorToErrorCode(OF.takeError()).message() << "\n";
177+
return false;
178+
}
174179
char *Module = Alloc.Allocate<char>(Size);
175-
std::memcpy(Module, (void *)ContentsReference.begin(), Size);
180+
std::memcpy(Module, (void *)ContentsReference->begin(), Size);
176181
Modules.push_back({Module, Size});
177182
}
178183
}

tools/swift-reflection-dump/swift-reflection-dump.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "llvm/Object/ELFObjectFile.h"
2727
#include "llvm/Object/MachOUniversal.h"
2828
#include "llvm/Support/CommandLine.h"
29+
#include "llvm/Support/Error.h"
2930

3031
#if defined(_WIN32)
3132
#include <io.h>
@@ -137,11 +138,11 @@ class Image {
137138
for (SectionRef S : O->sections()) {
138139
if (!needToRelocate(S))
139140
continue;
140-
StringRef Content;
141-
if (auto EC = S.getContents(Content))
142-
reportError(EC);
143-
std::memcpy(&Memory[getSectionAddress(S)], Content.data(),
144-
Content.size());
141+
llvm::Expected<llvm::StringRef> Content = S.getContents();
142+
if (!Content)
143+
reportError(errorToErrorCode(Content.takeError()));
144+
std::memcpy(&Memory[getSectionAddress(S)], Content->data(),
145+
Content->size());
145146
}
146147
}
147148

unittests/Parse/LexerTests.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,9 @@ TEST_F(LexerTest, DiagnoseEmbeddedNulOffset) {
825825
// This test requires mmap because llvm::sys::Memory doesn't support protecting
826826
// pages to have no permissions.
827827
TEST_F(LexerTest, EncodedStringSegmentPastTheEnd) {
828-
size_t PageSize = llvm::sys::Process::getPageSize();
828+
Expected<size_t> ExptPageSize = llvm::sys::Process::getPageSize();
829+
ASSERT_TRUE(bool(ExptPageSize));
830+
size_t PageSize = *ExptPageSize;
829831

830832
void *FirstPage = mmap(/*addr*/nullptr, PageSize * 2, PROT_NONE,
831833
MAP_PRIVATE | MAP_ANON, /*fd*/-1, /*offset*/0);

0 commit comments

Comments
 (0)