Search code examples
c++clangllvm-clanglibtooling

Skipping statements inside an IfStatements


I have created an if(isa<IfStmt>(s)) and if(isa<ReturnStmt>(s)) which prints something if found. I have created this simple c++ codes which contains return statement inside an if statement

#include <stdlib.h>
int main(int argc, char** argv) {
  int a = atoi(argv[1]);
  if(a == 0)
    return 1;
  else
    return 10;
  return 0;
}

Is it possible to skip/ignore the return statement inside an if else statement ?


Solution

  • clang-query test.cpp --

    match returnStmt(unless(hasAncestor(ifStmt()))
    

    To solve problems like this, the AST matcher's reference and clang's -ast-dump parameter are your friends. And also clang-query itself, as it accepts mostly the same DSL as clang's C++ API.