4

一段时间以来,我一直在使用一些解构代码来帮助进行一些调试,而不必编写数千行动态强制转换或实现返回类名的虚函数。

template <class CLASS>
std::string getClassName(CLASS &theObject)
{
    int status = 0;

    // Convert real name to readable string
    char *realName = abi::__cxa_demangle(typeid(theObject).name(), nullptr,
                                         nullptr, &status);
    ASSERT(status == 0); // Assert for success
    VERIFY(realName, return std::string());
    // Return as string + prevent memory leaks!
    const std::string result(realName);
    free(realName);
    return result;
}

这段代码背后的想法很简单,输出我们实际使用的类。虽然在切换到 Ubuntu 14.04 后,我不再能够使用 clang 和 c++-11/c++-14 标准进行编译,所以我已经切换到使用 libc++ 而不是 libstdc++。

切换到 libc++ 后,我注意到当我对 'std::string' 进行 demangle 时,它​​不再输出 'std::string',而是输出:

std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >

当然这是正确的,因为 std::string 是 std::basic_string 的 typedef。尽管据我所见,在 libc++ 和 libstdc++ 中,这都是使用 typedef 以相同的方式定义的。所以我真的不明白为什么通过切换到 libc++ 来改变这种拆解。

有人知道为什么这是不同的,如果 CLASS 是“std::string”,如何获得“std::string”,当 CLASS 是“myTemplate”时如何获得“myTemplate”?

提前Tnx!

JVA笔

4

1 回答 1

11

libc++ 用于inline namespaces对其 ABI 进行版本控制。它当前使用的内联命名空间是std::__1. 这样做是为了让 Apple 可以同时发布 gcc libstdc++ 和 libc++。Dylib A 可能链接到 libstdc++,dylib B 可能链接到 libc++,应用程序可能链接到两个 dylib。发生这种情况时,您不希望意外地将 libstdc++std::string与 libc++ 混淆std::string

std::string它们具有相同的 API,因此通过dylib 边界很容易意外地做到这一点。解决方案是告诉编译器以不同的方式处理它们,而这正是内联命名空间所做的(并且是为此而发明的)。现在,如果它们在应用程序中意外混合,将导致链接时间错误,因为链接器看到两种不同的类型,这可以从它们不同的错位名称中得到证明。

demangler 的工作是简单地告诉你真相:你提供给它的符号的 demangled 名称是什么。它运行良好。

确实存在一种方法可以在 libc++ 中关闭 ABI 版本控制。在 <__config> 中搜索_LIBCPP_BEGIN_NAMESPACE_STD_LIBCPP_END_NAMESPACE_STD。您可以看到一些平台如何定义它以打开内联命名空间,而有些则没有。这是一个非常大的锤子,可以用来解决您的问题。 如果您以这种方式更改 libc++ 的 ABI,则必须重新构建在您的平台上编译和链接到 libc++ 的所有内容。

这是我有时使用的针对您的问题的更简单的部分解决方案:

#include <iostream>
#include <type_traits>
#include <memory>
#include <algorithm>
#include <cstdlib>
#include <string>
#include <cxxabi.h>

namespace
{

inline
void
filter(std::string& r, const char* b)
{
    const char* e = b;
    for (; *e; ++e)
        ;
    const char* pb = "std::__1::";
    const int pl = std::strlen(pb);
    const char* pe = pb + pl;
    while (true)
    {
        const char* x = std::search(b, e, pb, pe);
        r.append(b, x);
        if (x == e)
            break;
        r += "std::";
        b = x + pl;
    }
}

}  // unnamed namespace

template <typename T>
std::string
type_name()
{
    typedef typename std::remove_reference<T>::type TR;
    std::unique_ptr<char, void(*)(void*)> own
           (
                __cxxabiv1::__cxa_demangle(typeid(TR).name(), nullptr,
                                           nullptr, nullptr),
                std::free
           );
    std::string r;
    if (own)
    {
        if (std::is_const<TR>::value)
            r += "const ";
        if (std::is_volatile<TR>::value)
            r += "volatile ";
        filter(r, own.get());
        if (std::is_lvalue_reference<T>::value)
            r += "&";
        else if (std::is_rvalue_reference<T>::value)
            r += "&&";
    }
    else
        r = typeid(TR).name();
    return r;
}

::__1这只是在将其呈现给您之前过滤掉了损坏的名称。如果您愿意,您也可以使用相同的技术然后转换std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >std::string

Itanium ABI只有少数与这种类型定义相对应的“压缩” 。它们是std::stringstd::istreamstd::ostreamstd::iostream

于 2014-08-03T16:17:17.677 回答