From fef3a16aeab660d0789c592985993bd68b51f517 Mon Sep 17 00:00:00 2001 From: Kazu Hirata Date: Fri, 2 Dec 2022 19:04:57 -0800 Subject: [PATCH] [ADT, Support] Use std::nullopt instead of None (NFC) This patch mechanically replaces None with std::nullopt where the compiler would warn if None were deprecated. The intent is to reduce the amount of manual work required in migrating from Optional to std::optional. 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 Differential Revision: https://reviews.llvm.org/D139241 --- llvm/include/llvm/ADT/AddressRanges.h | 4 ++-- llvm/include/llvm/ADT/BreadthFirstIterator.h | 10 +++++----- llvm/include/llvm/ADT/DepthFirstIterator.h | 6 +++--- llvm/include/llvm/ADT/Optional.h | 24 ++++++++++++------------ llvm/include/llvm/ADT/STLExtras.h | 2 +- llvm/include/llvm/ADT/StringMapEntry.h | 5 +++-- llvm/include/llvm/ADT/TinyPtrVector.h | 4 ++-- llvm/include/llvm/Support/Allocator.h | 2 +- llvm/include/llvm/Support/CheckedArithmetic.h | 6 +++--- llvm/include/llvm/Support/Error.h | 2 +- llvm/include/llvm/Support/FileUtilities.h | 2 +- llvm/include/llvm/Support/Format.h | 5 +++-- llvm/include/llvm/Support/FormatProviders.h | 4 ++-- llvm/include/llvm/Support/InstructionCost.h | 2 +- llvm/include/llvm/Support/JSON.h | 16 ++++++++-------- llvm/include/llvm/Support/MemoryBuffer.h | 18 ++++++++++-------- llvm/include/llvm/Support/NativeFormatting.h | 4 ++-- llvm/include/llvm/Support/VersionTuple.h | 6 +++--- llvm/include/llvm/Support/VirtualFileSystem.h | 24 +++++++++++++----------- 19 files changed, 76 insertions(+), 70 deletions(-) diff --git a/llvm/include/llvm/ADT/AddressRanges.h b/llvm/include/llvm/ADT/AddressRanges.h index c02844a..1fd7103 100644 --- a/llvm/include/llvm/ADT/AddressRanges.h +++ b/llvm/include/llvm/ADT/AddressRanges.h @@ -66,7 +66,7 @@ public: Optional getRangeThatContains(uint64_t Addr) const { Collection::const_iterator It = find(Addr); if (It == Ranges.end()) - return None; + return std::nullopt; return *It; } @@ -129,7 +129,7 @@ public: getRangeValueThatContains(uint64_t Addr) const { Collection::const_iterator It = find(Addr); if (It == Ranges.end()) - return None; + return std::nullopt; return std::make_pair(*It, Values[It - Ranges.begin()]); } diff --git a/llvm/include/llvm/ADT/BreadthFirstIterator.h b/llvm/include/llvm/ADT/BreadthFirstIterator.h index 807b0a9..56a1bb1 100644 --- a/llvm/include/llvm/ADT/BreadthFirstIterator.h +++ b/llvm/include/llvm/ADT/BreadthFirstIterator.h @@ -72,8 +72,8 @@ private: Level = 0; // Also, insert a dummy node as marker. - VisitQueue.push(QueueElement(Node, None)); - VisitQueue.push(None); + VisitQueue.push(QueueElement(Node, std::nullopt)); + VisitQueue.push(std::nullopt); } inline bf_iterator() = default; @@ -91,14 +91,14 @@ private: // Already visited? if (this->Visited.insert(Next).second) - VisitQueue.push(QueueElement(Next, None)); + VisitQueue.push(QueueElement(Next, std::nullopt)); } VisitQueue.pop(); // Go to the next element skipping markers if needed. if (!VisitQueue.empty()) { Head = VisitQueue.front(); - if (Head != None) + if (Head != std::nullopt) return; Level += 1; VisitQueue.pop(); @@ -106,7 +106,7 @@ private: // Don't push another marker if this is the last // element. if (!VisitQueue.empty()) - VisitQueue.push(None); + VisitQueue.push(std::nullopt); } } diff --git a/llvm/include/llvm/ADT/DepthFirstIterator.h b/llvm/include/llvm/ADT/DepthFirstIterator.h index cea6fbc..513cc56 100644 --- a/llvm/include/llvm/ADT/DepthFirstIterator.h +++ b/llvm/include/llvm/ADT/DepthFirstIterator.h @@ -105,7 +105,7 @@ private: inline df_iterator(NodeRef Node) { this->Visited.insert(Node); - VisitStack.push_back(StackElement(Node, None)); + VisitStack.push_back(StackElement(Node, std::nullopt)); } inline df_iterator() = default; // End is when stack is empty @@ -113,7 +113,7 @@ private: inline df_iterator(NodeRef Node, SetType &S) : df_iterator_storage(S) { if (this->Visited.insert(Node).second) - VisitStack.push_back(StackElement(Node, None)); + VisitStack.push_back(StackElement(Node, std::nullopt)); } inline df_iterator(SetType &S) @@ -137,7 +137,7 @@ private: // Has our next sibling been visited? if (this->Visited.insert(Next).second) { // No, do it now. - VisitStack.push_back(StackElement(Next, None)); + VisitStack.push_back(StackElement(Next, std::nullopt)); return; } } diff --git a/llvm/include/llvm/ADT/Optional.h b/llvm/include/llvm/ADT/Optional.h index 4d0cd45..2df8d35 100644 --- a/llvm/include/llvm/ADT/Optional.h +++ b/llvm/include/llvm/ADT/Optional.h @@ -297,7 +297,7 @@ public: auto transform(const Function &F) const & -> Optional { if (*this) return F(value()); - return None; + return std::nullopt; } T &&value() && { return std::move(Storage.value()); } @@ -313,7 +313,7 @@ public: const Function &F) && -> Optional { if (*this) return F(std::move(*this).value()); - return None; + return std::nullopt; } }; @@ -365,17 +365,17 @@ constexpr bool operator==(const Optional &X, std::nullopt_t) { template constexpr bool operator==(std::nullopt_t, const Optional &X) { - return X == None; + return X == std::nullopt; } template constexpr bool operator!=(const Optional &X, std::nullopt_t) { - return !(X == None); + return !(X == std::nullopt); } template constexpr bool operator!=(std::nullopt_t, const Optional &X) { - return X != None; + return X != std::nullopt; } template @@ -390,32 +390,32 @@ constexpr bool operator<(std::nullopt_t, const Optional &X) { template constexpr bool operator<=(const Optional &X, std::nullopt_t) { - return !(None < X); + return !(std::nullopt < X); } template constexpr bool operator<=(std::nullopt_t, const Optional &X) { - return !(X < None); + return !(X < std::nullopt); } template constexpr bool operator>(const Optional &X, std::nullopt_t) { - return None < X; + return std::nullopt < X; } template constexpr bool operator>(std::nullopt_t, const Optional &X) { - return X < None; + return X < std::nullopt; } template constexpr bool operator>=(const Optional &X, std::nullopt_t) { - return None <= X; + return std::nullopt <= X; } template constexpr bool operator>=(std::nullopt_t, const Optional &X) { - return X <= None; + return X <= std::nullopt; } template @@ -486,7 +486,7 @@ raw_ostream &operator<<(raw_ostream &OS, const Optional &O) { if (O) OS << *O; else - OS << None; + OS << std::nullopt; return OS; } diff --git a/llvm/include/llvm/ADT/STLExtras.h b/llvm/include/llvm/ADT/STLExtras.h index 11ef2f99..28c13a8 100644 --- a/llvm/include/llvm/ADT/STLExtras.h +++ b/llvm/include/llvm/ADT/STLExtras.h @@ -924,7 +924,7 @@ template auto deref_or_none(const Iter &I, const Iter &End) -> llvm::Optional< std::remove_const_t>> { if (I == End) - return None; + return std::nullopt; return *I; } diff --git a/llvm/include/llvm/ADT/StringMapEntry.h b/llvm/include/llvm/ADT/StringMapEntry.h index fb4ec66..27d8291 100644 --- a/llvm/include/llvm/ADT/StringMapEntry.h +++ b/llvm/include/llvm/ADT/StringMapEntry.h @@ -88,11 +88,12 @@ public: template <> class StringMapEntryStorage : public StringMapEntryBase { public: - explicit StringMapEntryStorage(size_t keyLength, std::nullopt_t = None) + explicit StringMapEntryStorage(size_t keyLength, + std::nullopt_t = std::nullopt) : StringMapEntryBase(keyLength) {} StringMapEntryStorage(StringMapEntryStorage &entry) = delete; - std::nullopt_t getValue() const { return None; } + std::nullopt_t getValue() const { return std::nullopt; } }; /// StringMapEntry - This is used to represent one value that is inserted into diff --git a/llvm/include/llvm/ADT/TinyPtrVector.h b/llvm/include/llvm/ADT/TinyPtrVector.h index ed20a76..b96b7ea 100644 --- a/llvm/include/llvm/ADT/TinyPtrVector.h +++ b/llvm/include/llvm/ADT/TinyPtrVector.h @@ -136,7 +136,7 @@ public: // implicit conversion operator to ArrayRef. operator ArrayRef() const { if (Val.isNull()) - return None; + return std::nullopt; if (Val.template is()) return *Val.getAddrOfPtr1(); return *Val.template get(); @@ -145,7 +145,7 @@ public: // implicit conversion operator to MutableArrayRef. operator MutableArrayRef() { if (Val.isNull()) - return None; + return std::nullopt; if (Val.template is()) return *Val.getAddrOfPtr1(); return *Val.template get(); diff --git a/llvm/include/llvm/Support/Allocator.h b/llvm/include/llvm/Support/Allocator.h index 041729f..8a70709 100644 --- a/llvm/include/llvm/Support/Allocator.h +++ b/llvm/include/llvm/Support/Allocator.h @@ -248,7 +248,7 @@ public: return InCustomSizedSlabIdx - static_cast(P - S); InCustomSizedSlabIdx -= static_cast(Size); } - return None; + return std::nullopt; } /// A wrapper around identifyObject that additionally asserts that diff --git a/llvm/include/llvm/Support/CheckedArithmetic.h b/llvm/include/llvm/Support/CheckedArithmetic.h index 09e6d7e..c9db239 100644 --- a/llvm/include/llvm/Support/CheckedArithmetic.h +++ b/llvm/include/llvm/Support/CheckedArithmetic.h @@ -33,7 +33,7 @@ checkedOp(T LHS, T RHS, F Op, bool Signed = true) { bool Overflow; llvm::APInt Out = (ALHS.*Op)(ARHS, Overflow); if (Overflow) - return llvm::None; + return std::nullopt; return Signed ? Out.getSExtValue() : Out.getZExtValue(); } } @@ -75,7 +75,7 @@ std::enable_if_t::value, llvm::Optional> checkedMulAdd(T A, T B, T C) { if (auto Product = checkedMul(A, B)) return checkedAdd(*Product, C); - return llvm::None; + return std::nullopt; } /// Add two unsigned integers \p LHS and \p RHS. @@ -104,7 +104,7 @@ std::enable_if_t::value, llvm::Optional> checkedMulAddUnsigned(T A, T B, T C) { if (auto Product = checkedMulUnsigned(A, B)) return checkedAddUnsigned(*Product, C); - return llvm::None; + return std::nullopt; } } // End llvm namespace diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h index 04c12cb..6c794ea 100644 --- a/llvm/include/llvm/Support/Error.h +++ b/llvm/include/llvm/Support/Error.h @@ -1054,7 +1054,7 @@ template Optional expectedToOptional(Expected &&E) { if (E) return std::move(*E); consumeError(E.takeError()); - return None; + return std::nullopt; } /// Helper for converting an Error to a bool. diff --git a/llvm/include/llvm/Support/FileUtilities.h b/llvm/include/llvm/Support/FileUtilities.h index 0033638..b641781 100644 --- a/llvm/include/llvm/Support/FileUtilities.h +++ b/llvm/include/llvm/Support/FileUtilities.h @@ -122,7 +122,7 @@ namespace llvm { /// Copy LastAccess and ModificationTime if \p CopyDates is true. /// Overwrite stored permissions if \p OverwritePermissions is specified. Error apply(StringRef OutputFilename, bool CopyDates = false, - Optional OverwritePermissions = None); + Optional OverwritePermissions = std::nullopt); private: FilePermissionsApplier(StringRef InputFilename, sys::fs::file_status Status) diff --git a/llvm/include/llvm/Support/Format.h b/llvm/include/llvm/Support/Format.h index a12f00b..d6308a8 100644 --- a/llvm/include/llvm/Support/Format.h +++ b/llvm/include/llvm/Support/Format.h @@ -236,7 +236,8 @@ public: }; inline FormattedBytes -format_bytes(ArrayRef Bytes, Optional FirstByteOffset = None, +format_bytes(ArrayRef Bytes, + Optional FirstByteOffset = std::nullopt, uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4, uint32_t IndentLevel = 0, bool Upper = false) { return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine, @@ -245,7 +246,7 @@ format_bytes(ArrayRef Bytes, Optional FirstByteOffset = None, inline FormattedBytes format_bytes_with_ascii(ArrayRef Bytes, - Optional FirstByteOffset = None, + Optional FirstByteOffset = std::nullopt, uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4, uint32_t IndentLevel = 0, bool Upper = false) { return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine, diff --git a/llvm/include/llvm/Support/FormatProviders.h b/llvm/include/llvm/Support/FormatProviders.h index c25453e..ab1245c 100644 --- a/llvm/include/llvm/Support/FormatProviders.h +++ b/llvm/include/llvm/Support/FormatProviders.h @@ -63,10 +63,10 @@ protected: size_t Prec; Optional Result; if (Str.empty()) - Result = None; + Result = std::nullopt; else if (Str.getAsInteger(10, Prec)) { assert(false && "Invalid precision specifier"); - Result = None; + Result = std::nullopt; } else { assert(Prec < 100 && "Precision out of range"); Result = std::min(99u, Prec); diff --git a/llvm/include/llvm/Support/InstructionCost.h b/llvm/include/llvm/Support/InstructionCost.h index 8427235..a4a945d 100644 --- a/llvm/include/llvm/Support/InstructionCost.h +++ b/llvm/include/llvm/Support/InstructionCost.h @@ -88,7 +88,7 @@ public: std::optional getValue() const { if (isValid()) return Value; - return None; + return std::nullopt; } /// For all of the arithmetic operators provided here any invalid state is diff --git a/llvm/include/llvm/Support/JSON.h b/llvm/include/llvm/Support/JSON.h index 3605622..0d07cad 100644 --- a/llvm/include/llvm/Support/JSON.h +++ b/llvm/include/llvm/Support/JSON.h @@ -404,12 +404,12 @@ public: llvm::Optional getAsNull() const { if (LLVM_LIKELY(Type == T_Null)) return nullptr; - return llvm::None; + return std::nullopt; } llvm::Optional getAsBoolean() const { if (LLVM_LIKELY(Type == T_Boolean)) return as(); - return llvm::None; + return std::nullopt; } llvm::Optional getAsNumber() const { if (LLVM_LIKELY(Type == T_Double)) @@ -418,7 +418,7 @@ public: return as(); if (LLVM_LIKELY(Type == T_UINT64)) return as(); - return llvm::None; + return std::nullopt; } // Succeeds if the Value is a Number, and exactly representable as int64_t. llvm::Optional getAsInteger() const { @@ -431,7 +431,7 @@ public: D <= double(std::numeric_limits::max()))) return D; } - return llvm::None; + return std::nullopt; } llvm::Optional getAsUINT64() const { if (Type == T_UINT64) @@ -441,14 +441,14 @@ public: if (N >= 0) return as(); } - return llvm::None; + return std::nullopt; } llvm::Optional getAsString() const { if (Type == T_String) return llvm::StringRef(as()); if (LLVM_LIKELY(Type == T_StringRef)) return as(); - return llvm::None; + return std::nullopt; } const json::Object *getAsObject() const { return LLVM_LIKELY(Type == T_Object) ? &as() : nullptr; @@ -764,7 +764,7 @@ inline bool fromJSON(const Value &E, std::nullptr_t &Out, Path P) { template bool fromJSON(const Value &E, llvm::Optional &Out, Path P) { if (E.getAsNull()) { - Out = llvm::None; + Out = std::nullopt; return true; } T Result; @@ -845,7 +845,7 @@ public: assert(*this && "Must check this is an object before calling map()"); if (const Value *E = O->get(Prop)) return fromJSON(*E, Out, P.field(Prop)); - Out = llvm::None; + Out = std::nullopt; return true; } diff --git a/llvm/include/llvm/Support/MemoryBuffer.h b/llvm/include/llvm/Support/MemoryBuffer.h index ed975c8..d6ae98e 100644 --- a/llvm/include/llvm/Support/MemoryBuffer.h +++ b/llvm/include/llvm/Support/MemoryBuffer.h @@ -97,7 +97,7 @@ public: static ErrorOr> getFile(const Twine &Filename, bool IsText = false, bool RequiresNullTerminator = true, bool IsVolatile = false, - Optional Alignment = None); + Optional Alignment = std::nullopt); /// Read all of the specified file into a MemoryBuffer as a stream /// (i.e. until EOF reached). This is useful for special files that @@ -111,7 +111,7 @@ public: static ErrorOr> getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize, int64_t Offset, bool IsVolatile = false, - Optional Alignment = None); + Optional Alignment = std::nullopt); /// Given an already-open file descriptor, read the file and return a /// MemoryBuffer. @@ -125,7 +125,7 @@ public: static ErrorOr> getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize, bool RequiresNullTerminator = true, bool IsVolatile = false, - Optional Alignment = None); + Optional Alignment = std::nullopt); /// Open the specified memory range as a MemoryBuffer. Note that InputData /// must be null terminated if RequiresNullTerminator is true. @@ -149,12 +149,13 @@ public: static ErrorOr> getFileOrSTDIN(const Twine &Filename, bool IsText = false, bool RequiresNullTerminator = true, - Optional Alignment = None); + Optional Alignment = std::nullopt); /// Map a subrange of the specified file as a MemoryBuffer. static ErrorOr> getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, - bool IsVolatile = false, Optional Alignment = None); + bool IsVolatile = false, + Optional Alignment = std::nullopt); //===--------------------------------------------------------------------===// // Provided for performance analysis. @@ -200,12 +201,13 @@ public: static ErrorOr> getFile(const Twine &Filename, bool IsVolatile = false, - Optional Alignment = None); + Optional Alignment = std::nullopt); /// Map a subrange of the specified file as a WritableMemoryBuffer. static ErrorOr> getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset, - bool IsVolatile = false, Optional Alignment = None); + bool IsVolatile = false, + Optional Alignment = std::nullopt); /// Allocate a new MemoryBuffer of the specified size that is not initialized. /// Note that the caller should initialize the memory allocated by this @@ -215,7 +217,7 @@ public: /// least the specified alignment. static std::unique_ptr getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "", - Optional Alignment = None); + Optional Alignment = std::nullopt); /// Allocate a new zero-initialized MemoryBuffer of the specified size. Note /// that the caller need not initialize the memory allocated by this method. diff --git a/llvm/include/llvm/Support/NativeFormatting.h b/llvm/include/llvm/Support/NativeFormatting.h index 80cb554..f94b017 100644 --- a/llvm/include/llvm/Support/NativeFormatting.h +++ b/llvm/include/llvm/Support/NativeFormatting.h @@ -38,9 +38,9 @@ void write_integer(raw_ostream &S, long long N, size_t MinDigits, IntegerStyle Style); void write_hex(raw_ostream &S, uint64_t N, HexPrintStyle Style, - Optional Width = None); + Optional Width = std::nullopt); void write_double(raw_ostream &S, double D, FloatStyle Style, - Optional Precision = None); + Optional Precision = std::nullopt); } #endif diff --git a/llvm/include/llvm/Support/VersionTuple.h b/llvm/include/llvm/Support/VersionTuple.h index 3adec07..75c1c09 100644 --- a/llvm/include/llvm/Support/VersionTuple.h +++ b/llvm/include/llvm/Support/VersionTuple.h @@ -75,21 +75,21 @@ public: /// Retrieve the minor version number, if provided. Optional getMinor() const { if (!HasMinor) - return None; + return std::nullopt; return Minor; } /// Retrieve the subminor version number, if provided. Optional getSubminor() const { if (!HasSubminor) - return None; + return std::nullopt; return Subminor; } /// Retrieve the build version number, if provided. Optional getBuild() const { if (!HasBuild) - return None; + return std::nullopt; return Build; } diff --git a/llvm/include/llvm/Support/VirtualFileSystem.h b/llvm/include/llvm/Support/VirtualFileSystem.h index 447437f..6aec30c 100644 --- a/llvm/include/llvm/Support/VirtualFileSystem.h +++ b/llvm/include/llvm/Support/VirtualFileSystem.h @@ -534,9 +534,10 @@ public: /// different contents. bool addFile(const Twine &Path, time_t ModificationTime, std::unique_ptr Buffer, - Optional User = None, Optional Group = None, - Optional Type = None, - Optional Perms = None); + Optional User = std::nullopt, + Optional Group = std::nullopt, + Optional Type = std::nullopt, + Optional Perms = std::nullopt); /// Add a hard link to a file. /// @@ -562,9 +563,10 @@ public: /// to refer to a file (or refer to anything, as it happens). Also, an /// in-memory directory for \p Target isn't automatically created. bool addSymbolicLink(const Twine &NewLink, const Twine &Target, - time_t ModificationTime, Optional User = None, - Optional Group = None, - Optional Perms = None); + time_t ModificationTime, + Optional User = std::nullopt, + Optional Group = std::nullopt, + Optional Perms = std::nullopt); /// Add a buffer to the VFS with a path. The VFS does not own the buffer. /// If present, User, Group, Type and Perms apply to the newly-created file @@ -574,10 +576,10 @@ public: /// different contents. bool addFileNoOwn(const Twine &Path, time_t ModificationTime, const llvm::MemoryBufferRef &Buffer, - Optional User = None, - Optional Group = None, - Optional Type = None, - Optional Perms = None); + Optional User = std::nullopt, + Optional Group = std::nullopt, + Optional Type = std::nullopt, + Optional Perms = std::nullopt); std::string toString() const; @@ -871,7 +873,7 @@ public: return StringRef(*ExternalRedirect); if (auto *FE = dyn_cast(E)) return FE->getExternalContentsPath(); - return None; + return std::nullopt; } }; -- 2.7.4