【问题标题】:Simplifying complex C++ template symbols简化复杂的 C++ 模板符号
【发布时间】:2012-09-01 12:19:22
【问题描述】:

我正在开发一个调试/内存工具。我想显示来自 C++ 的符号,问题是它们非常冗长。目前我只使用__cxa_demangle,但由于包含默认模板参数,这通常会导致超过 500 个字符的巨大字符串。

clang++在报告符号时显然可以做一些聪明的事情,我有什么办法可以利用它吗?

举个简单的例子:

std::map<std::string const, unsigned int, std::less<std::string const>, std::allocator<std::pair<std::string const, unsigned int> > >::find(std::string const&)

显然可以报告为:

std::map<std::string const, unsigned int>::find(std::string const&)

.. 如果我有一个足够聪明的工具。很明显,如果没有额外的知识(比如最初使用的包含 - 我可能会得到这些),这通常很难做到,但我将不胜感激。

到目前为止,我一直指向libcxxabi,但除了没有解析树的公共接口(它本身不会阻止我)之外,似乎我必须做艰苦的工作确定哪些参数是默认值。如果我能以某种方式欺骗 clang 为我做这件事,那就太好了。

【问题讨论】:

  • 我想我们都希望通过模板获得更好的调试体验。我记得不得不深入研究boost::ptr_map 进行调试,因为所有这些默认参数突然出现并填满页面,这简直是地狱。尽管如此,我的实用主义可能会为几种类型提供一个内置的默认参数列表,并基于此列表进行省略。
  • 您可以编写自己的逻辑:将参数列表解析为参数,为您关心的模板提供默认映射,并检查参数是否与默认映射匹配(从后面开始)。跨度>
  • 不知何故,您的简单示例已经很幸运了,因为您显示的是std::string const 而不是std::basic_string&lt;char,std::char_traits&lt;char&gt;,std::allocator&lt;char&gt; &gt; const。看来您已经利用了 typedef 和专业化?
  • @ÖöTiib:这只是 mangling 方案和 demangler 的一个功能。 std::string 用损坏的输出中的几个字符表示,而解解码器只返回 std::string
  • 你看过mixtion.org/gccfilter吗?

标签: c++ templates clang debug-symbols clang++


【解决方案1】:

STLFilt 可以帮助你。有两个 perl 脚本,STLFilt.pl(用于 Visual C++)和 gSTLFilt.p(用于 gcc)。它旨在用于简化错误消息,但我已经将它用于后处理 __cxa_demangle 的输出。

用于您的简单示例,没有任何选项:

echo "std::map<std::string const, unsigned int, std::less<std::string const>, std::allocator<std::pair<std::string const, unsigned int> > >::find(std::string const&)" \
| perl ./gSTLFilt.pl

获取输出:

BD Software STL Message Decryptor v3.10 for gcc 2/3/4
map<string const, unsigned int>::find(string const &)

如果您想使用它的选项,您应该能够获得自定义的重新格式化(我没有尝试过)。

【讨论】:

    猜你喜欢
    • 2013-09-01
    • 2014-12-11
    • 1970-01-01
    • 1970-01-01
    • 2018-12-24
    • 2011-11-16
    • 2016-01-09
    • 1970-01-01
    • 2012-11-17
    相关资源
    最近更新 更多