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

victor via cfe-dev cfe-dev at lists.llvm.org
Tue Feb 23 08:07:31 PST 2016


Thanks for answering, Manuel. 

However, your code didn't work:

clases_file.cpp:35:22: error: variable 'MD' with type 'const auto *' has incompatible initializer of type 'const clang::CXXMethodDecl'
if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(pv[size-1])) {
                                ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I changed "CXXMethodDecl" to "CXXMethodDecl*":

const auto* MD = llvm::dyn_cast<CXXMethodDecl*>(pv[size-1])

but it still gives a warning and an error:

In file included from /usr/lib/llvm-3.6/include/clang/Basic/LLVM.h:22:
/usr/lib/llvm-3.6/include/llvm/Support/Casting.h:292:10: warning: returning reference to local temporary object [-Wreturn-stack-address]
  return isa<X>(Val) ? cast<X>(Val) : nullptr;
         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
clases_file.cpp:35:33: note: in instantiation of function template specialization 'llvm::dyn_cast<clang::CXXMethodDecl *, const
      clang::ast_type_traits::DynTypedNode>' requested here
                                        if (const auto* MD = llvm::dyn_cast<CXXMethodDecl*>(pv[size-1])) {
                                                                   
....

In file included from /usr/lib/llvm-3.6/include/clang/Basic/LLVM.h:22:
/usr/lib/llvm-3.6/include/llvm/Support/Casting.h:56:12: error: type 'clang::CXXMethodDecl *' cannot be used prior to '::' because it has no
      members
    return To::classof(&Val);
                       ^
From: klimek at google.com
Date: Tue, 23 Feb 2016 15:29:58 +0000
Subject: Re: [cfe-dev] How to use clang::ast_type_traits::DynTypedNode?
To: pedretti_86 at hotmail.com; cfe-dev at lists.llvm.org

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 parent
if(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/885dfd57/attachment.html>


More information about the cfe-dev mailing list