[lld] Change Optional to std::optional

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716
This commit is contained in:
Fangrui Song 2022-11-27 17:25:33 -08:00
parent 66c3444f4c
commit c33511c8df
14 changed files with 44 additions and 41 deletions

View File

@ -906,7 +906,8 @@ ObjFile::getVariableLocation(StringRef var) {
}
if (config->machine == I386)
var.consume_front("_");
Optional<std::pair<std::string, unsigned>> ret = dwarf->getVariableLoc(var);
std::optional<std::pair<std::string, unsigned>> ret =
dwarf->getVariableLoc(var);
if (!ret)
return std::nullopt;
return std::make_pair(saver().save(ret->first), ret->second);
@ -914,8 +915,8 @@ ObjFile::getVariableLocation(StringRef var) {
// Used only for DWARF debug info, which is not common (except in MinGW
// environments).
Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
uint32_t sectionIndex) {
std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
uint32_t sectionIndex) {
if (!dwarf) {
dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
if (!dwarf)

View File

@ -209,8 +209,8 @@ public:
std::optional<std::pair<StringRef, uint32_t>>
getVariableLocation(StringRef var);
llvm::Optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
uint32_t sectionIndex);
std::optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
uint32_t sectionIndex);
private:
const coff_section* getSection(uint32_t i);

View File

@ -127,7 +127,7 @@ static std::vector<std::string> getSymbolLocations(BitcodeFile *file) {
static std::optional<std::pair<StringRef, uint32_t>>
getFileLineDwarf(const SectionChunk *c, uint32_t addr) {
Optional<DILineInfo> optionalLineInfo =
std::optional<DILineInfo> optionalLineInfo =
c->file->getDILineInfo(addr, c->getSectionNumber() - 1);
if (!optionalLineInfo)
return std::nullopt;

View File

@ -69,27 +69,27 @@ DWARFCache::DWARFCache(std::unique_ptr<llvm::DWARFContext> d)
// Returns the pair of file name and line number describing location of data
// object (variable, array, etc) definition.
Optional<std::pair<std::string, unsigned>>
std::optional<std::pair<std::string, unsigned>>
DWARFCache::getVariableLoc(StringRef name) {
// Return if we have no debug information about data object.
auto it = variableLoc.find(name);
if (it == variableLoc.end())
return None;
return std::nullopt;
// Take file name string from line table.
std::string fileName;
if (!it->second.lt->getFileNameByIndex(
it->second.file, {},
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName))
return None;
return std::nullopt;
return std::make_pair(fileName, it->second.line);
}
// Returns source line information for a given offset
// using DWARF debug info.
Optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
uint64_t sectionIndex) {
std::optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
uint64_t sectionIndex) {
DILineInfo info;
for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) {
if (lt->getFileLineInfoForAddress(
@ -97,7 +97,7 @@ Optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info))
return info;
}
return None;
return std::nullopt;
}
} // namespace lld

View File

@ -101,7 +101,7 @@ template <class ELFT> struct LLDRelocationResolver<Elf_Rel_Impl<ELFT, false>> {
// to llvm since it has no idea about InputSection.
template <class ELFT>
template <class RelTy>
Optional<RelocAddrEntry>
std::optional<RelocAddrEntry>
LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
ArrayRef<RelTy> rels) const {
auto it =
@ -132,8 +132,8 @@ LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
}
template <class ELFT>
Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s,
uint64_t pos) const {
std::optional<RelocAddrEntry>
LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {
auto &sec = static_cast<const LLDDWARFSection &>(s);
const RelsOrRelas<ELFT> rels = sec.sec->template relsOrRelas<ELFT>();
if (rels.areRelocsRel())

View File

@ -76,14 +76,14 @@ public:
return ELFT::TargetEndianness == llvm::support::little;
}
llvm::Optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec,
uint64_t pos) const override;
std::optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec,
uint64_t pos) const override;
private:
template <class RelTy>
llvm::Optional<llvm::RelocAddrEntry> findAux(const InputSectionBase &sec,
uint64_t pos,
ArrayRef<RelTy> rels) const;
std::optional<llvm::RelocAddrEntry> findAux(const InputSectionBase &sec,
uint64_t pos,
ArrayRef<RelTy> rels) const;
LLDDWARFSection gnuPubnamesSection;
LLDDWARFSection gnuPubtypesSection;

View File

@ -308,11 +308,11 @@ static std::string getSrcMsgAux(ObjFile<ELFT> &file, const Symbol &sym,
InputSectionBase &sec, uint64_t offset) {
// In DWARF, functions and variables are stored to different places.
// First, look up a function for a given offset.
if (Optional<DILineInfo> info = file.getDILineInfo(&sec, offset))
if (std::optional<DILineInfo> info = file.getDILineInfo(&sec, offset))
return createFileLineMsg(info->FileName, info->Line);
// If it failed, look up again as a variable.
if (Optional<std::pair<std::string, unsigned>> fileLine =
if (std::optional<std::pair<std::string, unsigned>> fileLine =
file.getVariableLoc(sym.getName()))
return createFileLineMsg(fileLine->first, fileLine->second);
@ -423,7 +423,7 @@ template <class ELFT> DWARFCache *ObjFile<ELFT>::getDwarf() {
// Returns the pair of file name and line number describing location of data
// object (variable, array, etc) definition.
template <class ELFT>
Optional<std::pair<std::string, unsigned>>
std::optional<std::pair<std::string, unsigned>>
ObjFile<ELFT>::getVariableLoc(StringRef name) {
return getDwarf()->getVariableLoc(name);
}
@ -431,8 +431,8 @@ ObjFile<ELFT>::getVariableLoc(StringRef name) {
// Returns source line information for a given offset
// using DWARF debug info.
template <class ELFT>
Optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
uint64_t offset) {
std::optional<DILineInfo> ObjFile<ELFT>::getDILineInfo(InputSectionBase *s,
uint64_t offset) {
// Detect SectionIndex for specified section.
uint64_t sectionIndex = object::SectionedAddress::UndefSection;
ArrayRef<InputSectionBase *> sections = s->file->getSections();

View File

@ -248,8 +248,9 @@ public:
return getSymbol(symIndex);
}
llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef name);
std::optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
std::optional<std::pair<std::string, unsigned>>
getVariableLoc(StringRef name);
// Name of source file obtained from STT_FILE symbol value,
// or empty string if there is no such symbol in object file

View File

@ -22,10 +22,10 @@ class DwarfObject final : public llvm::DWARFObject {
public:
bool isLittleEndian() const override { return true; }
llvm::Optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec,
uint64_t pos) const override {
std::optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec,
uint64_t pos) const override {
// TODO: implement this
return llvm::None;
return std::nullopt;
}
void forEachInfoSections(

View File

@ -111,7 +111,7 @@ std::string InputSection::getSourceLocation(uint64_t off) const {
};
// First, look up a function for a given offset.
if (Optional<DILineInfo> li = dwarf->getDILineInfo(
if (std::optional<DILineInfo> li = dwarf->getDILineInfo(
section.addr + off, object::SectionedAddress::UndefSection))
return createMsg(li->FileName, li->Line);
@ -123,7 +123,7 @@ std::string InputSection::getSourceLocation(uint64_t off) const {
if (!symName.empty() && symName[0] == '_')
symName = symName.substr(1);
if (Optional<std::pair<std::string, unsigned>> fileLine =
if (std::optional<std::pair<std::string, unsigned>> fileLine =
dwarf->getVariableLoc(symName))
return createMsg(fileLine->first, fileLine->second);
}

View File

@ -26,9 +26,9 @@ namespace lld {
class DWARFCache {
public:
DWARFCache(std::unique_ptr<llvm::DWARFContext> dwarf);
llvm::Optional<llvm::DILineInfo> getDILineInfo(uint64_t offset,
uint64_t sectionIndex);
llvm::Optional<std::pair<std::string, unsigned>>
std::optional<llvm::DILineInfo> getDILineInfo(uint64_t offset,
uint64_t sectionIndex);
std::optional<std::pair<std::string, unsigned>>
getVariableLoc(StringRef name);
llvm::DWARFContext *getContext() { return dwarf.get(); }

View File

@ -12,6 +12,7 @@
#include "llvm/DebugInfo/DWARF/DWARFRelocMap.h"
#include "llvm/DebugInfo/DWARF/DWARFSection.h"
#include "llvm/Object/ObjectFile.h"
#include <optional>
namespace llvm {
// This is responsible for low level access to the object file. It
@ -81,8 +82,8 @@ public:
virtual StringRef getCUIndexSection() const { return ""; }
virtual StringRef getGdbIndexSection() const { return ""; }
virtual StringRef getTUIndexSection() const { return ""; }
virtual Optional<RelocAddrEntry> find(const DWARFSection &Sec,
uint64_t Pos) const = 0;
virtual std::optional<RelocAddrEntry> find(const DWARFSection &Sec,
uint64_t Pos) const = 0;
};
} // namespace llvm

View File

@ -1877,12 +1877,12 @@ public:
S.IsNameUnique = false;
}
Optional<RelocAddrEntry> find(const DWARFSection &S,
uint64_t Pos) const override {
std::optional<RelocAddrEntry> find(const DWARFSection &S,
uint64_t Pos) const override {
auto &Sec = static_cast<const DWARFSectionMap &>(S);
RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos);
if (AI == Sec.Relocs.end())
return None;
return std::nullopt;
return AI->second;
}

View File

@ -54,7 +54,7 @@ uint64_t DWARFDataExtractor::getRelocatedValue(uint32_t Size, uint64_t *Off,
return getUnsigned(Off, Size, Err);
ErrorAsOutParameter ErrAsOut(Err);
Optional<RelocAddrEntry> E = Obj->find(*Section, *Off);
std::optional<RelocAddrEntry> E = Obj->find(*Section, *Off);
uint64_t LocData = getUnsigned(Off, Size, Err);
if (!E || (Err && *Err))
return LocData;