This revision was landed with ongoing or failed builds. This revision was automatically updated to reflect the committed changes. Closed by commit rGbb7940e25f6c: [llvm] Make llvm::Any similar to std::any (authored by sebastian-ne).
Repository: rG LLVM Github Monorepo CHANGES SINCE LAST ACTION https://reviews.llvm.org/D139973/new/ https://reviews.llvm.org/D139973 Files: clang/unittests/Tooling/RefactoringTest.cpp clang/unittests/Tooling/TransformerTest.cpp lldb/include/lldb/Core/RichManglingContext.h llvm/include/llvm/ADT/Any.h llvm/lib/CodeGen/MachinePassManager.cpp llvm/lib/IR/LLVMContextImpl.h llvm/lib/Passes/StandardInstrumentations.cpp llvm/lib/Transforms/IPO/SampleProfileProbe.cpp llvm/lib/Transforms/Scalar/LoopPassManager.cpp llvm/lib/Transforms/Utils/Debugify.cpp llvm/unittests/ADT/AnyTest.cpp llvm/unittests/IR/PassBuilderCallbacksTest.cpp
Index: llvm/unittests/IR/PassBuilderCallbacksTest.cpp =================================================================== --- llvm/unittests/IR/PassBuilderCallbacksTest.cpp +++ llvm/unittests/IR/PassBuilderCallbacksTest.cpp @@ -290,17 +290,17 @@ return std::string(name); } -template <> std::string getName(const llvm::Any &WrappedIR) { - if (any_isa<const Module *>(WrappedIR)) - return any_cast<const Module *>(WrappedIR)->getName().str(); - if (any_isa<const Function *>(WrappedIR)) - return any_cast<const Function *>(WrappedIR)->getName().str(); - if (any_isa<const Loop *>(WrappedIR)) - return any_cast<const Loop *>(WrappedIR)->getName().str(); - if (any_isa<const LoopNest *>(WrappedIR)) - return any_cast<const LoopNest *>(WrappedIR)->getName().str(); - if (any_isa<const LazyCallGraph::SCC *>(WrappedIR)) - return any_cast<const LazyCallGraph::SCC *>(WrappedIR)->getName(); +template <> std::string getName(const Any &WrappedIR) { + if (const auto *const *M = any_cast<const Module *>(&WrappedIR)) + return (*M)->getName().str(); + if (const auto *const *F = any_cast<const Function *>(&WrappedIR)) + return (*F)->getName().str(); + if (const auto *const *L = any_cast<const Loop *>(&WrappedIR)) + return (*L)->getName().str(); + if (const auto *const *L = any_cast<const LoopNest *>(&WrappedIR)) + return (*L)->getName().str(); + if (const auto *const *C = any_cast<const LazyCallGraph::SCC *>(&WrappedIR)) + return (*C)->getName(); return "<UNKNOWN>"; } /// Define a custom matcher for objects which support a 'getName' method. Index: llvm/unittests/ADT/AnyTest.cpp =================================================================== --- llvm/unittests/ADT/AnyTest.cpp +++ llvm/unittests/ADT/AnyTest.cpp @@ -24,55 +24,55 @@ // An empty Any is not anything. EXPECT_FALSE(A.has_value()); - EXPECT_FALSE(any_isa<int>(A)); + EXPECT_FALSE(any_cast<int>(&A)); // An int is an int but not something else. EXPECT_TRUE(B.has_value()); - EXPECT_TRUE(any_isa<int>(B)); - EXPECT_FALSE(any_isa<float>(B)); + EXPECT_TRUE(any_cast<int>(&B)); + EXPECT_FALSE(any_cast<float>(&B)); EXPECT_TRUE(C.has_value()); - EXPECT_TRUE(any_isa<int>(C)); + EXPECT_TRUE(any_cast<int>(&C)); // A const char * is a const char * but not an int. EXPECT_TRUE(D.has_value()); - EXPECT_TRUE(any_isa<const char *>(D)); - EXPECT_FALSE(any_isa<int>(D)); + EXPECT_TRUE(any_cast<const char *>(&D)); + EXPECT_FALSE(any_cast<int>(&D)); // A double is a double but not a float. EXPECT_TRUE(E.has_value()); - EXPECT_TRUE(any_isa<double>(E)); - EXPECT_FALSE(any_isa<float>(E)); + EXPECT_TRUE(any_cast<double>(&E)); + EXPECT_FALSE(any_cast<float>(&E)); // After copy constructing from an int, the new item and old item are both // ints. llvm::Any F(B); EXPECT_TRUE(B.has_value()); EXPECT_TRUE(F.has_value()); - EXPECT_TRUE(any_isa<int>(F)); - EXPECT_TRUE(any_isa<int>(B)); + EXPECT_TRUE(any_cast<int>(&F)); + EXPECT_TRUE(any_cast<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.has_value()); EXPECT_TRUE(G.has_value()); - EXPECT_TRUE(any_isa<int>(G)); - EXPECT_FALSE(any_isa<int>(C)); + EXPECT_TRUE(any_cast<int>(&G)); + EXPECT_FALSE(any_cast<int>(&C)); // After copy-assigning from an int, the new item and old item are both ints. A = F; EXPECT_TRUE(A.has_value()); EXPECT_TRUE(F.has_value()); - EXPECT_TRUE(any_isa<int>(A)); - EXPECT_TRUE(any_isa<int>(F)); + EXPECT_TRUE(any_cast<int>(&A)); + EXPECT_TRUE(any_cast<int>(&F)); // After move-assigning from an int, the new item and old item are both ints. B = std::move(G); EXPECT_TRUE(B.has_value()); EXPECT_FALSE(G.has_value()); - EXPECT_TRUE(any_isa<int>(B)); - EXPECT_FALSE(any_isa<int>(G)); + EXPECT_TRUE(any_cast<int>(&B)); + EXPECT_FALSE(any_cast<int>(&G)); } TEST(AnyTest, GoodAnyCast) { Index: llvm/lib/Transforms/Utils/Debugify.cpp =================================================================== --- llvm/lib/Transforms/Utils/Debugify.cpp +++ llvm/lib/Transforms/Utils/Debugify.cpp @@ -1031,19 +1031,19 @@ PIC.registerBeforeNonSkippedPassCallback([this](StringRef P, Any IR) { if (isIgnoredPass(P)) return; - if (any_isa<const Function *>(IR)) - applyDebugify(*const_cast<Function *>(any_cast<const Function *>(IR)), + if (const auto **F = any_cast<const Function *>(&IR)) + applyDebugify(*const_cast<Function *>(*F), Mode, DebugInfoBeforePass, P); - else if (any_isa<const Module *>(IR)) - applyDebugify(*const_cast<Module *>(any_cast<const Module *>(IR)), + else if (const auto **M = any_cast<const Module *>(&IR)) + applyDebugify(*const_cast<Module *>(*M), Mode, DebugInfoBeforePass, P); }); PIC.registerAfterPassCallback([this](StringRef P, Any IR, const PreservedAnalyses &PassPA) { if (isIgnoredPass(P)) return; - if (any_isa<const Function *>(IR)) { - auto &F = *const_cast<Function *>(any_cast<const Function *>(IR)); + if (const auto **CF = any_cast<const Function *>(&IR)) { + auto &F = *const_cast<Function *>(*CF); Module &M = *F.getParent(); auto It = F.getIterator(); if (Mode == DebugifyMode::SyntheticDebugInfo) @@ -1054,8 +1054,8 @@ M, make_range(It, std::next(It)), *DebugInfoBeforePass, "CheckModuleDebugify (original debuginfo)", P, OrigDIVerifyBugsReportFilePath); - } else if (any_isa<const Module *>(IR)) { - auto &M = *const_cast<Module *>(any_cast<const Module *>(IR)); + } else if (const auto **CM = any_cast<const Module *>(&IR)) { + auto &M = *const_cast<Module *>(*CM); if (Mode == DebugifyMode::SyntheticDebugInfo) checkDebugifyMetadata(M, M.functions(), P, "CheckModuleDebugify", /*Strip=*/true, DIStatsMap); Index: llvm/lib/Transforms/Scalar/LoopPassManager.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopPassManager.cpp +++ llvm/lib/Transforms/Scalar/LoopPassManager.cpp @@ -269,10 +269,11 @@ PI.pushBeforeNonSkippedPassCallback([&LAR, &LI](StringRef PassID, Any IR) { if (isSpecialPass(PassID, {"PassManager"})) return; - assert(any_isa<const Loop *>(IR) || any_isa<const LoopNest *>(IR)); - const Loop *L = any_isa<const Loop *>(IR) - ? any_cast<const Loop *>(IR) - : &any_cast<const LoopNest *>(IR)->getOutermostLoop(); + assert(any_cast<const Loop *>(&IR) || any_cast<const LoopNest *>(&IR)); + const Loop **LPtr = any_cast<const Loop *>(&IR); + const Loop *L = LPtr ? *LPtr : nullptr; + if (!L) + L = &any_cast<const LoopNest *>(IR)->getOutermostLoop(); assert(L && "Loop should be valid for printing"); // Verify the loop structure and LCSSA form before visiting the loop. Index: llvm/lib/Transforms/IPO/SampleProfileProbe.cpp =================================================================== --- llvm/lib/Transforms/IPO/SampleProfileProbe.cpp +++ llvm/lib/Transforms/IPO/SampleProfileProbe.cpp @@ -98,14 +98,14 @@ std::string Banner = "\n*** Pseudo Probe Verification After " + PassID.str() + " ***\n"; dbgs() << Banner; - if (any_isa<const Module *>(IR)) - runAfterPass(any_cast<const Module *>(IR)); - else if (any_isa<const Function *>(IR)) - runAfterPass(any_cast<const Function *>(IR)); - else if (any_isa<const LazyCallGraph::SCC *>(IR)) - runAfterPass(any_cast<const LazyCallGraph::SCC *>(IR)); - else if (any_isa<const Loop *>(IR)) - runAfterPass(any_cast<const Loop *>(IR)); + if (const auto **M = any_cast<const Module *>(&IR)) + runAfterPass(*M); + else if (const auto **F = any_cast<const Function *>(&IR)) + runAfterPass(*F); + else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) + runAfterPass(*C); + else if (const auto **L = any_cast<const Loop *>(&IR)) + runAfterPass(*L); else llvm_unreachable("Unknown IR unit"); } Index: llvm/lib/Passes/StandardInstrumentations.cpp =================================================================== --- llvm/lib/Passes/StandardInstrumentations.cpp +++ llvm/lib/Passes/StandardInstrumentations.cpp @@ -123,20 +123,18 @@ /// Extract Module out of \p IR unit. May return nullptr if \p IR does not match /// certain global filters. Will never return nullptr if \p Force is true. const Module *unwrapModule(Any IR, bool Force = false) { - if (any_isa<const Module *>(IR)) - return any_cast<const Module *>(IR); + if (const auto **M = any_cast<const Module *>(&IR)) + return *M; - if (any_isa<const Function *>(IR)) { - const Function *F = any_cast<const Function *>(IR); - if (!Force && !isFunctionInPrintList(F->getName())) + if (const auto **F = any_cast<const Function *>(&IR)) { + if (!Force && !isFunctionInPrintList((*F)->getName())) return nullptr; - return F->getParent(); + return (*F)->getParent(); } - if (any_isa<const LazyCallGraph::SCC *>(IR)) { - const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR); - for (const LazyCallGraph::Node &N : *C) { + if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) { + for (const LazyCallGraph::Node &N : **C) { const Function &F = N.getFunction(); if (Force || (!F.isDeclaration() && isFunctionInPrintList(F.getName()))) { return F.getParent(); @@ -146,9 +144,8 @@ return nullptr; } - if (any_isa<const Loop *>(IR)) { - const Loop *L = any_cast<const Loop *>(IR); - const Function *F = L->getHeader()->getParent(); + if (const auto **L = any_cast<const Loop *>(&IR)) { + const Function *F = (*L)->getHeader()->getParent(); if (!Force && !isFunctionInPrintList(F->getName())) return nullptr; return F->getParent(); @@ -190,23 +187,17 @@ } std::string getIRName(Any IR) { - if (any_isa<const Module *>(IR)) + if (any_cast<const Module *>(&IR)) return "[module]"; - if (any_isa<const Function *>(IR)) { - const Function *F = any_cast<const Function *>(IR); - return F->getName().str(); - } + if (const auto **F = any_cast<const Function *>(&IR)) + return (*F)->getName().str(); - if (any_isa<const LazyCallGraph::SCC *>(IR)) { - const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR); - return C->getName(); - } + if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) + return (*C)->getName(); - if (any_isa<const Loop *>(IR)) { - const Loop *L = any_cast<const Loop *>(IR); - return L->getName().str(); - } + if (const auto **L = any_cast<const Loop *>(&IR)) + return (*L)->getName().str(); llvm_unreachable("Unknown wrapped IR type"); } @@ -228,30 +219,22 @@ } bool shouldPrintIR(Any IR) { - if (any_isa<const Module *>(IR)) { - const Module *M = any_cast<const Module *>(IR); - return moduleContainsFilterPrintFunc(*M); - } + if (const auto **M = any_cast<const Module *>(&IR)) + return moduleContainsFilterPrintFunc(**M); - if (any_isa<const Function *>(IR)) { - const Function *F = any_cast<const Function *>(IR); - return isFunctionInPrintList(F->getName()); - } + if (const auto **F = any_cast<const Function *>(&IR)) + return isFunctionInPrintList((*F)->getName()); - if (any_isa<const LazyCallGraph::SCC *>(IR)) { - const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR); - return sccContainsFilterPrintFunc(*C); - } + if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) + return sccContainsFilterPrintFunc(**C); - if (any_isa<const Loop *>(IR)) { - const Loop *L = any_cast<const Loop *>(IR); - return isFunctionInPrintList(L->getHeader()->getParent()->getName()); - } + if (const auto **L = any_cast<const Loop *>(&IR)) + return isFunctionInPrintList((*L)->getHeader()->getParent()->getName()); llvm_unreachable("Unknown wrapped IR type"); } /// Generic IR-printing helper that unpacks a pointer to IRUnit wrapped into -/// llvm::Any and does actual print job. +/// Any and does actual print job. void unwrapAndPrint(raw_ostream &OS, Any IR) { if (!shouldPrintIR(IR)) return; @@ -263,27 +246,23 @@ return; } - if (any_isa<const Module *>(IR)) { - const Module *M = any_cast<const Module *>(IR); - printIR(OS, M); + if (const auto **M = any_cast<const Module *>(&IR)) { + printIR(OS, *M); return; } - if (any_isa<const Function *>(IR)) { - const Function *F = any_cast<const Function *>(IR); - printIR(OS, F); + if (const auto **F = any_cast<const Function *>(&IR)) { + printIR(OS, *F); return; } - if (any_isa<const LazyCallGraph::SCC *>(IR)) { - const LazyCallGraph::SCC *C = any_cast<const LazyCallGraph::SCC *>(IR); - printIR(OS, C); + if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) { + printIR(OS, *C); return; } - if (any_isa<const Loop *>(IR)) { - const Loop *L = any_cast<const Loop *>(IR); - printIR(OS, L); + if (const auto **L = any_cast<const Loop *>(&IR)) { + printIR(OS, *L); return; } llvm_unreachable("Unknown wrapped IR type"); @@ -313,10 +292,10 @@ // Return the module when that is the appropriate level of comparison for \p IR. const Module *getModuleForComparison(Any IR) { - if (any_isa<const Module *>(IR)) - return any_cast<const Module *>(IR); - if (any_isa<const LazyCallGraph::SCC *>(IR)) - return any_cast<const LazyCallGraph::SCC *>(IR) + if (const auto **M = any_cast<const Module *>(&IR)) + return *M; + if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) + return (*C) ->begin() ->getFunction() .getParent(); @@ -332,8 +311,8 @@ bool isInteresting(Any IR, StringRef PassID, StringRef PassName) { if (isIgnored(PassID) || !isPassInPrintList(PassName)) return false; - if (any_isa<const Function *>(IR)) - return isInterestingFunction(*any_cast<const Function *>(IR)); + if (const auto **F = any_cast<const Function *>(&IR)) + return isInterestingFunction(**F); return true; } @@ -655,13 +634,12 @@ return; } - const Function *F = nullptr; - if (any_isa<const Function *>(IR)) - F = any_cast<const Function *>(IR); - else { - assert(any_isa<const Loop *>(IR) && "Unknown IR unit."); - const Loop *L = any_cast<const Loop *>(IR); - F = L->getHeader()->getParent(); + const Function **FPtr = any_cast<const Function *>(&IR); + const Function *F = FPtr ? *FPtr : nullptr; + if (!F) { + const Loop **L = any_cast<const Loop *>(&IR); + assert(L && "Unknown IR unit."); + F = (*L)->getHeader()->getParent(); } assert(F && "Unknown IR unit."); generateFunctionData(Data, *F); @@ -816,11 +794,11 @@ } bool OptNoneInstrumentation::shouldRun(StringRef PassID, Any IR) { - const Function *F = nullptr; - if (any_isa<const Function *>(IR)) { - F = any_cast<const Function *>(IR); - } else if (any_isa<const Loop *>(IR)) { - F = any_cast<const Loop *>(IR)->getHeader()->getParent(); + const Function **FPtr = any_cast<const Function *>(&IR); + const Function *F = FPtr ? *FPtr : nullptr; + if (!F) { + if (const auto **L = any_cast<const Loop *>(&IR)) + F = (*L)->getHeader()->getParent(); } bool ShouldRun = !(F && F->hasOptNone()); if (!ShouldRun && DebugLogging) { @@ -895,14 +873,14 @@ auto &OS = print(); OS << "Running pass: " << PassID << " on " << getIRName(IR); - if (any_isa<const Function *>(IR)) { - unsigned Count = any_cast<const Function *>(IR)->getInstructionCount(); + if (const auto **F = any_cast<const Function *>(&IR)) { + unsigned Count = (*F)->getInstructionCount(); OS << " (" << Count << " instruction"; if (Count != 1) OS << 's'; OS << ')'; - } else if (any_isa<const LazyCallGraph::SCC *>(IR)) { - int Count = any_cast<const LazyCallGraph::SCC *>(IR)->size(); + } else if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) { + int Count = (*C)->size(); OS << " (" << Count << " node"; if (Count != 1) OS << 's'; @@ -1100,18 +1078,19 @@ report_fatal_error(Twine("CFG unexpectedly changed by ", Pass)); }; - PIC.registerBeforeNonSkippedPassCallback([this, &FAM](StringRef P, Any IR) { + PIC.registerBeforeNonSkippedPassCallback( + [this, &FAM](StringRef P, Any IR) { #ifdef LLVM_ENABLE_ABI_BREAKING_CHECKS - assert(&PassStack.emplace_back(P)); + assert(&PassStack.emplace_back(P)); #endif - (void)this; - if (!any_isa<const Function *>(IR)) - return; + (void)this; + const auto **F = any_cast<const Function *>(&IR); + if (!F) + return; - const auto *F = any_cast<const Function *>(IR); - // Make sure a fresh CFG snapshot is available before the pass. - FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(F)); - }); + // Make sure a fresh CFG snapshot is available before the pass. + FAM.getResult<PreservedCFGCheckerAnalysis>(*const_cast<Function *>(*F)); + }); PIC.registerAfterPassInvalidatedCallback( [this](StringRef P, const PreservedAnalyses &PassPA) { @@ -1131,18 +1110,18 @@ #endif (void)this; - if (!any_isa<const Function *>(IR)) + const auto **F = any_cast<const Function *>(&IR); + if (!F) return; if (!PassPA.allAnalysesInSetPreserved<CFGAnalyses>() && !PassPA.allAnalysesInSetPreserved<AllAnalysesOn<Function>>()) return; - const auto *F = any_cast<const Function *>(IR); if (auto *GraphBefore = FAM.getCachedResult<PreservedCFGCheckerAnalysis>( - *const_cast<Function *>(F))) - checkCFG(P, F->getName(), *GraphBefore, - CFG(F, /* TrackBBLifetime */ false)); + *const_cast<Function *>(*F))) + checkCFG(P, (*F)->getName(), *GraphBefore, + CFG(*F, /* TrackBBLifetime */ false)); }); } @@ -1152,46 +1131,50 @@ [this](StringRef P, Any IR, const PreservedAnalyses &PassPA) { if (isIgnored(P) || P == "VerifierPass") return; - if (any_isa<const Function *>(IR) || any_isa<const Loop *>(IR)) { - const Function *F; - if (any_isa<const Loop *>(IR)) - F = any_cast<const Loop *>(IR)->getHeader()->getParent(); - else - F = any_cast<const Function *>(IR); + const Function **FPtr = any_cast<const Function *>(&IR); + const Function *F = FPtr ? *FPtr : nullptr; + if (!F) { + if (const auto **L = any_cast<const Loop *>(&IR)) + F = (*L)->getHeader()->getParent(); + } + + if (F) { if (DebugLogging) dbgs() << "Verifying function " << F->getName() << "\n"; if (verifyFunction(*F, &errs())) report_fatal_error("Broken function found, compilation aborted!"); - } else if (any_isa<const Module *>(IR) || - any_isa<const LazyCallGraph::SCC *>(IR)) { - const Module *M; - if (any_isa<const LazyCallGraph::SCC *>(IR)) - M = any_cast<const LazyCallGraph::SCC *>(IR) - ->begin() - ->getFunction() - .getParent(); - else - M = any_cast<const Module *>(IR); - if (DebugLogging) - dbgs() << "Verifying module " << M->getName() << "\n"; - - if (verifyModule(*M, &errs())) - report_fatal_error("Broken module found, compilation aborted!"); + } else { + const Module **MPtr = any_cast<const Module *>(&IR); + const Module *M = MPtr ? *MPtr : nullptr; + if (!M) { + if (const auto **C = any_cast<const LazyCallGraph::SCC *>(&IR)) + M = (*C)->begin()->getFunction().getParent(); + } + + if (M) { + if (DebugLogging) + dbgs() << "Verifying module " << M->getName() << "\n"; + + if (verifyModule(*M, &errs())) + report_fatal_error("Broken module found, compilation aborted!"); + } } }); } InLineChangePrinter::~InLineChangePrinter() = default; -void InLineChangePrinter::generateIRRepresentation(Any IR, StringRef PassID, +void InLineChangePrinter::generateIRRepresentation(Any IR, + StringRef PassID, IRDataT<EmptyData> &D) { IRComparer<EmptyData>::analyzeIR(IR, D); } void InLineChangePrinter::handleAfter(StringRef PassID, std::string &Name, const IRDataT<EmptyData> &Before, - const IRDataT<EmptyData> &After, Any IR) { + const IRDataT<EmptyData> &After, + Any IR) { SmallString<20> Banner = formatv("*** IR Dump After {0} on {1} ***\n", PassID, Name); Out << Banner; Index: llvm/lib/IR/LLVMContextImpl.h =================================================================== --- llvm/lib/IR/LLVMContextImpl.h +++ llvm/lib/IR/LLVMContextImpl.h @@ -17,7 +17,6 @@ #include "ConstantsContext.h" #include "llvm/ADT/APFloat.h" #include "llvm/ADT/APInt.h" -#include "llvm/ADT/Any.h" #include "llvm/ADT/ArrayRef.h" #include "llvm/ADT/DenseMap.h" #include "llvm/ADT/DenseMapInfo.h" Index: llvm/lib/CodeGen/MachinePassManager.cpp =================================================================== --- llvm/lib/CodeGen/MachinePassManager.cpp +++ llvm/lib/CodeGen/MachinePassManager.cpp @@ -41,7 +41,7 @@ // current pipeline is the top-level pipeline. Callbacks are not used after // current pipeline. PI.pushBeforeNonSkippedPassCallback([&MFAM](StringRef PassID, Any IR) { - assert(any_isa<const MachineFunction *>(IR)); + assert(any_cast<const MachineFunction *>(&IR)); const MachineFunction *MF = any_cast<const MachineFunction *>(IR); assert(MF && "Machine function should be valid for printing"); std::string Banner = std::string("After ") + std::string(PassID); Index: llvm/include/llvm/ADT/Any.h =================================================================== --- llvm/include/llvm/ADT/Any.h +++ llvm/include/llvm/ADT/Any.h @@ -107,6 +107,13 @@ void reset() { Storage.reset(); } private: + // Only used for the internal llvm::Any implementation + template <typename T> bool isa() const { + if (!Storage) + return false; + return Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id; + } + template <class T> friend T any_cast(const Any &Value); template <class T> friend T any_cast(Any &Value); template <class T> friend T any_cast(Any &&Value); @@ -119,36 +126,37 @@ template <typename T> char Any::TypeId<T>::Id = 0; -template <typename T> bool any_isa(const Any &Value) { - if (!Value.Storage) - return false; - return Value.Storage->id() == &Any::TypeId<remove_cvref_t<T>>::Id; +template <typename T> +LLVM_DEPRECATED("Use any_cast(Any*) != nullptr instead", "any_cast") +bool any_isa(const Any &Value) { + return Value.isa<T>(); } template <class T> T any_cast(const Any &Value) { + assert(Value.isa<T>() && "Bad any cast!"); return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value)); } template <class T> T any_cast(Any &Value) { + assert(Value.isa<T>() && "Bad any cast!"); return static_cast<T>(*any_cast<remove_cvref_t<T>>(&Value)); } template <class T> T any_cast(Any &&Value) { + assert(Value.isa<T>() && "Bad any cast!"); return static_cast<T>(std::move(*any_cast<remove_cvref_t<T>>(&Value))); } template <class T> const T *any_cast(const Any *Value) { using U = remove_cvref_t<T>; - assert(Value && any_isa<T>(*Value) && "Bad any cast!"); - if (!Value || !any_isa<U>(*Value)) + if (!Value || !Value->isa<U>()) return nullptr; return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value; } template <class T> T *any_cast(Any *Value) { using U = std::decay_t<T>; - assert(Value && any_isa<U>(*Value) && "Bad any cast!"); - if (!Value || !any_isa<U>(*Value)) + if (!Value || !Value->isa<U>()) return nullptr; return &static_cast<Any::StorageImpl<U> &>(*Value->Storage).Value; } Index: lldb/include/lldb/Core/RichManglingContext.h =================================================================== --- lldb/include/lldb/Core/RichManglingContext.h +++ lldb/include/lldb/Core/RichManglingContext.h @@ -87,8 +87,8 @@ /// can't access CPlusPlusLanguage::MethodName from within the header. template <class ParserT> static ParserT *get(llvm::Any parser) { assert(parser.has_value()); - assert(llvm::any_isa<ParserT *>(parser)); - return llvm::any_cast<ParserT *>(parser); + assert(llvm::any_cast<ParserT *>(&parser)); + return *llvm::any_cast<ParserT *>(&parser); } }; Index: clang/unittests/Tooling/TransformerTest.cpp =================================================================== --- clang/unittests/Tooling/TransformerTest.cpp +++ clang/unittests/Tooling/TransformerTest.cpp @@ -806,7 +806,7 @@ "clang-tool", std::make_shared<PCHContainerOperations>(), {})); ASSERT_EQ(Changes.size(), 1u); const llvm::Any &Metadata = Changes[0].getMetadata(); - ASSERT_TRUE(llvm::any_isa<int>(Metadata)); + ASSERT_TRUE(llvm::any_cast<int>(&Metadata)); EXPECT_THAT(llvm::any_cast<int>(Metadata), 5); } Index: clang/unittests/Tooling/RefactoringTest.cpp =================================================================== --- clang/unittests/Tooling/RefactoringTest.cpp +++ clang/unittests/Tooling/RefactoringTest.cpp @@ -1288,7 +1288,7 @@ TEST_F(AtomicChangeTest, Metadata) { AtomicChange Change(Context.Sources, DefaultLoc, 17); const llvm::Any &Metadata = Change.getMetadata(); - ASSERT_TRUE(llvm::any_isa<int>(Metadata)); + ASSERT_TRUE(llvm::any_cast<int>(&Metadata)); EXPECT_EQ(llvm::any_cast<int>(Metadata), 17); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits