When debugging a c program using templates, the output can quickly become unreadable. For this reason it would be convenient, during a debugging session, to rewrite shorten specific type names.
For example
void std::__1::vector<std::__1::tuple<unsigned long,
state_change_t,
some::namespace::Board<some::namespace::Tile, unsigned long, 42ul>,
some::namespace::Path<unsigned int, unsigned int>>,
std::__1::allocator<std::__1::tuple<unsigned long,
state_change_t,
some::namespace::Board<some::namespace::Tile, unsigned long, 42ul>,
some::namepsace::Path<unsigned int, unsigned int>
>
>
>::some_interesting_method
Such an entry can appear multiple times in a single line in a backtrace; This quickly becomes unmanageable. It would therefore be convenient to shorten it to something along the lines of
void void std::__1::vector<my_known_type,
std::__1::allocator<my_known_type>
>::some_interesting_method
How can this be done?
CodePudding user response:
Clang have an attribute called preferred_name
that can be used to create template aliases for compile-time diagnostics. It requires a bit of forward declaring, like this:
template<typename T>
struct Heap;
using IntHeap = Heap<int>;
template<typename T>
struct [[clang::preferred_name(IntHeap)] Heap;
// Possibly add implementation, don't have to yet.
Unfortunately, since September 2021, clang won't use this when writing debug symbols. There is a mailing list thread discussing this change and the diff is here.
Clang versions 12 and 13 should still use preferred name in debug symbols, but that's a pretty narrow version window.
Opening a ticket asking to add a flag that enables preferred names in debug symbols might be worthwhile - it seems like a very trivial feature to implement and pretty useful.