[Lldb-commits] [lldb] [lldb] Add MSVC STL formatter for std::source_location (PR #217244)
via lldb-commits
lldb-commits at lists.llvm.org
Wed Aug 19 02:15:05 PDT 2026
https://github.com/Daedie-git created https://github.com/llvm/llvm-project/pull/217244
Summarize MSVC STL `std::source_location` from `_File` / `_Function` / `_Line` / `_Column`. LoadCommon last-match-wins, so this dispatches on the MSVC layout and otherwise keeps the existing libstdc++ formatter. A default-constructed location is left without a summary.
Tests: layout simulator plus the generic source_location suite's MSVC STL category (Windows).
Split out of #217177 at @Nerixyz's request. Leaving bitset (#175570), initializer_list (#178712), coroutine_handle (#194941), and chrono (#195125) to those PRs.
Part of #24834
CC @Nerixyz @DavidSpickett @Michael137
I've read the LLVM Developer Policy, Code-Review Policy, and [AI Tool Use Policy](https://llvm.org/docs/AIToolPolicy.html).
AI tool usage: Grok 4.6 was used to help write this change. I reviewed the result and can answer questions about the patch during review.
>From b97b48c1c843e4d1e7e7e65e343dee5aba010918 Mon Sep 17 00:00:00 2001
From: Bjorn Schobben <bjorn.schobben at aimsport.com>
Date: Wed, 19 Aug 2026 11:03:18 +0200
Subject: [PATCH] [lldb] Add MSVC STL formatter for std::source_location
Dispatch on the MSVC layout (_File/_Line) so the shared std:: name
does not steal libstdc++'s existing formatter. Leave a default-
constructed location without a summary.
Part of #24834
---
.../Language/CPlusPlus/CPlusPlusLanguage.cpp | 12 ++++++
.../Plugins/Language/CPlusPlus/MsvcStl.cpp | 41 +++++++++++++++++++
.../Plugins/Language/CPlusPlus/MsvcStl.h | 5 +++
.../TestDataFormatterStdSourceLocation.py | 20 +++++++++
.../msvc-simulators/source_location/Makefile | 2 +
...FormatterMsvcStlSourceLocationSimulator.py | 22 ++++++++++
.../msvc-simulators/source_location/main.cpp | 18 ++++++++
7 files changed, 120 insertions(+)
create mode 100644 lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/Makefile
create mode 100644 lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/TestDataFormatterMsvcStlSourceLocationSimulator.py
create mode 100644 lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/main.cpp
diff --git a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
index 8225c2141a84a..3306c6ed73f2c 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/CPlusPlusLanguage.cpp
@@ -1725,6 +1725,14 @@ GenericStrongOrderingSummaryProvider(ValueObject &valobj, Stream &stream,
return LibStdcppStrongOrderingSummaryProvider(valobj, stream, options);
}
+static bool
+GenericSourceLocationSummaryProvider(ValueObject &valobj, Stream &stream,
+ const TypeSummaryOptions &options) {
+ if (IsMsvcStlSourceLocation(valobj))
+ return MsvcStlSourceLocationSummaryProvider(valobj, stream, options);
+ return LibStdcppSourceLocationSummaryProvider(valobj, stream, options);
+}
+
/// Load formatters that are formatting types from more than one STL
static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
if (!cpp_category_sp)
@@ -1947,6 +1955,10 @@ static void LoadCommonStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
"MSVC STL/libstdc++ std::strong_ordering summary provider",
"std::strong_ordering",
eTypeOptionHideChildren | eTypeOptionHideValue, false);
+
+ AddCXXSummary(cpp_category_sp, GenericSourceLocationSummaryProvider,
+ "MSVC STL/libstdc++ std::source_location summary provider",
+ "std::source_location", stl_summary_flags);
}
static void LoadMsvcStlFormatters(lldb::TypeCategoryImplSP cpp_category_sp) {
diff --git a/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.cpp b/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.cpp
index 93a01acdc0f0a..f32d2be4c149e 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.cpp
+++ b/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.cpp
@@ -16,6 +16,7 @@
#include "lldb/Utility/Status.h"
#include "lldb/Utility/Stream.h"
#include "lldb/ValueObject/ValueObject.h"
+#include "llvm/ADT/StringRef.h"
#include "Plugins/Language/CPlusPlus/CxxStringTypes.h"
@@ -305,3 +306,43 @@ bool lldb_private::formatters::MsvcStlStrongOrderingSummaryProvider(
}
return true;
}
+
+bool lldb_private::formatters::IsMsvcStlSourceLocation(ValueObject &valobj) {
+ if (auto valobj_sp = valobj.GetNonSyntheticValue())
+ return valobj_sp->GetChildMemberWithName("_File") != nullptr &&
+ valobj_sp->GetChildMemberWithName("_Line") != nullptr;
+ return false;
+}
+
+bool lldb_private::formatters::MsvcStlSourceLocationSummaryProvider(
+ ValueObject &valobj, Stream &stream, const TypeSummaryOptions &) {
+ ValueObjectSP file_sp = valobj.GetChildMemberWithName("_File");
+ ValueObjectSP function_sp = valobj.GetChildMemberWithName("_Function");
+ ValueObjectSP line_sp = valobj.GetChildMemberWithName("_Line");
+ ValueObjectSP column_sp = valobj.GetChildMemberWithName("_Column");
+
+ if (!file_sp || !function_sp || !line_sp || !column_sp)
+ return false;
+
+ bool success = false;
+ uint64_t line = line_sp->GetValueAsUnsigned(0, &success);
+ if (!success)
+ return false;
+
+ uint64_t column = column_sp->GetValueAsUnsigned(0, &success);
+ if (!success)
+ return false;
+
+ const char *file = file_sp->GetSummaryAsCString();
+ // Default-constructed source_location is empty; don't invent a summary.
+ if (line == 0 && column == 0 &&
+ (!file || file[0] == '\0' || llvm::StringRef(file) == "\"\""))
+ return false;
+
+ stream.Format("{0}:{1}:{2}", file ? file : "<unknown>", line, column);
+
+ if (const char *function = function_sp->GetSummaryAsCString())
+ stream.Printf(" (%s)", function);
+
+ return true;
+}
diff --git a/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.h b/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.h
index 0bdd548dd99e3..1000a229118f2 100644
--- a/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.h
+++ b/lldb/source/Plugins/Language/CPlusPlus/MsvcStl.h
@@ -142,6 +142,11 @@ SyntheticChildrenFrontEnd *
MsvcStlSpanSyntheticFrontEndCreator(CXXSyntheticChildren *,
lldb::ValueObjectSP valobj_sp);
+// MSVC STL std::source_location
+bool IsMsvcStlSourceLocation(ValueObject &valobj);
+bool MsvcStlSourceLocationSummaryProvider(ValueObject &valobj, Stream &stream,
+ const TypeSummaryOptions &options);
+
} // namespace formatters
} // namespace lldb_private
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/source_location/TestDataFormatterStdSourceLocation.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/source_location/TestDataFormatterStdSourceLocation.py
index 4dd28c816a10c..5ffa9a62cc8e8 100644
--- a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/source_location/TestDataFormatterStdSourceLocation.py
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/generic/source_location/TestDataFormatterStdSourceLocation.py
@@ -51,3 +51,23 @@ def test_libcxx(self):
def test_libstdcxx(self):
self.build(dictionary={"USE_LIBSTDCPP": 1})
self.do_test()
+
+ @add_test_categories(["msvcstl"])
+ def test_msvcstl(self):
+ self.build()
+ lldbutil.run_to_source_breakpoint(
+ self, "// break here", lldb.SBFileSpec("main.cpp")
+ )
+
+ frame = self.frame()
+ loc_main = frame.FindVariable("loc_main")
+ self.assertTrue(loc_main.GetError().Success())
+ self.assertRegex(loc_main.summary, r"main\.cpp\":6:\d+.*main")
+
+ loc_foo = frame.FindVariable("loc_foo")
+ self.assertTrue(loc_foo.GetError().Success())
+ self.assertRegex(loc_foo.summary, r"main\.cpp\":3:\d+.*foo")
+
+ loc_empty = frame.FindVariable("loc_empty")
+ self.assertTrue(loc_empty.GetError().Success())
+ self.assertTrue(not loc_empty.summary)
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/Makefile b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/Makefile
new file mode 100644
index 0000000000000..3d0b98f13f3d7
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/Makefile
@@ -0,0 +1,2 @@
+CXX_SOURCES := main.cpp
+include Makefile.rules
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/TestDataFormatterMsvcStlSourceLocationSimulator.py b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/TestDataFormatterMsvcStlSourceLocationSimulator.py
new file mode 100644
index 0000000000000..5211f58279c7d
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/TestDataFormatterMsvcStlSourceLocationSimulator.py
@@ -0,0 +1,22 @@
+"""Simulate MSVC STL std::source_location and check the formatter."""
+
+from lldbsuite.test.lldbtest import *
+from lldbsuite.test import lldbutil
+
+
+class MsvcStlSourceLocationSimulatorTestCase(TestBase):
+ NO_DEBUG_INFO_TESTCASE = True
+
+ def test(self):
+ self.build()
+ lldbutil.run_to_source_breakpoint(
+ self, "break here", lldb.SBFileSpec("main.cpp")
+ )
+
+ self.expect(
+ "frame variable loc",
+ substrs=['"main.cpp":6:1', "__cdecl", "main"],
+ )
+ loc_empty = self.frame().FindVariable("loc_empty")
+ self.assertTrue(loc_empty.GetError().Success())
+ self.assertTrue(not loc_empty.summary)
diff --git a/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/main.cpp b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/main.cpp
new file mode 100644
index 0000000000000..da13cfcf1949d
--- /dev/null
+++ b/lldb/test/API/functionalities/data-formatter/data-formatter-stl/msvc-simulators/source_location/main.cpp
@@ -0,0 +1,18 @@
+// Layout approximation of MSVC STL std::source_location.
+
+#include <stdint.h>
+
+namespace std {
+struct source_location {
+ const char *_File;
+ const char *_Function;
+ uint32_t _Line;
+ uint32_t _Column;
+};
+} // namespace std
+
+int main() {
+ std::source_location loc{"main.cpp", "int __cdecl main(void)", 6, 1};
+ std::source_location loc_empty{"", "", 0, 0};
+ return 0; // break here
+}
More information about the lldb-commits
mailing list