Using MSVC formatter right now?

Hi! I don’t really see deep into functionality of llvm.
But I saw that data formatters for MSVC are largely done? (looking at this issue)

I could really use the the std::vector formatter right now for some debugging.
I assume it’s all just python code, but I saw some c++ in the commits too. (I’m sorry for not seeing it clearly atm, I’m dealing with brain fog)

Would it be enough for me to just copy the python formatters into my existing LLVM directory?

thanks in advance

The formatters for the MSVC STL are implemented entirely in C++ and only a subset of them is included in the 21 release, because they were added when the release branch was created. So currently, you’d need to build LLDB from the main branch.

If you only need the std::vector<T> (except vector<bool>) synthetic provider, the following should work:

MsvcStlFormatters.py
# This is largely the same as the vector formatter from libstdc++
# https://github.com/llvm/llvm-project/blob/main/lldb/examples/synthetic/gnu_libstdcpp.py#L356-L440
class MsvcStlVectorSyntheticChildren:
    def __init__(self, valobj):
        self.valobj = valobj
        self.count = None

    def num_children(self):
        if self.count is None:
            self.count = self.num_children_impl()
        return self.count

    def num_children_impl(self):
        try:
            start_val = self.start.GetValueAsUnsigned(0)
            finish_val = self.finish.GetValueAsUnsigned(0)

            if start_val == 0 or finish_val == 0:
                return 0
            if start_val >= finish_val:
                return 0
            num_children = finish_val - start_val
            if (num_children % self.data_size) != 0:
                return 0
            return num_children // self.data_size
        except:
            return 0

    def get_child_at_index(self, index):
        if index < 0:
            return None
        if index >= self.num_children():
            return None
        try:
            offset = index * self.data_size
            return self.start.CreateChildAtOffset(
                "[" + str(index) + "]", offset, self.data_type
            )
        except:
            return None

    def update(self):
        self.count = None
        try:
            data = self.valobj.GetChildAtNamePath(("_Mypair", "_Myval2"))
            self.start = data.GetChildMemberWithName("_Myfirst")
            self.finish = data.GetChildMemberWithName("_Mylast")
            self.element_type = self.start.GetType().GetPointeeType()
            self.data_size = self.element_type.GetByteSize()
            if (
                self.start.IsValid()
                and self.finish.IsValid()
                and self.end.IsValid()
                and self.data_type.IsValid()
            ):
                self.count = None
            else:
                self.count = 0
        except:
            self.count = 0
        return False
(lldb) command script import MsvcStlFormatters.py
(lldb) type synthetic add -x "^std:vector<.*>$" --python-class MsvcStlFormatters.MsvcStlVectorSyntheticChildren
(lldb) type summary add --expand -x "^std::vector<.*>$" --summary-string "size=${svar%#}"
1 Like