[llvm] e15359d - [ADT] Implement Any::has_value
Kazu Hirata via llvm-commits
llvm-commits at lists.llvm.org
Sat Aug 20 07:28:22 PDT 2022
Author: Kazu Hirata
Date: 2022-08-20T07:28:04-07:00
New Revision: e15359debf39afc7dd41a37cbb71b4d6b7a2bc7b
URL: https://github.com/llvm/llvm-project/commit/e15359debf39afc7dd41a37cbb71b4d6b7a2bc7b
DIFF: https://github.com/llvm/llvm-project/commit/e15359debf39afc7dd41a37cbb71b4d6b7a2bc7b.diff
LOG: [ADT] Implement Any::has_value
This patch implements Any::has_value for consistency with std::any in
C++17.
My plan is to deprecate Any::hasValue after migrating all of its uses
to Any::has_value. Since I am about to do so, this patch simply
replaces hasValue with has_value in the unit test instead of adding
tests for has_value.
Differential Revision: https://reviews.llvm.org/D132278
Added:
Modified:
llvm/include/llvm/ADT/Any.h
llvm/unittests/ADT/AnyTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ADT/Any.h b/llvm/include/llvm/ADT/Any.h
index 649a9986c0a41..457b40bae2002 100644
--- a/llvm/include/llvm/ADT/Any.h
+++ b/llvm/include/llvm/ADT/Any.h
@@ -101,6 +101,7 @@ class LLVM_EXTERNAL_VISIBILITY Any {
}
bool hasValue() const { return !!Storage; }
+ bool has_value() const { return !!Storage; }
void reset() { Storage.reset(); }
diff --git a/llvm/unittests/ADT/AnyTest.cpp b/llvm/unittests/ADT/AnyTest.cpp
index 2ca81bd61d571..312cd745251e7 100644
--- a/llvm/unittests/ADT/AnyTest.cpp
+++ b/llvm/unittests/ADT/AnyTest.cpp
@@ -23,54 +23,54 @@ TEST(AnyTest, ConstructionAndAssignment) {
llvm::Any E{3.7};
// An empty Any is not anything.
- EXPECT_FALSE(A.hasValue());
+ EXPECT_FALSE(A.has_value());
EXPECT_FALSE(any_isa<int>(A));
// An int is an int but not something else.
- EXPECT_TRUE(B.hasValue());
+ EXPECT_TRUE(B.has_value());
EXPECT_TRUE(any_isa<int>(B));
EXPECT_FALSE(any_isa<float>(B));
- EXPECT_TRUE(C.hasValue());
+ EXPECT_TRUE(C.has_value());
EXPECT_TRUE(any_isa<int>(C));
// A const char * is a const char * but not an int.
- EXPECT_TRUE(D.hasValue());
+ EXPECT_TRUE(D.has_value());
EXPECT_TRUE(any_isa<const char *>(D));
EXPECT_FALSE(any_isa<int>(D));
// A double is a double but not a float.
- EXPECT_TRUE(E.hasValue());
+ EXPECT_TRUE(E.has_value());
EXPECT_TRUE(any_isa<double>(E));
EXPECT_FALSE(any_isa<float>(E));
// After copy constructing from an int, the new item and old item are both
// ints.
llvm::Any F(B);
- EXPECT_TRUE(B.hasValue());
- EXPECT_TRUE(F.hasValue());
+ EXPECT_TRUE(B.has_value());
+ EXPECT_TRUE(F.has_value());
EXPECT_TRUE(any_isa<int>(F));
EXPECT_TRUE(any_isa<int>(B));
// After move constructing from an int, the new item is an int and the old one
// isn't.
llvm::Any G(std::move(C));
- EXPECT_FALSE(C.hasValue());
- EXPECT_TRUE(G.hasValue());
+ EXPECT_FALSE(C.has_value());
+ EXPECT_TRUE(G.has_value());
EXPECT_TRUE(any_isa<int>(G));
EXPECT_FALSE(any_isa<int>(C));
// After copy-assigning from an int, the new item and old item are both ints.
A = F;
- EXPECT_TRUE(A.hasValue());
- EXPECT_TRUE(F.hasValue());
+ EXPECT_TRUE(A.has_value());
+ EXPECT_TRUE(F.has_value());
EXPECT_TRUE(any_isa<int>(A));
EXPECT_TRUE(any_isa<int>(F));
// After move-assigning from an int, the new item and old item are both ints.
B = std::move(G);
- EXPECT_TRUE(B.hasValue());
- EXPECT_FALSE(G.hasValue());
+ EXPECT_TRUE(B.has_value());
+ EXPECT_FALSE(G.has_value());
EXPECT_TRUE(any_isa<int>(B));
EXPECT_FALSE(any_isa<int>(G));
}
@@ -111,7 +111,7 @@ TEST(AnyTest, GoodAnyCast) {
// Make sure we can any_cast from an rvalue and that it's properly destroyed
// in the process.
EXPECT_EQ(8, llvm::any_cast<int>(std::move(E)));
- EXPECT_TRUE(E.hasValue());
+ EXPECT_TRUE(E.has_value());
// Make sure moving from pointers gives back pointers, and that we can modify
// the underlying value through those pointers.
More information about the llvm-commits
mailing list