[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) if (config->machine == I386)
var.consume_front("_"); 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) if (!ret)
return std::nullopt; return std::nullopt;
return std::make_pair(saver().save(ret->first), ret->second); 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 // Used only for DWARF debug info, which is not common (except in MinGW
// environments). // environments).
Optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset, std::optional<DILineInfo> ObjFile::getDILineInfo(uint32_t offset,
uint32_t sectionIndex) { uint32_t sectionIndex) {
if (!dwarf) { if (!dwarf) {
dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj())); dwarf = make<DWARFCache>(DWARFContext::create(*getCOFFObj()));
if (!dwarf) if (!dwarf)

View File

@ -209,8 +209,8 @@ public:
std::optional<std::pair<StringRef, uint32_t>> std::optional<std::pair<StringRef, uint32_t>>
getVariableLocation(StringRef var); getVariableLocation(StringRef var);
llvm::Optional<llvm::DILineInfo> getDILineInfo(uint32_t offset, std::optional<llvm::DILineInfo> getDILineInfo(uint32_t offset,
uint32_t sectionIndex); uint32_t sectionIndex);
private: private:
const coff_section* getSection(uint32_t i); 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>> static std::optional<std::pair<StringRef, uint32_t>>
getFileLineDwarf(const SectionChunk *c, uint32_t addr) { getFileLineDwarf(const SectionChunk *c, uint32_t addr) {
Optional<DILineInfo> optionalLineInfo = std::optional<DILineInfo> optionalLineInfo =
c->file->getDILineInfo(addr, c->getSectionNumber() - 1); c->file->getDILineInfo(addr, c->getSectionNumber() - 1);
if (!optionalLineInfo) if (!optionalLineInfo)
return std::nullopt; 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 // Returns the pair of file name and line number describing location of data
// object (variable, array, etc) definition. // object (variable, array, etc) definition.
Optional<std::pair<std::string, unsigned>> std::optional<std::pair<std::string, unsigned>>
DWARFCache::getVariableLoc(StringRef name) { DWARFCache::getVariableLoc(StringRef name) {
// Return if we have no debug information about data object. // Return if we have no debug information about data object.
auto it = variableLoc.find(name); auto it = variableLoc.find(name);
if (it == variableLoc.end()) if (it == variableLoc.end())
return None; return std::nullopt;
// Take file name string from line table. // Take file name string from line table.
std::string fileName; std::string fileName;
if (!it->second.lt->getFileNameByIndex( if (!it->second.lt->getFileNameByIndex(
it->second.file, {}, it->second.file, {},
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName)) DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, fileName))
return None; return std::nullopt;
return std::make_pair(fileName, it->second.line); return std::make_pair(fileName, it->second.line);
} }
// Returns source line information for a given offset // Returns source line information for a given offset
// using DWARF debug info. // using DWARF debug info.
Optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset, std::optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
uint64_t sectionIndex) { uint64_t sectionIndex) {
DILineInfo info; DILineInfo info;
for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) { for (const llvm::DWARFDebugLine::LineTable *lt : lineTables) {
if (lt->getFileLineInfoForAddress( if (lt->getFileLineInfoForAddress(
@ -97,7 +97,7 @@ Optional<DILineInfo> DWARFCache::getDILineInfo(uint64_t offset,
DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info)) DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info))
return info; return info;
} }
return None; return std::nullopt;
} }
} // namespace lld } // 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. // to llvm since it has no idea about InputSection.
template <class ELFT> template <class ELFT>
template <class RelTy> template <class RelTy>
Optional<RelocAddrEntry> std::optional<RelocAddrEntry>
LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos, LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
ArrayRef<RelTy> rels) const { ArrayRef<RelTy> rels) const {
auto it = auto it =
@ -132,8 +132,8 @@ LLDDwarfObj<ELFT>::findAux(const InputSectionBase &sec, uint64_t pos,
} }
template <class ELFT> template <class ELFT>
Optional<RelocAddrEntry> LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, std::optional<RelocAddrEntry>
uint64_t pos) const { LLDDwarfObj<ELFT>::find(const llvm::DWARFSection &s, uint64_t pos) const {
auto &sec = static_cast<const LLDDWARFSection &>(s); auto &sec = static_cast<const LLDDWARFSection &>(s);
const RelsOrRelas<ELFT> rels = sec.sec->template relsOrRelas<ELFT>(); const RelsOrRelas<ELFT> rels = sec.sec->template relsOrRelas<ELFT>();
if (rels.areRelocsRel()) if (rels.areRelocsRel())

View File

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

View File

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

View File

@ -248,8 +248,9 @@ public:
return getSymbol(symIndex); return getSymbol(symIndex);
} }
llvm::Optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t); std::optional<llvm::DILineInfo> getDILineInfo(InputSectionBase *, uint64_t);
llvm::Optional<std::pair<std::string, unsigned>> getVariableLoc(StringRef name); std::optional<std::pair<std::string, unsigned>>
getVariableLoc(StringRef name);
// Name of source file obtained from STT_FILE symbol value, // Name of source file obtained from STT_FILE symbol value,
// or empty string if there is no such symbol in object file // 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: public:
bool isLittleEndian() const override { return true; } bool isLittleEndian() const override { return true; }
llvm::Optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec, std::optional<llvm::RelocAddrEntry> find(const llvm::DWARFSection &sec,
uint64_t pos) const override { uint64_t pos) const override {
// TODO: implement this // TODO: implement this
return llvm::None; return std::nullopt;
} }
void forEachInfoSections( 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. // 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)) section.addr + off, object::SectionedAddress::UndefSection))
return createMsg(li->FileName, li->Line); return createMsg(li->FileName, li->Line);
@ -123,7 +123,7 @@ std::string InputSection::getSourceLocation(uint64_t off) const {
if (!symName.empty() && symName[0] == '_') if (!symName.empty() && symName[0] == '_')
symName = symName.substr(1); symName = symName.substr(1);
if (Optional<std::pair<std::string, unsigned>> fileLine = if (std::optional<std::pair<std::string, unsigned>> fileLine =
dwarf->getVariableLoc(symName)) dwarf->getVariableLoc(symName))
return createMsg(fileLine->first, fileLine->second); return createMsg(fileLine->first, fileLine->second);
} }

View File

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

View File

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

View File

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

View File

@ -54,7 +54,7 @@ uint64_t DWARFDataExtractor::getRelocatedValue(uint32_t Size, uint64_t *Off,
return getUnsigned(Off, Size, Err); return getUnsigned(Off, Size, Err);
ErrorAsOutParameter ErrAsOut(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); uint64_t LocData = getUnsigned(Off, Size, Err);
if (!E || (Err && *Err)) if (!E || (Err && *Err))
return LocData; return LocData;