Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NatVis display substring of enum

Tags:

natvis

We often use prefix for our enums.

It is very verbose to display full name in NatVis.

Is it possible to remove prefix of enum name (AKA return substring of enum name) ?

enum FooFormat {
  FooFormat_Foo,
  FooFormat_Bar,
  FooFormat_Baz,
  FooFormat_COUNT
};

struct Bar {
  FooFormat format;
};
<AutoVisualizer>
  <Type Name="Bar">
    <DisplayString>fmt={format,How-to-get-substring-of-enum-name-?}</DisplayString>
  </Type>
</AutoVisualizer>
like image 303
Raymond Avatar asked Sep 16 '25 07:09

Raymond


2 Answers

Use format specifier en for this:

<AutoVisualizer>
  <Type Name="Bar">
    <DisplayString>fmt={format,en}</DisplayString>
  </Type>
</AutoVisualizer>

For example:

Bar f;
f.format = FooFormat_Bar;
... // breakpoint here

enter image description here

like image 73
mr NAE Avatar answered Sep 19 '25 14:09

mr NAE


This does not work:

<Type Name="FooFormat">
  <DisplayString Condition="this==FooFormat::FooFormat_Foo">Foo</DisplayString>
  <DisplayString Condition="this==FooFormat::FooFormat_Bar">Bar</DisplayString>
  <DisplayString>"bla"</DisplayString>
</Type>

But fortunately this works. Of course that is only feasible if your format string does not depend on too many variables, otherwise you might end up with a ton of conditional DisplayStrings.

<Type Name="Bar">
  <DisplayString Condition="format==FooFormat::FooFormat_Foo">fmt=Foo</DisplayString>
  <DisplayString Condition="format==FooFormat::FooFormat_Bar">fmt=Bar</DisplayString>
  <DisplayString>fmt={format}</DisplayString>
</Type>

Another approach, if you are using C++11 or above, I would go with scoped enums (enum class FooFormat { Foo, Bar, Baz, COUNT };). These are a bit better than regular enums and instead of FooFormat_Foo you write FooFormat::Foo. So you still have the verbose code, but the enum values have a shorter identifier and natvis only displays Foo. Of course this only works for C++, not for C.

like image 41
Werner Henze Avatar answered Sep 19 '25 14:09

Werner Henze