Skip to content

Commit b9ef564

Browse files
[lld] Use std::optional instead of llvm::Optional (NFC)
This is part of an effort to migrate from llvm::Optional to std::optional: https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
1 parent ae33124 commit b9ef564

13 files changed

+90
-78
lines changed

lld/MinGW/Driver.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "lld/Common/Memory.h"
3535
#include "lld/Common/Version.h"
3636
#include "llvm/ADT/ArrayRef.h"
37-
#include "llvm/ADT/Optional.h"
3837
#include "llvm/ADT/StringExtras.h"
3938
#include "llvm/ADT/StringRef.h"
4039
#include "llvm/ADT/Triple.h"
@@ -45,6 +44,7 @@
4544
#include "llvm/Support/FileSystem.h"
4645
#include "llvm/Support/Host.h"
4746
#include "llvm/Support/Path.h"
47+
#include <optional>
4848

4949
#if !defined(_MSC_VER) && !defined(__MINGW32__)
5050
#include <unistd.h>
@@ -115,7 +115,8 @@ opt::InputArgList MinGWOptTable::parse(ArrayRef<const char *> argv) {
115115
}
116116

117117
// Find a file by concatenating given paths.
118-
static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
118+
static std::optional<std::string> findFile(StringRef path1,
119+
const Twine &path2) {
119120
SmallString<128> s;
120121
sys::path::append(s, path1, path2);
121122
if (sys::fs::exists(s))
@@ -128,27 +129,27 @@ static std::string
128129
searchLibrary(StringRef name, ArrayRef<StringRef> searchPaths, bool bStatic) {
129130
if (name.startswith(":")) {
130131
for (StringRef dir : searchPaths)
131-
if (Optional<std::string> s = findFile(dir, name.substr(1)))
132+
if (std::optional<std::string> s = findFile(dir, name.substr(1)))
132133
return *s;
133134
error("unable to find library -l" + name);
134135
return "";
135136
}
136137

137138
for (StringRef dir : searchPaths) {
138139
if (!bStatic) {
139-
if (Optional<std::string> s = findFile(dir, "lib" + name + ".dll.a"))
140+
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".dll.a"))
140141
return *s;
141-
if (Optional<std::string> s = findFile(dir, name + ".dll.a"))
142+
if (std::optional<std::string> s = findFile(dir, name + ".dll.a"))
142143
return *s;
143144
}
144-
if (Optional<std::string> s = findFile(dir, "lib" + name + ".a"))
145+
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
146+
return *s;
147+
if (std::optional<std::string> s = findFile(dir, name + ".lib"))
145148
return *s;
146-
if (Optional<std::string> s = findFile(dir, name + ".lib"))
147-
return *s;
148149
if (!bStatic) {
149-
if (Optional<std::string> s = findFile(dir, "lib" + name + ".dll"))
150+
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".dll"))
150151
return *s;
151-
if (Optional<std::string> s = findFile(dir, name + ".dll"))
152+
if (std::optional<std::string> s = findFile(dir, name + ".dll"))
152153
return *s;
153154
}
154155
}

lld/wasm/Config.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "llvm/ADT/StringSet.h"
1515
#include "llvm/BinaryFormat/Wasm.h"
1616
#include "llvm/Support/CachePruning.h"
17+
#include <optional>
1718

1819
namespace lld {
1920
namespace wasm {
@@ -39,12 +40,12 @@ struct Configuration {
3940
bool extendedConst;
4041
bool growableTable;
4142
bool gcSections;
42-
llvm::Optional<std::pair<llvm::StringRef, llvm::StringRef>> memoryImport;
43-
llvm::Optional<llvm::StringRef> memoryExport;
43+
std::optional<std::pair<llvm::StringRef, llvm::StringRef>> memoryImport;
44+
std::optional<llvm::StringRef> memoryExport;
4445
bool sharedMemory;
4546
bool importTable;
4647
bool importUndefined;
47-
llvm::Optional<bool> is64;
48+
std::optional<bool> is64;
4849
bool mergeDataSegments;
4950
bool pie;
5051
bool printGcSections;
@@ -77,8 +78,8 @@ struct Configuration {
7778
std::vector<llvm::StringRef> requiredExports;
7879
llvm::SmallVector<llvm::StringRef, 0> searchPaths;
7980
llvm::CachePruningPolicy thinLTOCachePolicy;
80-
llvm::Optional<std::vector<std::string>> features;
81-
llvm::Optional<std::vector<std::string>> extraFeatures;
81+
std::optional<std::vector<std::string>> features;
82+
std::optional<std::vector<std::string>> extraFeatures;
8283

8384
// The following config options do not directly correspond to any
8485
// particular command line options.

lld/wasm/Driver.cpp

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "llvm/Support/Process.h"
3434
#include "llvm/Support/TarWriter.h"
3535
#include "llvm/Support/TargetSelect.h"
36+
#include <optional>
3637

3738
#define DEBUG_TYPE "lld"
3839

@@ -163,7 +164,8 @@ static cl::TokenizerCallback getQuotingStyle(opt::InputArgList &args) {
163164
}
164165

165166
// Find a file by concatenating given paths.
166-
static Optional<std::string> findFile(StringRef path1, const Twine &path2) {
167+
static std::optional<std::string> findFile(StringRef path1,
168+
const Twine &path2) {
167169
SmallString<128> s;
168170
path::append(s, path1, path2);
169171
if (fs::exists(s))
@@ -204,7 +206,7 @@ opt::InputArgList WasmOptTable::parse(ArrayRef<const char *> argv) {
204206
// attribute/flag in the object file itself.
205207
// See: https://github.com/WebAssembly/tool-conventions/issues/35
206208
static void readImportFile(StringRef filename) {
207-
if (Optional<MemoryBufferRef> buf = readFile(filename))
209+
if (std::optional<MemoryBufferRef> buf = readFile(filename))
208210
for (StringRef sym : args::getLines(*buf))
209211
config->allowUndefinedSymbols.insert(sym);
210212
}
@@ -237,7 +239,7 @@ std::vector<MemoryBufferRef> static getArchiveMembers(MemoryBufferRef mb) {
237239
}
238240

239241
void LinkerDriver::addFile(StringRef path) {
240-
Optional<MemoryBufferRef> buffer = readFile(path);
242+
std::optional<MemoryBufferRef> buffer = readFile(path);
241243
if (!buffer)
242244
return;
243245
MemoryBufferRef mbref = *buffer;
@@ -283,38 +285,38 @@ void LinkerDriver::addFile(StringRef path) {
283285
}
284286
}
285287

286-
static Optional<std::string> findFromSearchPaths(StringRef path) {
288+
static std::optional<std::string> findFromSearchPaths(StringRef path) {
287289
for (StringRef dir : config->searchPaths)
288-
if (Optional<std::string> s = findFile(dir, path))
290+
if (std::optional<std::string> s = findFile(dir, path))
289291
return s;
290292
return std::nullopt;
291293
}
292294

293295
// This is for -l<basename>. We'll look for lib<basename>.a from
294296
// search paths.
295-
static Optional<std::string> searchLibraryBaseName(StringRef name) {
297+
static std::optional<std::string> searchLibraryBaseName(StringRef name) {
296298
for (StringRef dir : config->searchPaths) {
297299
// Currently we don't enable dyanmic linking at all unless -shared or -pie
298300
// are used, so don't even look for .so files in that case..
299301
if (config->isPic && !config->isStatic)
300-
if (Optional<std::string> s = findFile(dir, "lib" + name + ".so"))
302+
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".so"))
301303
return s;
302-
if (Optional<std::string> s = findFile(dir, "lib" + name + ".a"))
304+
if (std::optional<std::string> s = findFile(dir, "lib" + name + ".a"))
303305
return s;
304306
}
305307
return std::nullopt;
306308
}
307309

308310
// This is for -l<namespec>.
309-
static Optional<std::string> searchLibrary(StringRef name) {
311+
static std::optional<std::string> searchLibrary(StringRef name) {
310312
if (name.startswith(":"))
311313
return findFromSearchPaths(name.substr(1));
312314
return searchLibraryBaseName(name);
313315
}
314316

315317
// Add a given library by searching it from input search paths.
316318
void LinkerDriver::addLibrary(StringRef name) {
317-
if (Optional<std::string> path = searchLibrary(name))
319+
if (std::optional<std::string> path = searchLibrary(name))
318320
addFile(saver().save(*path));
319321
else
320322
error("unable to find library -l" + name, ErrorTag::LibNotFound, {name});
@@ -406,7 +408,7 @@ static void readConfigs(opt::InputArgList &args) {
406408
std::pair<llvm::StringRef, llvm::StringRef>(defaultModule, memoryName);
407409
} else {
408410
config->memoryImport =
409-
llvm::Optional<std::pair<llvm::StringRef, llvm::StringRef>>();
411+
std::optional<std::pair<llvm::StringRef, llvm::StringRef>>();
410412
}
411413

412414
if (args.hasArg(OPT_export_memory_with_name)) {
@@ -415,7 +417,7 @@ static void readConfigs(opt::InputArgList &args) {
415417
} else if (args.hasArg(OPT_export_memory)) {
416418
config->memoryExport = memoryName;
417419
} else {
418-
config->memoryExport = llvm::Optional<llvm::StringRef>();
420+
config->memoryExport = std::optional<llvm::StringRef>();
419421
}
420422

421423
config->sharedMemory = args.hasArg(OPT_shared_memory);
@@ -488,14 +490,14 @@ static void readConfigs(opt::InputArgList &args) {
488490

489491
if (auto *arg = args.getLastArg(OPT_features)) {
490492
config->features =
491-
llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
493+
std::optional<std::vector<std::string>>(std::vector<std::string>());
492494
for (StringRef s : arg->getValues())
493495
config->features->push_back(std::string(s));
494496
}
495497

496498
if (auto *arg = args.getLastArg(OPT_extra_features)) {
497499
config->extraFeatures =
498-
llvm::Optional<std::vector<std::string>>(std::vector<std::string>());
500+
std::optional<std::vector<std::string>>(std::vector<std::string>());
499501
for (StringRef s : arg->getValues())
500502
config->extraFeatures->push_back(std::string(s));
501503
}

lld/wasm/InputChunks.h

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/ADT/CachedHashString.h"
2828
#include "llvm/MC/StringTableBuilder.h"
2929
#include "llvm/Object/Wasm.h"
30+
#include <optional>
3031

3132
namespace lld {
3233
namespace wasm {
@@ -252,7 +253,7 @@ class InputFunction : public InputChunk {
252253
: InputChunk(f, InputChunk::Function, func->SymbolName), signature(s),
253254
function(func),
254255
exportName(func && func->ExportName ? (*func->ExportName).str()
255-
: llvm::Optional<std::string>()) {
256+
: std::optional<std::string>()) {
256257
inputSectionOffset = function->CodeSectionOffset;
257258
rawData =
258259
file->codeSection->Content.slice(inputSectionOffset, function->Size);
@@ -268,9 +269,9 @@ class InputFunction : public InputChunk {
268269
c->kind() == InputChunk::SyntheticFunction;
269270
}
270271

271-
llvm::Optional<StringRef> getExportName() const {
272-
return exportName ? llvm::Optional<StringRef>(*exportName)
273-
: llvm::Optional<StringRef>();
272+
std::optional<StringRef> getExportName() const {
273+
return exportName ? std::optional<StringRef>(*exportName)
274+
: std::optional<StringRef>();
274275
}
275276
void setExportName(std::string exportName) { this->exportName = exportName; }
276277
uint32_t getFunctionInputOffset() const { return getInputSectionOffset(); }
@@ -299,9 +300,9 @@ class InputFunction : public InputChunk {
299300
const WasmFunction *function;
300301

301302
protected:
302-
llvm::Optional<std::string> exportName;
303-
llvm::Optional<uint32_t> functionIndex;
304-
llvm::Optional<uint32_t> tableIndex;
303+
std::optional<std::string> exportName;
304+
std::optional<uint32_t> functionIndex;
305+
std::optional<uint32_t> tableIndex;
305306
uint32_t compressedFuncSize = 0;
306307
uint32_t compressedSize = 0;
307308
};

lld/wasm/InputElement.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "WriterUtils.h"
1515
#include "lld/Common/LLVM.h"
1616
#include "llvm/Object/Wasm.h"
17+
#include <optional>
1718

1819
namespace lld {
1920
namespace wasm {
@@ -39,7 +40,7 @@ class InputElement {
3940

4041
protected:
4142
StringRef name;
42-
llvm::Optional<uint32_t> assignedIndex;
43+
std::optional<uint32_t> assignedIndex;
4344
};
4445

4546
inline WasmInitExpr intConst(uint64_t value, bool is64) {

lld/wasm/InputFiles.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include "llvm/Support/Path.h"
2020
#include "llvm/Support/TarWriter.h"
2121
#include "llvm/Support/raw_ostream.h"
22+
#include <optional>
2223

2324
#define DEBUG_TYPE "lld"
2425

@@ -55,7 +56,7 @@ void InputFile::checkArch(Triple::ArchType arch) const {
5556

5657
std::unique_ptr<llvm::TarWriter> tar;
5758

58-
Optional<MemoryBufferRef> readFile(StringRef path) {
59+
std::optional<MemoryBufferRef> readFile(StringRef path) {
5960
log("Loading: " + path);
6061

6162
auto mbOrErr = MemoryBuffer::getFile(path);

lld/wasm/InputFiles.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "llvm/Object/Archive.h"
1919
#include "llvm/Object/Wasm.h"
2020
#include "llvm/Support/MemoryBuffer.h"
21+
#include <optional>
2122
#include <vector>
2223

2324
namespace llvm {
@@ -192,7 +193,7 @@ InputFile *createObjectFile(MemoryBufferRef mb, StringRef archiveName = "",
192193
uint64_t offsetInArchive = 0);
193194

194195
// Opens a given file.
195-
llvm::Optional<MemoryBufferRef> readFile(StringRef path);
196+
std::optional<MemoryBufferRef> readFile(StringRef path);
196197

197198
} // namespace wasm
198199

lld/wasm/SymbolTable.cpp

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "InputElement.h"
1313
#include "WriterUtils.h"
1414
#include "lld/Common/CommonLinkerContext.h"
15+
#include <optional>
1516

1617
#define DEBUG_TYPE "lld"
1718

@@ -461,8 +462,9 @@ Symbol *SymbolTable::addDefinedTable(StringRef name, uint32_t flags,
461462
// become available when the LTO object is read. In this case we silently
462463
// replace the empty attributes with the valid ones.
463464
template <typename T>
464-
static void setImportAttributes(T *existing, Optional<StringRef> importName,
465-
Optional<StringRef> importModule,
465+
static void setImportAttributes(T *existing,
466+
std::optional<StringRef> importName,
467+
std::optional<StringRef> importModule,
466468
uint32_t flags, InputFile *file) {
467469
if (importName) {
468470
if (!existing->importName)
@@ -492,8 +494,8 @@ static void setImportAttributes(T *existing, Optional<StringRef> importName,
492494
}
493495

494496
Symbol *SymbolTable::addUndefinedFunction(StringRef name,
495-
Optional<StringRef> importName,
496-
Optional<StringRef> importModule,
497+
std::optional<StringRef> importName,
498+
std::optional<StringRef> importModule,
497499
uint32_t flags, InputFile *file,
498500
const WasmSignature *sig,
499501
bool isCalledDirectly) {
@@ -577,8 +579,8 @@ Symbol *SymbolTable::addUndefinedData(StringRef name, uint32_t flags,
577579
}
578580

579581
Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
580-
Optional<StringRef> importName,
581-
Optional<StringRef> importModule,
582+
std::optional<StringRef> importName,
583+
std::optional<StringRef> importModule,
582584
uint32_t flags, InputFile *file,
583585
const WasmGlobalType *type) {
584586
LLVM_DEBUG(dbgs() << "addUndefinedGlobal: " << name << "\n");
@@ -601,8 +603,8 @@ Symbol *SymbolTable::addUndefinedGlobal(StringRef name,
601603
}
602604

603605
Symbol *SymbolTable::addUndefinedTable(StringRef name,
604-
Optional<StringRef> importName,
605-
Optional<StringRef> importModule,
606+
std::optional<StringRef> importName,
607+
std::optional<StringRef> importModule,
606608
uint32_t flags, InputFile *file,
607609
const WasmTableType *type) {
608610
LLVM_DEBUG(dbgs() << "addUndefinedTable: " << name << "\n");
@@ -625,8 +627,8 @@ Symbol *SymbolTable::addUndefinedTable(StringRef name,
625627
}
626628

627629
Symbol *SymbolTable::addUndefinedTag(StringRef name,
628-
Optional<StringRef> importName,
629-
Optional<StringRef> importModule,
630+
std::optional<StringRef> importName,
631+
std::optional<StringRef> importModule,
630632
uint32_t flags, InputFile *file,
631633
const WasmSignature *sig) {
632634
LLVM_DEBUG(dbgs() << "addUndefinedTag: " << name << "\n");

0 commit comments

Comments
 (0)