Skip to content

Commit bb7940e

Browse files
committed
[llvm] Make llvm::Any similar to std::any
This facilitates replacing llvm::Any with std::any. - Deprecate any_isa in favor of using any_cast(Any*) and checking for nullptr because C++17 has no any_isa. - Remove the assert from any_cast(Any*), so it returns nullptr if the type is not correct. This aligns it with std::any_cast(any*). Use any_cast(Any*) throughout LLVM instead of checks with any_isa. This is the first part outlined in https://discourse.llvm.org/t/rfc-switching-from-llvm-any-to-std-any/67176 Differential Revision: https://reviews.llvm.org/D139973
1 parent 741396a commit bb7940e

File tree

12 files changed

+165
-174
lines changed

12 files changed

+165
-174
lines changed

clang/unittests/Tooling/RefactoringTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1288,7 +1288,7 @@ TEST_F(AtomicChangeTest, InsertAfterWithInvalidLocation) {
12881288
TEST_F(AtomicChangeTest, Metadata) {
12891289
AtomicChange Change(Context.Sources, DefaultLoc, 17);
12901290
const llvm::Any &Metadata = Change.getMetadata();
1291-
ASSERT_TRUE(llvm::any_isa<int>(Metadata));
1291+
ASSERT_TRUE(llvm::any_cast<int>(&Metadata));
12921292
EXPECT_EQ(llvm::any_cast<int>(Metadata), 17);
12931293
}
12941294

clang/unittests/Tooling/TransformerTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,7 @@ TEST_F(TransformerTest, WithMetadata) {
806806
"clang-tool", std::make_shared<PCHContainerOperations>(), {}));
807807
ASSERT_EQ(Changes.size(), 1u);
808808
const llvm::Any &Metadata = Changes[0].getMetadata();
809-
ASSERT_TRUE(llvm::any_isa<int>(Metadata));
809+
ASSERT_TRUE(llvm::any_cast<int>(&Metadata));
810810
EXPECT_THAT(llvm::any_cast<int>(Metadata), 5);
811811
}
812812

lldb/include/lldb/Core/RichManglingContext.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ class RichManglingContext {
8787
/// can't access CPlusPlusLanguage::MethodName from within the header.
8888
template <class ParserT> static ParserT *get(llvm::Any parser) {
8989
assert(parser.has_value());
90-
assert(llvm::any_isa<ParserT *>(parser));
91-
return llvm::any_cast<ParserT *>(parser);
90+
assert(llvm::any_cast<ParserT *>(&parser));
91+
return *llvm::any_cast<ParserT *>(&parser);
9292
}
9393
};
9494

llvm/include/llvm/ADT/Any.h

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@ class LLVM_EXTERNAL_VISIBILITY Any {
107107
void reset() { Storage.reset(); }
108108

109109
private:
110+
// Only used for the internal llvm::Any implementation
111+
template <typename T> bool isa() const {
112+
if (!Storage)
113+
return false;
114+
return Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
115+
}
116+
110117
template <class T> friend T any_cast(const Any &Value);
111118
template <class T> friend T any_cast(Any &Value);
112119
template <class T> friend T any_cast(Any &&Value);
@@ -119,36 +126,37 @@ class LLVM_EXTERNAL_VISIBILITY Any {
119126

120127
template <typename T> char Any::TypeId<T>::Id = 0;
121128

122-
template <typename T> bool any_isa(const Any &Value) {
123-
if (!Value.Storage)
124-
return false;
125-
return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id;
129+
template <typename T>
130+
LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast")
131+
bool any_isa(const Any &Value) {
132+
return Value.isa<T>();
126133
}
127134

128135
template <class T> T any_cast(const Any &Value) {
136+
assert(Value.isa<T>() && "Bad any cast!");
129137
return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
130138
}
131139

132140
template <class T> T any_cast(Any &Value) {
141+
assert(Value.isa<T>() && "Bad any cast!");
133142
return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value));
134143
}
135144

136145
template <class T> T any_cast(Any &&Value) {
146+
assert(Value.isa<T>() && "Bad any cast!");
137147
return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value)));
138148
}
139149

140150
template <class T> const T *any_cast(const Any *Value) {
141151
using U = remove_cvref_t<T>;
142-
assert(Value && any_isa<T>(*Value) && "Bad any cast!");
143-
if (!Value || !any_isa<U>(*Value))
152+
if (!Value || !Value->isa<U>())
144153
return nullptr;
145154
return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
146155
}
147156

148157
template <class T> T *any_cast(Any *Value) {
149158
using U = std::decay_t<T>;
150-
assert(Value && any_isa<U>(*Value) && "Bad any cast!");
151-
if (!Value || !any_isa<U>(*Value))
159+
if (!Value || !Value->isa<U>())
152160
return nullptr;
153161
return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value;
154162
}

llvm/lib/CodeGen/MachinePassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Error MachineFunctionPassManager::run(Module &M,
4141
// current pipeline is the top-level pipeline. Callbacks are not used after
4242
// current pipeline.
4343
PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) {
44-
assert(any_isa<const MachineFunction *>(IR));
44+
assert(any_cast<const MachineFunction *>(&IR));
4545
const MachineFunction *MF = any_cast<const MachineFunction *>(IR);
4646
assert(MF && "Machine function should be valid for printing");
4747
std::string Banner = std::string("After ") + std::string(PassID);

llvm/lib/IR/LLVMContextImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#include "ConstantsContext.h"
1818
#include "llvm/ADT/APFloat.h"
1919
#include "llvm/ADT/APInt.h"
20-
#include "llvm/ADT/Any.h"
2120
#include "llvm/ADT/ArrayRef.h"
2221
#include "llvm/ADT/DenseMap.h"
2322
#include "llvm/ADT/DenseMapInfo.h"

0 commit comments

Comments
 (0)