Clang AST Libtooling:如何在AST匹配上打印数组标识符
我尝试的代码如下:Clang AST Libtooling:如何在AST匹配上打印数组标识符,clang,llvm-clang,libtooling,clang-ast-matchers,Clang,Llvm Clang,Libtooling,Clang Ast Matchers,我尝试的代码如下: if(const ArraySubscriptExpr *array = Result.Nodes.getNodeAs<ArraySubscriptExpr>("array")) { llvm::outs() << array->getBase() <<'\n'; } if(const ArraySubscriptExpr*array=Result.Nodes.getNodeAs(
if(const ArraySubscriptExpr *array = Result.Nodes.getNodeAs<ArraySubscriptExpr>("array"))
{
llvm::outs() << array->getBase() <<'\n';
}
if(const ArraySubscriptExpr*array=Result.Nodes.getNodeAs(“数组”))
{
llvm::outs()getBase()getBase返回一个指向基表达式的指针,这就是为什么要打印地址。arr[i]的AST是:
| |-ArraySubscriptExpr 0xc04c608 <col:3, col:8> 'double' lvalue
| | |-ImplicitCastExpr 0xc04c5d8 <col:3> 'double *' <LValueToRValue>
| | | `-DeclRefExpr 0xc04c598 <col:3> 'double *' lvalue Var 0xc04c480 'arr' 'double *'
| | `-ImplicitCastExpr 0xc04c5f0 <col:7> 'int' <LValueToRValue>
| | `-DeclRefExpr 0xc04c5b8 <col:7> 'int' lvalue Var 0xc04c518 'i' 'int'
| |-ArraySubscriptExpr 0xc04c608“双精度”左值
|| |-implicitCastXPR 0xc04c5d8'双*'
|| |`-DeclRefExpr 0xc04c598'double*'左值变量0xc04c480'arr'double*'
||`-ImplicitCastExpr 0xc04c5f0'int'
||`-DeclRefExpr 0xc04c5b8'int'左值变量0xc04c518'i'int'
可以看到,数组的名称出现在隐式CastExpr节点的子节点中,该节点是ArraySubscriptExpr的子节点。这对我来说很有用:
if (auto *array = dyn_cast<ArraySubscriptExpr>(st)) {
if (auto *cast = dyn_cast<ImplicitCastExpr>(array->getBase())) {
if (auto *decl = dyn_cast<DeclRefExpr>(cast->getSubExpr())) {
cout << decl->getNameInfo().getAsString() << endl;
}
}
}
if(自动*array=dyn_cast(st)){
if(auto*cast=dyn\u cast(数组->getBase()){
if(auto*decl=dyn\u cast(cast->getSubExpr()){
cout getNameInfo().getAsString()