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

victor via cfe-dev cfe-dev at lists.llvm.org
Tue Feb 23 09:14:33 PST 2016


Ok, thanks. Now it compiles. However, I have the following code:

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

size_t size = pv.size();
                
                if(size > 0){
                    llvm::outs() << "Size: " << size << "\n";
                    
                    for(auto it: pv)
                        llvm::outs() << "NodeKind: " << it.getNodeKind().asStringRef() << "\n";

                    if (const CXXMethodDecl* MD = (pv[size-1]).get<CXXMethodDecl>()) {
                        llvm::outs() << "A method\n";
                    }
                }

And the output is:

Size: 1
NodeKind: ImplicitCastExprSize
              
 
Do you know why? I supposed it would be a CXXMethodDecl or a CXXRecordDecl. The code I'm using to check the program is just:

class A{
    public:
        int get_a(){ return a;}
    private:
        int a;
};



From: klimek at google.com
Date: Tue, 23 Feb 2016 16:47:09 +0000
Subject: Re: [cfe-dev] How to use clang::ast_type_traits::DynTypedNode?
To: pedretti_86 at hotmail.com; jonathan at codesourcery.com; cfe-dev at lists.llvm.org

Oh sorry, I didn't realize you actually have DynTypedNode. DynTypedNode has a get<T> method to retrieve a pointer to the underlying type.
On Tue, Feb 23, 2016 at 5:44 PM victor <pedretti_86 at hotmail.com> wrote:



Hi Jonathan,

Something is failing. I have this:

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

size_t size = pv.size();
                
if(size > 0){
     if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(&pv[size-1])) {}
}

The compiler says:

/usr/lib/llvm-3.6/include/llvm/Support/Casting.h:56:24: error: cannot initialize a parameter of type 'const clang::Decl *' with an rvalue of
      type 'const clang::ast_type_traits::DynTypedNode *'
    return To::classof(&Val);
                       ^~~~


> Subject: Re: [cfe-dev] How to use clang::ast_type_traits::DynTypedNode?
> To: pedretti_86 at hotmail.com; klimek at google.com; cfe-dev at lists.llvm.org
> From: jonathan at codesourcery.com
> Date: Tue, 23 Feb 2016 09:13:21 -0700
> 
> 
> 
> On 2/23/16 9:07 AM, victor via cfe-dev wrote:
> > 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])) {
> >
> >                                  ^
> > ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> 
> You need to take the address of it as dyn_cast works on pointers, not 
> references:
> 
>    if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(&pv[size-1])) {
> 
> 
> Jon
> 
> -- 
> Jon Roelofs
> jonathan at codesourcery.com
> CodeSourcery / Mentor Embedded
 		 	   		  
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20160223/2a936179/attachment.html>


More information about the cfe-dev mailing list