[cfe-dev] How to use clang::ast_type_traits::DynTypedNode?

victor via cfe-dev cfe-dev at lists.llvm.org
Tue Feb 23 06:58:27 PST 2016


Hi everyone,

There is a task when using Clang libraries which 
I've never been able to undertake. I asked some time ago, but now it 
seems that the libraries have changed and I would like to resume it.

I have a memberExpr object. I just want to know the class in which that memberExpr object is. For example:

class A{
    protected: 
        int field;
};

class B: public A{

    void method() {
        ...
        if( field == 1 )
        ...
    }

};

In
 this example, field is the memberExpr and what I want to know is that 
the memberExpr is inside class B. I know that there is a method in 
ASTContext called "getParents". When using this method, I know that 
you'll not always end up in a CXXRecordDecl - sometimes you'll also end 
up in a CXXMethodDcl that is defined out-of-line. 

I have tried with this (just an attempt...):

ArrayRef<ast_type_traits::DynTypedNode> pv = Context->getParents<MemberExpr>(*member);
CXXRecordDecl* rec;

size_t size = pv.size();

//Retrieve the outermost parent
if(llvm::isa<CXXMethodDecl>(pv[size-1]))
{
    CXXMethodDecl* md = (CXXMethodDecl*)(pv[size-1]);
    if(llvm::isa<CXXRecordDecl>(md->getParent()))
        rec = (CXXRecordDecl*)(md->getParent());
}
else
{
    if(llvm::isa<CXXRecordDecl>(pv[size-1]))
        rec = (CXXRecordDecl*)(pv[size-1]);
}


The compiler gives the following error (among others):

 error: cannot cast from type 'const clang::ast_type_traits::DynTypedNode' to pointer type 'clang::CXXMethodDecl *'
                                        CXXMethodDecl* md = (CXXMethodDecl*)(pv[size-1]);
                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~

Am I in the right path? How can I perform the cast?

Thanks in advance. 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160223/1f758983/attachment.html>


More information about the cfe-dev mailing list