Clang AST 匹配器:如何在 lambda 捕获的变量上进行匹配
Clang AST Matchers: How to match on lambda captured variables?
如何与 lambda 中在 lambda 外部定义并通过引用捕获的变量进行匹配?
我正在尝试解决的问题:我有一个数据库事务系统,其代码如下所示:
std::set<int> values;
auto f = [&](TransactionOp* op) -> Status {
for (auto v : readColumn("values"))
values.insert(v);
return Ok();
}
Status s = TransactionRunner::Run(f);
上面的代码有一个微妙的错误,因为 f 不清除值。TransactionRunner::Run 可以多次调用 f,直到事务成功。如果 f 未清除值,则值将具有以前尝试的垃圾值。
我正在写一个整洁的检查来查找这样的错误并阻止编写新错误。
到目前为止,我有这样的东西:
cxxRecordDecl(isLambda(), hasDescendant(cxxMethodDecl(returns(hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(hasName("Status")))))), parameterCountIs(1), hasParameter(0, hasType(pointsTo(cxxRecordDecl(hasName("TransactionOp"))))), hasBody(compoundStmt(allOf(hasDescendant(cxxMemberCallExpr(on(declRefExpr(to(varDecl().bind("insertee")))), thisPointerType(cxxRecordDecl(hasName("set"))), callee(cxxMethodDecl(hasName("insert"))))), unless(hasDescendant(cxxMemberCallExpr(on(declRefExpr(to(equalsBoundNode("insertee")))), thisPointerType(cxxRecordDecl(hasName("set"))), callee(cxxMethodDecl(hasName("clear"))))))))))))
上面将找到一个具有正确签名的 lambda,其中有一个集合插入,但没有明确调用同一集合。
我希望它不会在 lambda 内声明的集合上触发。所以我希望匹配器仅在集合被 lambda 捕获时才匹配。
我找到了解决方案。
我使用负匹配器(除非(说变量的声明不是 lambda 主体的后代。这并不完全符合我的要求(确定变量是捕获(,但它只会匹配捕获和全局变量,因此它适用于我的用例。
这是我的整个匹配器:
cxxRecordDecl(isLambda(), hasDescendant(cxxMethodDecl(returns(hasUnqualifiedDesugaredType(recordType(hasDeclaration(cxxRecordDecl(hasName("Status")))))), parameterCountIs(1), hasParameter(0, hasType(pointsTo(cxxRecordDecl(hasName("TransactionOp"))))), hasBody(compoundStmt(allOf(hasDescendant(cxxMemberCallExpr(on(declRefExpr(to(varDecl().bind("insertee")))), thisPointerType(cxxRecordDecl(hasName("set"))), callee(cxxMethodDecl(hasName("insert"))))), unless(hasDescendant(cxxMemberCallExpr(on(declRefExpr(to(equalsBoundNode("insertee")))), thisPointerType(cxxRecordDecl(hasName("set"))), callee(cxxMethodDecl(hasName("clear")))))), unless(hasDescendant(decl(equalsBoundNode("insertee"))))))))))
有趣的部分是我绑定插入在 cxxMethodDecl 中的集合的声明:
cxxMethodDecl(on(declRefExpr(to(varDecl().bind("insertee")))), ...)
然后说声明不是身体的后代(所以它必须在外面(:
unless(hasDescendant(decl(equalsBoundNode("insertee")))))))
希望此解决方案可以为其他人节省一些时间。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 将数组的地址分配给变量并删除
- 为"adjacent"变量赋值时出现问题
- enum是C++中的宏变量还是整数变量
- 在全局变量中保存类的实例以重新创建类(创建"backup")
- 用C++中的一个变量定义一个常量
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 你能重载对象变量名本身返回的内容吗
- 内置函数可查看CPP中的成员变量
- 是否可以初始化不可复制类型的成员变量(或基类)
- 尝试通过多个向量访问变量时,向量下标超出范围
- 试图让变量检查数组中的某些内容
- Cpp-Tuple使用带有变量的get
- 将包含C样式数组的对象初始化为成员变量(C++)
- 与不同变量类型相比,用于变量的 Clang AST 匹配器
- Clang AST 匹配器:如何在 lambda 捕获的变量上进行匹配
- 单行多变量声明的clang ast访问者
- Clang ast-dump 不显示某些全局变量
- 如何找出一个变量是否在clang AST的特定源位置的作用域中