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

Manuel Klimek via cfe-dev cfe-dev at lists.llvm.org
Tue Feb 23 07:29:58 PST 2016


On Tue, Feb 23, 2016 at 3:58 PM victor via cfe-dev <cfe-dev at lists.llvm.org>
wrote:

> 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
> parentif(llvm::isa<CXXMethodDecl>(pv[size-1]))*
>

You'll want to use:
if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(pv[size-1])) {
 ... use MD
}


>
>
>
>
>
>
>
>
>
>
>
>
> *{    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.
> _______________________________________________
> cfe-dev mailing list
> cfe-dev at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160223/0bcf451c/attachment.html>


More information about the cfe-dev mailing list