hasAncestor() hasDescendant() matchers scope

Hello all,

I am new to clang and I am trying to write a check to familiarize myself
with AST matchers. I am just looking for a confirmation here. When I
bind a node within a hasAncestor() matcher, am I guaranteed to get the
most direct (nearest) ancestor from the current node?

Consider the following example:

class MyClass {
public:
void myClassOp() { //<-- another parent CXXMemberDecl
class NestedClass {
void nestedClassOp() { //<-- parent CXXMemberDecl
(void) this; //<-- CXXThisExpr
}
};
}
};

I get the most direct ancestor (the desired behavior) when using a
matcher like:

cxxThisExpr(hasAncestor(cxxMethodDecl().bind("method"))).bind("this")

My question is: Is this behavior always guaranteed? or am I just being
lucky here?

Thanks,

Mike

Hello all,

I am new to clang and I am trying to write a check to familiarize myself
with AST matchers. I am just looking for a confirmation here. When I
bind a node within a hasAncestor() matcher, am I guaranteed to get the
most direct (nearest) ancestor from the current node?

Consider the following example:

class MyClass {
public:
void myClassOp() { //<-- another parent CXXMemberDecl
class NestedClass {
void nestedClassOp() { //<-- parent CXXMemberDecl
(void) this; //<-- CXXThisExpr
}
};
}
};

I get the most direct ancestor (the desired behavior) when using a
matcher like:

cxxThisExpr(hasAncestor(cxxMethodDecl().bind(“method”))).bind(“this”)

My question is: Is this behavior always guaranteed? or am I just being
lucky here?

It’s guaranteed if there is a single line of ancestry, but keep in mind that nodes can have multiple parents :slight_smile: