From c33511c8df54265f5c540c0d8605f2f6f35ee6cd Mon Sep 17 00:00:00 2001 From: Fangrui Song Date: Sun, 27 Nov 2022 17:25:33 -0800 Subject: [PATCH] [lld] Change Optional to std::optional https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716 --- lld/COFF/InputFiles.cpp | 7 ++++--- lld/COFF/InputFiles.h | 4 ++-- lld/COFF/SymbolTable.cpp | 2 +- lld/Common/DWARF.cpp | 12 ++++++------ lld/ELF/DWARF.cpp | 6 +++--- lld/ELF/DWARF.h | 10 +++++----- lld/ELF/InputFiles.cpp | 10 +++++----- lld/ELF/InputFiles.h | 5 +++-- lld/MachO/Dwarf.h | 6 +++--- lld/MachO/InputSection.cpp | 4 ++-- lld/include/lld/Common/DWARF.h | 6 +++--- llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h | 5 +++-- llvm/lib/DebugInfo/DWARF/DWARFContext.cpp | 6 +++--- llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp | 2 +- 14 files changed, 44 insertions(+), 41 deletions(-) diff --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp index ce0f6b4..66a6b85 100644 --- a/lld/COFF/InputFiles.cpp +++ b/lld/COFF/InputFiles.cpp @@ -906,7 +906,8 @@ ObjFile::getVariableLocation(StringRef var) { } if (config->machine == I386) var.consume_front("_"); - Optional> ret = dwarf->getVariableLoc(var); + std::optional> 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 ObjFile::getDILineInfo(uint32_t offset, - uint32_t sectionIndex) { +std::optional ObjFile::getDILineInfo(uint32_t offset, + uint32_t sectionIndex) { if (!dwarf) { dwarf = make(DWARFContext::create(*getCOFFObj())); if (!dwarf) diff --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h index 6af44b0..e7e3fdc 100644 --- a/lld/COFF/InputFiles.h +++ b/lld/COFF/InputFiles.h @@ -209,8 +209,8 @@ public: std::optional> getVariableLocation(StringRef var); - llvm::Optional getDILineInfo(uint32_t offset, - uint32_t sectionIndex); + std::optional getDILineInfo(uint32_t offset, + uint32_t sectionIndex); private: const coff_section* getSection(uint32_t i); diff --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp index dd60b51..f3bee72 100644 --- a/lld/COFF/SymbolTable.cpp +++ b/lld/COFF/SymbolTable.cpp @@ -127,7 +127,7 @@ static std::vector getSymbolLocations(BitcodeFile *file) { static std::optional> getFileLineDwarf(const SectionChunk *c, uint32_t addr) { - Optional optionalLineInfo = + std::optional optionalLineInfo = c->file->getDILineInfo(addr, c->getSectionNumber() - 1); if (!optionalLineInfo) return std::nullopt; diff --git a/lld/Common/DWARF.cpp b/lld/Common/DWARF.cpp index 077adbc..2cd8ca4 100644 --- a/lld/Common/DWARF.cpp +++ b/lld/Common/DWARF.cpp @@ -69,27 +69,27 @@ DWARFCache::DWARFCache(std::unique_ptr d) // Returns the pair of file name and line number describing location of data // object (variable, array, etc) definition. -Optional> +std::optional> 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 DWARFCache::getDILineInfo(uint64_t offset, - uint64_t sectionIndex) { +std::optional 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 DWARFCache::getDILineInfo(uint64_t offset, DILineInfoSpecifier::FileLineInfoKind::AbsoluteFilePath, info)) return info; } - return None; + return std::nullopt; } } // namespace lld diff --git a/lld/ELF/DWARF.cpp b/lld/ELF/DWARF.cpp index 99bb5e1..be4609f 100644 --- a/lld/ELF/DWARF.cpp +++ b/lld/ELF/DWARF.cpp @@ -101,7 +101,7 @@ template struct LLDRelocationResolver> { // to llvm since it has no idea about InputSection. template template -Optional +std::optional LLDDwarfObj::findAux(const InputSectionBase &sec, uint64_t pos, ArrayRef rels) const { auto it = @@ -132,8 +132,8 @@ LLDDwarfObj::findAux(const InputSectionBase &sec, uint64_t pos, } template -Optional LLDDwarfObj::find(const llvm::DWARFSection &s, - uint64_t pos) const { +std::optional +LLDDwarfObj::find(const llvm::DWARFSection &s, uint64_t pos) const { auto &sec = static_cast(s); const RelsOrRelas rels = sec.sec->template relsOrRelas(); if (rels.areRelocsRel()) diff --git a/lld/ELF/DWARF.h b/lld/ELF/DWARF.h index 1b6bb49..9a79939 100644 --- a/lld/ELF/DWARF.h +++ b/lld/ELF/DWARF.h @@ -76,14 +76,14 @@ public: return ELFT::TargetEndianness == llvm::support::little; } - llvm::Optional find(const llvm::DWARFSection &sec, - uint64_t pos) const override; + std::optional find(const llvm::DWARFSection &sec, + uint64_t pos) const override; private: template - llvm::Optional findAux(const InputSectionBase &sec, - uint64_t pos, - ArrayRef rels) const; + std::optional findAux(const InputSectionBase &sec, + uint64_t pos, + ArrayRef rels) const; LLDDWARFSection gnuPubnamesSection; LLDDWARFSection gnuPubtypesSection; diff --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp index 55c46b6..af44195 100644 --- a/lld/ELF/InputFiles.cpp +++ b/lld/ELF/InputFiles.cpp @@ -308,11 +308,11 @@ static std::string getSrcMsgAux(ObjFile &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 info = file.getDILineInfo(&sec, offset)) + if (std::optional info = file.getDILineInfo(&sec, offset)) return createFileLineMsg(info->FileName, info->Line); // If it failed, look up again as a variable. - if (Optional> fileLine = + if (std::optional> fileLine = file.getVariableLoc(sym.getName())) return createFileLineMsg(fileLine->first, fileLine->second); @@ -423,7 +423,7 @@ template DWARFCache *ObjFile::getDwarf() { // Returns the pair of file name and line number describing location of data // object (variable, array, etc) definition. template -Optional> +std::optional> ObjFile::getVariableLoc(StringRef name) { return getDwarf()->getVariableLoc(name); } @@ -431,8 +431,8 @@ ObjFile::getVariableLoc(StringRef name) { // Returns source line information for a given offset // using DWARF debug info. template -Optional ObjFile::getDILineInfo(InputSectionBase *s, - uint64_t offset) { +std::optional ObjFile::getDILineInfo(InputSectionBase *s, + uint64_t offset) { // Detect SectionIndex for specified section. uint64_t sectionIndex = object::SectionedAddress::UndefSection; ArrayRef sections = s->file->getSections(); diff --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h index b5d09d4..e3540e3 100644 --- a/lld/ELF/InputFiles.h +++ b/lld/ELF/InputFiles.h @@ -248,8 +248,9 @@ public: return getSymbol(symIndex); } - llvm::Optional getDILineInfo(InputSectionBase *, uint64_t); - llvm::Optional> getVariableLoc(StringRef name); + std::optional getDILineInfo(InputSectionBase *, uint64_t); + std::optional> + 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 diff --git a/lld/MachO/Dwarf.h b/lld/MachO/Dwarf.h index 7cd6ef1..31a9f82 100644 --- a/lld/MachO/Dwarf.h +++ b/lld/MachO/Dwarf.h @@ -22,10 +22,10 @@ class DwarfObject final : public llvm::DWARFObject { public: bool isLittleEndian() const override { return true; } - llvm::Optional find(const llvm::DWARFSection &sec, - uint64_t pos) const override { + std::optional find(const llvm::DWARFSection &sec, + uint64_t pos) const override { // TODO: implement this - return llvm::None; + return std::nullopt; } void forEachInfoSections( diff --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp index 1ef7ae5..1d8d584 100644 --- a/lld/MachO/InputSection.cpp +++ b/lld/MachO/InputSection.cpp @@ -111,7 +111,7 @@ std::string InputSection::getSourceLocation(uint64_t off) const { }; // First, look up a function for a given offset. - if (Optional li = dwarf->getDILineInfo( + if (std::optional 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> fileLine = + if (std::optional> fileLine = dwarf->getVariableLoc(symName)) return createMsg(fileLine->first, fileLine->second); } diff --git a/lld/include/lld/Common/DWARF.h b/lld/include/lld/Common/DWARF.h index b77985a..6310d7a 100644 --- a/lld/include/lld/Common/DWARF.h +++ b/lld/include/lld/Common/DWARF.h @@ -26,9 +26,9 @@ namespace lld { class DWARFCache { public: DWARFCache(std::unique_ptr dwarf); - llvm::Optional getDILineInfo(uint64_t offset, - uint64_t sectionIndex); - llvm::Optional> + std::optional getDILineInfo(uint64_t offset, + uint64_t sectionIndex); + std::optional> getVariableLoc(StringRef name); llvm::DWARFContext *getContext() { return dwarf.get(); } diff --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h index 60fcd3d..52bd91d 100644 --- a/llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h +++ b/llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h @@ -12,6 +12,7 @@ #include "llvm/DebugInfo/DWARF/DWARFRelocMap.h" #include "llvm/DebugInfo/DWARF/DWARFSection.h" #include "llvm/Object/ObjectFile.h" +#include 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 find(const DWARFSection &Sec, - uint64_t Pos) const = 0; + virtual std::optional find(const DWARFSection &Sec, + uint64_t Pos) const = 0; }; } // namespace llvm diff --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp index 19d7d65..aaa2ded 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp @@ -1877,12 +1877,12 @@ public: S.IsNameUnique = false; } - Optional find(const DWARFSection &S, - uint64_t Pos) const override { + std::optional find(const DWARFSection &S, + uint64_t Pos) const override { auto &Sec = static_cast(S); RelocAddrMap::const_iterator AI = Sec.Relocs.find(Pos); if (AI == Sec.Relocs.end()) - return None; + return std::nullopt; return AI->second; } diff --git a/llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp index b18b643..5f93c40 100644 --- a/llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp +++ b/llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp @@ -54,7 +54,7 @@ uint64_t DWARFDataExtractor::getRelocatedValue(uint32_t Size, uint64_t *Off, return getUnsigned(Off, Size, Err); ErrorAsOutParameter ErrAsOut(Err); - Optional E = Obj->find(*Section, *Off); + std::optional E = Obj->find(*Section, *Off); uint64_t LocData = getUnsigned(Off, Size, Err); if (!E || (Err && *Err)) return LocData; -- 2.7.4