[Support] Add llvm::transformOptional
authorKazu Hirata <kazu@google.com>
Wed, 14 Dec 2022 22:51:05 +0000 (14:51 -0800)
committerKazu Hirata <kazu@google.com>
Wed, 14 Dec 2022 22:51:05 +0000 (14:51 -0800)
commit21a3447564735833909b14b681454997991f35f8
treeb20eb449f57f652eccfd742ac622ef8a673c298e
parent71b3a0d1460ffe618bb38f0992ee970d74c3ec50
[Support] Add llvm::transformOptional

llvm::Optional<T> has transform, which is equivalent to
std::optional<T>::transform.  The problem is that
std::optional<T>::transform won't be available until C++23, implying
that we probably cannot use it in our codebase untli 2028 or so.  We
certainly don't want to keep llvm::Optional just for transform.

This patch adds llvm::transformOptional to STLForwardCompat.h so that
we can use transform during the migration to std::optional and beyond.

I've shamelessly borrowed the implementation and test from
llvm/include/llvm/ADT/Optional.h and
llvm/unittests/ADT/OptionalTest.cpp respectively.

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/D139779
llvm/include/llvm/ADT/STLForwardCompat.h
llvm/unittests/ADT/STLForwardCompatTest.cpp