Clang 叮当作响';s具有子代匹配器和深度嵌套的AST节点
我想匹配在AST上具有作为子代的调用表达式的任何语句。下面是一个简单的例子:Clang 叮当作响';s具有子代匹配器和深度嵌套的AST节点,clang,abstract-syntax-tree,Clang,Abstract Syntax Tree,我想匹配在AST上具有作为子代的调用表达式的任何语句。下面是一个简单的例子: int foo() { return 5; } int main() { int a; a = foo(); return 0: } 在这个例子中,我想匹配a=foo()语句。为此,我创建了以下matcher,它工作正常: StatementMatcher sm = stmt(hasParent(compoundStmt()), hasDesce
int foo() {
return 5;
}
int main() {
int a;
a = foo();
return 0:
}
在这个例子中,我想匹配a=foo()代码>语句。为此,我创建了以下matcher,它工作正常:
StatementMatcher sm = stmt(hasParent(compoundStmt()),
hasDescendant(callExpr()));
问题是,如果我将语句的RHS更改为更复杂的表达式,例如a=5+foo(),则此匹配器将无法工作代码>而不是a=foo()代码>。Clang文档中对HasGenerant
matcher的描述如下:
匹配具有子代AST节点的AST节点,该子代AST节点与
提供匹配器
根据这一点,matcher应该在第二种情况下也能工作,但它不能。为什么不呢?有没有其他方法来匹配这种说法
我为您提供这两种情况的ast转储
a=foo()代码>转储:
`-CompoundStmt 0x4223c40 <line:6:12, line:10:1>
|-DeclStmt 0x4223b08 <line:7:3, col:8>
| `-VarDecl 0x4223ab0 <col:3, col:7> a 'int'
|-BinaryOperator 0x4223bd8 <line:8:3, col:11> 'int' '='
| |-DeclRefExpr 0x4223b20 <col:3> 'int' lvalue Var 0x4223ab0 'a' 'int'
| `-CallExpr 0x4223bb0 <col:7, col:11> 'int'
| `-ImplicitCastExpr 0x4223b98 <col:7> 'int (*)()' <FunctionToPointerDecay>
| `-DeclRefExpr 0x4223b48 <col:7> 'int ()' Function 0x42238e0 'foo' 'int ()'
`-ReturnStmt 0x4223c20 <line:9:3, col:10>
`-IntegerLiteral 0x4223c00 <col:10> 'int' 0
`-CompoundStmt 0x4a9ec88 <line:6:12, line:10:1>
|-DeclStmt 0x4a9eb08 <line:7:3, col:8>
| `-VarDecl 0x4a9eab0 <col:3, col:7> a 'int'
|-BinaryOperator 0x4a9ec20 <line:8:3, col:15> 'int' '='
| |-DeclRefExpr 0x4a9eb20 <col:3> 'int' lvalue Var 0x4a9eab0 'a' 'int'
| `-BinaryOperator 0x4a9ebf8 <col:7, col:15> 'int' '+'
| |-IntegerLiteral 0x4a9eb48 <col:7> 'int' 5
| `-CallExpr 0x4a9ebd0 <col:11, col:15> 'int'
| `-ImplicitCastExpr 0x4a9ebb8 <col:11> 'int (*)()' <FunctionToPointerDecay>
| `-DeclRefExpr 0x4a9eb68 <col:11> 'int ()' Function 0x4a9e8e0 'foo' 'int ()'
`-ReturnStmt 0x4a9ec68 <line:9:3, col:10>
`-IntegerLiteral 0x4a9ec48 <col:10> 'int' 0
在最近的一次叮当声中,您的匹配器与RHS上的justfoo()
和5+foo()
匹配。