如何将 Clang AST 中的 TemplateTypeParm 节点与 AST_Matchers 匹配
How to match TemplateTypeParm node in Clang AST with AST_Matchers?
>我正在尝试获取TypeAliasDecl的RHS模板类型。
例:
using AliasOfType = AliasedType; // AliasedType itself is a template
我可以使用 clang::ast_matchers::typeAliasDecl
检索 AST 中的AliasOfType
。我想用clang::ast_matchers::
检索AliasedType
.
clang::ast_matchers::typeAliasDecl
的 AST 转储如下所示:
TypeAliasDecl 0x4fe22cf8 AliasOfType
-SubstTemplateTypeParmType 0x4fe22cc0
|-TemplateTypeParmType 0x4fe1a840 `AliasedType` dependent depth 0 index 0
| `-TemplateTypeParm 0x4fe1a7f8 'AliasedType'
所以直觉上,我想匹配TemplateTypeParm
,其中有我以前的匹配作为祖先。但是,我还没有找到这样做的ast_matcher
。有clang::ast_matchers::templateTypeParmType
,但是如果我尝试将任何内容作为缩小参数,例如:
templateTypeParmType(hasName("AliasedType"))
当我尝试时,我得到的错误是:
clang/ASTMatchers/ASTMatchersInternal.h:1347:13: error: ‘clang::ast_matchers::internal::Matcher< <template-parameter-1-1> >::Matcher(const clang::ast_matchers::internal::DynTypedMatcher&) [with T = clang::TemplateTypeParmType]’ is private within this context
return {Matcher<T>(std::get<Is>(Params))...};
你是
对的,没有直接匹配器可以检查类型别名的别名类型(自己实现它并不难,但我想这应该是最后的手段(。
但是,根据文档,有一个匹配器has
:
匹配具有与 提供匹配器。
另一个重要的一点是,类型别名在孩子时期肯定会有TypeLoc
。以下是关于区分Type
s和TypeLoc
s的小引文(来自内部手册(:
将它们我们在表示相同类型时重用类型节点(但为每个写入类型的实例维护单独的 TypeLoc(
放在一起,我们得到以下匹配器:
typeAliasDecl(has(typeLoc(loc(templateTypeParmType())).bind("x")))
对于此代码片段:
using NotInterestingAlias = int;
template <class AliasedType> class TemplateClass {
using AliasOfType = AliasedType;
using AliasOfSomeOtherType = double;
};
int main() { return 0; }
匹配器将生成以下输出:
main.cpp:4:3: note: "root" binds here
using AliasOfType = AliasedType;
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
main.cpp:4:23: note: "x" binds here
using AliasOfType = AliasedType;
^~~~~~~~~~~
我希望这些信息有用。与叮当一起快乐!
相关文章:
- 如何克服提升精神AST混乱
- clang 插件:在编译过程中修改 AST
- Boost.Spirit将表达转换为AST
- 类继承,ENUM 与 AST 类实现的问题
- Windows Clang Ast Dump
- 如何将 AST 用于自定义前端操作和 clang 静态分析
- 与不同变量类型相比,用于变量的 Clang AST 匹配器
- 如何检测 Clang AST C++中的静态函数
- 如何在 boost::spirit::qi 中将某些语义操作排除在 AST 之外
- python.clang AST 解析:获取字段声明的 c++ 模板参数
- 对函数的 AST 声明的抽象格式感到困惑
- 如何使用 clang 仅获取主函数的 AST
- 我可以用clang AST从模板专业化中获得默认的模板参数吗
- 将输入映射到提升精神的 ast 类型
- 叮当 AST 将调用与特定类匹配make_unique
- 如何在 Clang AST 中的源位置之后找到字符的源位置?
- 精神 x3 如何将矢量添加到 AST
- 有没有一种优雅的方式来遍历 Clang AST 语句
- 如何访问序列化的 clang 抽象语法树 (AST)
- 光标和节点的AST遍历之间有什么区别