[cfe-dev] Could I get the Parent AST of a Stmt?

Mikhail Ramalho via cfe-dev cfe-dev at lists.llvm.org
Sun Jan 3 23:28:15 PST 2016


Hello,

You can use the following code to walk through the parents of a stmt/expr:

const clang::Decl* get_DeclContext_from_Stmt(const clang::Stmt& stmt)
{
  auto it = ASTContext->getParents(stmt).begin();

  if(it == ASTContext->getParents(stmt).end())
    return nullptr;

  const clang::Decl *aDecl = it->get<clang::Decl>();
  if(aDecl)
    return aDecl;

  const clang::Stmt *aStmt = it->get<clang::Stmt>();
  if(aStmt)
    return get_DeclContext_from_Stmt(*aStmt);

  return nullptr;
}

It tries to find a declaration, but you can change it to find any stmt/expr.

Best regards,

-- 

Mikhail Ramalho.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160104/0399d1a4/attachment.html>


More information about the cfe-dev mailing list