比较不同变量类型的变量的 Clang AST 匹配器
Clang AST matcher for variables compared to different variable types
我是 clang-tidy 的新手,以下是练习,因此我可以转向更复杂的匹配器和工具。
假设我们有
typedef int my_type;
void foo()
{
int x = 0;//this should be identified as need to be fixed
my_type z = 0;
if( x == z){
//match this case
}
}
我的目标是识别与 "my_type" 进行比较的变量,以便通过将它们的类型更改为 my_type 来修复它们的声明。
现在我正在尝试执行以下操作
auto my_type_decl = varDecl(hasType(asString("my_type")));
auto my_type_decl_exp= declRefExpr(to(my_type_decl));
auto binop = binaryOperator(has(implicitCastExpr(has(my_type_decl_exp))));
auto other_decl_exp = declRefExpr(hasAncestor(binop), unless(to(my_type_decl)));
//get ancestor functionDecl
//get descendant varDecls that match the other_decl_exp
这里的问题是我忽略了上下文。
处理此类事情的正确方法是什么?
您可以将节点匹配器绑定到一个名称,然后从匹配结果中检索这些节点。
例如:
// Match binary operators
binaryOperator(
// that are equality comparisons,
hasOperatorName("=="),
// where one side refers to a variable
hasEitherOperand(ignoringImpCasts(declRefExpr(to(varDecl(
// whose type is a typedef or type alias
hasType(typedefNameDecl(
// named "::my_type"
hasName("::my_type"),
// that aliases any type, which is bound to the name "aliased",
hasType(type().bind("aliased"))))))))),
// and where one side refers to a variable
hasEitherOperand(ignoringImpCasts(declRefExpr(to(varDecl(
// whose type is the same as the type bound to "aliased",
// which is bound to the name "declToChange".
hasType(type(equalsBoundNode("aliased")))).bind("declToChange"))))));
然后:
const auto *declToChange = result.Nodes.getNodeAs<VarDecl>("declToChange");
请注意,这匹配相等比较,因此 declToChange
可能在多个匹配项中指向相同的 VarDecl
。
在以下示例中,此匹配器将生成两个匹配项,其中 declToChange
绑定到 x
,none 且 declToChange
绑定到 y
:
typedef int my_type;
void foo() {
int x = 0;
int y = 0;
my_type z = 0;
if (x == z) {
}
if (z == x) {
}
}
我是 clang-tidy 的新手,以下是练习,因此我可以转向更复杂的匹配器和工具。
假设我们有
typedef int my_type;
void foo()
{
int x = 0;//this should be identified as need to be fixed
my_type z = 0;
if( x == z){
//match this case
}
}
我的目标是识别与 "my_type" 进行比较的变量,以便通过将它们的类型更改为 my_type 来修复它们的声明。
现在我正在尝试执行以下操作
auto my_type_decl = varDecl(hasType(asString("my_type")));
auto my_type_decl_exp= declRefExpr(to(my_type_decl));
auto binop = binaryOperator(has(implicitCastExpr(has(my_type_decl_exp))));
auto other_decl_exp = declRefExpr(hasAncestor(binop), unless(to(my_type_decl)));
//get ancestor functionDecl
//get descendant varDecls that match the other_decl_exp
这里的问题是我忽略了上下文。 处理此类事情的正确方法是什么?
您可以将节点匹配器绑定到一个名称,然后从匹配结果中检索这些节点。
例如:
// Match binary operators
binaryOperator(
// that are equality comparisons,
hasOperatorName("=="),
// where one side refers to a variable
hasEitherOperand(ignoringImpCasts(declRefExpr(to(varDecl(
// whose type is a typedef or type alias
hasType(typedefNameDecl(
// named "::my_type"
hasName("::my_type"),
// that aliases any type, which is bound to the name "aliased",
hasType(type().bind("aliased"))))))))),
// and where one side refers to a variable
hasEitherOperand(ignoringImpCasts(declRefExpr(to(varDecl(
// whose type is the same as the type bound to "aliased",
// which is bound to the name "declToChange".
hasType(type(equalsBoundNode("aliased")))).bind("declToChange"))))));
然后:
const auto *declToChange = result.Nodes.getNodeAs<VarDecl>("declToChange");
请注意,这匹配相等比较,因此 declToChange
可能在多个匹配项中指向相同的 VarDecl
。
在以下示例中,此匹配器将生成两个匹配项,其中 declToChange
绑定到 x
,none 且 declToChange
绑定到 y
:
typedef int my_type;
void foo() {
int x = 0;
int y = 0;
my_type z = 0;
if (x == z) {
}
if (z == x) {
}
}