[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
This commit is contained in:
Kazu Hirata 2022-12-02 19:04:57 -08:00
parent d62480c199
commit fef3a16aea
19 changed files with 76 additions and 70 deletions

View File

@ -66,7 +66,7 @@ public:
Optional<AddressRange> 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()]);
}

View File

@ -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);
}
}

View File

@ -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<SetType, ExtStorage>(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;
}
}

View File

@ -297,7 +297,7 @@ public:
auto transform(const Function &F) const & -> Optional<decltype(F(value()))> {
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<decltype(F(std::move(*this).value()))> {
if (*this)
return F(std::move(*this).value());
return None;
return std::nullopt;
}
};
@ -365,17 +365,17 @@ constexpr bool operator==(const Optional<T> &X, std::nullopt_t) {
template <typename T>
constexpr bool operator==(std::nullopt_t, const Optional<T> &X) {
return X == None;
return X == std::nullopt;
}
template <typename T>
constexpr bool operator!=(const Optional<T> &X, std::nullopt_t) {
return !(X == None);
return !(X == std::nullopt);
}
template <typename T>
constexpr bool operator!=(std::nullopt_t, const Optional<T> &X) {
return X != None;
return X != std::nullopt;
}
template <typename T>
@ -390,32 +390,32 @@ constexpr bool operator<(std::nullopt_t, const Optional<T> &X) {
template <typename T>
constexpr bool operator<=(const Optional<T> &X, std::nullopt_t) {
return !(None < X);
return !(std::nullopt < X);
}
template <typename T>
constexpr bool operator<=(std::nullopt_t, const Optional<T> &X) {
return !(X < None);
return !(X < std::nullopt);
}
template <typename T>
constexpr bool operator>(const Optional<T> &X, std::nullopt_t) {
return None < X;
return std::nullopt < X;
}
template <typename T>
constexpr bool operator>(std::nullopt_t, const Optional<T> &X) {
return X < None;
return X < std::nullopt;
}
template <typename T>
constexpr bool operator>=(const Optional<T> &X, std::nullopt_t) {
return None <= X;
return std::nullopt <= X;
}
template <typename T>
constexpr bool operator>=(std::nullopt_t, const Optional<T> &X) {
return X <= None;
return X <= std::nullopt;
}
template <typename T>
@ -486,7 +486,7 @@ raw_ostream &operator<<(raw_ostream &OS, const Optional<T> &O) {
if (O)
OS << *O;
else
OS << None;
OS << std::nullopt;
return OS;
}

View File

@ -924,7 +924,7 @@ template <typename Iter>
auto deref_or_none(const Iter &I, const Iter &End) -> llvm::Optional<
std::remove_const_t<std::remove_reference_t<decltype(*I)>>> {
if (I == End)
return None;
return std::nullopt;
return *I;
}

View File

@ -88,11 +88,12 @@ public:
template <>
class StringMapEntryStorage<std::nullopt_t> : 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

View File

@ -136,7 +136,7 @@ public:
// implicit conversion operator to ArrayRef.
operator ArrayRef<EltTy>() const {
if (Val.isNull())
return None;
return std::nullopt;
if (Val.template is<EltTy>())
return *Val.getAddrOfPtr1();
return *Val.template get<VecTy*>();
@ -145,7 +145,7 @@ public:
// implicit conversion operator to MutableArrayRef.
operator MutableArrayRef<EltTy>() {
if (Val.isNull())
return None;
return std::nullopt;
if (Val.template is<EltTy>())
return *Val.getAddrOfPtr1();
return *Val.template get<VecTy*>();

View File

@ -248,7 +248,7 @@ public:
return InCustomSizedSlabIdx - static_cast<int64_t>(P - S);
InCustomSizedSlabIdx -= static_cast<int64_t>(Size);
}
return None;
return std::nullopt;
}
/// A wrapper around identifyObject that additionally asserts that

View File

@ -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<std::is_signed<T>::value, llvm::Optional<T>>
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<std::is_unsigned<T>::value, llvm::Optional<T>>
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

View File

@ -1054,7 +1054,7 @@ template <typename T> Optional<T> expectedToOptional(Expected<T> &&E) {
if (E)
return std::move(*E);
consumeError(E.takeError());
return None;
return std::nullopt;
}
/// Helper for converting an Error to a bool.

View File

@ -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<sys::fs::perms> OverwritePermissions = None);
Optional<sys::fs::perms> OverwritePermissions = std::nullopt);
private:
FilePermissionsApplier(StringRef InputFilename, sys::fs::file_status Status)

View File

@ -236,7 +236,8 @@ public:
};
inline FormattedBytes
format_bytes(ArrayRef<uint8_t> Bytes, Optional<uint64_t> FirstByteOffset = None,
format_bytes(ArrayRef<uint8_t> Bytes,
Optional<uint64_t> 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<uint8_t> Bytes, Optional<uint64_t> FirstByteOffset = None,
inline FormattedBytes
format_bytes_with_ascii(ArrayRef<uint8_t> Bytes,
Optional<uint64_t> FirstByteOffset = None,
Optional<uint64_t> FirstByteOffset = std::nullopt,
uint32_t NumPerLine = 16, uint8_t ByteGroupSize = 4,
uint32_t IndentLevel = 0, bool Upper = false) {
return FormattedBytes(Bytes, IndentLevel, FirstByteOffset, NumPerLine,

View File

@ -63,10 +63,10 @@ protected:
size_t Prec;
Optional<size_t> 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<size_t>(99u, Prec);

View File

@ -88,7 +88,7 @@ public:
std::optional<CostType> getValue() const {
if (isValid())
return Value;
return None;
return std::nullopt;
}
/// For all of the arithmetic operators provided here any invalid state is

View File

@ -404,12 +404,12 @@ public:
llvm::Optional<std::nullptr_t> getAsNull() const {
if (LLVM_LIKELY(Type == T_Null))
return nullptr;
return llvm::None;
return std::nullopt;
}
llvm::Optional<bool> getAsBoolean() const {
if (LLVM_LIKELY(Type == T_Boolean))
return as<bool>();
return llvm::None;
return std::nullopt;
}
llvm::Optional<double> getAsNumber() const {
if (LLVM_LIKELY(Type == T_Double))
@ -418,7 +418,7 @@ public:
return as<int64_t>();
if (LLVM_LIKELY(Type == T_UINT64))
return as<uint64_t>();
return llvm::None;
return std::nullopt;
}
// Succeeds if the Value is a Number, and exactly representable as int64_t.
llvm::Optional<int64_t> getAsInteger() const {
@ -431,7 +431,7 @@ public:
D <= double(std::numeric_limits<int64_t>::max())))
return D;
}
return llvm::None;
return std::nullopt;
}
llvm::Optional<uint64_t> getAsUINT64() const {
if (Type == T_UINT64)
@ -441,14 +441,14 @@ public:
if (N >= 0)
return as<uint64_t>();
}
return llvm::None;
return std::nullopt;
}
llvm::Optional<llvm::StringRef> getAsString() const {
if (Type == T_String)
return llvm::StringRef(as<std::string>());
if (LLVM_LIKELY(Type == T_StringRef))
return as<llvm::StringRef>();
return llvm::None;
return std::nullopt;
}
const json::Object *getAsObject() const {
return LLVM_LIKELY(Type == T_Object) ? &as<json::Object>() : nullptr;
@ -764,7 +764,7 @@ inline bool fromJSON(const Value &E, std::nullptr_t &Out, Path P) {
template <typename T>
bool fromJSON(const Value &E, llvm::Optional<T> &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;
}

View File

@ -97,7 +97,7 @@ public:
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFile(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true, bool IsVolatile = false,
Optional<Align> Alignment = None);
Optional<Align> 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<std::unique_ptr<MemoryBuffer>>
getOpenFileSlice(sys::fs::file_t FD, const Twine &Filename, uint64_t MapSize,
int64_t Offset, bool IsVolatile = false,
Optional<Align> Alignment = None);
Optional<Align> Alignment = std::nullopt);
/// Given an already-open file descriptor, read the file and return a
/// MemoryBuffer.
@ -125,7 +125,7 @@ public:
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getOpenFile(sys::fs::file_t FD, const Twine &Filename, uint64_t FileSize,
bool RequiresNullTerminator = true, bool IsVolatile = false,
Optional<Align> Alignment = None);
Optional<Align> 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<std::unique_ptr<MemoryBuffer>>
getFileOrSTDIN(const Twine &Filename, bool IsText = false,
bool RequiresNullTerminator = true,
Optional<Align> Alignment = None);
Optional<Align> Alignment = std::nullopt);
/// Map a subrange of the specified file as a MemoryBuffer.
static ErrorOr<std::unique_ptr<MemoryBuffer>>
getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsVolatile = false, Optional<Align> Alignment = None);
bool IsVolatile = false,
Optional<Align> Alignment = std::nullopt);
//===--------------------------------------------------------------------===//
// Provided for performance analysis.
@ -200,12 +201,13 @@ public:
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFile(const Twine &Filename, bool IsVolatile = false,
Optional<Align> Alignment = None);
Optional<Align> Alignment = std::nullopt);
/// Map a subrange of the specified file as a WritableMemoryBuffer.
static ErrorOr<std::unique_ptr<WritableMemoryBuffer>>
getFileSlice(const Twine &Filename, uint64_t MapSize, uint64_t Offset,
bool IsVolatile = false, Optional<Align> Alignment = None);
bool IsVolatile = false,
Optional<Align> 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<WritableMemoryBuffer>
getNewUninitMemBuffer(size_t Size, const Twine &BufferName = "",
Optional<Align> Alignment = None);
Optional<Align> 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.

View File

@ -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<size_t> Width = None);
Optional<size_t> Width = std::nullopt);
void write_double(raw_ostream &S, double D, FloatStyle Style,
Optional<size_t> Precision = None);
Optional<size_t> Precision = std::nullopt);
}
#endif

View File

@ -75,21 +75,21 @@ public:
/// Retrieve the minor version number, if provided.
Optional<unsigned> getMinor() const {
if (!HasMinor)
return None;
return std::nullopt;
return Minor;
}
/// Retrieve the subminor version number, if provided.
Optional<unsigned> getSubminor() const {
if (!HasSubminor)
return None;
return std::nullopt;
return Subminor;
}
/// Retrieve the build version number, if provided.
Optional<unsigned> getBuild() const {
if (!HasBuild)
return None;
return std::nullopt;
return Build;
}

View File

@ -534,9 +534,10 @@ public:
/// different contents.
bool addFile(const Twine &Path, time_t ModificationTime,
std::unique_ptr<llvm::MemoryBuffer> Buffer,
Optional<uint32_t> User = None, Optional<uint32_t> Group = None,
Optional<llvm::sys::fs::file_type> Type = None,
Optional<llvm::sys::fs::perms> Perms = None);
Optional<uint32_t> User = std::nullopt,
Optional<uint32_t> Group = std::nullopt,
Optional<llvm::sys::fs::file_type> Type = std::nullopt,
Optional<llvm::sys::fs::perms> 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<uint32_t> User = None,
Optional<uint32_t> Group = None,
Optional<llvm::sys::fs::perms> Perms = None);
time_t ModificationTime,
Optional<uint32_t> User = std::nullopt,
Optional<uint32_t> Group = std::nullopt,
Optional<llvm::sys::fs::perms> 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<uint32_t> User = None,
Optional<uint32_t> Group = None,
Optional<llvm::sys::fs::file_type> Type = None,
Optional<llvm::sys::fs::perms> Perms = None);
Optional<uint32_t> User = std::nullopt,
Optional<uint32_t> Group = std::nullopt,
Optional<llvm::sys::fs::file_type> Type = std::nullopt,
Optional<llvm::sys::fs::perms> Perms = std::nullopt);
std::string toString() const;
@ -871,7 +873,7 @@ public:
return StringRef(*ExternalRedirect);
if (auto *FE = dyn_cast<FileEntry>(E))
return FE->getExternalContentsPath();
return None;
return std::nullopt;
}
};