c++ - 如何在 Clang 中获取模板类的实例化 CXXRecordDecl?

标签 c++ clang c++17 abstract-syntax-tree libtooling

比如我有一个类:

template<typename T>
class Foo {
public:
    T getBar();

private:
    T bar_;
};

它被实例化为:

using FooBarT = Foo<Bar>;

我如何获得 CXXRecordDecl已解决 Foo<bar> 的字段和方法?


我试过:

const auto *typeAliasDecl = llvm::dyn_cast<clang::TypeAliasDecl>(decl);
typeAliasDecl->getUnderlyingType()->getAsCXXRecordDecl()->dump();

我得到的输出是:

ClassTemplateSpecializationDecl 0x0000000 class Foo
`-TemplateArgument type 'Bar'

但是,我想要 CXXRecordDecl字段和方法也一样,所以我可以遍历它们。我也试过:

for (const auto *contextDecl: typeAliasDecl->getUnderlyingType()->getUnqualifiedDesugaredType()->getAsCXXRecordDecl()->getDeclContext()->decls()) {
    const auto *classTemplateDecl = llvm::dyn_cast<clang::ClassTemplateDecl>(contextDecl);
    classTemplateDecl->dump();
}

输出:

ClassTemplateDecl Foo
|-TemplateTypeParmDecl 0x0000000 referenced typename depth 0 index 0 T
|-CXXRecordDecl class Foo definition
| ... 
| |-FieldDecl 0x0000000 referenced bar_ 'T'
|-ClassTemplateSpecializationDecl 0x0000000 class Foo
  `-TemplateArgument type 'Bar'

如您所见CXXRecordDecl class Foo definition可以访问 FieldDecl , 但不知道 bar_ 的类型实例化, 而 ClassTemplateSpecializationDecl

我想要 CXXRecordDecl使用 FieldDecl bar_ 的实例化类型

最佳答案

仅供引用,您想要的 CXXRecordDecl 只是 AST 中的 ClassTemplateSpecializationDecl,因为 ClassTemplateSpecializationDecl 是 CXXRecordDecl 的子类。您真正想要的不是您已经拥有的 CXXRecordDecl,而是该 CXXRecordDecl 中的 FieldDecl。

您在 ClassTemplateSpecializationDecl 下没有 FieldDecl 的原因是您的模板实例化代码没有使用 bar_。试试下面的来源:

template<typename T>
class Foo {
public:
    T getBar() { return bar_; };

private:
    T bar_;
};
using FooBarT = Foo<int>;
void func() {
    FooBarT().getBar();
}

然后 FieldDecl 将在 ClassTemplateSpecializationDecl 下:

| `-ClassTemplateSpecializationDecl 0x1fe7f2a9d80 <line:2:1, line:9:1> line:3:7 class Foo definition
...
|   |-FieldDecl 0x1fe7f2aa3c8 <line:8:2, col:4> col:4 referenced bar_ 'int':'int'

关于c++ - 如何在 Clang 中获取模板类的实例化 CXXRecordDecl?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56813060/

相关文章:

c++ - 一行替换多个字符串

c++ - 最小数——算法

clang - 用clang解析评论

C++17 模板推导指南不用于空参数集?

c++ - 索引 4 处路径中的非法字符 - Eclipse C++

c++ - 是否有#ifdef 剥离 C/C++ 实用程序

c - 在 clang 中强制执行 C99 兼容编译

compiler-construction - 在哪里可以找到 llvm -stats 信息

C++ 新的带有初始化器的 if 语句

c++ - 自动非类型模板参数 : ambiguous partial specializations in Clang