<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Hi all,<div><br></div><div>I have the following code to try to get functionDecl from a given statement:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div>const clang::Decl* llvm_convertert::get_DeclContext_from_Stmt(</div></div><div><div> const clang::Stmt& stmt)</div></div><div><div>{</div></div><div><div> auto it = ASTContext->getParents(stmt).begin();</div></div><div><div><br></div></div><div><div> if(it == ASTContext->getParents(stmt).end())</div></div><div><div> return nullptr;</div></div><div><div><br></div></div><div><div> const clang::Decl *aDecl = it->get<clang::Decl>();</div></div><div><div> if(aDecl)</div></div><div><div> return aDecl;</div></div><div><div><br></div></div><div><div> const clang::Stmt *aStmt = it->get<clang::Stmt>();</div></div><div><div> if(aStmt)</div></div><div><div> return get_DeclContext_from_Stmt(*aStmt);</div></div><div><div><br></div></div><div><div> return nullptr;</div></div><div><div>}</div></div><div><div><br></div></div><div><div>const clang::FunctionDecl* llvm_convertert::get_top_FunctionDecl_from_Stmt(</div></div><div><div> const clang::Stmt& stmt)</div></div><div><div>{</div></div><div><div> const clang::Decl *decl = get_DeclContext_from_Stmt(stmt);</div></div><div><div> if(decl)</div></div><div><div> return static_cast<const clang::FunctionDecl*>(decl->getNonClosureContext());</div></div><div><div><br></div></div><div><div> return nullptr;</div></div><div><div>}</div></div></blockquote><div><div><br></div><div>Right now, I only call the method for a return statement:</div><div><br></div></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><div>const clang::ReturnStmt &ret =</div></div></div><div><div><div> static_cast<const clang::ReturnStmt&>(stmt);</div></div></div><div><div><div><br></div></div></div><div><div><div>const clang::FunctionDecl *fd = get_top_FunctionDecl_from_Stmt(ret);</div></div></div></blockquote><div><div><br></div><div>The code works fine when I compile it with O0 but stops working when compiling with O2. </div><div><br></div><div>Here is the assembly generated with -O0:</div><div><br></div><div><div>000000000000c29e <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)>:</div><div> c29e:<span style="white-space:pre-wrap"> </span>55 <span style="white-space:pre-wrap"> </span>push %rbp</div><div> c29f:<span style="white-space:pre-wrap"> </span>48 89 e5 <span style="white-space:pre-wrap"> </span>mov %rsp,%rbp</div><div> c2a2:<span style="white-space:pre-wrap"> </span>48 83 ec 20 <span style="white-space:pre-wrap"> </span>sub $0x20,%rsp</div><div> c2a6:<span style="white-space:pre-wrap"> </span>48 89 7d e8 <span style="white-space:pre-wrap"> </span>mov %rdi,-0x18(%rbp)</div><div> c2aa:<span style="white-space:pre-wrap"> </span>48 89 75 e0 <span style="white-space:pre-wrap"> </span>mov %rsi,-0x20(%rbp)</div><div> c2ae:<span style="white-space:pre-wrap"> </span>48 8b 55 e0 <span style="white-space:pre-wrap"> </span>mov -0x20(%rbp),%rdx</div><div> c2b2:<span style="white-space:pre-wrap"> </span>48 8b 45 e8 <span style="white-space:pre-wrap"> </span>mov -0x18(%rbp),%rax</div><div> c2b6:<span style="white-space:pre-wrap"> </span>48 89 d6 <span style="white-space:pre-wrap"> </span>mov %rdx,%rsi</div><div> c2b9:<span style="white-space:pre-wrap"> </span>48 89 c7 <span style="white-space:pre-wrap"> </span>mov %rax,%rdi</div><div> c2bc:<span style="white-space:pre-wrap"> </span>e8 00 00 00 00 <span style="white-space:pre-wrap"> </span>callq c2c1 <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x23></div><div> c2c1:<span style="white-space:pre-wrap"> </span>48 89 45 f8 <span style="white-space:pre-wrap"> </span>mov %rax,-0x8(%rbp)</div><div> c2c5:<span style="white-space:pre-wrap"> </span>48 83 7d f8 00 <span style="white-space:pre-wrap"> </span>cmpq $0x0,-0x8(%rbp)</div><div> c2ca:<span style="white-space:pre-wrap"> </span>74 0e <span style="white-space:pre-wrap"> </span>je c2da <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x3c></div><div> c2cc:<span style="white-space:pre-wrap"> </span>48 8b 45 f8 <span style="white-space:pre-wrap"> </span>mov -0x8(%rbp),%rax</div><div> c2d0:<span style="white-space:pre-wrap"> </span>48 89 c7 <span style="white-space:pre-wrap"> </span>mov %rax,%rdi</div><div> c2d3:<span style="white-space:pre-wrap"> </span>e8 00 00 00 00 <span style="white-space:pre-wrap"> </span>callq c2d8 <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x3a></div><div> c2d8:<span style="white-space:pre-wrap"> </span>eb 05 <span style="white-space:pre-wrap"> </span>jmp c2df <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x41></div><div> c2da:<span style="white-space:pre-wrap"> </span>b8 00 00 00 00 <span style="white-space:pre-wrap"> </span>mov $0x0,%eax</div><div> c2df:<span style="white-space:pre-wrap"> </span>c9 <span style="white-space:pre-wrap"> </span>leaveq </div><div> c2e0:<span style="white-space:pre-wrap"> </span>c3 <span style="white-space:pre-wrap"> </span>retq </div><div> c2e1:<span style="white-space:pre-wrap"> </span>90 <span style="white-space:pre-wrap"> </span>nop</div></div><div><br></div><div>and the assembly generated with -O2:</div><div><br></div><div><div>0000000000002160 <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)>:</div><div> 2160:<span style="white-space:pre-wrap"> </span>48 83 ec 08 <span style="white-space:pre-wrap"> </span>sub $0x8,%rsp</div><div> 2164:<span style="white-space:pre-wrap"> </span>e8 00 00 00 00 <span style="white-space:pre-wrap"> </span>callq 2169 <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x9></div><div> 2169:<span style="white-space:pre-wrap"> </span>48 85 c0 <span style="white-space:pre-wrap"> </span>test %rax,%rax</div><div> 216c:<span style="white-space:pre-wrap"> </span>74 12 <span style="white-space:pre-wrap"> </span>je 2180 <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x20></div><div> 216e:<span style="white-space:pre-wrap"> </span>48 89 c7 <span style="white-space:pre-wrap"> </span>mov %rax,%rdi</div><div> 2171:<span style="white-space:pre-wrap"> </span>48 83 c4 08 <span style="white-space:pre-wrap"> </span>add $0x8,%rsp</div><div> 2175:<span style="white-space:pre-wrap"> </span>e9 00 00 00 00 <span style="white-space:pre-wrap"> </span>jmpq 217a <llvm_convertert::get_top_FunctionDecl_from_Stmt(clang::Stmt const&)+0x1a></div><div> 217a:<span style="white-space:pre-wrap"> </span>66 0f 1f 44 00 00 <span style="white-space:pre-wrap"> </span>nopw 0x0(%rax,%rax,1)</div><div> 2180:<span style="white-space:pre-wrap"> </span>31 c0 <span style="white-space:pre-wrap"> </span>xor %eax,%eax</div><div> 2182:<span style="white-space:pre-wrap"> </span>48 83 c4 08 <span style="white-space:pre-wrap"> </span>add $0x8,%rsp</div><div> 2186:<span style="white-space:pre-wrap"> </span>c3 <span style="white-space:pre-wrap"> </span>retq </div><div> 2187:<span style="white-space:pre-wrap"> </span>90 <span style="white-space:pre-wrap"> </span>nop</div><div> 2188:<span style="white-space:pre-wrap"> </span>0f 1f 84 00 00 00 00 <span style="white-space:pre-wrap"> </span>nopl 0x0(%rax,%rax,1)</div><div> 218f:<span style="white-space:pre-wrap"> </span>00 </div></div><div><br></div><div>Does anyone has any idea why this is happening and how to fix it? The whole assembly ended up being too big to be attached so I added them to <a href="https://www.dropbox.com/sh/vd5nhjzqaxk0ess/AADVPFkaxrGPWdAUcBkjmVPga?dl=0">dropbox</a>.</div><div><br></div><div>I'm using clang 3.8 RC3 to link and building with g++ 5.3.1.</div><div><br></div><div>Thank you,</div><span class="HOEnZb"><font color="#888888"><div><br></div></font></span></div></div></div>-- <br><div class="gmail_signature"><div dir="ltr"><div><br></div><div>Mikhail Ramalho.</div></div></div>
</div>