Skip to content

[LLDB] Add formatters for MSVC STL std::optional #149545

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 20 additions & 10 deletions lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1545,20 +1545,10 @@ static void LoadLibStdcppFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
"std::bitset synthetic child", "^std::(__debug::)?bitset<.+>(( )?&)?$",
stl_deref_flags, true);

AddCXXSynthetic(
cpp_category_sp,
lldb_private::formatters::LibStdcppOptionalSyntheticFrontEndCreator,
"std::optional synthetic child", "^std::optional<.+>(( )?&)?$",
stl_deref_flags, true);

AddCXXSummary(cpp_category_sp,
lldb_private::formatters::StdlibCoroutineHandleSummaryProvider,
"libstdc++ std::coroutine_handle summary provider",
libstdcpp_std_coroutine_handle_regex, stl_summary_flags, true);
AddCXXSummary(cpp_category_sp,
lldb_private::formatters::GenericOptionalSummaryProvider,
"libstd++ std::optional summary provider",
"^std::optional<.+>(( )?&)?$", stl_summary_flags, true);
}

static lldb_private::SyntheticChildrenFrontEnd *
Expand Down Expand Up @@ -1648,6 +1638,17 @@ GenericForwardListSyntheticFrontEndCreator(CXXSyntheticChildren *children,
*valobj_sp);
}

static SyntheticChildrenFrontEnd *
GenericOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *children,
lldb::ValueObjectSP valobj_sp) {
if (!valobj_sp)
return nullptr;

if (IsMsvcStlOptional(*valobj_sp))
return MsvcStlOptionalSyntheticFrontEndCreator(children, valobj_sp);
return LibStdcppOptionalSyntheticFrontEndCreator(children, valobj_sp);
}

/// Load formatters that are formatting types from more than one STL
static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
if (!cpp_category_sp)
Expand Down Expand Up @@ -1713,6 +1714,12 @@ static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
"std::forward_list synthetic children",
"^std::forward_list<.+>(( )?&)?$", stl_synth_flags, true);

SyntheticChildren::Flags stl_deref_flags = stl_synth_flags;
stl_deref_flags.SetFrontEndWantsDereference();
AddCXXSynthetic(cpp_category_sp, GenericOptionalSyntheticFrontEndCreator,
"std::optional synthetic children",
"^std::optional<.+>(( )?&)?$", stl_deref_flags, true);

AddCXXSummary(cpp_category_sp, GenericSmartPointerSummaryProvider,
"MSVC STL/libstdc++ std::shared_ptr summary provider",
"^std::shared_ptr<.+>(( )?&)?$", stl_summary_flags, true);
Expand All @@ -1739,6 +1746,9 @@ static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
TypeSummaryImplSP(new ScriptSummaryFormat(
stl_summary_flags,
"lldb.formatters.cpp.gnu_libstdcpp.ForwardListSummaryProvider")));
AddCXXSummary(cpp_category_sp, GenericOptionalSummaryProvider,
"MSVC STL/libstd++ std::optional summary provider",
"^std::optional<.+>(( )?&)?$", stl_summary_flags, true);
}

static void LoadMsvcStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
Expand Down
26 changes: 24 additions & 2 deletions lldb/source/Plugins/Language/CPlusPlus/GenericOptional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "Generic.h"
#include "LibCxx.h"
#include "LibStdcpp.h"
#include "MsvcStl.h"
#include "Plugins/TypeSystem/Clang/TypeSystemClang.h"
#include "lldb/DataFormatters/FormattersHelpers.h"
#include "lldb/Target/Target.h"
Expand All @@ -32,6 +33,7 @@ class GenericOptionalFrontend : public SyntheticChildrenFrontEnd {
enum class StdLib {
LibCxx,
LibStdcpp,
MsvcStl,
};

GenericOptionalFrontend(ValueObject &valobj, StdLib stdlib);
Expand Down Expand Up @@ -77,7 +79,8 @@ lldb::ChildCacheState GenericOptionalFrontend::Update() {
else if (m_stdlib == StdLib::LibStdcpp) {
if (ValueObjectSP payload = m_backend.GetChildMemberWithName("_M_payload"))
engaged_sp = payload->GetChildMemberWithName("_M_engaged");
}
} else if (m_stdlib == StdLib::MsvcStl)
engaged_sp = m_backend.GetChildMemberWithName("_Has_value");

if (!engaged_sp)
return lldb::ChildCacheState::eRefetch;
Expand Down Expand Up @@ -114,7 +117,12 @@ ValueObjectSP GenericOptionalFrontend::GetChildAtIndex(uint32_t _idx) {
ValueObjectSP candidate = val_sp->GetChildMemberWithName("_M_value");
if (candidate)
val_sp = candidate;
}
} else if (m_stdlib == StdLib::MsvcStl)
// Same issue as with LibCxx
val_sp = m_backend.GetChildMemberWithName("_Has_value")
->GetParent()
->GetChildAtIndex(0)
->GetChildMemberWithName("_Value");

if (!val_sp)
return ValueObjectSP();
Expand Down Expand Up @@ -143,3 +151,17 @@ SyntheticChildrenFrontEnd *formatters::LibcxxOptionalSyntheticFrontEndCreator(
GenericOptionalFrontend::StdLib::LibCxx);
return nullptr;
}

bool formatters::IsMsvcStlOptional(ValueObject &valobj) {
if (auto valobj_sp = valobj.GetNonSyntheticValue())
return valobj_sp->GetChildMemberWithName("_Has_value") != nullptr;
return false;
}

SyntheticChildrenFrontEnd *formatters::MsvcStlOptionalSyntheticFrontEndCreator(
CXXSyntheticChildren *, lldb::ValueObjectSP valobj_sp) {
if (valobj_sp)
return new GenericOptionalFrontend(
*valobj_sp, GenericOptionalFrontend::StdLib::MsvcStl);
return nullptr;
}
6 changes: 6 additions & 0 deletions lldb/source/Plugins/Language/CPlusPlus/MsvcStl.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@ SyntheticChildrenFrontEnd *
MsvcStlListSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP valobj_sp);

// MSVC STL std::optional<>
bool IsMsvcStlOptional(ValueObject &valobj);
SyntheticChildrenFrontEnd *
MsvcStlOptionalSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP valobj_sp);

} // namespace formatters
} // namespace lldb_private

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,9 @@
from lldbsuite.test.lldbtest import *
from lldbsuite.test import lldbutil

USE_LIBSTDCPP = "USE_LIBSTDCPP"
USE_LIBCPP = "USE_LIBCPP"


class GenericOptionalDataFormatterTestCase(TestBase):
def do_test_with_run_command(self, stdlib_type):
def do_test_with_run_command(self):
"""Test that that file and class static variables display correctly."""

# This is the function to remove the custom formats in order to have a
Expand All @@ -21,7 +18,6 @@ def cleanup():

self.addTearDownHook(cleanup)

self.build(dictionary={stdlib_type: "1"})
self.runCmd("file " + self.getBuildArtifact("a.out"), CURRENT_EXECUTABLE_SET)

bkpt = self.target().FindBreakpointByID(
Expand Down Expand Up @@ -100,7 +96,8 @@ def cleanup():
## We are skipping gcc version less that 5.1 since this test requires -std=c++17
@skipIf(compiler="gcc", compiler_version=["<", "5.1"])
def test_with_run_command_libcpp(self):
self.do_test_with_run_command(USE_LIBCPP)
self.build(dictionary={"USE_LIBCPP": 1})
self.do_test_with_run_command()

@add_test_categories(["libstdcxx"])
## Clang 7.0 is the oldest Clang that can reliably parse newer libc++ versions
Expand All @@ -109,4 +106,11 @@ def test_with_run_command_libcpp(self):
## We are skipping gcc version less that 5.1 since this test requires -std=c++17
@skipIf(compiler="gcc", compiler_version=["<", "5.1"])
def test_with_run_command_libstdcpp(self):
self.do_test_with_run_command(USE_LIBSTDCPP)
self.build(dictionary={"USE_LIBSTDCPP": 1})
self.do_test_with_run_command()

@add_test_categories(["msvcstl"])
def test_with_run_command_msvcstl(self):
# No flags, because the "msvcstl" category checks that the MSVC STL is used by default.
self.build()
self.do_test_with_run_command()
Loading