Search code examples
c++clanglibtooling

clang AST Matcher: Whats the best way to match the current Matcher over hasParent()


I am trying to search for ifStmts, which aren't an else if. To accomplish that, I want to test if the Stmt has a Parent, which is not an ifStmt(hasElse(ifStmt(equals(<myCurrentNode>))))

The following obviously does not work, but I could not find a better solution.

clang-query> m ifStmt(<myMatcherforMyNode>, unless(hasParent(ifStmt(hasElse(ifStmt(equalsBoundNode("ifelse"))))))).bind("ifelse")

Solution

  • Ok just found it out myself by searching the source of clang-tidy:

    Just use something like ifStmt(ifStmt().bind("if"), <myMatcherForMyNode>, unless(hasParent(ifStmt(hasElse(ifStmt(equalsBoundNode("if"))))))).

    Also, ifStmt(stmt().bind("if"), <myMatcherForMyNode>, unless(hasParent(ifStmt(hasElse(ifStmt(equalsBoundNode("if"))))))) will do the job