使用Clang ASTMatcher发出匹配的非嵌套for语句
Issue matching non-nested for statements with Clang ASTMatcher
我正在尝试使用Clang ASTMatcher's在c++代码中匹配单个for循环,即在源文件中如下
for(int x=0;x<10;x++){ }
for(int y=0;y<10;y++){for(int z=0;z<5;z++){}}
我只想匹配' for(int x=0;x<10<x++){ }
'
StatementMatcher forStmtMatcher = forStmt(unless(anyOf(hasAncestor(forStmt()),hasDescendant(forStmt()))))
,我认为它应该工作,但它没有。匹配包括我不想要的for(int y=0;y<10;y++)
,如果我交换anyOf()匹配器中条件的位置,它将匹配for(int z=0;z<5;z++)
,而我也不想要。
谁能解释一下为什么或告诉我如何解决它?
我同意你所发布的看起来应该工作-但是它无法产生clang-query 3.8和4.0的预期结果。如果您尝试绑定中间匹配的变量,它似乎可以工作。
Clang查询代码(为清晰设置类型,删除换行符以运行)
set bind-root false
match forStmt(
unless(hasAncestor(forStmt().bind('x'))),
unless(hasDescendant(forStmt().bind('y')))).bind('for')
等效示例:
int main()
{
for(int x=0;x<10;x++){ }
for(int y=0;y<10;y++){for(int z=0;z<5;z++){}}
}
生产:
Match #1:
/vagrant/stackoverflow-ast-query/loops.cpp:3:5: note: "for" binds here
for(int x=0;x<10;x++){ }
^~~~~~~~~~~~~~~~~~~~~~~~
1 match.
看起来像是AST匹配器构造函数转换中可能存在的错误。
相关文章:
- 嵌套for循环C++的问题(初学者)
- 从列表/向量制作嵌套 for 循环的最佳方法是什么?
- C++:带有 "auto" 的二维数组上的嵌套 For 循环
- 比较嵌套 for 循环中的两个 wchar 数组?
- 需要帮助在 C++ 中使用嵌套 for 循环
- 嵌套 for 循环仅运行内部循环 (c++)
- 优化四重嵌套"for"循环
- 执行三个嵌套for循环的最快方法是什么
- 在 c++ 中嵌套 for()
- 为什么我在程序集中的嵌套 for 循环会导致无限循环
- 多个嵌套for循环与单个for循环
- 在与 OpenMP 并行的嵌套 for 循环中写入共享数组(通过指针)如何产生错误的结果
- 为什么OpenCV Mat对象在嵌套for循环中指定了预期值后却不包含这些值
- 在我的嵌套 for 循环中找不到符号"k"?
- 仅对"specific"索引迭代嵌套"for"循环。C++
- 嵌套 for 循环的任何快速搜索算法
- 在C++中使用嵌套 for 循环的更复杂的形状
- 嵌套 for 循环 (C++) 无法正常工作
- 压缩多个嵌套 for 循环
- 嵌套 for 循环的运行时间