[cfe-dev] hasAncestor() hasDescendant() matchers scope

Michael Jabbour via cfe-dev cfe-dev at lists.llvm.org
Wed Jul 4 07:23:54 PDT 2018


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




More information about the cfe-dev mailing list