<div dir="ltr">You need to mark the 2 argument constructor as ODR-used. I think you want to all Sema.MarkFunctionReferenced.</div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Jan 17, 2017 at 5:57 AM, Jakub Kuderski via cfe-dev <span dir="ltr"><<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hi,<br><br>I'm working on a clang plugin that would replace some constructor calls with calls to other constructors.<br><br>Here's a simplified example:<br><br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace, monospace">template <typename> struct C {<br></font><font face="monospace, monospace"> C() {}<br></font><font face="monospace, monospace"> C(int, int) {}<br></font><font face="monospace, monospace">};</font><font face="monospace, monospace"><br></font><font face="monospace, monospace">C<void> c = {};<br></font><font face="monospace, monospace">//C<void> d = {0, 0};</font><font face="monospace, monospace"><br></font><font face="monospace, monospace">int main() {}</font></blockquote><div><br>I want to call the 2-argument constructor to initialize c to make it act like the initialization of the d variable below.<br><br>I wrote a plugin that runs before the main AST action and does the transformation in HandleTopLevelDecl. The problem is that the 2-argument constructor is not begin emitted and linking fails.<br><br>I'm doing the transformation this way:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace, monospace">void ChangeCtor(VarDecl *VD) {<br></font><font face="monospace, monospace"> VD->dump();</font><font face="monospace, monospace"><br></font><font face="monospace, monospace"> auto &Sema = Instance.getSema();<br></font><font face="monospace, monospace"> auto &Context = Sema.getASTContext();<br></font><font face="monospace, monospace"> auto Loc = VD->getSourceRange().getBegin(<wbr>);<br></font><font face="monospace, monospace"> auto VarQT = VD->getType();</font><font face="monospace, monospace"><br></font><font face="monospace, monospace"> auto *Zero = clang::IntegerLiteral::Create(<wbr>Context, llvm::APInt(32, 0),<br></font><font face="monospace, monospace"> Context.IntTy, Loc);</font><font face="monospace, monospace"><br></font><font face="monospace, monospace"> auto *RDecl = VarQT->getAsCXXRecordDecl();<br></font><font face="monospace, monospace"> CXXConstructorDecl *Ctor = nullptr;<br></font><font face="monospace, monospace"> for (auto *CD : RDecl->ctors())<br></font><font face="monospace, monospace"> if (CD->getNumParams() == 2)<br></font><font face="monospace, monospace"> Ctor = CD;</font> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace, monospace"><br></font><font face="monospace, monospace"> Expr *Params[] = {Zero, Zero};<br></font><font face="monospace, monospace"> auto *ConstructExpr = CXXConstructExpr::Create(<br></font><font face="monospace, monospace"> Context, VarQT, Loc, Ctor, false, Params, true, true, false, false,<br></font><font face="monospace, monospace"> CXXConstructExpr::CK_Complete, VD->getSourceRange());</font><font face="monospace, monospace"><br></font><font face="monospace, monospace"> </font> </blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><font face="monospace, monospace">ConstructExpr->dump();<br></font><font face="monospace, monospace"> VD->setInit(ConstructExpr);<br></font><font face="monospace, monospace"> VD->dump();<br></font><font face="monospace, monospace">}</font></blockquote><div><br></div><div>(Here's a gist with all the code: <a href="https://gist.github.com/kuhar/fd205c2f38e1baa4331e70dd5cb45b42" target="_blank">https://gist.github.com/<wbr>kuhar/<wbr>fd205c2f38e1baa4331e70dd5cb45b<wbr>42</a>)<br><br>When I uncomment the d variable, or insert a call to the 2-argument ctor somewhere else, it gets emitted and links fine - no surprise here.<br><br>What am I doing wrong? Any suggestion on how to make it work?<br><br>Thanks,<br>~Kuba</div></div></div>
<br>______________________________<wbr>_________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org">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/<wbr>mailman/listinfo/cfe-dev</a><br>
<br></blockquote></div><br></div>