<html>
<head>
<style><!--
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 12pt;
font-family:Calibri
}
--></style></head>
<body class='hmmessage'><div dir='ltr'>Thanks for answering, Manuel. <br><br>However, your code didn't work:<br><br><blockquote>clases_file.cpp:35:22: error: variable 'MD' with type 'const auto *' has incompatible initializer of type 'const clang::CXXMethodDecl'<br>if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(pv[size-1])) {<br></blockquote>                                ^    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br><br>I changed "CXXMethodDecl" to "CXXMethodDecl*":<br><br><blockquote>const auto* MD = llvm::dyn_cast<CXXMethodDecl*>(pv[size-1])<br></blockquote><br>but it still gives a warning and an error:<br><br><blockquote>In file included from /usr/lib/llvm-3.6/include/clang/Basic/LLVM.h:22:<br>/usr/lib/llvm-3.6/include/llvm/Support/Casting.h:292:10: warning: returning reference to local temporary object [-Wreturn-stack-address]<br>  return isa<X>(Val) ? cast<X>(Val) : nullptr;<br>         ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>clases_file.cpp:35:33: note: in instantiation of function template specialization 'llvm::dyn_cast<clang::CXXMethodDecl *, const<br>      clang::ast_type_traits::DynTypedNode>' requested here<br>                                        if (const auto* MD = llvm::dyn_cast<CXXMethodDecl*>(pv[size-1])) {<br>                                                                   <br>....<br></blockquote><br><blockquote>In file included from /usr/lib/llvm-3.6/include/clang/Basic/LLVM.h:22:<br>/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<br>      members<br>    return To::classof(&Val);<br></blockquote>                       ^<br><div><hr id="stopSpelling">From: klimek@google.com<br>Date: Tue, 23 Feb 2016 15:29:58 +0000<br>Subject: Re: [cfe-dev] How to use clang::ast_type_traits::DynTypedNode?<br>To: pedretti_86@hotmail.com; cfe-dev@lists.llvm.org<br><br><div dir="ltr"><div class="ecxgmail_quote"><div dir="ltr">On Tue, Feb 23, 2016 at 3:58 PM victor via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="ecxgmail_quote" style="border-left:1px #ccc solid;padding-left:1ex;">


<div><div dir="ltr">Hi everyone,<br><br>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.<br><br>I have a memberExpr object. I just want to know <b>the class in which that memberExpr object is</b>. For example:<br><br><i>class A{<br>    protected: <br>        int field;<br>};<br><br>class B: public A{<br><br>    void method() {<br>        ...<br>        if( field == 1 )<br>        ...<br>    }<br><br>};</i><br><br>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. <br><br>I have tried with this (just an attempt...):<br><br><i>ArrayRef<ast_type_traits::DynTypedNode> pv = Context->getParents<MemberExpr>(*member);<br>CXXRecordDecl* rec;<br><br>size_t size = pv.size();<br><br>//Retrieve the outermost parent<br>if(llvm::isa<CXXMethodDecl>(pv[size-1]))<br></i></div></div></blockquote><div><br></div><div>You'll want to use:</div><div>if (const auto* MD = llvm::dyn_cast<CXXMethodDecl>(pv[size-1])) {</div><div> ... use MD</div><div>}</div><div>  </div><blockquote class="ecxgmail_quote" style="border-left:1px #ccc solid;padding-left:1ex;"><div><div dir="ltr"><i>{<br>    CXXMethodDecl* md = (CXXMethodDecl*)(pv[size-1]);<br>    if(llvm::isa<CXXRecordDecl>(md->getParent()))<br>        rec = (CXXRecordDecl*)(md->getParent());<br>}<br>else<br>{<br>    if(llvm::isa<CXXRecordDecl>(pv[size-1]))<br>        rec = (CXXRecordDecl*)(pv[size-1]);<br>}<br><br><br></i>The compiler gives the following error (among others):<br><br> <b>error</b>: cannot cast from type 'const clang::ast_type_traits::DynTypedNode' to pointer type 'clang::CXXMethodDecl *'<br>                                        CXXMethodDecl* md = (CXXMethodDecl*)(pv[size-1]);<br>                                                            ^~~~~~~~~~~~~~~~~~~~~~~~~<br><br>Am I in the right path? How can I perform the cast?<br><br>Thanks in advance.                                           </div></div>
_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div></div></div>                                           </div></body>
</html>