[llvm] c33511c - [lld] Change Optional to std::optional

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 17:25:39 PST 2022


Author: Fangrui Song
Date: 2022-11-27T17:25:34-08:00
New Revision: c33511c8df54265f5c540c0d8605f2f6f35ee6cd

URL: https://github.com/llvm/llvm-project/commit/c33511c8df54265f5c540c0d8605f2f6f35ee6cd
DIFF: https://github.com/llvm/llvm-project/commit/c33511c8df54265f5c540c0d8605f2f6f35ee6cd.diff

LOG: [lld] Change Optional to std::optional

https://discourse.llvm.org/t/deprecating-llvm-optional-x-hasvalue-getvalue-getvalueor/63716

Added: 
    

Modified: 
    lld/COFF/InputFiles.cpp
    lld/COFF/InputFiles.h
    lld/COFF/SymbolTable.cpp
    lld/Common/DWARF.cpp
    lld/ELF/DWARF.cpp
    lld/ELF/DWARF.h
    lld/ELF/InputFiles.cpp
    lld/ELF/InputFiles.h
    lld/MachO/Dwarf.h
    lld/MachO/InputSection.cpp
    lld/include/lld/Common/DWARF.h
    llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h
    llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
    llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp

Removed: 
    


################################################################################
diff  --git a/lld/COFF/InputFiles.cpp b/lld/COFF/InputFiles.cpp
index ce0f6b4661c9c..66a6b85224686 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<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)

diff  --git a/lld/COFF/InputFiles.h b/lld/COFF/InputFiles.h
index 6af44b09af355..e7e3fdc049f38 100644
--- a/lld/COFF/InputFiles.h
+++ b/lld/COFF/InputFiles.h
@@ -209,8 +209,8 @@ class ObjFile : public InputFile {
   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);

diff  --git a/lld/COFF/SymbolTable.cpp b/lld/COFF/SymbolTable.cpp
index dd60b519237e4..f3bee72d8f4d6 100644
--- a/lld/COFF/SymbolTable.cpp
+++ b/lld/COFF/SymbolTable.cpp
@@ -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;

diff  --git a/lld/Common/DWARF.cpp b/lld/Common/DWARF.cpp
index 077adbcaf8581..2cd8ca4575dee 100644
--- a/lld/Common/DWARF.cpp
+++ b/lld/Common/DWARF.cpp
@@ -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

diff  --git a/lld/ELF/DWARF.cpp b/lld/ELF/DWARF.cpp
index 99bb5e1cc6a98..be4609fa229b1 100644
--- a/lld/ELF/DWARF.cpp
+++ b/lld/ELF/DWARF.cpp
@@ -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())

diff  --git a/lld/ELF/DWARF.h b/lld/ELF/DWARF.h
index 1b6bb49c57c64..9a7993903d866 100644
--- a/lld/ELF/DWARF.h
+++ b/lld/ELF/DWARF.h
@@ -76,14 +76,14 @@ template <class ELFT> class LLDDwarfObj final : public llvm::DWARFObject {
     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;

diff  --git a/lld/ELF/InputFiles.cpp b/lld/ELF/InputFiles.cpp
index 55c46b600c4ea..af441953b7bec 100644
--- a/lld/ELF/InputFiles.cpp
+++ b/lld/ELF/InputFiles.cpp
@@ -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 
diff erent 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();

diff  --git a/lld/ELF/InputFiles.h b/lld/ELF/InputFiles.h
index b5d09d4ec5886..e3540e337f50a 100644
--- a/lld/ELF/InputFiles.h
+++ b/lld/ELF/InputFiles.h
@@ -248,8 +248,9 @@ template <class ELFT> class ObjFile : public ELFFileBase {
     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

diff  --git a/lld/MachO/Dwarf.h b/lld/MachO/Dwarf.h
index 7cd6ef1bef42b..31a9f82ee0076 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<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(

diff  --git a/lld/MachO/InputSection.cpp b/lld/MachO/InputSection.cpp
index 1ef7ae5bb9c58..1d8d58477139f 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<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);
   }

diff  --git a/lld/include/lld/Common/DWARF.h b/lld/include/lld/Common/DWARF.h
index b77985a6919da..6310d7a876fe0 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<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(); }

diff  --git a/llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h b/llvm/include/llvm/DebugInfo/DWARF/DWARFObject.h
index 60fcd3daf5b1b..52bd91d9140f9 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 <optional>
 
 namespace llvm {
 // This is responsible for low level access to the object file. It
@@ -81,8 +82,8 @@ class DWARFObject {
   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

diff  --git a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
index 19d7d659a86aa..aaa2ded1ccb87 100644
--- a/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
+++ b/llvm/lib/DebugInfo/DWARF/DWARFContext.cpp
@@ -1877,12 +1877,12 @@ class DWARFObjInMemory final : public DWARFObject {
         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;
   }
 

diff  --git a/llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp b/llvm/lib/DebugInfo/DWARF/DWARFDataExtractor.cpp
index b18b64382b412..5f93c40379a8c 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<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;


        


More information about the llvm-commits mailing list