[Lldb-commits] [lldb] [llvm] [NFC] Replace `expectedToStdOptional` with `expectedToOptional` (PR #191359)

Juan Manuel Martinez CaamaƱo via lldb-commits lldb-commits at lists.llvm.org
Mon Apr 13 02:03:44 PDT 2026


https://github.com/jmmartinez updated https://github.com/llvm/llvm-project/pull/191359

>From ce3a423158f91d405b3ab3840617bb57fb2b8b5b Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
 <jmartinezcaamao at gmail.com>
Date: Fri, 10 Apr 2026 10:07:10 +0200
Subject: [PATCH 1/3] [NFC] Replace `expectedToStdOptional` with
 `expectedToOptional`

Both implementations are currently equivalent. This is likely a leftover
from the past, when `llvm::Optional` existed.
---
 llvm/include/llvm/Support/Error.h     | 7 -------
 llvm/tools/llvm-readobj/ELFDumper.cpp | 2 +-
 2 files changed, 1 insertion(+), 8 deletions(-)

diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index c9fd16fdb7c2b..464ca8d66a517 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1098,13 +1098,6 @@ template <typename T> std::optional<T> expectedToOptional(Expected<T> &&E) {
   return std::nullopt;
 }
 
-template <typename T> std::optional<T> expectedToStdOptional(Expected<T> &&E) {
-  if (E)
-    return std::move(*E);
-  consumeError(E.takeError());
-  return std::nullopt;
-}
-
 /// Helper for converting an Error to a bool.
 ///
 /// This method returns true if Err is in an error state, or false if it is
diff --git a/llvm/tools/llvm-readobj/ELFDumper.cpp b/llvm/tools/llvm-readobj/ELFDumper.cpp
index bcb580119fb85..988afcb16148d 100644
--- a/llvm/tools/llvm-readobj/ELFDumper.cpp
+++ b/llvm/tools/llvm-readobj/ELFDumper.cpp
@@ -6361,7 +6361,7 @@ static void processNotesHelper(
     for (const typename ELFT::Shdr &S : Sections) {
       if (S.sh_type != SHT_NOTE)
         continue;
-      StartNotesFn(expectedToStdOptional(Obj.getSectionName(S)), S.sh_offset,
+      StartNotesFn(expectedToOptional(Obj.getSectionName(S)), S.sh_offset,
                    S.sh_size, S.sh_addralign);
       Error Err = Error::success();
       size_t I = 0;

>From 52bfd9de8440fe6161d5ead0ee2416c266febefc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
 <jmartinezcaamao at gmail.com>
Date: Fri, 10 Apr 2026 11:02:38 +0200
Subject: [PATCH 2/3] Missing replacements on LLDB's side

---
 lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp | 4 ++--
 lldb/source/Symbol/Variable.cpp                          | 2 +-
 lldb/source/ValueObject/ValueObject.cpp                  | 2 +-
 3 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
index 624cdeada4eb9..ccb91c682df55 100644
--- a/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
+++ b/lldb/source/Plugins/TypeSystem/Clang/TypeSystemClang.cpp
@@ -6354,8 +6354,8 @@ llvm::Expected<CompilerType> TypeSystemClang::GetChildCompilerTypeAtIndex(
       if (omit_empty_base_classes) {
         CompilerType base_class_clang_type = GetType(
             getASTContext().getObjCInterfaceType(superclass_interface_decl));
-        if (llvm::expectedToStdOptional(base_class_clang_type.GetNumChildren(
-                                            omit_empty_base_classes, exe_ctx))
+        if (llvm::expectedToOptional(base_class_clang_type.GetNumChildren(
+                                         omit_empty_base_classes, exe_ctx))
                 .value_or(0) > 0) {
           if (idx == 0) {
             clang::QualType ivar_qual_type(getASTContext().getObjCInterfaceType(
diff --git a/lldb/source/Symbol/Variable.cpp b/lldb/source/Symbol/Variable.cpp
index 55926a0b150ff..679fd19cf9844 100644
--- a/lldb/source/Symbol/Variable.cpp
+++ b/lldb/source/Symbol/Variable.cpp
@@ -578,7 +578,7 @@ static void PrivateAutoComplete(
       case eTypeClassObjCObjectPointer:
       case eTypeClassPointer: {
         bool omit_empty_base_classes = true;
-        if (llvm::expectedToStdOptional(
+        if (llvm::expectedToOptional(
                 compiler_type.GetNumChildren(omit_empty_base_classes, nullptr))
                 .value_or(0))
           request.AddCompletion((prefix_path + "->").str());
diff --git a/lldb/source/ValueObject/ValueObject.cpp b/lldb/source/ValueObject/ValueObject.cpp
index 5b343848dda6b..689de76674744 100644
--- a/lldb/source/ValueObject/ValueObject.cpp
+++ b/lldb/source/ValueObject/ValueObject.cpp
@@ -2520,7 +2520,7 @@ ValueObjectSP ValueObject::GetValueForExpressionPath_Impl(
             child_valobj_sp = root->GetSyntheticArrayMember(index, true);
           if (!child_valobj_sp)
             if (root->HasSyntheticValue() &&
-                llvm::expectedToStdOptional(
+                llvm::expectedToOptional(
                     root->GetSyntheticValue()->GetNumChildren())
                         .value_or(0) > index)
               child_valobj_sp =

>From 0536f5328905d59bc43fafe54665cd69821e5a54 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Juan=20Manuel=20Martinez=20Caama=C3=B1o?=
 <jmartinezcaamao at gmail.com>
Date: Mon, 13 Apr 2026 11:03:20 +0200
Subject: [PATCH 3/3] Optional -> std::optional in the docs

---
 llvm/include/llvm/Support/Error.h | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/include/llvm/Support/Error.h b/llvm/include/llvm/Support/Error.h
index 464ca8d66a517..51292163de89d 100644
--- a/llvm/include/llvm/Support/Error.h
+++ b/llvm/include/llvm/Support/Error.h
@@ -1084,13 +1084,13 @@ inline void consumeError(Error Err) {
   handleAllErrors(std::move(Err), [](const ErrorInfoBase &) {});
 }
 
-/// Convert an Expected to an Optional without doing anything. This method
+/// Convert an Expected to an std::optional without doing anything. This method
 /// should be used only where an error can be considered a reasonable and
 /// expected return value.
 ///
 /// Uses of this method are potentially indicative of problems: perhaps the
 /// error should be propagated further, or the error-producer should just
-/// return an Optional in the first place.
+/// return an std::optional in the first place.
 template <typename T> std::optional<T> expectedToOptional(Expected<T> &&E) {
   if (E)
     return std::move(*E);



More information about the lldb-commits mailing list