Skip to content

Commit 2410fb4

Browse files
committed
Support: Use Expected<T>::moveInto() in a few places
These are some usage examples for `Expected<T>::moveInto()`. Differential Revision: https://reviews.llvm.org/D112280
1 parent c4ba110 commit 2410fb4

File tree

5 files changed

+24
-33
lines changed

5 files changed

+24
-33
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1657,16 +1657,17 @@ void clang::EmitBackendOutput(DiagnosticsEngine &Diags,
16571657
// If we are performing a ThinLTO importing compile, load the function index
16581658
// into memory and pass it into runThinLTOBackend, which will run the
16591659
// function importer and invoke LTO passes.
1660-
Expected<std::unique_ptr<ModuleSummaryIndex>> IndexOrErr =
1661-
llvm::getModuleSummaryIndexForFile(CGOpts.ThinLTOIndexFile,
1662-
/*IgnoreEmptyThinLTOIndexFile*/true);
1663-
if (!IndexOrErr) {
1664-
logAllUnhandledErrors(IndexOrErr.takeError(), errs(),
1660+
std::unique_ptr<ModuleSummaryIndex> CombinedIndex;
1661+
if (Error E = llvm::getModuleSummaryIndexForFile(
1662+
CGOpts.ThinLTOIndexFile,
1663+
/*IgnoreEmptyThinLTOIndexFile*/ true)
1664+
.moveInto(CombinedIndex)) {
1665+
logAllUnhandledErrors(std::move(E), errs(),
16651666
"Error loading index file '" +
16661667
CGOpts.ThinLTOIndexFile + "': ");
16671668
return;
16681669
}
1669-
std::unique_ptr<ModuleSummaryIndex> CombinedIndex = std::move(*IndexOrErr);
1670+
16701671
// A null CombinedIndex means we should skip ThinLTO compilation
16711672
// (LLVM will optionally ignore empty index files, returning null instead
16721673
// of an error).

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,10 +179,8 @@ static Expected<std::string> readIdentificationBlock(BitstreamCursor &Stream) {
179179

180180
while (true) {
181181
BitstreamEntry Entry;
182-
if (Expected<BitstreamEntry> Res = Stream.advance())
183-
Entry = Res.get();
184-
else
185-
return Res.takeError();
182+
if (Error E = Stream.advance().moveInto(Entry))
183+
return std::move(E);
186184

187185
switch (Entry.Kind) {
188186
default:
@@ -226,10 +224,8 @@ static Expected<std::string> readIdentificationCode(BitstreamCursor &Stream) {
226224
return "";
227225

228226
BitstreamEntry Entry;
229-
if (Expected<BitstreamEntry> Res = Stream.advance())
230-
Entry = std::move(Res.get());
231-
else
232-
return Res.takeError();
227+
if (Error E = Stream.advance().moveInto(Entry))
228+
return std::move(E);
233229

234230
switch (Entry.Kind) {
235231
case BitstreamEntry::EndBlock:
@@ -305,10 +301,8 @@ static Expected<bool> hasObjCCategory(BitstreamCursor &Stream) {
305301
// need to understand them all.
306302
while (true) {
307303
BitstreamEntry Entry;
308-
if (Expected<BitstreamEntry> Res = Stream.advance())
309-
Entry = std::move(Res.get());
310-
else
311-
return Res.takeError();
304+
if (Error E = Stream.advance().moveInto(Entry))
305+
return std::move(E);
312306

313307
switch (Entry.Kind) {
314308
case BitstreamEntry::Error:

llvm/lib/DebugInfo/Symbolize/Symbolize.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,7 @@ bool getGNUDebuglinkContents(const ObjectFile *Obj, std::string &DebugName,
280280
return false;
281281
for (const SectionRef &Section : Obj->sections()) {
282282
StringRef Name;
283-
if (Expected<StringRef> NameOrErr = Section.getName())
284-
Name = *NameOrErr;
285-
else
286-
consumeError(NameOrErr.takeError());
283+
consumeError(Section.getName().moveInto(Name));
287284

288285
Name = Name.substr(Name.find_first_not_of("._"));
289286
if (Name == "gnu_debuglink") {

llvm/lib/Object/ObjectFile.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,15 @@ bool SectionRef::containsSymbol(SymbolRef S) const {
5555
}
5656

5757
Expected<uint64_t> ObjectFile::getSymbolValue(DataRefImpl Ref) const {
58-
if (Expected<uint32_t> FlagsOrErr = getSymbolFlags(Ref)) {
59-
if (*FlagsOrErr & SymbolRef::SF_Undefined)
60-
return 0;
61-
if (*FlagsOrErr & SymbolRef::SF_Common)
62-
return getCommonSymbolSize(Ref);
63-
} else
58+
uint32_t Flags;
59+
if (Error E = getSymbolFlags(Ref).moveInto(Flags))
6460
// TODO: Test this error.
65-
return FlagsOrErr.takeError();
61+
return std::move(E);
62+
63+
if (Flags & SymbolRef::SF_Undefined)
64+
return 0;
65+
if (Flags & SymbolRef::SF_Common)
66+
return getCommonSymbolSize(Ref);
6667
return getSymbolValueImpl(Ref);
6768
}
6869

llvm/lib/XRay/InstrumentationMap.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,8 @@ loadObj(StringRef Filename, object::OwningBinary<object::ObjectFile> &ObjFile,
8686
"Failed to find XRay instrumentation map.",
8787
std::make_error_code(std::errc::executable_format_error));
8888

89-
if (Expected<StringRef> E = I->getContents())
90-
Contents = *E;
91-
else
92-
return E.takeError();
89+
if (Error E = I->getContents().moveInto(Contents))
90+
return E;
9391

9492
RelocMap Relocs;
9593
if (ObjFile.getBinary()->isELF()) {

0 commit comments

Comments
 (0)