Skip to content

Commit 5cc641e

Browse files
kazutakahiratabenlangmuir
authored andcommitted
[ADT] Use has_value (NFC)
This patch switches to has_value within Optional. Since Optional<clang::FileEntryRef> uses custom storage class, this patch adds has_entry to MapEntryOptionalStorage. (cherry picked from commit 813f487)
1 parent 6f5ecd5 commit 5cc641e

File tree

2 files changed

+21
-20
lines changed

2 files changed

+21
-20
lines changed

clang/include/clang/Basic/DirectoryEntry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ template <class RefTy> class MapEntryOptionalStorage {
130130

131131
void reset() { MaybeRef = optional_none_tag(); }
132132

133+
bool has_value() const { return MaybeRef.hasOptionalValue(); }
133134
bool hasValue() const { return MaybeRef.hasOptionalValue(); }
134135

135136
RefTy &getValue() & {

llvm/include/llvm/ADT/Optional.h

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,12 @@ class OptionalStorage {
7070
constexpr OptionalStorage() noexcept : empty() {}
7171

7272
constexpr OptionalStorage(OptionalStorage const &other) : OptionalStorage() {
73-
if (other.hasValue()) {
73+
if (other.has_value()) {
7474
emplace(other.val);
7575
}
7676
}
7777
constexpr OptionalStorage(OptionalStorage &&other) : OptionalStorage() {
78-
if (other.hasValue()) {
78+
if (other.has_value()) {
7979
emplace(std::move(other.val));
8080
}
8181
}
@@ -126,7 +126,7 @@ class OptionalStorage {
126126
}
127127

128128
OptionalStorage &operator=(T const &y) {
129-
if (hasValue()) {
129+
if (has_value()) {
130130
val = y;
131131
} else {
132132
::new ((void *)std::addressof(val)) T(y);
@@ -135,7 +135,7 @@ class OptionalStorage {
135135
return *this;
136136
}
137137
OptionalStorage &operator=(T &&y) {
138-
if (hasValue()) {
138+
if (has_value()) {
139139
val = std::move(y);
140140
} else {
141141
::new ((void *)std::addressof(val)) T(std::move(y));
@@ -145,8 +145,8 @@ class OptionalStorage {
145145
}
146146

147147
OptionalStorage &operator=(OptionalStorage const &other) {
148-
if (other.hasValue()) {
149-
if (hasValue()) {
148+
if (other.has_value()) {
149+
if (has_value()) {
150150
val = other.val;
151151
} else {
152152
::new ((void *)std::addressof(val)) T(other.val);
@@ -159,8 +159,8 @@ class OptionalStorage {
159159
}
160160

161161
OptionalStorage &operator=(OptionalStorage &&other) {
162-
if (other.hasValue()) {
163-
if (hasValue()) {
162+
if (other.has_value()) {
163+
if (has_value()) {
164164
val = std::move(other.val);
165165
} else {
166166
::new ((void *)std::addressof(val)) T(std::move(other.val));
@@ -237,7 +237,7 @@ template <typename T> class OptionalStorage<T, true> {
237237
}
238238

239239
OptionalStorage &operator=(T const &y) {
240-
if (hasValue()) {
240+
if (has_value()) {
241241
val = y;
242242
} else {
243243
::new ((void *)std::addressof(val)) T(y);
@@ -246,7 +246,7 @@ template <typename T> class OptionalStorage<T, true> {
246246
return *this;
247247
}
248248
OptionalStorage &operator=(T &&y) {
249-
if (hasValue()) {
249+
if (has_value()) {
250250
val = std::move(y);
251251
} else {
252252
::new ((void *)std::addressof(val)) T(std::move(y));
@@ -307,19 +307,19 @@ template <typename T> class Optional {
307307
T &value() & { return Storage.getValue(); }
308308
T &getValue() & { return Storage.getValue(); }
309309

310-
constexpr explicit operator bool() const { return hasValue(); }
311-
constexpr bool has_value() const { return Storage.hasValue(); }
312-
constexpr bool hasValue() const { return Storage.hasValue(); }
310+
constexpr explicit operator bool() const { return has_value(); }
311+
constexpr bool has_value() const { return Storage.has_value(); }
312+
constexpr bool hasValue() const { return Storage.has_value(); }
313313
constexpr const T *operator->() const { return getPointer(); }
314314
T *operator->() { return getPointer(); }
315315
constexpr const T &operator*() const & { return getValue(); }
316316
T &operator*() & { return getValue(); }
317317

318318
template <typename U> constexpr T value_or(U &&alt) const & {
319-
return hasValue() ? getValue() : std::forward<U>(alt);
319+
return has_value() ? getValue() : std::forward<U>(alt);
320320
}
321321
template <typename U> constexpr T getValueOr(U &&alt) const & {
322-
return hasValue() ? getValue() : std::forward<U>(alt);
322+
return has_value() ? getValue() : std::forward<U>(alt);
323323
}
324324

325325
/// Apply a function to the value if present; otherwise return None.
@@ -335,10 +335,10 @@ template <typename T> class Optional {
335335
T &&operator*() && { return std::move(Storage.getValue()); }
336336

337337
template <typename U> T value_or(U &&alt) && {
338-
return hasValue() ? std::move(getValue()) : std::forward<U>(alt);
338+
return has_value() ? std::move(getValue()) : std::forward<U>(alt);
339339
}
340340
template <typename U> T getValueOr(U &&alt) && {
341-
return hasValue() ? std::move(getValue()) : std::forward<U>(alt);
341+
return has_value() ? std::move(getValue()) : std::forward<U>(alt);
342342
}
343343

344344
/// Apply a function to the value if present; otherwise return None.
@@ -359,7 +359,7 @@ template <typename T, typename U>
359359
constexpr bool operator==(const Optional<T> &X, const Optional<U> &Y) {
360360
if (X && Y)
361361
return *X == *Y;
362-
return X.hasValue() == Y.hasValue();
362+
return X.has_value() == Y.has_value();
363363
}
364364

365365
template <typename T, typename U>
@@ -371,7 +371,7 @@ template <typename T, typename U>
371371
constexpr bool operator<(const Optional<T> &X, const Optional<U> &Y) {
372372
if (X && Y)
373373
return *X < *Y;
374-
return X.hasValue() < Y.hasValue();
374+
return X.has_value() < Y.has_value();
375375
}
376376

377377
template <typename T, typename U>
@@ -414,7 +414,7 @@ template <typename T> constexpr bool operator<(const Optional<T> &, NoneType) {
414414
}
415415

416416
template <typename T> constexpr bool operator<(NoneType, const Optional<T> &X) {
417-
return X.hasValue();
417+
return X.has_value();
418418
}
419419

420420
template <typename T>

0 commit comments

Comments
 (0)